From 843d55de295b0eeca2a028badde3b2f9a409cc72 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 3 Apr 2018 11:31:59 -0400 Subject: Android: Suppress implicit -lstdc++ linker flag The chosen STL libraries are already linked explicitly so we shouldn't let the compiler add its implicit `-lstdc++` (the default) when invoking the linker. Fixes: #17863 NDK-Issue: https://github.com/android-ndk/ndk/issues/105 Inspired-by: Tom Hughes --- Modules/Platform/Android-Clang-CXX.cmake | 7 +++++++ Modules/Platform/Android-GNU-CXX.cmake | 3 +++ Modules/Platform/Android/ndk-stl-c++.cmake | 1 + Modules/Platform/Android/ndk-stl-gabi++.cmake | 1 + Modules/Platform/Android/ndk-stl-gnustl.cmake | 1 + Modules/Platform/Android/ndk-stl-none.cmake | 1 + Modules/Platform/Android/ndk-stl-stlport.cmake | 1 + Modules/Platform/Android/ndk-stl-system.cmake | 1 + Tests/RunCMake/Android/android_lib.cxx | 4 ++++ Tests/RunCMake/Android/check_binary.cmake | 8 ++++++++ Tests/RunCMake/Android/common.cmake | 17 +++++++++++++++++ 11 files changed, 45 insertions(+) create mode 100644 Tests/RunCMake/Android/android_lib.cxx create mode 100644 Tests/RunCMake/Android/check_binary.cmake diff --git a/Modules/Platform/Android-Clang-CXX.cmake b/Modules/Platform/Android-Clang-CXX.cmake index 7111836..85d5088 100644 --- a/Modules/Platform/Android-Clang-CXX.cmake +++ b/Modules/Platform/Android-Clang-CXX.cmake @@ -1,2 +1,9 @@ include(Platform/Android-Clang) __android_compiler_clang(CXX) +if(_ANDROID_STL_NOSTDLIBXX) + if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6) + string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " -nostdlib++") + else() + string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " -nodefaultlibs -lgcc -lc -lm -ldl") + endif() +endif() diff --git a/Modules/Platform/Android-GNU-CXX.cmake b/Modules/Platform/Android-GNU-CXX.cmake index 41279d1..d30d0ff 100644 --- a/Modules/Platform/Android-GNU-CXX.cmake +++ b/Modules/Platform/Android-GNU-CXX.cmake @@ -1,2 +1,5 @@ include(Platform/Android-GNU) __android_compiler_gnu(CXX) +if(_ANDROID_STL_NOSTDLIBXX) + string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " -nodefaultlibs -lgcc -lc -lm -ldl") +endif() diff --git a/Modules/Platform/Android/ndk-stl-c++.cmake b/Modules/Platform/Android/ndk-stl-c++.cmake index a12411c..1cafd1f 100644 --- a/Modules/Platform/Android/ndk-stl-c++.cmake +++ b/Modules/Platform/Android/ndk-stl-c++.cmake @@ -1,6 +1,7 @@ # /sources/cxx-stl/llvm-libc++/Android.mk set(_ANDROID_STL_RTTI 1) set(_ANDROID_STL_EXCEPTIONS 1) +set(_ANDROID_STL_NOSTDLIBXX 1) macro(__android_stl_cxx lang filename) # Add the include directory. if(EXISTS "${CMAKE_ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libcxx/include/cstddef") diff --git a/Modules/Platform/Android/ndk-stl-gabi++.cmake b/Modules/Platform/Android/ndk-stl-gabi++.cmake index 850a47a..d3b9e45 100644 --- a/Modules/Platform/Android/ndk-stl-gabi++.cmake +++ b/Modules/Platform/Android/ndk-stl-gabi++.cmake @@ -1,6 +1,7 @@ # /sources/cxx-stl/gabi++/Android.mk set(_ANDROID_STL_RTTI 1) set(_ANDROID_STL_EXCEPTIONS 1) +set(_ANDROID_STL_NOSTDLIBXX 1) macro(__android_stl_gabixx lang filename) __android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gabi++/include" 1) __android_stl_lib(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gabi++/libs/${CMAKE_ANDROID_ARCH_ABI}/${filename}" 1) diff --git a/Modules/Platform/Android/ndk-stl-gnustl.cmake b/Modules/Platform/Android/ndk-stl-gnustl.cmake index b3226ee..46cedc6 100644 --- a/Modules/Platform/Android/ndk-stl-gnustl.cmake +++ b/Modules/Platform/Android/ndk-stl-gnustl.cmake @@ -1,6 +1,7 @@ # /sources/cxx-stl/gnu-libstdc++/Android.mk set(_ANDROID_STL_RTTI 1) set(_ANDROID_STL_EXCEPTIONS 1) +set(_ANDROID_STL_NOSTDLIBXX 1) macro(__android_stl_gnustl lang filename) __android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${CMAKE_${lang}_ANDROID_TOOLCHAIN_VERSION}/include" 1) __android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${CMAKE_${lang}_ANDROID_TOOLCHAIN_VERSION}/libs/${CMAKE_ANDROID_ARCH_ABI}/include" 1) diff --git a/Modules/Platform/Android/ndk-stl-none.cmake b/Modules/Platform/Android/ndk-stl-none.cmake index 9049c91..45122f7 100644 --- a/Modules/Platform/Android/ndk-stl-none.cmake +++ b/Modules/Platform/Android/ndk-stl-none.cmake @@ -1,2 +1,3 @@ +set(_ANDROID_STL_NOSTDLIBXX 1) macro(__android_stl lang) endmacro() diff --git a/Modules/Platform/Android/ndk-stl-stlport.cmake b/Modules/Platform/Android/ndk-stl-stlport.cmake index eab6b94..efad33b 100644 --- a/Modules/Platform/Android/ndk-stl-stlport.cmake +++ b/Modules/Platform/Android/ndk-stl-stlport.cmake @@ -1,6 +1,7 @@ # /sources/cxx-stl/stlport/Android.mk set(_ANDROID_STL_RTTI 1) set(_ANDROID_STL_EXCEPTIONS 1) +set(_ANDROID_STL_NOSTDLIBXX 0) macro(__android_stl_stlport lang filename) __android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/stlport/stlport" 1) __android_stl_lib(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/stlport/libs/${CMAKE_ANDROID_ARCH_ABI}/${filename}" 1) diff --git a/Modules/Platform/Android/ndk-stl-system.cmake b/Modules/Platform/Android/ndk-stl-system.cmake index dd554fe..7d86a40 100644 --- a/Modules/Platform/Android/ndk-stl-system.cmake +++ b/Modules/Platform/Android/ndk-stl-system.cmake @@ -1,6 +1,7 @@ # /android-ndk-r11c/sources/cxx-stl/system/Android.mk set(_ANDROID_STL_RTTI 0) set(_ANDROID_STL_EXCEPTIONS 0) +set(_ANDROID_STL_NOSTDLIBXX 0) macro(__android_stl lang) __android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/system/include" 1) endmacro() diff --git a/Tests/RunCMake/Android/android_lib.cxx b/Tests/RunCMake/Android/android_lib.cxx new file mode 100644 index 0000000..82f9d27 --- /dev/null +++ b/Tests/RunCMake/Android/android_lib.cxx @@ -0,0 +1,4 @@ +int android_lib() +{ + return 0; +} diff --git a/Tests/RunCMake/Android/check_binary.cmake b/Tests/RunCMake/Android/check_binary.cmake new file mode 100644 index 0000000..1d1b01a --- /dev/null +++ b/Tests/RunCMake/Android/check_binary.cmake @@ -0,0 +1,8 @@ +if(NOT EXISTS "${file}") + message(FATAL_ERROR "Missing file:\n ${file}") +endif() +execute_process(COMMAND "${objdump}" -p ${file} OUTPUT_VARIABLE out) +if(out MATCHES "NEEDED[^\n]*stdc\\+\\+") + string(REPLACE "\n" "\n " out " ${out}") + message(FATAL_ERROR "File:\n ${file}\ndepends on libstdc++:\n${out}") +endif() diff --git a/Tests/RunCMake/Android/common.cmake b/Tests/RunCMake/Android/common.cmake index 015f202..f931be1 100644 --- a/Tests/RunCMake/Android/common.cmake +++ b/Tests/RunCMake/Android/common.cmake @@ -92,6 +92,23 @@ if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a") endif() add_executable(android_c android.c) add_executable(android_cxx android.cxx) +add_library(android_cxx_lib SHARED android_lib.cxx) + +set(objdump "${CMAKE_CXX_ANDROID_TOOLCHAIN_PREFIX}objdump") +if(NOT EXISTS "${objdump}") + message(FATAL_ERROR "Expected tool missing:\n ${objdump}") +endif() + +if(NOT CMAKE_ANDROID_STL_TYPE MATCHES "^(system|stlport_static|stlport_shared)$") + foreach(tgt android_cxx android_cxx_lib) + add_custom_command(TARGET ${tgt} POST_BUILD + COMMAND ${CMAKE_COMMAND} + -Dobjdump=${objdump} + -Dfile=$ + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_binary.cmake + ) + endforeach() +endif() # Test that an explicit /usr/include is ignored in favor of # appearing as a standard include directory at the end. -- cgit v0.12