Skip to content

Commit b86e34d

Browse files
authored
[native_assets_cli] Make CCompilerConfig fields less nullable (#1809)
Addressing: * #1738 (comment) Currently on Flutter and the Dart CI provide this information, and they always provide all 3. So this should not be a breaking change on the protocol level. It is a breaking change on the API level.
1 parent 4d81ce6 commit b86e34d

File tree

10 files changed

+74
-71
lines changed

10 files changed

+74
-71
lines changed

pkgs/native_assets_builder/test/build_runner/helpers.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,13 @@ final CCompilerConfig? dartCICompilerConfig = (() {
299299
.toList();
300300
final hasEnvScriptArgs = envScriptArgs != null && envScriptArgs.isNotEmpty;
301301

302-
if (cc != null ||
303-
ar != null ||
304-
ld != null ||
305-
envScript != null ||
306-
hasEnvScriptArgs) {
302+
if (cc != null && ar != null && ld != null) {
307303
return CCompilerConfig(
308-
archiver: ar != null ? Uri.file(ar) : null,
309-
compiler: cc != null ? Uri.file(cc) : null,
304+
archiver: Uri.file(ar),
305+
compiler: Uri.file(cc),
310306
envScript: envScript != null ? Uri.file(envScript) : null,
311307
envScriptArgs: hasEnvScriptArgs ? envScriptArgs : null,
312-
linker: ld != null ? Uri.file(ld) : null,
308+
linker: Uri.file(ld),
313309
);
314310
}
315311
return null;

pkgs/native_assets_builder/test/helpers.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@ final List<String>? _envScriptArgs = Platform
161161
/// Configuration for the native toolchain.
162162
///
163163
/// Provided on Dart CI.
164-
final cCompiler = CCompilerConfig(
165-
compiler: _cc,
166-
archiver: _ar,
167-
linker: _ld,
168-
envScript: _envScript,
169-
envScriptArgs: _envScriptArgs,
170-
);
164+
final cCompiler = (_cc == null || _ar == null || _ld == null)
165+
? null
166+
: CCompilerConfig(
167+
compiler: _cc!,
168+
archiver: _ar!,
169+
linker: _ld!,
170+
envScript: _envScript,
171+
envScriptArgs: _envScriptArgs,
172+
);
171173

172174
extension on String {
173175
Uri asFileUri() => Uri.file(this);

pkgs/native_assets_cli/lib/src/code_assets/c_compiler_config.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import '../utils/map.dart';
1010
/// The configuration for a C toolchain.
1111
final class CCompilerConfig {
1212
/// Path to a C compiler.
13-
late final Uri? compiler;
13+
late final Uri compiler;
1414

1515
/// Path to a native linker.
16-
late final Uri? linker;
16+
late final Uri linker;
1717

1818
/// Path to a native archiver.
19-
late final Uri? archiver;
19+
late final Uri archiver;
2020

2121
/// Path to script that sets environment variables for [compiler], [linker],
2222
/// and [archiver].
@@ -27,9 +27,9 @@ final class CCompilerConfig {
2727

2828
/// Constructs a new [CCompilerConfig] based on the given toolchain tools.
2929
CCompilerConfig({
30-
this.archiver,
31-
this.compiler,
32-
this.linker,
30+
required this.archiver,
31+
required this.compiler,
32+
required this.linker,
3333
this.envScript,
3434
this.envScriptArgs,
3535
});
@@ -40,21 +40,21 @@ final class CCompilerConfig {
4040
/// [CCompilerConfig.toJson].
4141
factory CCompilerConfig.fromJson(Map<String, Object?> json) =>
4242
CCompilerConfig(
43-
archiver: json.optionalPath(_arConfigKey),
44-
compiler: json.optionalPath(_ccConfigKey),
43+
archiver: json.path(_arConfigKey),
44+
compiler: json.path(_ccConfigKey),
4545
envScript: json.optionalPath(_envScriptConfigKey),
4646
envScriptArgs: json.optionalStringList(_envScriptArgsConfigKey),
47-
linker: json.optionalPath(_ldConfigKey),
47+
linker: json.path(_ldConfigKey),
4848
);
4949

5050
/// The json representation of this [CCompilerConfig].
5151
///
5252
/// The returned json can be used in [CCompilerConfig.fromJson] to
5353
/// obtain a [CCompilerConfig] again.
5454
Map<String, Object> toJson() => {
55-
if (archiver != null) _arConfigKey: archiver!.toFilePath(),
56-
if (compiler != null) _ccConfigKey: compiler!.toFilePath(),
57-
if (linker != null) _ldConfigKey: linker!.toFilePath(),
55+
_arConfigKey: archiver.toFilePath(),
56+
_ccConfigKey: compiler.toFilePath(),
57+
_ldConfigKey: linker.toFilePath(),
5858
if (envScript != null) _envScriptConfigKey: envScript!.toFilePath(),
5959
if (envScriptArgs != null) _envScriptArgsConfigKey: envScriptArgs!,
6060
}.sortOnKey();

pkgs/native_assets_cli/lib/src/code_assets/config.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class CodeConfig {
4242
final Architecture? _targetArchitecture;
4343

4444
final LinkModePreference linkModePreference;
45-
final CCompilerConfig cCompiler;
45+
final CCompilerConfig? cCompiler;
4646
final int? targetIOSVersion;
4747
final int? targetMacOSVersion;
4848
final int? targetAndroidNdkApi;
@@ -62,7 +62,7 @@ class CodeConfig {
6262
targetOS = OS.fromString(config.json.string(_targetOSConfigKey)),
6363
cCompiler = switch (config.json.optionalMap(_compilerKey)) {
6464
final Map<String, Object?> map => CCompilerConfig.fromJson(map),
65-
null => CCompilerConfig(),
65+
null => null,
6666
},
6767
targetIOSVersion = config.json.optionalInt(_targetIOSVersionKey),
6868
targetMacOSVersion = config.json.optionalInt(_targetMacOSVersionKey),

pkgs/native_assets_cli/lib/src/code_assets/validation.dart

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,24 @@ ValidationErrors _validateCodeConfig(
5353
break;
5454
}
5555
final compilerConfig = codeConfig.cCompiler;
56-
final compiler = compilerConfig.compiler?.toFilePath();
57-
if (compiler != null && !File(compiler).existsSync()) {
58-
errors.add('$configName.codeConfig.compiler ($compiler) does not exist.');
59-
}
60-
final linker = compilerConfig.linker?.toFilePath();
61-
if (linker != null && !File(linker).existsSync()) {
62-
errors.add('$configName.codeConfig.linker ($linker) does not exist.');
63-
}
64-
final archiver = compilerConfig.archiver?.toFilePath();
65-
if (archiver != null && !File(archiver).existsSync()) {
66-
errors.add('$configName.codeConfig.archiver ($archiver) does not exist.');
67-
}
68-
final envScript = compilerConfig.envScript?.toFilePath();
69-
if (envScript != null && !File(envScript).existsSync()) {
70-
errors.add('$configName.codeConfig.envScript ($envScript) does not exist.');
56+
if (compilerConfig != null) {
57+
final compiler = compilerConfig.compiler.toFilePath();
58+
if (!File(compiler).existsSync()) {
59+
errors.add('$configName.codeConfig.compiler ($compiler) does not exist.');
60+
}
61+
final linker = compilerConfig.linker.toFilePath();
62+
if (!File(linker).existsSync()) {
63+
errors.add('$configName.codeConfig.linker ($linker) does not exist.');
64+
}
65+
final archiver = compilerConfig.archiver.toFilePath();
66+
if (!File(archiver).existsSync()) {
67+
errors.add('$configName.codeConfig.archiver ($archiver) does not exist.');
68+
}
69+
final envScript = compilerConfig.envScript?.toFilePath();
70+
if (envScript != null && !File(envScript).existsSync()) {
71+
errors
72+
.add('$configName.codeConfig.envScript ($envScript) does not exist.');
73+
}
7174
}
7275
return errors;
7376
}

pkgs/native_assets_cli/test/code_assets/config_test.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ void main() async {
5555
expect(() => codeConfig.targetArchitecture, throwsStateError);
5656
expect(codeConfig.targetAndroidNdkApi, null);
5757
expect(codeConfig.linkModePreference, LinkModePreference.preferStatic);
58-
expect(codeConfig.cCompiler.compiler, null);
59-
expect(codeConfig.cCompiler.linker, null);
60-
expect(codeConfig.cCompiler.archiver, null);
58+
expect(codeConfig.cCompiler, null);
6159
}
6260

6361
void expectCorrectCodeConfig(
@@ -81,9 +79,9 @@ void main() async {
8179
expect(codeConfig.targetArchitecture, Architecture.arm64);
8280
expect(codeConfig.targetAndroidNdkApi, 30);
8381
expect(codeConfig.linkModePreference, LinkModePreference.preferStatic);
84-
expect(codeConfig.cCompiler.compiler, fakeClang);
85-
expect(codeConfig.cCompiler.linker, fakeLd);
86-
expect(codeConfig.cCompiler.archiver, fakeAr);
82+
expect(codeConfig.cCompiler?.compiler, fakeClang);
83+
expect(codeConfig.cCompiler?.linker, fakeLd);
84+
expect(codeConfig.cCompiler?.archiver, fakeAr);
8785
}
8886

8987
test('BuildConfig.codeConfig (dry-run)', () {

pkgs/native_assets_cli/test/helpers.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ final List<String>? _envScriptArgs = Platform
115115
/// Configuration for the native toolchain.
116116
///
117117
/// Provided on Dart CI.
118-
final cCompiler = CCompilerConfig(
119-
compiler: _cc,
120-
archiver: _ar,
121-
linker: _ld,
122-
envScript: _envScript,
123-
envScriptArgs: _envScriptArgs,
124-
);
118+
final cCompiler = (_cc == null || _ar == null || _ld == null)
119+
? null
120+
: CCompilerConfig(
121+
compiler: _cc!,
122+
archiver: _ar!,
123+
linker: _ld!,
124+
envScript: _envScript,
125+
envScriptArgs: _envScriptArgs,
126+
);
125127

126128
extension on String {
127129
Uri asFileUri() => Uri.file(this);

pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class CompilerResolver {
9999
}
100100

101101
Future<ToolInstance?> _tryLoadCompilerFromConfig() async {
102-
final configCcUri = codeConfig.cCompiler.compiler;
102+
final configCcUri = codeConfig.cCompiler?.compiler;
103103
if (configCcUri != null) {
104104
assert(await File.fromUri(configCcUri).exists());
105105
logger?.finer('Using compiler ${configCcUri.toFilePath()} '
@@ -184,7 +184,7 @@ class CompilerResolver {
184184
}
185185

186186
Future<ToolInstance?> _tryLoadArchiverFromConfig() async {
187-
final configArUri = codeConfig.cCompiler.archiver;
187+
final configArUri = codeConfig.cCompiler?.archiver;
188188
if (configArUri != null) {
189189
assert(await File.fromUri(configArUri).exists());
190190
logger?.finer('Using archiver ${configArUri.toFilePath()} '
@@ -197,7 +197,7 @@ class CompilerResolver {
197197
}
198198

199199
Future<Uri?> toolchainEnvironmentScript(ToolInstance compiler) async {
200-
final fromConfig = codeConfig.cCompiler.envScript;
200+
final fromConfig = codeConfig.cCompiler?.envScript;
201201
if (fromConfig != null) {
202202
logger?.fine('Using envScript from config: $fromConfig');
203203
return fromConfig;
@@ -211,7 +211,7 @@ class CompilerResolver {
211211
}
212212

213213
List<String>? toolchainEnvironmentScriptArguments() {
214-
final fromConfig = codeConfig.cCompiler.envScriptArgs;
214+
final fromConfig = codeConfig.cCompiler?.envScriptArgs;
215215
if (fromConfig != null) {
216216
logger?.fine('Using envScriptArgs from config: $fromConfig');
217217
return fromConfig;
@@ -245,7 +245,7 @@ class CompilerResolver {
245245
}
246246

247247
Future<ToolInstance?> _tryLoadLinkerFromConfig() async {
248-
final configLdUri = codeConfig.cCompiler.linker;
248+
final configLdUri = codeConfig.cCompiler?.linker;
249249
if (configLdUri != null) {
250250
assert(await File.fromUri(configLdUri).exists());
251251
logger?.finer('Using linker ${configLdUri.toFilePath()} '

pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ void main() {
7171
CompilerResolver(codeConfig: buildConfig.codeConfig, logger: logger);
7272
final compiler = await resolver.resolveCompiler();
7373
final archiver = await resolver.resolveArchiver();
74-
expect(compiler.uri, buildConfig.codeConfig.cCompiler.compiler);
75-
expect(archiver.uri, buildConfig.codeConfig.cCompiler.archiver);
74+
expect(compiler.uri, buildConfig.codeConfig.cCompiler?.compiler);
75+
expect(archiver.uri, buildConfig.codeConfig.cCompiler?.archiver);
7676
});
7777

7878
test('No compiler found', () async {

pkgs/native_toolchain_c/test/helpers.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ final List<String>? _envScriptArgs = Platform
155155
/// Configuration for the native toolchain.
156156
///
157157
/// Provided on Dart CI.
158-
final cCompiler = CCompilerConfig(
159-
compiler: _cc,
160-
archiver: _ar,
161-
linker: _ld,
162-
envScript: _envScript,
163-
envScriptArgs: _envScriptArgs,
164-
);
158+
final cCompiler = (_cc == null || _ar == null || _ld == null)
159+
? null
160+
: CCompilerConfig(
161+
compiler: _cc!,
162+
archiver: _ar!,
163+
linker: _ld!,
164+
envScript: _envScript,
165+
envScriptArgs: _envScriptArgs,
166+
);
165167

166168
extension on String {
167169
Uri asFileUri() => Uri.file(this);

0 commit comments

Comments
 (0)