|
| 1 | +# swift_build_support/products/wasmswiftsdk.py ------------------*- python -*- |
| 2 | +# |
| 3 | +# This source file is part of the Swift.org open source project |
| 4 | +# |
| 5 | +# Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +# |
| 8 | +# See https://swift.org/LICENSE.txt for license information |
| 9 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +# |
| 11 | +# ---------------------------------------------------------------------------- |
| 12 | + |
| 13 | +import os |
| 14 | + |
| 15 | +from . import product |
| 16 | +from . import wasisysroot |
| 17 | +from .wasmstdlib import WasmStdlib, WasmThreadsStdlib |
| 18 | +from .. import shell |
| 19 | + |
| 20 | + |
| 21 | +class WasmSwiftSDK(product.Product): |
| 22 | + @classmethod |
| 23 | + def product_source_name(cls): |
| 24 | + return "swift-sdk-generator" |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def is_build_script_impl_product(cls): |
| 28 | + return False |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def is_before_build_script_impl_product(cls): |
| 32 | + return False |
| 33 | + |
| 34 | + def should_build(self, host_target): |
| 35 | + return self.args.build_wasmstdlib |
| 36 | + |
| 37 | + def should_test(self, host_target): |
| 38 | + return False |
| 39 | + |
| 40 | + def _target_package_path(self, target_triple): |
| 41 | + return os.path.join(self.build_dir, 'Toolchains', target_triple) |
| 42 | + |
| 43 | + def _build_target_package(self, target_triple, |
| 44 | + stdlib_build_path, llvm_runtime_libs_build_path): |
| 45 | + |
| 46 | + dest_dir = self._target_package_path(target_triple) |
| 47 | + shell.rmtree(dest_dir) |
| 48 | + shell.makedirs(dest_dir) |
| 49 | + |
| 50 | + # Build toolchain package for standalone stdlib |
| 51 | + with shell.pushd(stdlib_build_path): |
| 52 | + shell.call([self.toolchain.cmake, '--install', '.'], |
| 53 | + env={'DESTDIR': dest_dir}) |
| 54 | + |
| 55 | + # Copy clang builtin libraries |
| 56 | + with shell.pushd(llvm_runtime_libs_build_path): |
| 57 | + for dirname in ['clang', 'swift/clang', 'swift_static/clang']: |
| 58 | + clang_dir = os.path.join(dest_dir, f'usr/lib/{dirname}') |
| 59 | + shell.call([self.toolchain.cmake, '--install', '.', |
| 60 | + '--component', 'clang_rt.builtins-wasm32'], |
| 61 | + env={'DESTDIR': clang_dir}) |
| 62 | + |
| 63 | + return dest_dir |
| 64 | + |
| 65 | + def build(self, host_target): |
| 66 | + build_root = os.path.dirname(self.build_dir) |
| 67 | + llvm_runtime_libs_build_path = os.path.join( |
| 68 | + build_root, '%s-%s' % ('wasmllvmruntimelibs', host_target)) |
| 69 | + |
| 70 | + target_packages = [] |
| 71 | + for target_triple, short_triple, build_basename in [ |
| 72 | + ('wasm32-unknown-wasi', 'wasm32-wasi', 'wasmstdlib'), |
| 73 | + # TODO: Enable threads stdlib once sdk-generator supports multi-target SDK |
| 74 | + # ('wasm32-unknown-wasip1-threads', 'wasmthreadsstdlib'), |
| 75 | + ]: |
| 76 | + stdlib_build_path = os.path.join( |
| 77 | + build_root, '%s-%s' % (build_basename, host_target)) |
| 78 | + package_path = self._build_target_package( |
| 79 | + target_triple, stdlib_build_path, llvm_runtime_libs_build_path) |
| 80 | + target_packages.append((target_triple, package_path)) |
| 81 | + |
| 82 | + swiftc_path = os.path.abspath(self.toolchain.swiftc) |
| 83 | + toolchain_path = os.path.dirname(os.path.dirname(swiftc_path)) |
| 84 | + swift_run = os.path.join(toolchain_path, 'bin', 'swift-run') |
| 85 | + |
| 86 | + swift_version = os.environ.get('TOOLCHAIN_VERSION', |
| 87 | + 'swift-DEVELOPMENT-SNAPSHOT').lstrip('swift-') |
| 88 | + run_args = [ |
| 89 | + swift_run, |
| 90 | + '--package-path', self.source_dir, |
| 91 | + '--build-path', self.build_dir, |
| 92 | + 'swift-sdk-generator', |
| 93 | + 'make-wasm-sdk', |
| 94 | + '--swift-version', swift_version, |
| 95 | + ] |
| 96 | + for target_triple, package_path in target_packages: |
| 97 | + run_args.extend(['--target', target_triple]) |
| 98 | + run_args.extend(['--target-swift-package-path', package_path]) |
| 99 | + wasi_sysroot = wasisysroot.WASILibc.sysroot_install_path( |
| 100 | + build_root, short_triple) |
| 101 | + run_args.extend(['--wasi-sysroot', wasi_sysroot]) |
| 102 | + |
| 103 | + env = dict(os.environ) |
| 104 | + env['SWIFTCI_USE_LOCAL_DEPS'] = '1' |
| 105 | + |
| 106 | + shell.call(run_args, env=env) |
| 107 | + |
| 108 | + def test(self, host_target): |
| 109 | + pass |
| 110 | + |
| 111 | + def should_install(self, host_target): |
| 112 | + return False |
| 113 | + |
| 114 | + @classmethod |
| 115 | + def get_dependencies(cls): |
| 116 | + return [WasmStdlib, WasmThreadsStdlib] |
0 commit comments