-
Notifications
You must be signed in to change notification settings - Fork 4
/
go_native_toolchain.dart
228 lines (199 loc) · 6.42 KB
/
go_native_toolchain.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import 'dart:io';
import 'dart:math';
import './run_process.dart';
import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
// Not ideal:
import 'package:native_toolchain_c/src/cbuilder/compiler_resolver.dart';
/// Golang artifact builder
class GoBuilder {
/// Name of the library to build.
///
/// File will be placed in [BuildConfig.outDir].
final String name;
/// Asset identifier.
///
/// Used to output the [BuildOutput.assets].
///
/// If omitted, no asset will be added to the build output.
final String? assetName;
/// Golang bridge to build.
///
/// Resolved against [BuildConfig.packageRoot].
///
/// Used to output the [BuildOutput.dependencies].
final String bridgePath;
/// Sources to build the library.
///
/// Resolved against [BuildConfig.packageRoot].
///
/// Used to output the [BuildOutput.dependencies].
final List<String> sources;
/// The dart files involved in building this artifact.
///
/// Resolved against [BuildConfig.packageRoot].
///
/// Used to output the [BuildOutput.dependencies].
final List<String> dartBuildFiles;
GoBuilder({
required this.name,
required this.assetName,
required this.bridgePath,
this.sources = const [],
this.dartBuildFiles = const [],
});
/// Runs the Golang Compiler.
///
/// Completes with an error if the build fails.
Future<void> run({
required BuildConfig buildConfig,
required BuildOutput buildOutput,
required Logger? logger,
}) async {
final outDir = buildConfig.outputDirectory;
final packageRoot = buildConfig.packageRoot;
await Directory.fromUri(outDir).create(recursive: true);
var linkMode = _linkMode(buildConfig.linkModePreference);
final libUri = outDir.resolve(
buildConfig.targetOS.libraryFileName(name, linkMode),
);
final bridgePath = packageRoot.resolveUri(Uri.file(this.bridgePath));
final resolver = CompilerResolver(buildConfig: buildConfig, logger: logger);
if (!buildConfig.dryRun) {
final compiler = await resolver.resolveCompiler();
var buildMode = linkMode == StaticLinking() ? "c-archive" : "c-shared";
var goLib = libUri;
final env = {
"CGO_ENABLED": "1",
};
String buildArch;
switch (buildConfig.targetArchitecture) {
case Architecture.arm:
buildArch = "arm";
case Architecture.arm64:
buildArch = "arm64";
case Architecture.ia32:
buildArch = "386";
case Architecture.x64:
buildArch = "amd64";
default:
throw Exception("Unknown architecture");
}
String buildOs;
switch (buildConfig.targetOS) {
case OS.windows:
buildOs = "windows";
case OS.linux:
buildOs = "linux";
case OS.android:
buildOs = "android";
// The Android Gradle plugin does not honor API level 19 and 20 when
// invoking clang. Mimic that behavior here.
// See https://github.com/dart-lang/native/issues/171.
final minimumApi =
buildConfig.targetArchitecture == Architecture.riscv64 ? 35 : 21;
final targetAndroidNdkApi =
max(buildConfig.targetAndroidNdkApi!, minimumApi);
final cc = compiler.uri.resolve(
'./${androidNdkClangTargetFlags[buildConfig.targetArchitecture]!}$targetAndroidNdkApi-clang');
env["CC"] = cc.toFilePath();
case OS.iOS:
buildOs = "ios";
buildMode = "c-archive";
goLib = outDir.resolve('out.o');
case OS.macOS:
buildOs = "darwin";
default:
throw Exception("Unknown os");
}
env["GOOS"] = buildOs;
env["GOARCH"] = buildArch;
await runProcess(
executable: Uri.file("go"),
environment: env,
arguments: [
"build",
"-buildmode=$buildMode",
if (buildConfig.buildMode != BuildMode.debug) '-ldflags=-s -w',
'-o',
goLib.toFilePath(),
bridgePath.toFilePath(),
],
logger: logger,
captureOutput: false,
throwOnUnexpectedExitCode: true,
);
if (buildConfig.targetOS == OS.iOS) {
//xcrun -sdk iphoneos clang -arch armv7 -fpic -shared -Wl,-all_load
// libmystatic.a -framework Corefoundation -o libmydynamic.dylib
await runProcess(
executable: compiler.uri,
arguments: [
'-fpic',
'-shared',
'-Wl,-all_load,-force_load',
goLib.toFilePath(),
'-framework',
'CoreFoundation',
'-o',
libUri.toFilePath(),
],
logger: logger,
captureOutput: false,
throwOnUnexpectedExitCode: true,
);
}
}
if (assetName != null) {
buildOutput.addAssets([
NativeCodeAsset(
package: buildConfig.packageName,
name: assetName!,
file: libUri,
linkMode: linkMode,
os: buildConfig.targetOS,
architecture:
buildConfig.dryRun ? null : buildConfig.targetArchitecture,
)
]);
}
if (!buildConfig.dryRun) {
final sources = [
bridgePath,
for (final source in this.sources)
packageRoot.resolveUri(Uri.file(source)),
];
final dartBuildFiles = [
for (final source in this.dartBuildFiles) packageRoot.resolve(source),
];
final sourceFiles = await Stream.fromIterable(sources)
.asyncExpand(
(path) => Directory(path.toFilePath())
.list(recursive: true)
.where((entry) => entry is File)
.map((file) => file.uri),
)
.toList();
buildOutput.addDependencies({
...sourceFiles,
...dartBuildFiles,
});
}
}
}
LinkMode _linkMode(LinkModePreference preference) {
if (preference == LinkModePreference.dynamic ||
preference == LinkModePreference.preferDynamic) {
return DynamicLoadingBundled();
}
assert(preference == LinkModePreference.static ||
preference == LinkModePreference.preferStatic);
return StaticLinking();
}
const androidNdkClangTargetFlags = {
Architecture.arm: 'armv7a-linux-androideabi',
Architecture.arm64: 'aarch64-linux-android',
Architecture.ia32: 'i686-linux-android',
Architecture.x64: 'x86_64-linux-android',
Architecture.riscv64: 'riscv64-linux-android',
};