VS: FetchContent may not work with `-A` when cross-compiling for Android
This is describing the behavior of the Visual Studio generator when cross-compiling for Android.
Preamble
While the architecture can be omitted when invoking the generator (i.e. -A
param) and the NDK architecture can be set from a toolchain file through CMAKE_ANDROID_ARCH_ABI
, this would lead to an inconsistent architecture (i.e. the one in the .vcxproj
will be x64
, while the one from the toolchain would be ARM64
for example), but also the wrong .props
file being loaded by Visual Studio as for an Android
application-type, .props file are defined for each Android architecture (x86
, x64
, ARM
and ARM64
).
CMake properly maps the CMAKE_GENERATOR_PLATFORM
to CMAKE_ANDROID_ARCH_ABI
if the later is not defined. As such, invoking the generator with -A ARM64
for example seems preferable. And this works perfectly.
Issue
With that said, this may not work with FetchContent
, because the toolchain file is not forwarded, but even if it would be (because I tried), it would still fail because it is not loaded until a project()
call. As FetchContent
creates its own CMake context, the later will try to find the VCTargetsPath
again, but without the context that it is cross-compiling. With that said, CMAKE_GENERATOR_PLATFORM
and CMAKE_GENERATOR_TOOLSET
are still forwarded to the underlying CMake context. As such, when cross-compiling for Android, the .vcxproj
trying to resolve the VCTargetsPath
will not be an Android
application, but will still use the ARM64
platform. If you did not install the ARM64 platform for Windows, this will fail. And considering that x86
is named Win32
on Windows, that architecture will not work either way.
It feels like the FetchContent sub-project cannot be cross-compiling (or maybe it should?) but at the same time may inherit some properties that are specific to the cross-compiled platform. Maybe it should use a generic platform, but it seems almost impossible to know what to use.
Test Project
The following project will reproduce the issue: cmake-android-fetchcontent.zip
Without fetching anything (success):
F:\Tools\cmake-3.29\bin\cmake.exe -G "Visual Studio 17 2022" -A "ARM64" -D CMAKE_TOOLCHAIN_FILE=android.cmake ./
With fetching something (failure):
F:\Tools\cmake-3.29\bin\cmake.exe -G "Visual Studio 17 2022" -A "ARM64" -D CMAKE_TOOLCHAIN_FILE=android.cmake -D ENABLE_FETCH_CONTENT=TRUE ./