Skip to content

Commit

Permalink
[android] Move to the NDK's unified sysroot
Browse files Browse the repository at this point in the history
Since the NDK removes the platforms/ and sysroot/ directories in the latest NDK
22, switch to the unified sysroot in toolchains/llvm/ and take advantage of a
bunch of simplification that's now possible.
  • Loading branch information
finagolfin committed Feb 4, 2021
1 parent a25e2cd commit 10f2bb3
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 215 deletions.
18 changes: 5 additions & 13 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ function(_add_host_variant_c_compile_link_flags name)
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
endif()

if(SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID)
set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL})
endif()

# MSVC, clang-cl, gcc don't understand -target.
if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND NOT SWIFT_COMPILER_IS_MSVC_LIKE)
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
Expand Down Expand Up @@ -271,18 +275,6 @@ function(_add_host_variant_c_compile_flags target)
target_compile_options(${target} PRIVATE -funwind-tables)
endif()

if(SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID)
target_compile_options(${target} PRIVATE -nostdinc++)
swift_android_libcxx_include_paths(CFLAGS_CXX_INCLUDES)
swift_android_include_for_arch("${SWIFT_HOST_VARIANT_ARCH}"
"${SWIFT_HOST_VARIANT_ARCH}_INCLUDE")
target_include_directories(${target} SYSTEM PRIVATE
${CFLAGS_CXX_INCLUDES}
${${SWIFT_HOST_VARIANT_ARCH}_INCLUDE})
target_compile_definitions(${target} PRIVATE
__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL})
endif()

if(SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX")
if(SWIFT_HOST_VARIANT_ARCH STREQUAL x86_64)
# this is the minimum architecture that supports 16 byte CAS, which is
Expand Down Expand Up @@ -350,7 +342,7 @@ function(_add_host_variant_link_flags target)
target_link_libraries(${target} PRIVATE
${cxx_link_libraries})

swift_android_lib_for_arch(${SWIFT_HOST_VARIANT_ARCH}
swift_android_libgcc_for_arch_cross_compile(${SWIFT_HOST_VARIANT_ARCH}
${SWIFT_HOST_VARIANT_ARCH}_LIB)
target_link_directories(${target} PRIVATE
${${SWIFT_HOST_VARIANT_ARCH}_LIB})
Expand Down
2 changes: 1 addition & 1 deletion cmake/modules/AddSwiftUnittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function(add_swift_unittest test_dirname)
COMMAND "${SWIFT_SOURCE_DIR}/utils/swift-rpathize.py"
"$<TARGET_FILE:${test_dirname}>")
elseif("${SWIFT_HOST_VARIANT}" STREQUAL "android")
swift_android_lib_for_arch(${SWIFT_HOST_VARIANT_ARCH} android_system_libs)
swift_android_libgcc_for_arch_cross_compile(${SWIFT_HOST_VARIANT_ARCH} android_system_libs)
set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_DIRECTORIES
"${android_system_libs}")
set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES "log")
Expand Down
94 changes: 30 additions & 64 deletions cmake/modules/SwiftAndroidSupport.cmake
Original file line number Diff line number Diff line change
@@ -1,72 +1,50 @@
function(swift_android_libcxx_include_paths var)
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
set(${var}
"${SWIFT_ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/include"
"${SWIFT_ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++abi/include"
PARENT_SCOPE)
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
set(${var}
"${SWIFT_ANDROID_NATIVE_SYSROOT}/usr/include/c++/v1"
PARENT_SCOPE)
function(swift_android_prebuilt_host_name prebuilt_var_name)
# Get the prebuilt suffix to create the correct toolchain path when using the NDK
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
set(${prebuilt_var_name} darwin-x86_64 PARENT_SCOPE)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
set(${prebuilt_var_name} linux-x86_64 PARENT_SCOPE)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
set(${prebuilt_var_name} Windows-x86_64 PARENT_SCOPE)
else()
message(SEND_ERROR "Couldn't set libc++ include paths for Android")
message(SEND_ERROR "cannot cross-compile to android from ${CMAKE_HOST_SYSTEM_NAME}")
endif()
endfunction()

function(swift_android_include_for_arch arch var)
function(swift_android_libgcc_for_arch_cross_compile arch var)
set(paths)
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
list(APPEND paths
"${SWIFT_ANDROID_NDK_PATH}/sources/android/support/include"
"${SWIFT_ANDROID_NDK_PATH}/sysroot/usr/include"
"${SWIFT_ANDROID_NDK_PATH}/sysroot/usr/include/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}")
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
list(APPEND paths
"${SWIFT_ANDROID_NATIVE_SYSROOT}/usr/include"
"${SWIFT_ANDROID_NATIVE_SYSROOT}/usr/include/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}")
else()
message(SEND_ERROR "Couldn't set ${arch} include paths for Android")
list(APPEND paths "${SWIFT_SDK_ANDROID_ARCH_${arch}_PATH}/../lib/gcc/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}/${SWIFT_ANDROID_NDK_GCC_VERSION}.x")
endif()
set(${var} ${paths} PARENT_SCOPE)
endfunction()

