Android: Wrong 'ar' selected on Darwin hosts
Some time between CMake 3.22 and 3.26, CMake started incorrectly identifying the ar binary for Android targets from macOS hosts:
$ cmake --version
cmake version 3.26.4
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.26)
project(CMakeArTest C CXX)
if(APPLE)
message(FATAL_ERROR "should not be APPLE")
endif()
foreach(TEST_VAR CMAKE_C_COMPILER CMAKE_CXX_COMPILER CMAKE_AR CMAKE_STRIP CMAKE_RANLIB)
if(NOT DEFINED "${TEST_VAR}")
message(FATAL_ERROR "${TEST_VAR} not set")
elseif(NOT ${TEST_VAR} MATCHES "${CMAKE_ANDROID_NDK}")
message(FATAL_ERROR "${TEST_VAR} (${${TEST_VAR}}) is outside the NDK (${CMAKE_ANDROID_NDK})")
else()
message(WARNING "${TEST_VAR} is ${${TEST_VAR}}")
endif()
endforeach()
$ mkdir build
$ cd build
$ cmake -DCMAKE_ANDROID_NDK=/Users/danalbert/Library/Android/sdk/ndk/25.2.9519653 -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 ..
-- Android: Targeting API '21' with architecture 'arm', ABI 'armeabi-v7a', and processor 'armv7-a'
-- Android: Selected unified Clang toolchain
-- The C compiler identification is Clang 14.0.7
-- The CXX compiler identification is Clang 14.0.7
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Users/danalbert/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Users/danalbert/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at CMakeLists.txt:14 (message):
CMAKE_C_COMPILER is
/Users/danalbert/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
CMake Warning at CMakeLists.txt:14 (message):
CMAKE_CXX_COMPILER is
/Users/danalbert/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++
CMake Error at CMakeLists.txt:12 (message):
CMAKE_AR (/usr/bin/ar) is outside the NDK
(/Users/danalbert/Library/Android/sdk/ndk/25.2.9519653)
-- Configuring incomplete, errors occurred!
("25.2.9519653" decodes to NDK r25c, downloadable at https://github.com/android/ndk/wiki)
I suspect !7039 (merged) is the culprit. If you run that command with --trace
you'll see something very odd:
/Applications/CMake.app/Contents/share/cmake-3.26/Modules/CMakeFindBinUtils.cmake(176): if(APPLE )
/Applications/CMake.app/Contents/share/cmake-3.26/Modules/CMakeFindBinUtils.cmake(181): list(APPEND _CMAKE_AR_NAMES llvm-ar )
https://gitlab.kitware.com/cmake/cmake/-/blob/v3.26.4/Modules/CMakeFindBinUtils.cmake#L176-184
APPLE
is evaluating to true despite -DCMAKE_SYSTEM_NAME=Android
. It seems that at the time CMakeFindBinUtils.cmake is executed, the build target has not yet been set correctly.
ac2562af and a54e25b5 are related commits that might cause similar problems that are not yet released (I think).
See https://github.com/android/ndk/issues/1698 for more context if needed.