Skip to content

Commit 292a570

Browse files
committed
Allow environment overriding of C Compiler Config
1 parent b07575d commit 292a570

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

pkgs/native_assets_cli/lib/src/model/hook_config.dart

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,57 @@ abstract class HookConfigImpl implements HookConfig {
411411

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

416-
final compiler = _parseCompiler(baseUri, cCompilerJson);
415+
Uri? archiver;
416+
Uri? compiler;
417+
Uri? linker;
418+
Uri? envScript;
419+
List<String>? envScriptArgs;
420+
if (cCompilerJson != null) {
421+
compiler = _parseCompiler(baseUri, cCompilerJson);
422+
archiver = _parseArchiver(baseUri, cCompilerJson);
423+
envScript = _parseEnvScript(baseUri, cCompilerJson, compiler);
424+
envScriptArgs = _parseEnvScriptArgs(cCompilerJson);
425+
linker = _parseLinker(baseUri, cCompilerJson);
426+
}
427+
428+
// If the bundling tool didn't specify a C compiler we fallback to
429+
// identifying the C compiler based on specific environment variables.
430+
{
431+
final env = Platform.environment;
432+
String? unparseKey(String key) => key.replaceAll('.', '__').toUpperCase();
433+
String? lookup(String key) => env[unparseKey(key)];
434+
Uri? lookupUri(String key) {
435+
final value = lookup(key);
436+
return value != null ? Uri.file(value) : null;
437+
}
438+
439+
List<String>? lookupList(String key) {
440+
final value = lookup(key);
441+
if (value == null) return null;
442+
final list = value
443+
.split(' ')
444+
.map((arg) => arg.trim())
445+
.where((arg) => arg.isNotEmpty)
446+
.toList();
447+
if (list.isEmpty) return null;
448+
return list;
449+
}
450+
451+
archiver ??= lookupUri(CCompilerConfigImpl.arConfigKeyFull);
452+
compiler ??= lookupUri(CCompilerConfigImpl.ccConfigKeyFull);
453+
linker ??= lookupUri(CCompilerConfigImpl.ldConfigKeyFull);
454+
envScript ??= lookupUri(CCompilerConfigImpl.envScriptConfigKeyFull);
455+
envScriptArgs ??=
456+
lookupList(CCompilerConfigImpl.envScriptArgsConfigKeyFull);
457+
}
458+
417459
return CCompilerConfigImpl(
418-
archiver: _parseArchiver(baseUri, cCompilerJson),
460+
archiver: archiver,
419461
compiler: compiler,
420-
envScript: _parseEnvScript(baseUri, cCompilerJson, compiler),
421-
envScriptArgs: _parseEnvScriptArgs(cCompilerJson),
422-
linker: _parseLinker(baseUri, cCompilerJson),
462+
envScript: envScript,
463+
envScriptArgs: envScriptArgs,
464+
linker: linker,
423465
);
424466
}
425467

0 commit comments

Comments
 (0)