diff --git a/build/cmake/android.toolchain.cmake b/build/cmake/android.toolchain.cmake index f1bc702c..18d577f2 100644 --- a/build/cmake/android.toolchain.cmake +++ b/build/cmake/android.toolchain.cmake @@ -268,6 +268,25 @@ 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/docs/changelogs/Changelog-r23.md b/docs/changelogs/Changelog-r23.md index 266dfdea..fb612e70 100644 --- a/docs/changelogs/Changelog-r23.md +++ b/docs/changelogs/Changelog-r23.md @@ -47,13 +47,21 @@ For Android Studio issues, follow the docs on the [Android Studio site]. * [Issue 1573]: Fixed `ANDROID_USE_LEGACY_TOOLCHAIN_FILE` not being obeyed during CMake try-compile. * [Issue 1569]: Fixed `-fno-integrated-as` not being able to find the assembler. - +* [Issue 1581]: Added workaround for [CMake Issue 22647], which was causing + `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. + +[CMake Issue 22647]: https://gitlab.kitware.com/cmake/cmake/-/issues/22647 [Issue 1536]: https://github.com/android/ndk/issues/1536 [Issue 1544]: https://github.com/android/ndk/issues/1544 [Issue 1553]: https://github.com/android/ndk/issues/1553 [Issue 1560]: https://github.com/android/ndk/issues/1560 [Issue 1573]: https://github.com/android/ndk/issues/1573 [Issue 1569]: https://github.com/android/ndk/issues/1569 +[Issue 1581]: https://github.com/android/ndk/issues/1581 ## Changes diff --git a/tests/build/cmake_not_mingw/CMakeLists.txt b/tests/build/cmake_not_mingw/CMakeLists.txt new file mode 100644 index 00000000..90082142 --- /dev/null +++ b/tests/build/cmake_not_mingw/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.6) +project(CMakeNotMinGW ASM C CXX) + +if(DEFINED MINGW) + message(FATAL_ERROR "MINGW should not be defined") +endif()