From 87f9a1175ecf284e0009e693f4479deb4938b180 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Tue, 17 Feb 2026 15:36:44 -0800 Subject: [PATCH 1/2] Android: Use CMAKE_SYSROOT for Android sysroot The NDK configures `CMAKE_SYSROOT` in `android-legacy.toolchain.cmake`. I've verified this from Android NDK r26b to r29. This gets the build system cross-compiling to Android from a non-Windows builder. --- Runtimes/Overlay/Android/clang/CMakeLists.txt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Runtimes/Overlay/Android/clang/CMakeLists.txt b/Runtimes/Overlay/Android/clang/CMakeLists.txt index 378aefa2e641d..2054240afa823 100644 --- a/Runtimes/Overlay/Android/clang/CMakeLists.txt +++ b/Runtimes/Overlay/Android/clang/CMakeLists.txt @@ -1,5 +1,3 @@ - -# FIXME: how do we determine the sysroot? `CMAKE_SYSROOT` does not contain the sysroot. file(CONFIGURE OUTPUT android-ndk-overlay.yaml CONTENT [[ @@ -8,7 +6,7 @@ version: 0 case-sensitive: false use-external-names: true roots: - - name: "@CMAKE_ANDROID_NDK@/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include" + - name: "@CMAKE_SYSROOT@/usr/include" type: directory contents: - name: module.modulemap @@ -25,7 +23,7 @@ ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF) add_library(SwiftAndroid INTERFACE) target_compile_options(SwiftAndroid INTERFACE - "$<$:SHELL:-Xcc --sysroot=\"${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot\">" + "$<$:SHELL:-Xcc --sysroot=\"${CMAKE_SYSROOT}\">" "$<$:SHELL:-vfsoverlay ${CMAKE_CURRENT_BINARY_DIR}/android-ndk-overlay.yaml>") install(TARGETS SwiftAndroid From 0cef5bcd00c4f1d4864cb3df88d6da80159931d0 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Tue, 24 Mar 2026 15:28:38 -0700 Subject: [PATCH 2/2] Android: Update build.ps1 to use CMAKE_SYSROOT When cross-compiling, the toolchain file is supposed to set the `CMAKE_SYSROOT` variable, which In this case, there is no toolchain file to set it as build.ps1 is passing everything through explicitly. Setting it explicitly now for the C and C++ bits and for computing flags. CMake doesn't yet support passing `CMAKE_SYSROOT` to the Swift compiler via the `sysroot` flag though: https://gitlab.kitware.com/cmake/cmake/-/work_items/27717 For now, we need to keep passing it as an explicit Swift flag. --- utils/build.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/utils/build.ps1 b/utils/build.ps1 index e6859e50be8c3..f42f18757278f 100644 --- a/utils/build.ps1 +++ b/utils/build.ps1 @@ -1747,6 +1747,7 @@ function Build-CMakeProject { Add-KeyValueIfNew $Defines CMAKE_ANDROID_API "$AndroidAPILevel" Add-KeyValueIfNew $Defines CMAKE_ANDROID_ARCH_ABI $Platform.Architecture.ABI Add-KeyValueIfNew $Defines CMAKE_ANDROID_NDK "$AndroidNDKPath" + Add-KeyValueIfNew $Defines CMAKE_SYSROOT "$AndroidSysroot" if ($UseASM) { } @@ -1754,7 +1755,7 @@ function Build-CMakeProject { if ($UseC) { Add-KeyValueIfNew $Defines CMAKE_C_COMPILER_TARGET $Platform.Triple - $CFLAGS = @("--sysroot=${AndroidSysroot}", "-ffunction-sections", "-fdata-sections") + $CFLAGS = @("-ffunction-sections", "-fdata-sections") if ($DebugInfo) { $CFLAGS += @("-gdwarf") } @@ -1764,7 +1765,7 @@ function Build-CMakeProject { if ($UseCXX) { Add-KeyValueIfNew $Defines CMAKE_CXX_COMPILER_TARGET $Platform.Triple - $CXXFLAGS = @("--sysroot=${AndroidSysroot}", "-ffunction-sections", "-fdata-sections") + $CXXFLAGS = @("-ffunction-sections", "-fdata-sections") if ($DebugInfo) { $CXXFLAGS += @("-gdwarf") } @@ -1790,6 +1791,8 @@ function Build-CMakeProject { [string[]] $SwiftFlags = @() $SwiftFlags += if ($SwiftSDK) { + # TODO: CMake does not yet have support for passing `CMAKE_SYSROOT` to the Swift compiler yet. + # Once we have that, we can drop `-sysroot $AndroidSysroot` here. @("-sdk", $SwiftSDK, "-sysroot", $AndroidSysroot) } else { @()