Skip to content

Commit eac419b

Browse files
authored
Merge pull request swiftlang#81178 from swiftlang/maxd/install-wasmkit
This enables `swift run` and `swift test` to use WasmKit when cross-compiling to Wasm with Swift SDKs that have toolsets pointing to WasmKit. rdar://150382758
2 parents 9b6c04e + 06bee27 commit eac419b

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

utils/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,7 @@ playgroundsupport
13571357
indexstore-db
13581358
sourcekit-lsp
13591359
swiftdocc
1360+
wasmkit
13601361

13611362
# Build with debug info, this allows us to symbolicate crashes from
13621363
# production builds.

utils/swift_build_support/swift_build_support/build_script_invocation.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,14 @@ def compute_product_pipelines(self):
672672
is_enabled=self.args.build_wasmstdlib)
673673
builder.add_product(products.WasmThreadsLLVMRuntimeLibs,
674674
is_enabled=self.args.build_wasmstdlib)
675+
676+
builder.add_product(products.SwiftTestingMacros,
677+
is_enabled=self.args.build_swift_testing_macros)
678+
builder.add_product(products.SwiftTesting,
679+
is_enabled=self.args.build_swift_testing)
680+
builder.add_product(products.SwiftPM,
681+
is_enabled=self.args.build_swiftpm)
682+
675683
builder.add_product(products.WasmKit,
676684
is_enabled=self.args.build_wasmkit)
677685
builder.add_product(products.WasmStdlib,
@@ -681,12 +689,6 @@ def compute_product_pipelines(self):
681689
builder.add_product(products.WasmSwiftSDK,
682690
is_enabled=self.args.build_wasmstdlib)
683691

684-
builder.add_product(products.SwiftTestingMacros,
685-
is_enabled=self.args.build_swift_testing_macros)
686-
builder.add_product(products.SwiftTesting,
687-
is_enabled=self.args.build_swift_testing)
688-
builder.add_product(products.SwiftPM,
689-
is_enabled=self.args.build_swiftpm)
690692
builder.add_product(products.SwiftFoundationTests,
691693
is_enabled=self.args.build_foundation)
692694
builder.add_product(products.FoundationTests,

utils/swift_build_support/swift_build_support/products/wasmkit.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import shutil
1515

1616
from . import product
17+
from . import swiftpm
1718
from .. import shell
1819

1920

@@ -37,7 +38,7 @@ def is_before_build_script_impl_product(cls):
3738

3839
@classmethod
3940
def get_dependencies(cls):
40-
return []
41+
return [swiftpm.SwiftPM]
4142

4243
def should_build(self, host_target):
4344
return self.args.build_wasmkit
@@ -47,10 +48,16 @@ def should_test(self, host_target):
4748

4849
def should_install(self, host_target):
4950
# Currently, it's only used for testing stdlib.
50-
return False
51+
return True
5152

5253
def install(self, host_target):
53-
pass
54+
"""
55+
Install WasmKit to the target location
56+
"""
57+
install_destdir = self.host_install_destdir(host_target)
58+
build_toolchain_path = install_destdir + self.args.install_prefix + '/bin'
59+
bin_path = run_swift_build(host_target, self, 'wasmkit-cli', set_installation_rpath=True)
60+
shutil.copy(bin_path, build_toolchain_path + '/wasmkit')
5461

5562
def build(self, host_target):
5663
bin_path = run_swift_build(host_target, self, 'wasmkit-cli')
@@ -66,19 +73,27 @@ def cli_file_path(cls, build_dir):
6673
return os.path.join(build_dir, 'bin', 'wasmkit-cli')
6774

6875

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')
76+
def run_swift_build(host_target, product, swiftpm_package_product_name, set_installation_rpath=False):
77+
# Building with the freshly-built SwiftPM
78+
swift_build = os.path.join(product.install_toolchain_path(host_target), "bin", "swift-build")
79+
80+
build_os = host_target.split('-')[0]
81+
if set_installation_rpath and not host_target.startswith('macos'):
82+
# Library rpath for swift, dispatch, Foundation, etc. when installing
83+
rpath_args = [
84+
'--disable-local-rpath', '-Xswiftc', '-no-toolchain-stdlib-rpath',
85+
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/' + build_os
86+
]
87+
else:
88+
rpath_args = []
7489

7590
build_args = [
7691
swift_build,
77-
'--product', swpft_package_product_name,
92+
'--product', swiftpm_package_product_name,
7893
'--package-path', os.path.join(product.source_dir),
7994
'--build-path', product.build_dir,
8095
'--configuration', 'release',
81-
]
96+
] + rpath_args
8297

8398
if product.args.verbose_build:
8499
build_args.append('--verbose')
@@ -90,4 +105,4 @@ def run_swift_build(host_target, product, swpft_package_product_name):
90105

91106
bin_dir_path = shell.capture(
92107
build_args + ['--show-bin-path'], dry_run=False, echo=False).rstrip()
93-
return os.path.join(bin_dir_path, swpft_package_product_name)
108+
return os.path.join(bin_dir_path, swiftpm_package_product_name)

0 commit comments

Comments
 (0)