Skip to content

Commit

Permalink
Prevent CMake from identifying Android as MinGW.
Browse files Browse the repository at this point in the history
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.

Test: added
Bug: android/ndk#1581
Bug: https://gitlab.kitware.com/cmake/cmake/-/issues/22647
Change-Id: I3c4829eefd4fad6fbe85709c0d045b5e595b7b8e
  • Loading branch information
DanAlbert committed Sep 15, 2021
1 parent 770f59e commit 063d2b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
19 changes: 19 additions & 0 deletions build/cmake/android.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion docs/changelogs/Changelog-r23.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions tests/build/cmake_not_mingw/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 063d2b8

Please sign in to comment.