diff --git a/utils/build-presets.ini b/utils/build-presets.ini index 98eacd8970755..2955ff3ea31c7 100644 --- a/utils/build-presets.ini +++ b/utils/build-presets.ini @@ -1214,6 +1214,9 @@ compiler-vendor=apple dash-dash +# Cross compile for Apple Silicon +cross-compile-hosts=macosx-arm64 + lldb-no-debugserver lldb-use-system-debugserver lldb-build-type=Release diff --git a/utils/build-script b/utils/build-script index 84417ed56153a..99974bc65b868 100755 --- a/utils/build-script +++ b/utils/build-script @@ -942,8 +942,8 @@ class BuildScriptInvocation(object): for product_class in impl_product_classes: self._execute_install_action(host_target, product_class) - # Lipo... - self._execute_merged_host_lipo_action() + # Core Lipo... + self._execute_merged_host_lipo_core_action() # Non-build-script-impl products... # Note: currently only supports building for the host. @@ -984,6 +984,9 @@ class BuildScriptInvocation(object): for host_target in all_hosts: self._execute_package_action(host_target) + # Lipo... + self._execute_merged_host_lipo_action() + def _execute_build_action(self, host_target, product_class): action_name = "{}-{}-build".format(host_target.name, product_class.product_name()) @@ -1010,6 +1013,9 @@ class BuildScriptInvocation(object): def _execute_merged_host_lipo_action(self): self._execute_action("merged-hosts-lipo") + def _execute_merged_host_lipo_core_action(self): + self._execute_action("merged-hosts-lipo-core") + def _execute_action(self, action_name): shell.call_without_sleeping( [BUILD_SCRIPT_IMPL_PATH] + self.impl_args + diff --git a/utils/build-script-impl b/utils/build-script-impl index 127f6d8c30f32..9ce5c660dfe0d 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -422,6 +422,7 @@ function set_build_options_for_host() { swift_cmake_options=() cmark_cmake_options=() lldb_cmake_options=() + llbuild_cmake_options=() SWIFT_HOST_VARIANT= SWIFT_HOST_VARIANT_SDK= SWIFT_HOST_VARIANT_ARCH= @@ -643,6 +644,7 @@ function set_build_options_for_host() { -DCMAKE_CXX_FLAGS="$(cmark_c_flags ${host})" -DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}" -DCMAKE_OSX_DEPLOYMENT_TARGET="${cmake_osx_deployment_target}" + -DCMAKE_OSX_ARCHITECTURES="${architecture}" ) llvm_cmake_options=( -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING="${cmake_osx_deployment_target}" @@ -652,6 +654,7 @@ function set_build_options_for_host() { -DCOMPILER_RT_ENABLE_TVOS:BOOL=FALSE -DSANITIZER_MIN_OSX_VERSION="${cmake_osx_deployment_target}" -DLLVM_ENABLE_MODULES:BOOL="$(true_false ${LLVM_ENABLE_MODULES})" + -DCMAKE_OSX_ARCHITECTURES="${architecture}" ) if [[ $(is_llvm_lto_enabled) == "TRUE" ]]; then llvm_cmake_options+=( @@ -683,6 +686,10 @@ function set_build_options_for_host() { lldb_cmake_options+=( -DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}" + -DCMAKE_OSX_ARCHITECTURES="${architecture}" + ) + llbuild_cmake_options+=( + -DCMAKE_OSX_ARCHITECTURES="${architecture}" ) ;; esac @@ -2021,6 +2028,7 @@ for host in "${ALL_HOSTS[@]}"; do llbuild) cmake_options=( "${cmake_options[@]}" + "${llbuild_cmake_options[@]}" -DCMAKE_BUILD_TYPE:STRING="${LLBUILD_BUILD_TYPE}" -DCMAKE_C_COMPILER:PATH="${CLANG_BIN}/clang" @@ -3026,11 +3034,7 @@ function build_and_test_installable_package() { local host_install_destdir="$(get_host_install_destdir ${host})" local host_install_prefix="$(get_host_install_prefix ${host})" - if [[ $(has_cross_compile_hosts) ]]; then - package_for_host="${INSTALLABLE_PACKAGE}-${host}" - else - package_for_host="${INSTALLABLE_PACKAGE}" - fi + package_for_host="${INSTALLABLE_PACKAGE}" echo "--- Creating installable package ---" echo "-- Package file: ${package_for_host} --" @@ -3100,7 +3104,7 @@ function build_and_test_installable_package() { PKG_TESTS_SANDBOX_PARENT="$(build_directory swift_package_sandbox_${host} none)" PKG_TESTS_TEMPS="${PKG_TESTS_SANDBOX_PARENT}"/"tests" - if [[ "${host}" == "macosx-"* ]] ; then + if [[ "${host}" == "macosx-"* ]] || [[ "${host}" == "merged-hosts" ]]; then PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"/"${TOOLCHAIN_PREFIX}" else # Linux PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}" @@ -3141,7 +3145,7 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then # This is from multiple hosts; Which host should we say it is? # Let's call it 'merged-hosts' so that we can identify it. - if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then + if [[ $(should_execute_action "${mergedHost}-lipo") || $(should_execute_action "${mergedHost}-lipo-core") ]]; then # Allow passing lipo with --host-lipo if [[ -z "${HOST_LIPO}" ]] ; then LIPO_PATH=$(xcrun_find_tool lipo) @@ -3150,8 +3154,10 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then fi call "${SWIFT_SOURCE_DIR}"/utils/recursive-lipo --lipo=${LIPO_PATH} --copy-subdirs="$(get_host_install_prefix ${host})lib/swift $(get_host_install_prefix ${host})lib/swift_static" --destination="$(get_host_install_destdir ${mergedHost})" ${LIPO_SRC_DIRS[@]} - # Build and test the lipo-ed package. - build_and_test_installable_package ${mergedHost} + if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then + # Build and test the lipo-ed package. + build_and_test_installable_package ${mergedHost} + fi fi fi # END diff --git a/utils/swift_build_support/swift_build_support/products/benchmarks.py b/utils/swift_build_support/swift_build_support/products/benchmarks.py index cac1e241e4451..b7a14ecc658fa 100644 --- a/utils/swift_build_support/swift_build_support/products/benchmarks.py +++ b/utils/swift_build_support/swift_build_support/products/benchmarks.py @@ -82,7 +82,9 @@ def get_dependencies(cls): def run_build_script_helper(host_target, product, args): - toolchain_path = args.install_destdir + toolchain_path = swiftpm.SwiftPM.get_install_destdir(args, + host_target, + product.build_dir) if platform.system() == 'Darwin': # The prefix is an absolute path, so concatenate without os.path. toolchain_path += \ diff --git a/utils/swift_build_support/swift_build_support/products/indexstoredb.py b/utils/swift_build_support/swift_build_support/products/indexstoredb.py index 65d720c4889fa..83b4ac91811c4 100644 --- a/utils/swift_build_support/swift_build_support/products/indexstoredb.py +++ b/utils/swift_build_support/swift_build_support/products/indexstoredb.py @@ -76,7 +76,12 @@ def run_build_script_helper(action, host_target, product, args, script_path = os.path.join( product.source_dir, 'Utilities', 'build-script-helper.py') - toolchain_path = targets.toolchain_path(args.install_destdir, + install_destdir = args.install_destdir + if swiftpm.SwiftPM.has_cross_compile_hosts(args): + install_destdir = swiftpm.SwiftPM.get_install_destdir(args, + host_target, + product.build_dir) + toolchain_path = targets.toolchain_path(install_destdir, args.install_prefix) is_release = product.is_release() configuration = 'release' if is_release else 'debug' diff --git a/utils/swift_build_support/swift_build_support/products/product.py b/utils/swift_build_support/swift_build_support/products/product.py index ed4c944bc008e..0270950e36603 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -11,6 +11,7 @@ # ---------------------------------------------------------------------------- import abc +import os from .. import cmake from .. import targets @@ -148,13 +149,17 @@ def is_release(self): """ return is_release_variant(self.args.build_variant) - def install_toolchain_path(self): + def install_toolchain_path(self, host_target): """toolchain_path() -> string Returns the path to the toolchain that is being created as part of this build. """ - return targets.toolchain_path(self.args.install_destdir, + install_destdir = self.args.install_destdir + if self.args.cross_compile_hosts: + build_root = os.path.dirname(self.build_dir) + install_destdir = '%s/intermediate-install/%s' % (build_root, host_target) + return targets.toolchain_path(install_destdir, self.args.install_prefix) diff --git a/utils/swift_build_support/swift_build_support/products/skstresstester.py b/utils/swift_build_support/swift_build_support/products/skstresstester.py index ae79373d99c7c..0656a2bfd7d3a 100644 --- a/utils/swift_build_support/swift_build_support/products/skstresstester.py +++ b/utils/swift_build_support/swift_build_support/products/skstresstester.py @@ -50,7 +50,7 @@ def is_swiftpm_unified_build_product(cls): def package_name(self): return 'SourceKitStressTester' - def run_build_script_helper(self, action, additional_params=[]): + def run_build_script_helper(self, action, host_target, additional_params=[]): script_path = os.path.join( self.source_dir, 'build-script-helper.py') @@ -60,7 +60,7 @@ def run_build_script_helper(self, action, additional_params=[]): script_path, action, '--package-dir', self.package_name(), - '--toolchain', self.install_toolchain_path(), + '--toolchain', self.install_toolchain_path(host_target), '--config', configuration, '--build-dir', self.build_dir, '--multiroot-data-file', MULTIROOT_DATA_FILE_PATH, @@ -84,20 +84,23 @@ def build(self, host_target): "than Darwin".format( product=self.package_name())) - self.run_build_script_helper('build') + self.run_build_script_helper('build', host_target) def should_test(self, host_target): return self.args.test_skstresstester def test(self, host_target): - self.run_build_script_helper('test') + self.run_build_script_helper('test', host_target) def should_install(self, host_target): return self.args.install_skstresstester def install(self, host_target): - install_prefix = self.args.install_destdir + self.args.install_prefix - self.run_build_script_helper('install', [ + install_destdir = swiftpm.SwiftPM.get_install_destdir(self.args, + host_target, + self.build_dir) + install_prefix = install_destdir + self.args.install_prefix + self.run_build_script_helper('install', host_target, [ '--prefix', install_prefix ]) diff --git a/utils/swift_build_support/swift_build_support/products/swiftformat.py b/utils/swift_build_support/swift_build_support/products/swiftformat.py index ae6323cddadf8..940ccd1043e6c 100644 --- a/utils/swift_build_support/swift_build_support/products/swiftformat.py +++ b/utils/swift_build_support/swift_build_support/products/swiftformat.py @@ -46,7 +46,7 @@ def is_build_script_impl_product(cls): def is_swiftpm_unified_build_product(cls): return True - def run_build_script_helper(self, action, additional_params=[]): + def run_build_script_helper(self, action, host_target, additional_params=[]): script_path = os.path.join( self.source_dir, 'build-script-helper.py') @@ -55,7 +55,7 @@ def run_build_script_helper(self, action, additional_params=[]): helper_cmd = [ script_path, action, - '--toolchain', self.install_toolchain_path(), + '--toolchain', self.install_toolchain_path(host_target), '--configuration', configuration, '--build-path', self.build_dir, '--multiroot-data-file', MULTIROOT_DATA_FILE_PATH, @@ -74,13 +74,13 @@ def should_build(self, host_target): return True def build(self, host_target): - self.run_build_script_helper('build') + self.run_build_script_helper('build', host_target) def should_test(self, host_target): return self.args.test_swiftformat def test(self, host_target): - self.run_build_script_helper('test') + self.run_build_script_helper('test', host_target) def should_install(self, host_target): return False diff --git a/utils/swift_build_support/swift_build_support/products/swiftpm.py b/utils/swift_build_support/swift_build_support/products/swiftpm.py index 48c18256629b5..53765ff39aa88 100644 --- a/utils/swift_build_support/swift_build_support/products/swiftpm.py +++ b/utils/swift_build_support/swift_build_support/products/swiftpm.py @@ -40,7 +40,7 @@ def should_build(self, host_target): def run_bootstrap_script(self, action, host_target, additional_params=[]): script_path = os.path.join( self.source_dir, 'Utilities', 'bootstrap') - toolchain_path = self.install_toolchain_path() + toolchain_path = self.install_toolchain_path(host_target) swiftc = os.path.join(toolchain_path, "bin", "swiftc") # FIXME: We require llbuild build directory in order to build. Is @@ -81,6 +81,12 @@ def run_bootstrap_script(self, action, host_target, additional_params=[]): "--foundation-build-dir", foundation_build_dir ] + # Pass Cross compile host info + if self.has_cross_compile_hosts(self.args): + helper_cmd += ['--cross-compile-hosts'] + for cross_compile_host in self.args.cross_compile_hosts: + helper_cmd += [cross_compile_host] + helper_cmd.extend(additional_params) shell.call(helper_cmd) @@ -97,8 +103,24 @@ def test(self, host_target): def should_install(self, host_target): return self.args.install_swiftpm + @classmethod + def has_cross_compile_hosts(self, args): + return args.cross_compile_hosts + + @classmethod + def get_install_destdir(self, args, host_target, build_dir): + install_destdir = args.install_destdir + if self.has_cross_compile_hosts(args): + build_root = os.path.dirname(build_dir) + install_destdir = '%s/intermediate-install/%s' % (build_root, host_target) + return install_destdir + def install(self, host_target): - install_prefix = self.args.install_destdir + self.args.install_prefix + install_destdir = self.get_install_destdir(self.args, + host_target, + self.build_dir) + install_prefix = install_destdir + self.args.install_prefix + self.run_bootstrap_script('install', host_target, [ '--prefix', install_prefix ]) diff --git a/utils/swift_build_support/swift_build_support/products/swiftsyntax.py b/utils/swift_build_support/swift_build_support/products/swiftsyntax.py index 9dd6a8436b30f..00cdef0dd0c5c 100644 --- a/utils/swift_build_support/swift_build_support/products/swiftsyntax.py +++ b/utils/swift_build_support/swift_build_support/products/swiftsyntax.py @@ -55,7 +55,7 @@ def run_swiftsyntax_build_script(self, target, additional_params=[]): script_path, '--build-dir', self.build_dir, '--multiroot-data-file', MULTIROOT_DATA_FILE_PATH, - '--toolchain', self.install_toolchain_path(), + '--toolchain', self.install_toolchain_path(target), '--filecheck-exec', os.path.join(llvm_build_dir, 'bin', 'FileCheck'), ]