Skip to content

Commit

Permalink
Make the package:native_assets_builder have special logic to create…
Browse files Browse the repository at this point in the history
… C Compiler configuration for testing on Dart CI

This removes the somewhat ugly piece of recognition of environment
variables in `HookConfig.fromJson()` to the
`package:native_assets_builder`.

We also rename those variables to prefix them with `DART_HOOK_TESTING`.
This way we make it clear that this isn't intended to be used by
end-users.

=> Rolling this to Dart SDK will require updating the test runner to
   also do this prefixing.
  • Loading branch information
mkustermann committed Sep 25, 2024
1 parent 7f206e3 commit 301c361
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 52 deletions.
37 changes: 37 additions & 0 deletions pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,43 @@ class NativeAssetsBuildRunner {
assert(hook == Hook.link || buildResult == null);
assert(hook == Hook.build || linkingEnabled == null);

// Specifically for running our tests on Dart CI with the test runner, we
// recognize specific variables to setup the C Compiler configuration.
if (cCompilerConfig == null) {
String? unparseKey(String key) =>
'DART_HOOK_TESTING_${key.replaceAll('.', '__').toUpperCase()}';

final env = Platform.environment;
String? lookup(String key) => env[unparseKey(key)];

final cc = lookup(CCompilerConfigImpl.ccConfigKeyFull);
final ar = lookup(CCompilerConfigImpl.arConfigKeyFull);
final ld = lookup(CCompilerConfigImpl.ldConfigKeyFull);
final envScript = lookup(CCompilerConfigImpl.envScriptConfigKeyFull);
final envScriptArgs =
lookup(CCompilerConfigImpl.envScriptArgsConfigKeyFull)
?.split(' ')
.map((arg) => arg.trim())
.where((arg) => arg.isNotEmpty)
.toList();
final hasEnvScriptArgs =
envScriptArgs != null && envScriptArgs.isNotEmpty;

if (cc != null ||
ar != null ||
ld != null ||
envScript != null ||
hasEnvScriptArgs) {
cCompilerConfig = CCompilerConfigImpl(
archiver: ar != null ? Uri.file(ar) : null,
compiler: cc != null ? Uri.file(cc) : null,
envScript: envScript != null ? Uri.file(envScript) : null,
envScriptArgs: hasEnvScriptArgs ? envScriptArgs : null,
linker: ld != null ? Uri.file(ld) : null,
);
}
}

packageLayout ??= await PackageLayout.fromRootPackageRoot(workingDirectory);
final (buildPlan, packageGraph, planSuccess) = await _makePlan(
hook: hook,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import 'helpers.dart';
const Timeout longTimeout = Timeout(Duration(minutes: 5));

void main() async {
String unparseKey(String key) => key.replaceAll('.', '__').toUpperCase();
String unparseKey(String key) =>
'DART_HOOK_TESTING_${key.replaceAll('.', '__').toUpperCase()}';

final arKey = unparseKey(CCompilerConfigImpl.arConfigKeyFull);
final ccKey = unparseKey(CCompilerConfigImpl.ccConfigKeyFull);
final ldKey = unparseKey(CCompilerConfigImpl.ldConfigKeyFull);
Expand Down
3 changes: 2 additions & 1 deletion pkgs/native_assets_builder/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ final pkgNativeAssetsBuilderUri = findPackageRoot('native_assets_builder');

final testDataUri = pkgNativeAssetsBuilderUri.resolve('test_data/');

String unparseKey(String key) => key.replaceAll('.', '__').toUpperCase();
String unparseKey(String key) =>
'DART_HOOK_TESTING_${key.replaceAll('.', '__').toUpperCase()}';

/// Archiver provided by the environment.
///
Expand Down
54 changes: 6 additions & 48 deletions pkgs/native_assets_cli/lib/src/model/hook_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,57 +411,15 @@ abstract class HookConfigImpl implements HookConfig {

final cCompilerJson =
config.getOptional<Map<String, Object?>>(CCompilerConfigImpl.configKey);
if (cCompilerJson == null) return CCompilerConfigImpl();

Uri? archiver;
Uri? compiler;
Uri? linker;
Uri? envScript;
List<String>? envScriptArgs;
if (cCompilerJson != null) {
compiler = _parseCompiler(baseUri, cCompilerJson);
archiver = _parseArchiver(baseUri, cCompilerJson);
envScript = _parseEnvScript(baseUri, cCompilerJson, compiler);
envScriptArgs = _parseEnvScriptArgs(cCompilerJson);
linker = _parseLinker(baseUri, cCompilerJson);
}

// If the bundling tool didn't specify a C compiler we fallback to
// identifying the C compiler based on specific environment variables.
{
final env = Platform.environment;
String? unparseKey(String key) => key.replaceAll('.', '__').toUpperCase();
String? lookup(String key) => env[unparseKey(key)];
Uri? lookupUri(String key) {
final value = lookup(key);
return value != null ? Uri.file(value) : null;
}

List<String>? lookupList(String key) {
final value = lookup(key);
if (value == null) return null;
final list = value
.split(' ')
.map((arg) => arg.trim())
.where((arg) => arg.isNotEmpty)
.toList();
if (list.isEmpty) return null;
return list;
}

archiver ??= lookupUri(CCompilerConfigImpl.arConfigKeyFull);
compiler ??= lookupUri(CCompilerConfigImpl.ccConfigKeyFull);
linker ??= lookupUri(CCompilerConfigImpl.ldConfigKeyFull);
envScript ??= lookupUri(CCompilerConfigImpl.envScriptConfigKeyFull);
envScriptArgs ??=
lookupList(CCompilerConfigImpl.envScriptArgsConfigKeyFull);
}

final compiler = _parseCompiler(baseUri, cCompilerJson);
return CCompilerConfigImpl(
archiver: archiver,
archiver: _parseArchiver(baseUri, cCompilerJson),
compiler: compiler,
envScript: envScript,
envScriptArgs: envScriptArgs,
linker: linker,
envScript: _parseEnvScript(baseUri, cCompilerJson, compiler),
envScriptArgs: _parseEnvScriptArgs(cCompilerJson),
linker: _parseLinker(baseUri, cCompilerJson),
);
}

Expand Down
3 changes: 2 additions & 1 deletion pkgs/native_assets_cli/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ extension on Uri {
String get name => pathSegments.where((e) => e != '').last;
}

String unparseKey(String key) => key.replaceAll('.', '__').toUpperCase();
String unparseKey(String key) =>
'DART_HOOK_TESTING_${key.replaceAll('.', '__').toUpperCase()}';

/// Archiver provided by the environment.
///
Expand Down
3 changes: 2 additions & 1 deletion pkgs/native_toolchain_c/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ extension on Uri {
String get name => pathSegments.where((e) => e != '').last;
}

String unparseKey(String key) => key.replaceAll('.', '__').toUpperCase();
String unparseKey(String key) =>
'DART_HOOK_TESTING_${key.replaceAll('.', '__').toUpperCase()}';

/// Archiver provided by the environment.
///
Expand Down

0 comments on commit 301c361

Please sign in to comment.