-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.dart
72 lines (64 loc) · 2.15 KB
/
build.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io' show File, Process, exit, stderr, stdout;
import 'package:path/path.dart' as p;
import 'package:native_assets_cli/native_assets_cli.dart';
const packageName = 'quickjs';
const _repoLibName = 'libquickjs.so';
/// Implements the protocol from `package:native_assets_cli` by building
/// the C code in `src/` and reporting what native assets it built.
void main(List<String> args) async {
await build(args, _builder);
}
Future<void> _builder(BuildConfig buildConfig, BuildOutput buildOutput) async {
final pkgRoot = buildConfig.packageRoot;
final srcDir = pkgRoot.resolve('src');
final proc = await Process.start(
'make',
[
'-j',
_repoLibName,
],
workingDirectory: srcDir.path,
);
stdout.addStream(proc.stdout);
stderr.addStream(proc.stderr);
final code = await proc.exitCode;
if (code != 0) {
exit(code);
}
final linkMode = _linkMode(buildConfig.linkModePreference);
final libName = buildConfig.targetOS.libraryFileName(packageName, linkMode);
final libUri = buildConfig.outputDirectory.resolve(libName);
File(p.join(srcDir.path, _repoLibName)).renameSync(libUri.path);
buildOutput.addAsset(NativeCodeAsset(
package: packageName,
name: 'src/lib_$packageName.dart',
linkMode: linkMode,
os: buildConfig.targetOS,
file: libUri,
architecture: buildConfig.targetArchitecture,
));
final src = [
'src/quickjs.c',
'src/libregexp.c',
'src/libunicode.c',
'src/cutils.c',
'src/libc.c',
'src/libbf.c',
];
buildOutput.addDependencies([
...src.map((s) => pkgRoot.resolve(s)),
pkgRoot.resolve('build.dart'),
]);
}
LinkMode _linkMode(LinkModePreference preference) {
if (preference == LinkModePreference.dynamic ||
preference == LinkModePreference.preferDynamic) {
return DynamicLoadingBundled();
}
assert(preference == LinkModePreference.static ||
preference == LinkModePreference.preferStatic);
return StaticLinking();
}