|
| 1 | +# swift_build_support/products/wasmkit.py ------------------------------------ |
| 2 | +# |
| 3 | +# This source file is part of the Swift.org open source project |
| 4 | +# |
| 5 | +# Copyright (c) 2014 - 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 | +import shutil |
| 15 | + |
| 16 | +from . import product |
| 17 | +from .. import shell |
| 18 | + |
| 19 | + |
| 20 | +class WasmKit(product.Product): |
| 21 | + """ |
| 22 | + A product for WasmKit, which is a WebAssembly runtime implementation |
| 23 | + written in Swift. |
| 24 | + """ |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def product_source_name(cls): |
| 28 | + return "wasmkit" |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def is_build_script_impl_product(cls): |
| 32 | + return False |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def is_before_build_script_impl_product(cls): |
| 36 | + return True |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def get_dependencies(cls): |
| 40 | + return [] |
| 41 | + |
| 42 | + def should_build(self, host_target): |
| 43 | + return self.args.build_wasmkit |
| 44 | + |
| 45 | + def should_test(self, host_target): |
| 46 | + return False |
| 47 | + |
| 48 | + def should_install(self, host_target): |
| 49 | + # Currently, it's only used for testing stdlib. |
| 50 | + return False |
| 51 | + |
| 52 | + def install(self, host_target): |
| 53 | + pass |
| 54 | + |
| 55 | + def build(self, host_target): |
| 56 | + bin_path = run_swift_build(host_target, self, 'wasmkit-cli') |
| 57 | + print("Built wasmkit-cli at: " + bin_path) |
| 58 | + # Copy the built binary to ./bin |
| 59 | + dest_bin_path = self.__class__.cli_file_path(self.build_dir) |
| 60 | + print("Copying wasmkit-cli to: " + dest_bin_path) |
| 61 | + os.makedirs(os.path.dirname(dest_bin_path), exist_ok=True) |
| 62 | + shutil.copy(bin_path, dest_bin_path) |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def cli_file_path(cls, build_dir): |
| 66 | + return os.path.join(build_dir, 'bin', 'wasmkit-cli') |
| 67 | + |
| 68 | + |
| 69 | +def run_swift_build(host_target, product, swpft_package_product_name): |
| 70 | + # Building with the host toolchain's SwiftPM |
| 71 | + swiftc_path = os.path.abspath(product.toolchain.swiftc) |
| 72 | + toolchain_path = os.path.dirname(os.path.dirname(swiftc_path)) |
| 73 | + swift_build = os.path.join(toolchain_path, 'bin', 'swift-build') |
| 74 | + |
| 75 | + build_args = [ |
| 76 | + swift_build, |
| 77 | + '--product', swpft_package_product_name, |
| 78 | + '--package-path', os.path.join(product.source_dir), |
| 79 | + '--build-path', product.build_dir, |
| 80 | + '--configuration', 'release', |
| 81 | + ] |
| 82 | + |
| 83 | + if product.args.verbose_build: |
| 84 | + build_args.append('--verbose') |
| 85 | + |
| 86 | + env = dict(os.environ) |
| 87 | + env['SWIFTCI_USE_LOCAL_DEPS'] = '1' |
| 88 | + |
| 89 | + shell.call(build_args, env=env) |
| 90 | + |
| 91 | + bin_dir_path = shell.capture( |
| 92 | + build_args + ['--show-bin-path'], dry_run=False, echo=False).rstrip() |
| 93 | + return os.path.join(bin_dir_path, swpft_package_product_name) |
0 commit comments