[CMake] Android NDK --gcc-toolchain issue with Clang

Stephen White senorblanco at chromium.org
Wed Apr 3 16:06:02 EDT 2019


Hi there,

Apologies in advance if this is not the correct mailing list.

I'm working on a C++ project which has existing CMake files and works on
Linux and Mac. I was attempting to build it on the Android NDK
cross-compiling to ARM64, and ran into an issue.

In particular, the toolchain determination for test compilation fails as
follows:


$HOME/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
--target=aarch64-none-linux-android
--gcc-toolchain=$HOME/Android/Sdk/ndk-bundle/toolchains//prebuilt/linux-x86_64
--sysroot=$HOME/Android/Sdk/ndk-bundle/platforms/android-28/arch-arm64
-funwind-tables -no-canonical-prefixes -D__ANDROID_API__=28 -fexceptions
-g  -fPIE -pie -Wl,--gc-sections
CMakeFiles/cmTC_34db0.dir/testCCompiler.c.o  -o cmTC_34db0
    /usr/bin/ld: unrecognised emulation mode: aarch64linux

The problem seems to be where --gcc-toolchain is set to:

$HOME/Android/Sdk/ndk-bundle/toolchains//prebuilt/linux-x86_64

(note the double slash).

On my installation of the NDK (r19c, Linux), the toolchain seems to be in:

$HOME/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/

My android.toolchain.cmake file is as follows:

set(CMAKE_SYSTEM_NAME Android)

set(CMAKE_ANDROID_NDK $ENV{HOME}/Android/Sdk/ndk-bundle)

set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)

set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)

set(CMAKE_ANDROID_STL_TYPE c++_shared)

I was able to get it to work by applying the small patch below:

diff --git a/Modules/Platform/Android/Determine-Compiler-NDK.cmake
b/Modules/Platform/Android/Determine-Compiler-NDK.cmake

index 5f2cc524e5..c211bbceee 100644

--- a/Modules/Platform/Android/Determine-Compiler-NDK.cmake

+++ b/Modules/Platform/Android/Determine-Compiler-NDK.cmake

@@ -239,7 +239,7 @@ set(_ANDROID_TOOL_CXX_TOOLCHAIN_SUFFIX
"${_ANDROID_TOOL_C_TOOLCHAIN_SUFFIX}")

 if(_ANDROID_TOOL_CLANG_NAME)

   message(STATUS "Android: Selected Clang toolchain
'${_ANDROID_TOOL_CLANG_NAME}' with GCC toolchain '${_ANDROID_TOOL_NAME}'")

   set(_ANDROID_TOOL_C_COMPILER
"${CMAKE_ANDROID_NDK}/toolchains/${_ANDROID_TOOL_LLVM_NAME}/prebuilt/${_ANDROID_HOST_DIR}/bin/clang${_ANDROID_HOST_EXT}")

-  set(_ANDROID_TOOL_C_COMPILER_EXTERNAL_TOOLCHAIN
${CMAKE_ANDROID_NDK}/toolchains/${_ANDROID_TOOL_NAME}/prebuilt/${_ANDROID_HOST_DIR})

+  set(_ANDROID_TOOL_C_COMPILER_EXTERNAL_TOOLCHAIN
${CMAKE_ANDROID_NDK}/toolchains/${_ANDROID_TOOL_LLVM_NAME}/prebuilt/${_ANDROID_HOST_DIR})

   set(_ANDROID_TOOL_CXX_COMPILER
"${CMAKE_ANDROID_NDK}/toolchains/${_ANDROID_TOOL_LLVM_NAME}/prebuilt/${_ANDROID_HOST_DIR}/bin/clang++${_ANDROID_HOST_EXT}")

   set(_ANDROID_TOOL_CXX_COMPILER_EXTERNAL_TOOLCHAIN
"${_ANDROID_TOOL_C_COMPILER_EXTERNAL_TOOLCHAIN}")

 else()


The reason seems to be that if ANDROID_TOOL_NAME contains -clang, it is set
to the empty string on line 120 (CMake 10.13), so the external toolchain
path contains an empty directory element (//). ANDROID_TOOL_LLVM_NAME
contains "llvm", which is the correct string to make the above path correct
on my installation.


If I'm doing something wrong and there's a better way to do this which
doesn't require patching CMake, please let me know.


Thanks,


Stephen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190403/31eef5dc/attachment-0001.html>


More information about the CMake mailing list