|
| 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 | +} |
0 commit comments