function(swift_android_lib_for_arch arch var)
set(_prebuilt "${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_PREBUILT_PATH}")
set(_host "${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}")

set(paths)
function(swift_android_sysroot sysroot_var_name)
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
if(arch STREQUAL armv7)
list(APPEND paths "${_prebuilt}/${_host}/lib/armv7-a")
elseif(arch STREQUAL aarch64)
list(APPEND paths "${_prebuilt}/${_host}/lib64")
elseif(arch STREQUAL i686)
list(APPEND paths "${_prebuilt}/${_host}/lib")
elseif(arch STREQUAL x86_64)
list(APPEND paths "${_prebuilt}/${_host}/lib64")
else()
message(SEND_ERROR "unknown architecture (${arch}) for android")
endif()
list(APPEND paths "${_prebuilt}/lib/gcc/${_host}/${SWIFT_ANDROID_NDK_GCC_VERSION}.x")
swift_android_prebuilt_host_name(prebuilt_build)
set(${sysroot_var_name} "${SWIFT_ANDROID_NDK_PATH}/toolchains/llvm/prebuilt/${prebuilt_build}/sysroot" PARENT_SCOPE)
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
list(APPEND paths "${SWIFT_ANDROID_NATIVE_SYSROOT}/usr/lib")
if("${arch}" MATCHES armv7)
list(APPEND paths "/system/lib")
elseif("${arch}" MATCHES aarch64)
list(APPEND paths "/system/lib64")
else()
message(SEND_ERROR "unknown architecture (${arch}) when compiling for Android host")
endif()
set(${sysroot_var_name} "${SWIFT_ANDROID_NATIVE_SYSROOT}" PARENT_SCOPE)
else()
message(SEND_ERROR "Couldn't set ${arch} library paths for Android")
message(SEND_ERROR "Couldn't find Android sysroot")
endif()

set(${var} ${paths} PARENT_SCOPE)
endfunction()

function(swift_android_tools_path arch path_var_name)
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
set(${path_var_name} "${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_PREBUILT_PATH}/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}/bin" PARENT_SCOPE)
swift_android_prebuilt_host_name(prebuilt_build)
if("${arch}" STREQUAL "i686")
set(ndk_prebuilt_path
"${SWIFT_ANDROID_NDK_PATH}/toolchains/x86-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${prebuilt_build}")
elseif("${arch}" STREQUAL "x86_64")
set(ndk_prebuilt_path
"${SWIFT_ANDROID_NDK_PATH}/toolchains/x86_64-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${prebuilt_build}")
else()
set(ndk_prebuilt_path
"${SWIFT_ANDROID_NDK_PATH}/toolchains/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${prebuilt_build}")
endif()

set(${path_var_name} "${ndk_prebuilt_path}/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}/bin" PARENT_SCOPE)
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
set(${path_var_name} "${SWIFT_ANDROID_NATIVE_SYSROOT}/usr/bin" PARENT_SCOPE)
else()
Expand All @@ -77,19 +55,7 @@ endfunction ()
function(swift_android_cxx_libraries_for_arch arch libraries_var_name)
set(link_libraries)
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
if("${arch}" MATCHES armv7)
set(cxx_arch armeabi-v7a)
elseif("${arch}" MATCHES aarch64)
set(cxx_arch arm64-v8a)
elseif("${arch}" MATCHES i686)
set(cxx_arch x86)
elseif("${arch}" MATCHES x86_64)
set(cxx_arch x86_64)
else()
message(SEND_ERROR "unknown architecture (${arch}) when cross-compiling for Android")
endif()

