Skip to content

Commit d0e1be5

Browse files
liamappelbecommit-bot@chromium.org
authored andcommitted
Setup script for package:wasm that builds the Wasmer runtime library.
This replaces third_party/wasmer and the related build rules, so I'll delete that stuff once the package is published. Bug: #37882 Change-Id: I33728e42c734bc8c25a2d32522112f2a3dbf4384 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/180446 Commit-Queue: Liam Appelbe <[email protected]> Reviewed-by: Siva Annamalai <[email protected]>
1 parent aa03d4a commit d0e1be5

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

pkg/wasm/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22

33
This package provides utilities for loading and running WASM modules. It is
44
built on top of the [Wasmer](https://github.com/wasmerio/wasmer) runtime.
5+
6+
## Setup
7+
8+
Run `dart bin/setup.dart` to build the Wasmer runtime.

pkg/wasm/bin/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cargo.lock
2+
/out

pkg/wasm/bin/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "wasmer"
3+
version = "1.0.0-alpha5"
4+
5+
[lib]
6+
name = "wasmer"
7+
crate-type = ["staticlib"]
8+
path = "wasmer.rs"
9+
10+
[dependencies.wasmer-c-api]
11+
version = "1.0.0-alpha5"
12+
default-features = false
13+
features = ["jit", "cranelift", "wasi"]

pkg/wasm/bin/finalizers.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#include "include/dart_api.h"
6+
#include "include/dart_api_dl.h"
7+
8+
#define FINALIZER(type) \
9+
extern "C" void wasm_##type##_delete(void*); \
10+
extern "C" void wasm_##type##_finalizer(void*, void* native_object) { \
11+
wasm_##type##_delete(native_object); \
12+
} \
13+
DART_EXPORT void set_finalizer_for_##type(Dart_Handle dart_object, \
14+
void* native_object) { \
15+
Dart_NewFinalizableHandle_DL(dart_object, native_object, 0, \
16+
wasm_##type##_finalizer); \
17+
}
18+
19+
FINALIZER(engine);
20+
FINALIZER(store);
21+
FINALIZER(module);
22+
FINALIZER(instance);
23+
FINALIZER(trap);
24+
FINALIZER(memorytype);
25+
FINALIZER(memory);
26+
FINALIZER(func);

pkg/wasm/bin/setup.dart

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Builds the wasmer runtime library, to by used by package:wasm. Requires
6+
// rustc, cargo, clang, and clang++. If a target triple is not specified, it
7+
// will default to the host target.
8+
// Usage: dart setup.dart [target-triple]
9+
10+
import 'dart:convert';
11+
import 'dart:io';
12+
13+
Uri getSdkDir() {
14+
// The common case, and how cli_util.dart computes the Dart SDK directory,
15+
// path.dirname called twice on Platform.resolvedExecutable.
16+
final exe = Uri.file(Platform.resolvedExecutable);
17+
final commonSdkDir = exe.resolve('../..');
18+
if (Directory(commonSdkDir.path).existsSync()) {
19+
return commonSdkDir;
20+
}
21+
22+
// This is the less common case where the user is in the checked out Dart
23+
// SDK, and is executing dart via:
24+
// ./out/ReleaseX64/dart ...
25+
final checkedOutSdkDir = exe.resolve('../dart-sdk');
26+
if (Directory(checkedOutSdkDir.path).existsSync()) {
27+
return checkedOutSdkDir;
28+
}
29+
30+
// If neither returned above, we return the common case:
31+
return commonSdkDir;
32+
}
33+
34+
String getOutLib(String target) {
35+
final os = RegExp(r'^.*-.*-(.*)').firstMatch(target)?.group(1) ?? '';
36+
if (os == 'darwin' || os == 'ios') {
37+
return 'libwasmer.dylib';
38+
} else if (os == 'windows') {
39+
return 'wasmer.dll';
40+
}
41+
return 'libwasmer.so';
42+
}
43+
44+
getTargetTriple() async {
45+
final process = await Process.start('rustc', ['--print', 'cfg']);
46+
process.stderr
47+
.transform(utf8.decoder)
48+
.transform(const LineSplitter())
49+
.listen((line) => stderr.writeln(line));
50+
final cfg = {};
51+
await process.stdout
52+
.transform(utf8.decoder)
53+
.transform(const LineSplitter())
54+
.listen((line) {
55+
final match = RegExp(r'^([^=]+)="(.*)"$').firstMatch(line);
56+
if (match != null) cfg[match.group(1)] = match.group(2);
57+
}).asFuture();
58+
String arch = cfg['target_arch'] ?? 'unknown';
59+
String vendor = cfg['target_vendor'] ?? 'unknown';
60+
String os = cfg['target_os'] ?? 'unknown';
61+
String env = cfg['target_env'] ?? 'unknown';
62+
return '$arch-$vendor-$os-$env';
63+
}
64+
65+
run(String exe, List<String> args) async {
66+
print('\n$exe ${args.join(' ')}\n');
67+
final process = await Process.start(exe, args);
68+
process.stdout
69+
.transform(utf8.decoder)
70+
.transform(const LineSplitter())
71+
.listen((line) => print(line));
72+
process.stderr
73+
.transform(utf8.decoder)
74+
.transform(const LineSplitter())
75+
.listen((line) => stderr.writeln(line));
76+
final exitCode = await process.exitCode;
77+
if (exitCode != 0) {
78+
print('Command failed with exit code ${exitCode}');
79+
exit(exitCode);
80+
}
81+
}
82+
83+
main(List<String> args) async {
84+
if (args.length > 1) {
85+
print('Usage: dart setup.dart [target-triple]');
86+
exit(1);
87+
}
88+
89+
final target = args.length >= 1 ? args[0] : await getTargetTriple();
90+
final sdkDir = getSdkDir();
91+
final binDir = Platform.script;
92+
final outLib = binDir.resolve('out/' + getOutLib(target)).path;
93+
94+
print('Dart SDK directory: ${sdkDir.path}');
95+
print('Script directory: ${binDir.path}');
96+
print('Target: $target');
97+
print('Output library: $outLib');
98+
99+
// Build wasmer crate.
100+
await run('cargo', [
101+
'build',
102+
'--target',
103+
target,
104+
'--target-dir',
105+
binDir.resolve('out').path,
106+
'--manifest-path',
107+
binDir.resolve('Cargo.toml').path,
108+
'--release'
109+
]);
110+
111+
// Build dart_api_dl.o.
112+
await run('clang', [
113+
'-DDART_SHARED_LIB',
114+
'-DNDEBUG',
115+
'-fno-exceptions',
116+
'-fPIC',
117+
'-O3',
118+
'-target',
119+
target,
120+
'-c',
121+
sdkDir.resolve('runtime/include/dart_api_dl.c').path,
122+
'-o',
123+
binDir.resolve('out/dart_api_dl.o').path
124+
]);
125+
126+
// Build finalizers.o.
127+
await run('clang++', [
128+
'-DDART_SHARED_LIB',
129+
'-DNDEBUG',
130+
'-fno-exceptions',
131+
'-fno-rtti',
132+
'-fPIC',
133+
'-O3',
134+
'-std=c++11',
135+
'-target',
136+
target,
137+
'-I',
138+
sdkDir.resolve('runtime').path,
139+
'-c',
140+
binDir.resolve('finalizers.cc').path,
141+
'-o',
142+
binDir.resolve('out/finalizers.o').path
143+
]);
144+
145+
// Link wasmer, dart_api_dl, and finalizers to create the output library.
146+
await run('clang++', [
147+
'-shared',
148+
'-Wl,--no-as-needed',
149+
'-Wl,--fatal-warnings',
150+
'-Wl,-z,now',
151+
'-Wl,-z,noexecstack',
152+
'-Wl,-z,relro',
153+
'-Wl,--build-id=none',
154+
'-fPIC',
155+
'-Wl,-O1',
156+
'-Wl,--gc-sections',
157+
'-target',
158+
target,
159+
binDir.resolve('out/dart_api_dl.o').path,
160+
binDir.resolve('out/finalizers.o').path,
161+
binDir.resolve('out/' + target + '/release/libwasmer.a').path,
162+
'-o',
163+
outLib
164+
]);
165+
}

pkg/wasm/bin/wasmer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub extern crate wasmer_c_api;

0 commit comments

Comments
 (0)