Android system STL broken with rtti/exceptions enabled
If I configure CMake to use the system STL but enable either of rtti or exceptions, then CMake tries to link with -landroid_support, but it uses a ;
in CMAKE_CXX_STANDARD_LIBRARIES when it needs to use a
, so the link command fails.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.21.0)
project(test C CXX ASM)
message(STATUS "CMAKE_CXX_STANDARD_LIBRARIES=[${CMAKE_CXX_STANDARD_LIBRARIES}]")
add_executable(hello hello.cpp)
hello.cpp:
int main() { return 0; }
CMake Invocation:
$ cmake -GNinja -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=... \
-DCMAKE_SYSTEM_VERSION=19 \
-DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a \
-DCMAKE_ANDROID_STL_TYPE=system \
-DCMAKE_ANDROID_EXCEPTIONS=ON
Output:
...
-- CMAKE_CXX_STANDARD_LIBRARIES=[ -lc++abi;-landroid_support -latomic -lm]
...
$ ninja
[2/2] Linking CXX executable hello
FAILED: hello
: && /x/android-ndk-r23b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi19 -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -mfpu=vfpv3-d16 -Wformat -Werror=format-security -fexceptions -fno-rtti -stdlib=libstdc++ -O2 -g -DNDEBUG -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Qunused-arguments -Wl,--no-undefined -Wl,--gc-sections CMakeFiles/hello.dir/hello.cpp.o -o hello -lc++abi;-landroid_support -latomic -lm && :
/bin/sh: line 1: -landroid_support: command not found
ninja: build stopped: subcommand failed.
The fix is trivial. I wasn't sure I needed to file a bug, but maybe it's helpful.
This code in Android-Common.json uses list-append but needs to use string-append:
macro(__android_stl lang)
string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libstdc++")
if(_ANDROID_STL_EXCEPTIONS OR _ANDROID_STL_RTTI)
string(APPEND CMAKE_${lang}_STANDARD_LIBRARIES " -lc++abi")
if(CMAKE_SYSTEM_VERSION LESS 21)
list(APPEND CMAKE_${lang}_STANDARD_LIBRARIES "-landroid_support")
endif()
endif()
endmacro()