-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcbuilder.dart
197 lines (186 loc) · 6 KB
/
cbuilder.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
// 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';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'ctool.dart';
import 'language.dart';
import 'linkmode.dart';
import 'output_type.dart';
import 'run_cbuilder.dart';
/// Specification for building an artifact with a C compiler.
class CBuilder extends CTool implements Builder {
/// The dart files involved in building this artifact.
///
/// Resolved against [BuildConfig.packageRoot].
///
/// Used to output the [BuildOutput.dependencies].
@Deprecated(
'Newer Dart and Flutter SDKs automatically add the Dart hook '
'sources as dependencies.',
)
final List<String> dartBuildFiles;
/// Whether to define a macro for the current [BuildMode].
///
/// The macro name is the uppercase name of the build mode and does not have a
/// value.
///
/// Defaults to `true`.
final bool buildModeDefine;
/// Whether to define the standard `NDEBUG` macro when _not_ building with
/// [BuildMode.debug].
///
/// When `NDEBUG` is defined, the C/C++ standard library
/// [`assert` macro in `assert.h`](https://en.wikipedia.org/wiki/Assert.h)
/// becomes a no-op. Other C/C++ code commonly use `NDEBUG` to disable debug
/// features, as well.
///
/// Defaults to `true`.
final bool ndebugDefine;
CBuilder.library({
required super.name,
super.assetName,
super.sources = const [],
super.includes = const [],
super.frameworks = CTool.defaultFrameworks,
@Deprecated(
'Newer Dart and Flutter SDKs automatically add the Dart hook '
'sources as dependencies.',
)
this.dartBuildFiles = const [],
@visibleForTesting super.installName,
super.flags = const [],
super.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
super.pic = true,
super.std,
super.language = Language.c,
super.cppLinkStdLib,
super.linkModePreference,
}) : super(type: OutputType.library);
CBuilder.executable({
required super.name,
super.sources = const [],
super.includes = const [],
super.frameworks = CTool.defaultFrameworks,
@Deprecated(
'Newer Dart and Flutter SDKs automatically add the Dart hook '
'sources as dependencies.',
)
this.dartBuildFiles = const [],
super.flags = const [],
super.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
bool? pie = false,
super.std,
super.language = Language.c,
super.cppLinkStdLib,
}) : super(
type: OutputType.executable,
assetName: null,
installName: null,
pic: pie,
linkModePreference: null,
);
/// Runs the C Compiler with on this C build spec.
///
/// Completes with an error if the build fails.
@override
Future<void> run({
required BuildConfig config,
required BuildOutput output,
required Logger? logger,
String? linkInPackage,
}) async {
assert(
config.linkingEnabled || linkInPackage == null,
'linkInPackage can only be provided if config.linkingEnabled is true.',
);
final outDir = config.outputDirectory;
final packageRoot = config.packageRoot;
await Directory.fromUri(outDir).create(recursive: true);
final linkMode =
getLinkMode(linkModePreference ?? config.linkModePreference);
final libUri =
outDir.resolve(config.targetOS.libraryFileName(name, linkMode));
final exeUri = outDir.resolve(config.targetOS.executableFileName(name));
final sources = [
for (final source in this.sources)
packageRoot.resolveUri(Uri.file(source)),
];
final includes = [
for (final directory in this.includes)
packageRoot.resolveUri(Uri.file(directory)),
];
final dartBuildFiles = [
// ignore: deprecated_member_use_from_same_package
for (final source in this.dartBuildFiles) packageRoot.resolve(source),
];
if (!config.dryRun) {
final task = RunCBuilder(
config: config,
logger: logger,
sources: sources,
includes: includes,
frameworks: frameworks,
dynamicLibrary:
type == OutputType.library && linkMode == DynamicLoadingBundled()
? libUri
: null,
staticLibrary: type == OutputType.library && linkMode == StaticLinking()
? libUri
: null,
executable: type == OutputType.executable ? exeUri : null,
// ignore: invalid_use_of_visible_for_testing_member
installName: installName,
flags: flags,
defines: {
...defines,
if (buildModeDefine) config.buildMode.name.toUpperCase(): null,
if (ndebugDefine && config.buildMode != BuildMode.debug)
'NDEBUG': null,
},
pic: pic,
std: std,
language: language,
cppLinkStdLib: cppLinkStdLib,
);
await task.run();
}
if (assetName != null) {
output.addAssets(
[
NativeCodeAsset(
package: config.packageName,
name: assetName!,
file: libUri,
linkMode: linkMode,
os: config.targetOS,
architecture: config.dryRun ? null : config.targetArchitecture,
)
],
linkInPackage: linkInPackage,
);
}
if (!config.dryRun) {
final includeFiles = await Stream.fromIterable(includes)
.asyncExpand(
(include) => Directory(include.toFilePath())
.list(recursive: true)
.where((entry) => entry is File)
.map((file) => file.uri),
)
.toList();
output.addDependencies({
// Note: We use a Set here to deduplicate the dependencies.
...sources,
...includeFiles,
...dartBuildFiles,
});
}
}
}