Skip to content

[android] Support building the host tools and with the static stdlib #34963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY

set(ELFISH_SDKS)
set(COFF_SDKS)
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
foreach(sdk ${SWIFT_SDKS})
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
list(APPEND ELFISH_SDKS ${sdk})
elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
Expand Down Expand Up @@ -144,7 +144,7 @@ add_swift_target_library(swiftImageRegistrationObjectCOFF
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
INSTALL_IN_COMPONENT none)

foreach(sdk ${SWIFT_CONFIGURED_SDKS})
foreach(sdk ${SWIFT_SDKS})
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
Expand Down Expand Up @@ -224,11 +224,18 @@ foreach(sdk ${SWIFT_CONFIGURED_SDKS})
set(libicu_data_a ${ICU_UC_LIBDIR}/libicudata.a)
endif()
endif()
set(libpthread -lpthread)
set(android_libraries)
if(sdk STREQUAL ANDROID)
set(android_libraries -llog)
set(libpthread)
endif()

set(linkfile ${lowercase_sdk}/static-stdlib-args.lnk)
file(WRITE "${SWIFTSTATICLIB_DIR}/${linkfile}" "
-ldl
-lpthread
${libpthread}
${android_libraries}
-lswiftCore
${libicu_i18n_a}
${libicu_uc_a}
Expand Down
45 changes: 33 additions & 12 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,35 @@ function set_build_options_for_host() {
SWIFT_HOST_VARIANT_ARCH=$architecture

case ${host} in
android-aarch64)
SWIFT_HOST_TRIPLE="aarch64-unknown-linux-android"
llvm_target_arch="AArch64"
;;
android-armv7)
SWIFT_HOST_TRIPLE="armv7-unknown-linux-androideabi"
llvm_target_arch="ARM"
;;
android-x86_64)
SWIFT_HOST_TRIPLE="x86_64-unknown-linux-android${ANDROID_API_LEVEL}"
llvm_target_arch="X86"
android-*)
# Clang uses a different sysroot natively on Android in the Termux
# app, which the Termux build scripts pass in through a $PREFIX
# variable.
if [[ "${PREFIX}" ]] ; then
llvm_cmake_options+=(
-DDEFAULT_SYSROOT:STRING="$(dirname ${PREFIX})"
)
fi
# Android doesn't support building all of compiler-rt yet.
if [[ ! $(is_cross_tools_host "${host}") ]] ; then
llvm_cmake_options+=(
-DCOMPILER_RT_INCLUDE_TESTS:BOOL=FALSE
)
fi
case ${host} in
android-aarch64)
SWIFT_HOST_TRIPLE="aarch64-unknown-linux-android"
llvm_target_arch="AArch64"
;;
android-armv7)
SWIFT_HOST_TRIPLE="armv7-unknown-linux-androideabi"
llvm_target_arch="ARM"
;;
android-x86_64)
SWIFT_HOST_TRIPLE="x86_64-unknown-linux-android${ANDROID_API_LEVEL}"
llvm_target_arch="X86"
;;
esac
;;
linux-armv6)
SWIFT_HOST_TRIPLE="armv6-unknown-linux-gnueabihf"
Expand Down Expand Up @@ -1756,7 +1774,8 @@ for host in "${ALL_HOSTS[@]}"; do
)
fi

if [[ ! "${SKIP_BUILD_ANDROID}" ]]; then
if [[ ! "${SKIP_BUILD_ANDROID}" ]] ||
[[ $(is_cross_tools_host ${host}) && "${host}" == "android-"* ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because if you don't skip building Android, the stdlib would also be built? Isn't there a cleaner or clearer way of doing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is a minor addition to signify that the NDK can also be used to cross-compile the host tools and not just the Android stdlib, as SKIP_BUILD_ANDROID confusingly just means "don't build the Android stdlib." It fixes a bug I hit where I was only cross-compiling the host tools for Android and not the stdlib, which the unmodified version of this check would screw up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that you are trying to cross-compile the tools. What I don't understand is that all these variables (if I remember correctly) are used during the compilation of the stdlib. Some host tools use Swift, and so they require a stdlib to be built, so I don't understand how the stdlib is both skipped, and used at the same time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe that any of the Swift host tools, like the Swift compiler or SourceKit, use any Swift yet, as I was building the host tools alone till earlier this year. A search for .swift files in the lib/ and tools/ directories only turns up anything in tools/swift-inspect, which I don't think is built by default.

cmake_options=(
"${cmake_options[@]}"
-DSWIFT_ANDROID_NDK_PATH:STRING="${ANDROID_NDK}"
Expand Down Expand Up @@ -2447,6 +2466,8 @@ for host in "${ALL_HOSTS[@]}"; do
elif [[ "$(uname -s)" == "Haiku" ]] ; then
HOST_CXX_HEADERS_DIR="/boot/system/develop/headers/c++"
elif [[ "${ANDROID_DATA}" ]] ; then
# This means we're building natively on Android in the Termux
# app, which supplies the $PREFIX variable.
HOST_CXX_HEADERS_DIR="$PREFIX/include/c++"
else # Linux
HOST_CXX_HEADERS_DIR="/usr/include/c++"
Expand Down