Skip to content

Commit

Permalink
[native_assets_cli] Make CCompilerConfig fields less nullable (#1809)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dcharkes authored Dec 12, 2024
1 parent 4d81ce6 commit b86e34d
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 71 deletions.
12 changes: 4 additions & 8 deletions pkgs/native_assets_builder/test/build_runner/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,13 @@ final CCompilerConfig? dartCICompilerConfig = (() {
.toList();
final hasEnvScriptArgs = envScriptArgs != null && envScriptArgs.isNotEmpty;

if (cc != null ||
ar != null ||
ld != null ||
envScript != null ||
hasEnvScriptArgs) {
if (cc != null && ar != null && ld != null) {
return CCompilerConfig(
archiver: ar != null ? Uri.file(ar) : null,
compiler: cc != null ? Uri.file(cc) : null,
archiver: Uri.file(ar),
compiler: Uri.file(cc),
envScript: envScript != null ? Uri.file(envScript) : null,
envScriptArgs: hasEnvScriptArgs ? envScriptArgs : null,
linker: ld != null ? Uri.file(ld) : null,
linker: Uri.file(ld),
);
}
return null;
Expand Down
16 changes: 9 additions & 7 deletions pkgs/native_assets_builder/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,15 @@ final List<String>? _envScriptArgs = Platform
/// Configuration for the native toolchain.
///
/// Provided on Dart CI.
final cCompiler = CCompilerConfig(
compiler: _cc,
archiver: _ar,
linker: _ld,
envScript: _envScript,
envScriptArgs: _envScriptArgs,
);
final cCompiler = (_cc == null || _ar == null || _ld == null)
? null
: CCompilerConfig(
compiler: _cc!,
archiver: _ar!,
linker: _ld!,
envScript: _envScript,
envScriptArgs: _envScriptArgs,
);

extension on String {
Uri asFileUri() => Uri.file(this);
Expand Down
24 changes: 12 additions & 12 deletions pkgs/native_assets_cli/lib/src/code_assets/c_compiler_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import '../utils/map.dart';
/// The configuration for a C toolchain.
final class CCompilerConfig {
/// Path to a C compiler.
late final Uri? compiler;
late final Uri compiler;

/// Path to a native linker.
late final Uri? linker;
late final Uri linker;

/// Path to a native archiver.
late final Uri? archiver;
late final Uri archiver;

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

/// Constructs a new [CCompilerConfig] based on the given toolchain tools.
CCompilerConfig({
this.archiver,
this.compiler,
this.linker,
required this.archiver,
required this.compiler,
required this.linker,
this.envScript,
this.envScriptArgs,
});
Expand All @@ -40,21 +40,21 @@ final class CCompilerConfig {
/// [CCompilerConfig.toJson].
factory CCompilerConfig.fromJson(Map<String, Object?> json) =>
CCompilerConfig(
archiver: json.optionalPath(_arConfigKey),
compiler: json.optionalPath(_ccConfigKey),
archiver: json.path(_arConfigKey),
compiler: json.path(_ccConfigKey),
envScript: json.optionalPath(_envScriptConfigKey),
envScriptArgs: json.optionalStringList(_envScriptArgsConfigKey),
linker: json.optionalPath(_ldConfigKey),
linker: json.path(_ldConfigKey),
);

/// The json representation of this [CCompilerConfig].
///
/// The returned json can be used in [CCompilerConfig.fromJson] to
/// obtain a [CCompilerConfig] again.
Map<String, Object> toJson() => {
if (archiver != null) _arConfigKey: archiver!.toFilePath(),
if (compiler != null) _ccConfigKey: compiler!.toFilePath(),
if (linker != null) _ldConfigKey: linker!.toFilePath(),
_arConfigKey: archiver.toFilePath(),
_ccConfigKey: compiler.toFilePath(),
_ldConfigKey: linker.toFilePath(),
if (envScript != null) _envScriptConfigKey: envScript!.toFilePath(),
if (envScriptArgs != null) _envScriptArgsConfigKey: envScriptArgs!,
}.sortOnKey();
Expand Down
4 changes: 2 additions & 2 deletions pkgs/native_assets_cli/lib/src/code_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CodeConfig {
final Architecture? _targetArchitecture;

final LinkModePreference linkModePreference;
final CCompilerConfig cCompiler;
final CCompilerConfig? cCompiler;
final int? targetIOSVersion;
final int? targetMacOSVersion;
final int? targetAndroidNdkApi;
Expand All @@ -62,7 +62,7 @@ class CodeConfig {
targetOS = OS.fromString(config.json.string(_targetOSConfigKey)),
cCompiler = switch (config.json.optionalMap(_compilerKey)) {
final Map<String, Object?> map => CCompilerConfig.fromJson(map),
null => CCompilerConfig(),
null => null,
},
targetIOSVersion = config.json.optionalInt(_targetIOSVersionKey),
targetMacOSVersion = config.json.optionalInt(_targetMacOSVersionKey),
Expand Down
33 changes: 18 additions & 15 deletions pkgs/native_assets_cli/lib/src/code_assets/validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,24 @@ ValidationErrors _validateCodeConfig(
break;
}
final compilerConfig = codeConfig.cCompiler;
final compiler = compilerConfig.compiler?.toFilePath();
if (compiler != null && !File(compiler).existsSync()) {
errors.add('$configName.codeConfig.compiler ($compiler) does not exist.');
}
final linker = compilerConfig.linker?.toFilePath();
if (linker != null && !File(linker).existsSync()) {
errors.add('$configName.codeConfig.linker ($linker) does not exist.');
}
final archiver = compilerConfig.archiver?.toFilePath();
if (archiver != null && !File(archiver).existsSync()) {
errors.add('$configName.codeConfig.archiver ($archiver) does not exist.');
}
final envScript = compilerConfig.envScript?.toFilePath();
if (envScript != null && !File(envScript).existsSync()) {
errors.add('$configName.codeConfig.envScript ($envScript) does not exist.');
if (compilerConfig != null) {
final compiler = compilerConfig.compiler.toFilePath();
if (!File(compiler).existsSync()) {
errors.add('$configName.codeConfig.compiler ($compiler) does not exist.');
}
final linker = compilerConfig.linker.toFilePath();
if (!File(linker).existsSync()) {
errors.add('$configName.codeConfig.linker ($linker) does not exist.');
}
final archiver = compilerConfig.archiver.toFilePath();
if (!File(archiver).existsSync()) {
errors.add('$configName.codeConfig.archiver ($archiver) does not exist.');
}
final envScript = compilerConfig.envScript?.toFilePath();
if (envScript != null && !File(envScript).existsSync()) {
errors
.add('$configName.codeConfig.envScript ($envScript) does not exist.');
}
}
return errors;
}
Expand Down
10 changes: 4 additions & 6 deletions pkgs/native_assets_cli/test/code_assets/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ void main() async {
expect(() => codeConfig.targetArchitecture, throwsStateError);
expect(codeConfig.targetAndroidNdkApi, null);
expect(codeConfig.linkModePreference, LinkModePreference.preferStatic);
expect(codeConfig.cCompiler.compiler, null);
expect(codeConfig.cCompiler.linker, null);
expect(codeConfig.cCompiler.archiver, null);
expect(codeConfig.cCompiler, null);
}

void expectCorrectCodeConfig(
Expand All @@ -81,9 +79,9 @@ void main() async {
expect(codeConfig.targetArchitecture, Architecture.arm64);
expect(codeConfig.targetAndroidNdkApi, 30);
expect(codeConfig.linkModePreference, LinkModePreference.preferStatic);
expect(codeConfig.cCompiler.compiler, fakeClang);
expect(codeConfig.cCompiler.linker, fakeLd);
expect(codeConfig.cCompiler.archiver, fakeAr);
expect(codeConfig.cCompiler?.compiler, fakeClang);
expect(codeConfig.cCompiler?.linker, fakeLd);
expect(codeConfig.cCompiler?.archiver, fakeAr);
}

test('BuildConfig.codeConfig (dry-run)', () {
Expand Down
16 changes: 9 additions & 7 deletions pkgs/native_assets_cli/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ final List<String>? _envScriptArgs = Platform
/// Configuration for the native toolchain.
///
/// Provided on Dart CI.
final cCompiler = CCompilerConfig(
compiler: _cc,
archiver: _ar,
linker: _ld,
envScript: _envScript,
envScriptArgs: _envScriptArgs,
);
final cCompiler = (_cc == null || _ar == null || _ld == null)
? null
: CCompilerConfig(
compiler: _cc!,
archiver: _ar!,
linker: _ld!,
envScript: _envScript,
envScriptArgs: _envScriptArgs,
);

extension on String {
Uri asFileUri() => Uri.file(this);
Expand Down
10 changes: 5 additions & 5 deletions pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class CompilerResolver {
}

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

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

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

List<String>? toolchainEnvironmentScriptArguments() {
final fromConfig = codeConfig.cCompiler.envScriptArgs;
final fromConfig = codeConfig.cCompiler?.envScriptArgs;
if (fromConfig != null) {
logger?.fine('Using envScriptArgs from config: $fromConfig');
return fromConfig;
Expand Down Expand Up @@ -245,7 +245,7 @@ class CompilerResolver {
}

Future<ToolInstance?> _tryLoadLinkerFromConfig() async {
final configLdUri = codeConfig.cCompiler.linker;
final configLdUri = codeConfig.cCompiler?.linker;
if (configLdUri != null) {
assert(await File.fromUri(configLdUri).exists());
logger?.finer('Using linker ${configLdUri.toFilePath()} '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ void main() {
CompilerResolver(codeConfig: buildConfig.codeConfig, logger: logger);
final compiler = await resolver.resolveCompiler();
final archiver = await resolver.resolveArchiver();
expect(compiler.uri, buildConfig.codeConfig.cCompiler.compiler);
expect(archiver.uri, buildConfig.codeConfig.cCompiler.archiver);
expect(compiler.uri, buildConfig.codeConfig.cCompiler?.compiler);
expect(archiver.uri, buildConfig.codeConfig.cCompiler?.archiver);
});

test('No compiler found', () async {
Expand Down
16 changes: 9 additions & 7 deletions pkgs/native_toolchain_c/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ final List<String>? _envScriptArgs = Platform
/// Configuration for the native toolchain.
///
/// Provided on Dart CI.
final cCompiler = CCompilerConfig(
compiler: _cc,
archiver: _ar,
linker: _ld,
envScript: _envScript,
envScriptArgs: _envScriptArgs,
);
final cCompiler = (_cc == null || _ar == null || _ld == null)
? null
: CCompilerConfig(
compiler: _cc!,
archiver: _ar!,
linker: _ld!,
envScript: _envScript,
envScriptArgs: _envScriptArgs,
);

extension on String {
Uri asFileUri() => Uri.file(this);
Expand Down

0 comments on commit b86e34d

Please sign in to comment.