-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.dart
146 lines (133 loc) · 4.21 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 Directory, File, Platform, Process, exit, stderr, stdout;
import 'package:glob/glob.dart' show Glob;
import 'package:glob/list_local_fs.dart';
import 'package:path/path.dart' as p;
import 'package:native_assets_cli/native_assets_cli.dart';
const packageName = 'opencc';
final _repoLibName = Platform.isMacOS
? 'libopencc.dylib'
: 'libopencc.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> _checkCmd(String cmd) async {
final proc = await Process.start(
Platform.isWindows ? 'powershell' : 'which',
Platform.isWindows ? ['/c', 'Get-Command', cmd] : [cmd],
);
final code = await proc.exitCode;
if (code != 0) {
throw Exception("'$cmd' not found!");
}
}
Future<void> _builder(BuildConfig buildConfig, BuildOutput buildOutput) async {
await _checkCmd('cmake');
final pkgRoot = buildConfig.packageRoot;
final buildDir = p.join(p.fromUri(pkgRoot), 'src', 'build');
final dir = Directory(buildDir);
if (!dir.existsSync()) {
dir.createSync(recursive: true);
}
final output = buildConfig.outputDirectory.path;
final cmake = await Process.start(
'cmake',
[
'..',
'-DCMAKE_BUILD_TYPE=Release',
'-DBUILD_SHARED_LIBS=ON',
'-DSHARE_INSTALL_PREFIX=$output',
],
workingDirectory: buildDir,
);
stdout.addStream(cmake.stdout);
stderr.addStream(cmake.stderr);
final code = await cmake.exitCode;
if (code != 0) {
exit(code);
}
final make = await Process.start(
'cmake',
[
'--build',
'.',
'--config',
'Release',
'--target',
'Dictionaries',
],
workingDirectory: buildDir,
);
stdout.addStream(make.stdout);
stderr.addStream(make.stderr);
final code2 = await make.exitCode;
if (code2 != 0) {
exit(code2);
}
final linkMode = _linkMode(buildConfig.linkModePreference);
final libName = buildConfig.targetOS.libraryFileName(packageName, linkMode);
final libUri = buildConfig.outputDirectory.resolve(libName);
final uri = pkgRoot.resolve(p.join(buildDir, 'src', _repoLibName));
final file = File.fromUri(uri).resolveSymbolicLinksSync();
File(file).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/src/BinaryDict.cpp',
'src/src/Config.cpp',
'src/src/ConversionChain.cpp',
'src/src/Conversion.cpp',
'src/src/Converter.cpp',
'src/src/DartsDict.cpp',
'src/src/DictConverter.cpp',
'src/src/Dict.cpp',
'src/src/DictEntry.cpp',
'src/src/DictGroup.cpp',
'src/src/Lexicon.cpp',
'src/src/MarisaDict.cpp',
'src/src/MaxMatchSegmentation.cpp',
'src/src/PhraseExtract.cpp',
'src/src/Segmentation.cpp',
'src/src/SerializedValues.cpp',
'src/src/SimpleConverter.cpp',
'src/src/TextDict.cpp',
'src/src/UTF8StringSlice.cpp',
'src/src/UTF8Util.cpp',
];
buildOutput.addDependencies([
...src.map((s) => pkgRoot.resolve(s)),
pkgRoot.resolve('build.dart'),
]);
final dataDir = p.join(output, 'opencc');
final d = Directory(dataDir);
if (!d.existsSync()) {
d.createSync();
}
for (final f in Glob('src/build/data/*.ocd2').listSync()) {
final path = p.join(dataDir, p.basename(f.path));
f.renameSync(path);
}
for (final f in Glob('src/data/config/*.json').listSync()) {
final path = p.join(dataDir, p.basename(f.path));
File(f.path).copySync(path);
}
}
LinkMode _linkMode(LinkModePreference preference) {
if (preference == LinkModePreference.dynamic ||
preference == LinkModePreference.preferDynamic) {
return DynamicLoadingBundled();
}
assert(preference == LinkModePreference.static ||
preference == LinkModePreference.preferStatic);
return StaticLinking();
}