Commit c2f561e5 authored by Brad King's avatar Brad King 💬
Browse files

Android: Add test cases covering use of the NDK and standalone toolchains

parent 6b84df8d
Pipeline #22982 passed with stage
^CMake Error at .*/Modules/Platform/Android-Determine.cmake:[0-9]+ \(message\):
The value of CMAKE_SYSROOT:
.*
does not match any of the forms:
<ndk>/platforms/android-<api>/arch-<arch>
<standalone-toolchain>/sysroot
where:
<ndk> = Android NDK directory \(with forward slashes\)
<api> = Android API version number \(decimal digits\)
<arch> = Android ARCH name \(lower case\)
<standalone-toolchain> = Path to standalone toolchain prefix
Call Stack \(most recent call first\):
.*/Modules/CMakeDetermineSystem.cmake:[0-9]+ \(include\)
CMakeLists.txt:2 \(project\)$
cmake_minimum_required(VERSION 3.6)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)
cmake_minimum_required(VERSION 3.6)
include(RunCMake)
foreach(v TEST_ANDROID_NDK TEST_ANDROID_STANDALONE_TOOLCHAIN)
string(REPLACE "|" ";" ${v} "${${v}}")
endforeach()
function(run_Android case)
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
${RunCMake_TEST_OPTIONS}
${ARGN}
)
# Use a single build tree for a few tests without cleaning.
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${case}-build)
set(RunCMake_TEST_NO_CLEAN 1)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
run_cmake(${case})
run_cmake_command(${case}-build ${CMAKE_COMMAND} --build .)
endfunction()
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_SYSROOT=${CMAKE_CURRENT_SOURCE_DIR}
)
run_cmake(BadSYSROOT)
unset(RunCMake_TEST_OPTIONS)
foreach(ndk IN LISTS TEST_ANDROID_NDK)
# Load available toolchain versions and abis.
file(GLOB _config_mks
"${ndk}/build/core/toolchains/*/config.mk"
"${ndk}/toolchains/*/config.mk"
)
set(_versions "")
set(_latest_gcc 0)
set(_latest_clang "")
set(_latest_clang_vers 0)
foreach(config_mk IN LISTS _config_mks)
file(STRINGS "${config_mk}" _abis REGEX "^TOOLCHAIN_ABIS +:= +[^ ].*( |$)")
if(_abis AND "${config_mk}" MATCHES [[-((clang)?([0-9]\.[0-9]|))/config\.mk$]])
set(_version "${CMAKE_MATCH_1}")
set(_is_clang "${CMAKE_MATCH_2}")
set(_cur_vers "${CMAKE_MATCH_3}")
if(_is_clang)
if(_latest_clang_vers STREQUAL "")
# already the latest possible
elseif(_cur_vers STREQUAL "" OR _cur_vers VERSION_GREATER _latest_clang_vers)
set(_latest_clang_vers "${_cur_vers}")
set(_latest_clang "${_version}")
endif()
else()
if(_version VERSION_GREATER _latest_gcc)
set(_latest_gcc ${_version})
endif()
endif()
list(APPEND _versions "${_version}")
string(REGEX MATCHALL "[a-z][a-z0-9_-]+" _abis "${_abis}")
list(APPEND _abis_${_version} ${_abis})
endif()
endforeach()
set(_abis_ ${_abis_${_latest_gcc}})
set(_abis_clang ${_abis_${_latest_clang}})
if(_versions MATCHES "clang")
set(_versions "clang" ${_versions})
endif()
list(REMOVE_DUPLICATES _versions)
list(SORT _versions)
set(_versions ";${_versions}")
foreach(vers IN LISTS _versions)
list(REMOVE_DUPLICATES _abis_${vers})
endforeach()
# Test failure cases.
message(STATUS "ndk='${ndk}'")
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_NDK=${ndk}
-DCMAKE_ANDROID_ARCH_ABI=badabi
)
run_cmake(ndk-badabi)
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_NDK=${ndk}
-DCMAKE_ANDROID_ARCH_ABI=x86
-DCMAKE_ANDROID_ARM_MODE=0
)
run_cmake(ndk-badarm)
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_NDK=${ndk}
-DCMAKE_ANDROID_ARM_NEON=0
)
run_cmake(ndk-badneon)
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_NDK=${ndk}
-DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=badver
)
run_cmake(ndk-badver)
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_NDK=${ndk}
-DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=1.0
)
run_cmake(ndk-badvernum)
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_NDK=${ndk}
-DCMAKE_ANDROID_STL_TYPE=badstl
)
run_cmake(ndk-badstl)
unset(RunCMake_TEST_OPTIONS)
# Find a sysroot to test.
file(GLOB _sysroots "${ndk}/platforms/android-[0-9][0-9]/arch-arm")
if(_sysroots)
list(GET _sysroots 0 _sysroot)
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_SYSROOT=${_sysroot}
)
run_cmake(ndk-sysroot-armeabi)
unset(RunCMake_TEST_OPTIONS)
endif()
# Find available STLs.
set(stl_types
none
system
gnustl_static
gnustl_shared
)
if(IS_DIRECTORY "${ndk}/sources/cxx-stl/gabi++/libs")
list(APPEND stl_types gabi++_static gabi++_shared)
endif()
if(IS_DIRECTORY "${ndk}/sources/cxx-stl/stlport/libs")
list(APPEND stl_types stlport_static stlport_shared)
endif()
if(IS_DIRECTORY "${ndk}/sources/cxx-stl/llvm-libc++/libs")
list(APPEND stl_types c++_static c++_shared)
endif()
# List possible ABIs.
set(abi_names
armeabi
armeabi-v6
armeabi-v7a
arm64-v8a
mips
mips64
x86
x86_64
)
# Test all combinations.
foreach(vers IN LISTS _versions)
foreach(stl IN LISTS stl_types)
foreach(config Release Debug)
# Test this combination for all available abis.
message(STATUS "ndk='${ndk}' vers='${vers}' stl='${stl}' config='${config}'")
set(RunCMake_TEST_OPTIONS
-DCMAKE_ANDROID_NDK=${ndk}
-DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=${vers}
-DCMAKE_ANDROID_STL_TYPE=${stl}
-DCMAKE_BUILD_TYPE=${config}
)
foreach(abi IN LISTS abi_names)
# Skip ABIs not supported by this compiler.
if(NOT ";${_abis_${vers}};" MATCHES ";${abi};")
continue()
endif()
# Skip combinations that seem to be broken.
if("${stl};${abi}" MATCHES [[^c\+\+_static;armeabi]])
continue()
endif()
# Run the tests for this combination.
if("${abi}" STREQUAL "armeabi")
run_Android(ndk-armeabi-thumb) # default: -DCMAKE_ANDROID_ARCH_ABI=armeabi -DCMAKE_ANDROID_ARM_MODE=0
run_Android(ndk-armeabi-arm -DCMAKE_ANDROID_ARM_MODE=1) # default: -DCMAKE_ANDROID_ARCH_ABI=armeabi
else()
run_Android(ndk-${abi} -DCMAKE_ANDROID_ARCH_ABI=${abi})
if("${abi}" STREQUAL "armeabi-v7a")
run_Android(ndk-${abi}-neon -DCMAKE_ANDROID_ARCH_ABI=${abi} -DCMAKE_ANDROID_ARM_NEON=1)
endif()
endif()
endforeach()
unset(RunCMake_TEST_OPTIONS)
endforeach()
endforeach()
endforeach()
endforeach()
foreach(toolchain IN LISTS TEST_ANDROID_STANDALONE_TOOLCHAIN)
message(STATUS "toolchain='${toolchain}'")
set(RunCMake_TEST_OPTIONS
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_SYSROOT=${toolchain}/sysroot
)
run_cmake(standalone-sysroot)
unset(RunCMake_TEST_OPTIONS)
foreach(config Release Debug)
message(STATUS "toolchain='${toolchain}' config='${config}'")
set(RunCMake_TEST_OPTIONS
-DCMAKE_ANDROID_STANDALONE_TOOLCHAIN=${toolchain}
-DCMAKE_BUILD_TYPE=${config}
)
run_Android(standalone)
unset(RunCMake_TEST_OPTIONS)
endforeach()
endforeach()
#include "android.h"
int main(void)
{
return 0;
}
#include "android.h"
#ifndef STL_NONE
#include <cmath>
#include <cstdio>
#ifndef STL_SYSTEM
#include <exception>
#include <typeinfo>
#ifndef STL_GABI
#include <iostream>
#include <string>
#endif
#endif
#endif
int main()
{
#if !defined(STL_NONE)
// Require -lm implied by linking as C++.
std::printf("%p\n", static_cast<double (*)(double)>(&std::sin));
#endif
#if defined(STL_NONE)
return 0;
#elif defined(STL_SYSTEM)
return 0;
#else
try {
delete (new int);
} catch (std::exception const& e) {
#if defined(STL_GABI)
e.what();
typeid(e).name();
#else
std::cerr << e.what() << std::endl;
std::cerr << typeid(e).name() << std::endl;
#endif
}
#if defined(STL_GABI)
return 0;
#else
std::string s;
return static_cast<int>(s.size());
#endif
#endif
}
#ifndef __ANDROID__
#error "__ANDROID__ not defined"
#endif
#include <android/api-level.h>
#if API_LEVEL != __ANDROID_API__
#error "API levels do not match"
#endif
#ifdef COMPILER_IS_CLANG
#ifndef __clang__
#error "COMPILER_IS_CLANG but __clang__ is not defined"
#endif
#else
#ifdef __clang__
#error "!COMPILER_IS_CLANG but __clang__ is defined"
#endif
#endif
#ifdef ARM_MODE
#if ARM_MODE == 1 && defined(__thumb__)
#error "ARM_MODE==1 but __thumb__ is defined"
#elif ARM_MODE == 0 && !defined(__thumb__)
#error "ARM_MODE==0 but __thumb__ is not defined"
#endif
#endif
#ifdef ARM_NEON
#if ARM_NEON == 0 && defined(__ARM_NEON__)
#error "ARM_NEON==0 but __ARM_NEON__ is defined"
#elif ARM_NEON == 1 && !defined(__ARM_NEON__)
#error "ARM_NEON==1 but __ARM_NEON__ is not defined"
#endif
#endif
#ifdef ABI_armeabi
#ifndef __ARM_EABI__
#error "ABI_armeabi: __ARM_EABI__ not defined"
#endif
#if __ARM_ARCH != 5
#error "ABI_armeabi: __ARM_ARCH is not 5"
#endif
#endif
#ifdef ABI_armeabi_v6
#ifndef __ARM_EABI__
#error "ABI_armeabi_v6: __ARM_EABI__ not defined"
#endif
#if __ARM_ARCH != 6
#error "ABI_armeabi_v6: __ARM_ARCH is not 6"
#endif
#endif
#ifdef ABI_armeabi_v7a
#ifndef __ARM_EABI__
#error "ABI_armeabi_v7a: __ARM_EABI__ not defined"
#endif
#if __ARM_ARCH != 7
#error "ABI_armeabi_v7a: __ARM_ARCH is not 7"
#endif
#endif
#ifdef ABI_arm64_v8a
#ifdef __ARM_EABI__
#error "ABI_arm64_v8a: __ARM_EABI__ defined"
#endif
#ifndef __aarch64__
#error "ABI_arm64_v8a: __aarch64__ not defined"
#endif
#endif
#ifdef ABI_mips
#if __mips != 32
#error "ABI_mips: __mips != 32"
#endif
#ifndef _ABIO32
#error "ABI_mips: _ABIO32 not defined"
#endif
#endif
#ifdef ABI_mips64
#if __mips != 64
#error "ABI_mips64: __mips != 64"
#endif
#ifndef _ABI64
#error "ABI_mips: _ABI64 not defined"
#endif
#endif
#ifdef ABI_x86
#ifndef __i686__
#error "ABI_x86: __i686__ not defined"
#endif
#endif
#ifdef ABI_x86_64
#ifndef __x86_64__
#error "ABI_x86_64: __x86_64__ not defined"
#endif
#endif
#include <stddef.h>
enable_language(C)
enable_language(CXX)
foreach(f
"${CMAKE_C_ANDROID_TOOLCHAIN_PREFIX}gcc${CMAKE_C_ANDROID_TOOLCHAIN_SUFFIX}"
"${CMAKE_CXX_ANDROID_TOOLCHAIN_PREFIX}g++${CMAKE_CXX_ANDROID_TOOLCHAIN_SUFFIX}"
"${CMAKE_CXX_ANDROID_TOOLCHAIN_PREFIX}cpp${CMAKE_CXX_ANDROID_TOOLCHAIN_SUFFIX}"
"${CMAKE_CXX_ANDROID_TOOLCHAIN_PREFIX}ar${CMAKE_CXX_ANDROID_TOOLCHAIN_SUFFIX}"
"${CMAKE_CXX_ANDROID_TOOLCHAIN_PREFIX}ld${CMAKE_CXX_ANDROID_TOOLCHAIN_SUFFIX}"
)
if(NOT EXISTS "${f}")
message(SEND_ERROR "Expected file does not exist:\n \"${f}\"")
endif()
endforeach()
string(APPEND CMAKE_C_FLAGS " -Werror")
string(APPEND CMAKE_CXX_FLAGS " -Werror")
string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,-no-undefined")
if(CMAKE_ANDROID_NDK)
if(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION MATCHES "clang")
add_definitions(-DCOMPILER_IS_CLANG)
endif()
elseif(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
execute_process(
COMMAND ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang --version
OUTPUT_VARIABLE _out
ERROR_VARIABLE _err
RESULT_VARIABLE _res
)
if(_res EQUAL 0)
add_definitions(-DCOMPILER_IS_CLANG)
endif()
endif()
if(CMAKE_ANDROID_STL_TYPE STREQUAL "none")
add_definitions(-DSTL_NONE)
elseif(CMAKE_ANDROID_STL_TYPE STREQUAL "system")
add_definitions(-DSTL_SYSTEM)
elseif(CMAKE_ANDROID_STL_TYPE MATCHES [[^gabi\+\+]])
add_definitions(-DSTL_GABI)
endif()
string(REPLACE "-" "_" abi "${CMAKE_ANDROID_ARCH_ABI}")
add_definitions(-DABI_${abi})
add_definitions(-DAPI_LEVEL=${CMAKE_SYSTEM_VERSION})
if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi")
add_definitions(-DARM_MODE=${CMAKE_ANDROID_ARM_MODE})
message(STATUS "CMAKE_ANDROID_ARM_MODE=${CMAKE_ANDROID_ARM_MODE}")
endif()
if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
add_definitions(-DARM_NEON=${CMAKE_ANDROID_ARM_NEON})
message(STATUS "CMAKE_ANDROID_ARM_NEON=${CMAKE_ANDROID_ARM_NEON}")
endif()
add_executable(android_c android.c)
add_executable(android_cxx android.cxx)
-- Android: Targeting API '[0-9]+' with architecture 'arm64', ABI 'arm64-v8a', and processor 'aarch64'
-- Android: Selected (Clang toolchain '[^']+' with )?GCC toolchain '[^']+'
-- Android: Targeting API '[0-9]+' with architecture 'arm', ABI 'armeabi', and processor 'armv5te'
-- Android: Selected (Clang toolchain '[^']+' with )?GCC toolchain '[^']+'
.*-- CMAKE_ANDROID_ARM_MODE=1
-- Android: Targeting API '[0-9]+' with architecture 'arm', ABI 'armeabi', and processor 'armv5te'
-- Android: Selected (Clang toolchain '[^']+' with )?GCC toolchain '[^']+'
.*-- CMAKE_ANDROID_ARM_MODE=0
-- Android: Targeting API '[0-9]+' with architecture 'arm', ABI 'armeabi-v7a', and processor 'armv7-a'
-- Android: Selected (Clang toolchain '[^']+' with )?GCC toolchain '[^']+'
.*-- CMAKE_ANDROID_ARM_NEON=1
-- Android: Targeting API '[0-9]+' with architecture 'arm', ABI 'armeabi-v7a', and processor 'armv7-a'
-- Android: Selected (Clang toolchain '[^']+' with )?GCC toolchain '[^']+'
.*-- CMAKE_ANDROID_ARM_NEON=0
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment