Skip to content

Commit a615d2c

Browse files
authored
Merge pull request #38146 from buttaface/cross-compile
[5.5][build] Add the flags to enable cross-compiling the corelibs
2 parents 44aab46 + 0aea098 commit a615d2c

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ endif()
461461

462462
set(SWIFT_BUILD_HOST_DISPATCH FALSE)
463463
if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
464-
if(SWIFT_BUILD_SYNTAXPARSERLIB OR SWIFT_BUILD_SOURCEKIT)
464+
# Only build libdispatch for the host if the host tools are being built and
465+
# specifically if these two libraries that depend on it are built.
466+
if(SWIFT_INCLUDE_TOOLS AND (SWIFT_BUILD_SYNTAXPARSERLIB OR SWIFT_BUILD_SOURCEKIT))
465467
set(SWIFT_BUILD_HOST_DISPATCH TRUE)
466468
endif()
467469

utils/build-script

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ class BuildScriptInvocation(object):
836836
"""
837837

838838
args = self.args
839+
args.build_root = self.workspace.build_root
839840

840841
options = {}
841842
for host_target in [args.host_target] + args.cross_compile_hosts:
@@ -857,6 +858,8 @@ class BuildScriptInvocation(object):
857858
config.swift_benchmark_run_targets),
858859
"SWIFT_TEST_TARGETS": " ".join(
859860
config.swift_test_run_targets),
861+
"SWIFT_FLAGS": config.swift_flags,
862+
"SWIFT_TARGET_CMAKE_OPTIONS": config.cmake_options,
860863
}
861864

862865
return options

utils/build-script-impl

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ KNOWN_SETTINGS=(
137137
common-cmake-options "" "CMake options used for all targets, including LLVM/Clang"
138138
extra-cmake-options "" "Extra options to pass to CMake for all targets"
139139
ninja-cmake-options "" "CMake options used for all ninja targets"
140-
ninja-cmake-options "" "CMake options used for all ninja targets"
141140

142141
## Build ...
143142
build-llvm "1" "set to 1 to build LLVM and Clang"
@@ -203,6 +202,7 @@ KNOWN_SETTINGS=(
203202
swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)"
204203
swift-stdlib-stable-abi "" "should stdlib be built with stable ABI, if not set defaults to true on Darwin, false otherwise"
205204
swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools"
205+
common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs"
206206

207207
## FREESTANDING Stdlib Options
208208
swift-freestanding-sdk "" "which SDK to use when building the FREESTANDING stdlib"
@@ -227,6 +227,7 @@ KNOWN_SETTINGS=(
227227
cross-compile-hosts "" "space-separated list of targets to cross-compile host Swift tools for"
228228
cross-compile-with-host-tools "" "set to use the clang we build for the host to then build the cross-compile hosts"
229229
cross-compile-install-prefixes "" "semicolon-separated list of install prefixes to use for the cross-compiled hosts. The list expands, so if there are more cross-compile hosts than prefixes, unmatched hosts use the last prefix in the list"
230+
cross-compile-deps-path "" "path for CMake to look for cross-compiled library dependencies, such as libXML2"
230231
skip-merge-lipo-cross-compile-tools "" "set to skip running merge-lipo after installing cross-compiled host Swift tools"
231232
coverage-db "" "If set, coverage database to use when prioritizing testing"
232233
skip-local-host-install "" "If we are cross-compiling multiple targets, skip an install pass locally if the hosts match"
@@ -1239,6 +1240,8 @@ function calculate_targets_for_host() {
12391240
SWIFT_BENCHMARK_TARGETS=($(get_host_specific_variable ${host} SWIFT_BENCHMARK_TARGETS))
12401241
SWIFT_RUN_BENCHMARK_TARGETS=($(get_host_specific_variable ${host} SWIFT_RUN_BENCHMARK_TARGETS))
12411242
SWIFT_TEST_TARGETS=($(get_host_specific_variable ${host} SWIFT_TEST_TARGETS))
1243+
SWIFT_FLAGS=($(get_host_specific_variable ${host} SWIFT_FLAGS))
1244+
SWIFT_TARGET_CMAKE_OPTIONS=($(get_host_specific_variable ${host} SWIFT_TARGET_CMAKE_OPTIONS))
12421245
}
12431246

12441247

@@ -1380,6 +1383,14 @@ function swift_c_flags() {
13801383
fi
13811384
}
13821385

1386+
function common_swift_flags() {
1387+
if [ "${module_cache}" == "" ] ; then
1388+
echo "error: a module cache path has not been set"
1389+
exit 1
1390+
fi
1391+
echo -n "${SWIFT_FLAGS[@]} ${COMMON_SWIFT_FLAGS} -module-cache-path \"${module_cache}\" "
1392+
}
1393+
13831394
function cmake_config_opt() {
13841395
product=$1
13851396
if [[ "${CMAKE_GENERATOR}" == "Xcode" ]] ; then
@@ -1685,6 +1696,9 @@ for host in "${ALL_HOSTS[@]}"; do
16851696
-DCMAKE_BUILD_TYPE:STRING="${CMARK_BUILD_TYPE}"
16861697
"${cmark_cmake_options[@]}"
16871698
)
1699+
if [[ $(is_cross_tools_host ${host}) ]] ; then
1700+
cmake_options+=("${SWIFT_TARGET_CMAKE_OPTIONS[@]}")
1701+
fi
16881702
build_targets=(all)
16891703
;;
16901704

@@ -1812,6 +1826,7 @@ for host in "${ALL_HOSTS[@]}"; do
18121826
-DCLANG_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/clang-tblgen
18131827
-DLLVM_NATIVE_BUILD=$(build_directory "${LOCAL_HOST}" llvm)
18141828
)
1829+
cmake_options+=("${SWIFT_TARGET_CMAKE_OPTIONS[@]}")
18151830
fi
18161831

18171832
;;
@@ -2222,7 +2237,7 @@ for host in "${ALL_HOSTS[@]}"; do
22222237
-DCMAKE_CXX_COMPILER:PATH="${CLANG_BIN}/clang++"
22232238
-DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
22242239
-DCMAKE_Swift_COMPILER:PATH=${SWIFTC_BIN}
2225-
-DCMAKE_Swift_FLAGS:STRING="-module-cache-path \"${module_cache}\""
2240+
-DCMAKE_Swift_FLAGS:STRING="$(common_swift_flags)"
22262241

22272242
-DLLBUILD_ENABLE_ASSERTIONS:BOOL=$(true_false "${LLBUILD_ENABLE_ASSERTIONS}")
22282243
-DLLBUILD_SUPPORT_BINDINGS:=Swift
@@ -2238,6 +2253,22 @@ for host in "${ALL_HOSTS[@]}"; do
22382253
-DFoundation_DIR:PATH=$(build_directory ${host} foundation)/cmake/modules
22392254
)
22402255

2256+
if [[ $(is_cross_tools_host ${host}) ]] ; then
2257+
cmake_options+=("${SWIFT_TARGET_CMAKE_OPTIONS[@]}")
2258+
2259+
# llbuild looks for the SQlite3 library using find_package(),
2260+
# so search for it in CROSS_COMPILE_DEPS_PATH using the CMake
2261+
# process for doing so and don't use cross-compiled
2262+
# executables, see the linked CMake docs for more info:
2263+
#
2264+
# https://cmake.org/cmake/help/latest/command/find_package.html
2265+
# https://cmake.org/cmake/help/latest/command/find_program.html
2266+
cmake_options+=(
2267+
-DCMAKE_FIND_ROOT_PATH:PATH="${CROSS_COMPILE_DEPS_PATH}"
2268+
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER
2269+
)
2270+
fi
2271+
22412272
# Ensure on Darwin platforms that we consider only the SQLite headers
22422273
# from the SDK instead of picking ones found elsewhere
22432274
# (e.g. in /usr/include )
@@ -2304,7 +2335,7 @@ for host in "${ALL_HOSTS[@]}"; do
23042335
-DCMAKE_C_COMPILER:PATH="${CLANG_BIN}/clang"
23052336
-DCMAKE_CXX_COMPILER:PATH="${CLANG_BIN}/clang++"
23062337
-DCMAKE_Swift_COMPILER:PATH=${SWIFTC_BIN}
2307-
-DCMAKE_Swift_FLAGS:STRING="-module-cache-path \"${module_cache}\""
2338+
-DCMAKE_Swift_FLAGS:STRING="$(common_swift_flags)"
23082339
-DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
23092340
-DCMAKE_INSTALL_LIBDIR:PATH="lib"
23102341

@@ -2320,6 +2351,9 @@ for host in "${ALL_HOSTS[@]}"; do
23202351

23212352
-DENABLE_TESTING=YES
23222353
)
2354+
if [[ $(is_cross_tools_host ${host}) ]] ; then
2355+
cmake_options+=("${SWIFT_TARGET_CMAKE_OPTIONS[@]}")
2356+
fi
23232357
;;
23242358
esac
23252359

@@ -2373,7 +2407,7 @@ for host in "${ALL_HOSTS[@]}"; do
23732407
-DCMAKE_CXX_COMPILER:PATH=${CLANG_BIN}/clang++
23742408
-DCMAKE_SWIFT_COMPILER:PATH=${SWIFTC_BIN}
23752409
-DCMAKE_Swift_COMPILER:PATH=${SWIFTC_BIN}
2376-
-DCMAKE_Swift_FLAGS:STRING="-module-cache-path \"${module_cache}\""
2410+
-DCMAKE_Swift_FLAGS:STRING="$(common_swift_flags)"
23772411
-DCMAKE_INSTALL_PREFIX:PATH=$(get_host_install_prefix ${host})
23782412

23792413
${LIBICU_BUILD_ARGS[@]}
@@ -2389,6 +2423,24 @@ for host in "${ALL_HOSTS[@]}"; do
23892423
-DBUILD_SHARED_LIBS=$([[ ${product} == foundation_static ]] && echo "NO" || echo "YES")
23902424
)
23912425

2426+
if [[ $(is_cross_tools_host ${host}) ]] ; then
2427+
cmake_options+=("${SWIFT_TARGET_CMAKE_OPTIONS[@]}")
2428+
2429+
# Foundation looks for the ICU, libXML2 and libcurl libraries
2430+
# using find_package(), so search for them in
2431+
# CROSS_COMPILE_DEPS_PATH using the CMake process for doing
2432+
# so, see the linked CMake docs for more info:
2433+
#
2434+
# https://cmake.org/cmake/help/latest/command/find_package.html
2435+
cmake_options+=(
2436+
-DCMAKE_FIND_ROOT_PATH:PATH="${CROSS_COMPILE_DEPS_PATH}"
2437+
)
2438+
fi
2439+
if [[ "${host}" == "android-"* ]]; then
2440+
cmake_options+=(
2441+
-DCMAKE_HAVE_LIBC_PTHREAD=True
2442+
)
2443+
fi
23922444
;;
23932445
libdispatch|libdispatch_static)
23942446
LIBDISPATCH_BUILD_DIR=$(build_directory ${host} ${product})
@@ -2415,7 +2467,7 @@ for host in "${ALL_HOSTS[@]}"; do
24152467
-DCMAKE_CXX_COMPILER:PATH="${CLANG_BIN}/clang++"
24162468
-DCMAKE_SWIFT_COMPILER:PATH="${SWIFTC_BIN}"
24172469
-DCMAKE_Swift_COMPILER:PATH="${SWIFTC_BIN}"
2418-
-DCMAKE_Swift_FLAGS:STRING="-module-cache-path \"${module_cache}\""
2470+
-DCMAKE_Swift_FLAGS:STRING="$(common_swift_flags)"
24192471
-DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
24202472
-DCMAKE_INSTALL_LIBDIR:PATH="lib"
24212473

@@ -2424,6 +2476,9 @@ for host in "${ALL_HOSTS[@]}"; do
24242476
-DENABLE_TESTING=YES
24252477
-DBUILD_SHARED_LIBS=$([[ ${product} == libdispatch_static ]] && echo "NO" || echo "YES")
24262478
)
2479+
if [[ $(is_cross_tools_host ${host}) ]] ; then
2480+
cmake_options+=("${SWIFT_TARGET_CMAKE_OPTIONS[@]}")
2481+
fi
24272482
;;
24282483
esac
24292484

utils/swift_build_support/swift_build_support/host_specific_configuration.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def __init__(self, host_target, args):
7272
self.swift_test_run_targets = []
7373
self.swift_benchmark_build_targets = []
7474
self.swift_benchmark_run_targets = []
75+
self.swift_flags = ''
76+
self.cmake_options = ''
7577
for deployment_target_name in stdlib_targets_to_configure:
7678
# Get the target object.
7779
deployment_target = StdlibDeploymentTarget.get_target_for_name(
@@ -194,6 +196,16 @@ def __init__(self, host_target, args):
194196
"check-swift{}-optimize_none_with_implicit_dynamic-{}"
195197
.format(subset_suffix, name))
196198

199+
# Only pull in these flags when cross-compiling with
200+
# --cross-compile-hosts.
201+
if deployment_target_name != args.host_target and \
202+
host_target != args.host_target:
203+
self.add_flags_for_cross_compilation(args, deployment_target)
204+
205+
def add_flags_for_cross_compilation(self, args, deployment_target):
206+
self.swift_flags = deployment_target.platform.swift_flags(args)
207+
self.cmake_options = deployment_target.platform.cmake_options(args)
208+
197209
def __platforms_to_skip_build(self, args):
198210
platforms_to_skip_build = set()
199211
if not args.build_linux:

utils/swift_build_support/swift_build_support/targets.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ def contains(self, target_name):
6868
return True
6969
return False
7070

71+
def swift_flags(self, args):
72+
"""
73+
Swift compiler flags for a platform, useful for cross-compiling
74+
"""
75+
return ''
76+
77+
def cmake_options(self, args):
78+
"""
79+
CMake flags to build for a platform, useful for cross-compiling
80+
"""
81+
return ''
82+
7183

7284
class DarwinPlatform(Platform):
7385
def __init__(self, name, archs, sdk_name=None, is_simulator=False):
@@ -136,6 +148,29 @@ def uses_host_tests(self):
136148
"""
137149
return True
138150

151+
def swift_flags(self, args):
152+
flags = '-target %s-unknown-linux-android%s ' % (args.android_arch,
153+
args.android_api_level)
154+
155+
flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
156+
args.build_root, self.name, args.android_arch)
157+
158+
android_toolchain_path = '%s/toolchains/llvm/prebuilt/%s' % (
159+
args.android_ndk, StdlibDeploymentTarget.host_target().name)
160+
161+
flags += '-sdk %s/sysroot ' % (android_toolchain_path)
162+
flags += '-tools-directory %s/bin' % (android_toolchain_path)
163+
return flags
164+
165+
def cmake_options(self, args):
166+
options = '-DCMAKE_SYSTEM_NAME=Android '
167+
options += '-DCMAKE_SYSTEM_VERSION=%s ' % (args.android_api_level)
168+
options += '-DCMAKE_SYSTEM_PROCESSOR=%s ' % (args.android_arch if not
169+
args.android_arch == 'armv7'
170+
else 'armv7-a')
171+
options += '-DCMAKE_ANDROID_NDK:PATH=%s' % (args.android_ndk)
172+
return options
173+
139174

140175
class Target(object):
141176
"""
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# REQUIRES: standalone_build
2+
# REQUIRES: OS=linux-gnu
3+
4+
# RUN: %empty-directory(%t)
5+
# RUN: SWIFT_BUILD_ROOT=%t %swift_src_root/utils/build-script --dry-run --cmake %cmake --libdispatch --cross-compile-hosts=android-aarch64 --skip-local-build --android --android-ndk %t/ndk/ --android-arch aarch64 --android-icu-uc %t/lib/libicuuc.so --android-icu-uc-include %t/include/ --android-icu-i18n %t/lib/libicui18n.so --android-icu-i18n-include %t/include/ --android-icu-data %t/lib/libicudata.so 2>&1 | %FileCheck %s
6+
7+
# CHECK: -DCMAKE_Swift_FLAGS{{.*}}-target {{.*}}unknown-linux-android{{.*}} -sdk
8+
# CHECK: -DCMAKE_SYSTEM_NAME=Android {{.*}} -DCMAKE_ANDROID_NDK

0 commit comments

Comments
 (0)