set(android_libcxx_path "${SWIFT_ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/${cxx_arch}")
set(android_libcxx_path "${SWIFT_SDK_ANDROID_ARCH_${arch}_PATH}/usr/lib/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}")
list(APPEND link_libraries ${android_libcxx_path}/libc++abi.a
${android_libcxx_path}/libc++_shared.so)
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
Expand Down
58 changes: 4 additions & 54 deletions cmake/modules/SwiftConfigureSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ function(_report_sdk prefix)
if(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
message(STATUS " Sysroot: ${SWIFT_ANDROID_NATIVE_SYSROOT}")
endif()
foreach(arch ${SWIFT_SDK_${prefix}_ARCHITECTURES})
swift_android_include_for_arch(${arch} ${arch}_INCLUDE)
swift_android_lib_for_arch(${arch} ${arch}_LIB)
message(STATUS " ${arch} INCLUDE: ${${arch}_INCLUDE}")
message(STATUS " ${arch} LIB: ${${arch}_LIB}")
endforeach()
else()
foreach(arch ${SWIFT_SDK_${prefix}_ARCHITECTURES})
message(STATUS " ${arch} Path: ${SWIFT_SDK_${prefix}_ARCH_${arch}_PATH}")
Expand Down Expand Up @@ -273,76 +267,32 @@ macro(configure_sdk_unix name architectures)

foreach(arch ${architectures})
if("${prefix}" STREQUAL "ANDROID")
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_LIBC_INCLUDE_DIRECTORY "${SWIFT_ANDROID_NDK_PATH}/sysroot/usr/include" CACHE STRING "Path to C library headers")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_ANDROID_NDK_PATH}/sysroot/usr/include" CACHE STRING "Path to C library architecture headers")
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_LIBC_INCLUDE_DIRECTORY "${SWIFT_ANDROID_NATIVE_SYSROOT}/usr/include" CACHE STRING "Path to C library headers")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_ANDROID_NATIVE_SYSROOT}/usr/include" CACHE STRING "Path to C library architecture headers")
else()
message(SEND_ERROR "Couldn't find LIBC_INCLUDE_DIRECTORY for Android")
endif()
swift_android_sysroot(android_sysroot)
set(SWIFT_SDK_ANDROID_ARCH_${arch}_PATH "${android_sysroot}")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_LIBC_INCLUDE_DIRECTORY "${android_sysroot}/usr/include" CACHE STRING "Path to C library headers")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${android_sysroot}/usr/include" CACHE STRING "Path to C library architecture headers")

if("${arch}" STREQUAL "armv7")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE "arm-linux-androideabi")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_ALT_SPELLING "arm")
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_PATH "${SWIFT_ANDROID_NDK_PATH}/platforms/android-${SWIFT_ANDROID_API_LEVEL}/arch-arm")
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_PATH "${SWIFT_ANDROID_NATIVE_SYSROOT}")
else()
message(SEND_ERROR "Couldn't find SWIFT_SDK_ANDROID_ARCH_armv7_PATH")
endif()
set(SWIFT_SDK_ANDROID_ARCH_${arch}_TRIPLE "armv7-unknown-linux-androideabi")
# The Android ABI isn't part of the module triple.
set(SWIFT_SDK_ANDROID_ARCH_${arch}_MODULE "armv7-unknown-linux-android")
elseif("${arch}" STREQUAL "aarch64")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE "aarch64-linux-android")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_ALT_SPELLING "aarch64")
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_PATH "${SWIFT_ANDROID_NDK_PATH}/platforms/android-${SWIFT_ANDROID_API_LEVEL}/arch-arm64")
elseif(NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_PATH "${SWIFT_ANDROID_NATIVE_SYSROOT}")
else()
message(SEND_ERROR "Couldn't find SWIFT_SDK_ANDROID_ARCH_aarch64_PATH")
endif()
set(SWIFT_SDK_ANDROID_ARCH_${arch}_TRIPLE "aarch64-unknown-linux-android")
elseif("${arch}" STREQUAL "i686")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE "i686-linux-android")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_ALT_SPELLING "i686")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_PATH "${SWIFT_ANDROID_NDK_PATH}/platforms/android-${SWIFT_ANDROID_API_LEVEL}/arch-x86")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_TRIPLE "i686-unknown-linux-android")
elseif("${arch}" STREQUAL "x86_64")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE "x86_64-linux-android")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_ALT_SPELLING "x86_64")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_PATH "${SWIFT_ANDROID_NDK_PATH}/platforms/android-${SWIFT_ANDROID_API_LEVEL}/arch-x86_64")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_TRIPLE "x86_64-unknown-linux-android")
else()
message(FATAL_ERROR "unknown arch for android SDK: ${arch}")
endif()

# Get the prebuilt suffix to create the correct toolchain path when using the NDK
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
set(_swift_android_prebuilt_build darwin-x86_64)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
set(_swift_android_prebuilt_build linux-x86_64)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
set(_swift_android_prebuilt_build Windows-x86_64)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Android)
# When building natively on an Android host, there's no NDK or prebuilt suffix.
else()
message(SEND_ERROR "cannot cross-compile to android from ${CMAKE_HOST_SYSTEM_NAME}")
endif()
if("${arch}" STREQUAL "i686")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_PREBUILT_PATH
"${SWIFT_ANDROID_NDK_PATH}/toolchains/x86-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${_swift_android_prebuilt_build}")
elseif("${arch}" STREQUAL "x86_64")
set(SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_PREBUILT_PATH
"${SWIFT_ANDROID_NDK_PATH}/toolchains/x86_64-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${_swift_android_prebuilt_build}")
else()
set(SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_PREBUILT_PATH
"${SWIFT_ANDROID_NDK_PATH}/toolchains/${SWIFT_SDK_ANDROID_ARCH_${arch}_NDK_TRIPLE}-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${_swift_android_prebuilt_build}")
endif()
else()
set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH "/" CACHE STRING "CMAKE_SYSROOT for ${prefix} ${arch}")

Expand Down
Loading

0 comments on commit 10f2bb3

Please sign in to comment.