Skip to content

Commit

Permalink
Improve the cmake compiler ID workaround.
Browse files Browse the repository at this point in the history
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: android/ndk#1581
Change-Id: I39a1c8646d5853eea4bf557c6f6efa2ca5244cf2
  • Loading branch information
DanAlbert committed Sep 16, 2021
1 parent d5a6aad commit 02dfb02
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
19 changes: 0 additions & 19 deletions build/cmake/android.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions build/cmake/hooks/pre/Determine-Compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
3 changes: 1 addition & 2 deletions docs/changelogs/Changelog-r23.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 02dfb02

Please sign in to comment.