From 02dfb0233b2a99d195c29163f96fd19204b3bd70 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 16 Sep 2021 15:24:48 -0700 Subject: [PATCH] Improve the cmake compiler ID workaround. Move this into the hook so it works for non-toolchain file users too. Thanks hhb for the pointer! Test: re-tested this on Windows Bug: https://github.com/android/ndk/issues/1581 Change-Id: I39a1c8646d5853eea4bf557c6f6efa2ca5244cf2 --- build/cmake/android.toolchain.cmake | 19 --------------- .../cmake/hooks/pre/Determine-Compiler.cmake | 23 +++++++++++++++++++ docs/changelogs/Changelog-r23.md | 3 +-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/build/cmake/android.toolchain.cmake b/build/cmake/android.toolchain.cmake index 18d577f2..f1bc702c 100644 --- a/build/cmake/android.toolchain.cmake +++ b/build/cmake/android.toolchain.cmake @@ -268,25 +268,6 @@ endif() set(ANDROID_TOOLCHAIN_ROOT "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/${ANDROID_HOST_TAG}") -# If we don't explicitly set the target CMake will ID the compiler using the -# default target, causing MINGW to be defined when a Windows host is used. -# https://github.com/android/ndk/issues/1581 -# https://gitlab.kitware.com/cmake/cmake/-/issues/22647 -if(CMAKE_ANDROID_ARCH_ABI STREQUAL armeabi-v7a) - set(ANDROID_LLVM_TRIPLE armv7-none-linux-androideabi) -elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL arm64-v8a) - set(ANDROID_LLVM_TRIPLE aarch64-none-linux-android) -elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL x86) - set(ANDROID_LLVM_TRIPLE i686-none-linux-android) -elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL x86_64) - set(ANDROID_LLVM_TRIPLE x86_64-none-linux-android) -else() - message(FATAL_ERROR "Invalid Android ABI: ${ANDROID_ABI}.") -endif() -set(CMAKE_ASM_COMPILER_TARGET "${ANDROID_LLVM_TRIPLE}${CMAKE_SYSTEM_VERSION}") -set(CMAKE_C_COMPILER_TARGET "${ANDROID_LLVM_TRIPLE}${CMAKE_SYSTEM_VERSION}") -set(CMAKE_CXX_COMPILER_TARGET "${ANDROID_LLVM_TRIPLE}${CMAKE_SYSTEM_VERSION}") - # NB: This variable causes CMake to automatically pass --sysroot to the # toolchain. Studio currently relies on this to recognize Android builds. If # this variable is removed, ensure that flag is still passed. diff --git a/build/cmake/hooks/pre/Determine-Compiler.cmake b/build/cmake/hooks/pre/Determine-Compiler.cmake index b8ed1bfe..ef0228f1 100644 --- a/build/cmake/hooks/pre/Determine-Compiler.cmake +++ b/build/cmake/hooks/pre/Determine-Compiler.cmake @@ -15,3 +15,26 @@ # This is a hook file that will be included by cmake at the beginning of # Modules/Platform/Android/Determine-Compiler.cmake. +# Skip hook for the legacy toolchain workflow. +if(CMAKE_SYSTEM_VERSION EQUAL 1) + return() +endif() + +# If we don't explicitly set the target CMake will ID the compiler using the +# default target, causing MINGW to be defined when a Windows host is used. +# https://github.com/android/ndk/issues/1581 +# https://gitlab.kitware.com/cmake/cmake/-/issues/22647 +if(CMAKE_ANDROID_ARCH_ABI STREQUAL armeabi-v7a) + set(ANDROID_LLVM_TRIPLE armv7-none-linux-androideabi) +elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL arm64-v8a) + set(ANDROID_LLVM_TRIPLE aarch64-none-linux-android) +elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL x86) + set(ANDROID_LLVM_TRIPLE i686-none-linux-android) +elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL x86_64) + set(ANDROID_LLVM_TRIPLE x86_64-none-linux-android) +else() + message(FATAL_ERROR "Invalid Android ABI: ${ANDROID_ABI}.") +endif() +set(CMAKE_ASM_COMPILER_TARGET "${ANDROID_LLVM_TRIPLE}${CMAKE_SYSTEM_VERSION}") +set(CMAKE_C_COMPILER_TARGET "${ANDROID_LLVM_TRIPLE}${CMAKE_SYSTEM_VERSION}") +set(CMAKE_CXX_COMPILER_TARGET "${ANDROID_LLVM_TRIPLE}${CMAKE_SYSTEM_VERSION}") diff --git a/docs/changelogs/Changelog-r23.md b/docs/changelogs/Changelog-r23.md index de411231..c7e37acd 100644 --- a/docs/changelogs/Changelog-r23.md +++ b/docs/changelogs/Changelog-r23.md @@ -55,8 +55,7 @@ For Android Studio issues, follow the docs on the [Android Studio site]. `MINGW` to be incorrectly defined by CMake when building for Android on a Windows host. This only affected those using the Android toolchain file when CMake 3.21 or newer was used. This likely was not a regression for users not - using the Android toolchain, and the workaround only affects toolchain file - users. + using the Android toolchain. The change will fix both use cases. [CMake Issue 22647]: https://gitlab.kitware.com/cmake/cmake/-/issues/22647 [Issue 1536]: https://github.com/android/ndk/issues/1536