diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart index 29b4795cc..3b2d1433b 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart @@ -7,6 +7,7 @@ import 'dart:io'; import 'package:logging/logging.dart'; import 'package:native_assets_cli/code_assets.dart'; +import '../../native_toolchain_c.dart'; import '../native_toolchain/android_ndk.dart'; import '../native_toolchain/apple_clang.dart'; import '../native_toolchain/clang.dart'; @@ -196,29 +197,36 @@ class CompilerResolver { return null; } - Future toolchainEnvironmentScript(ToolInstance compiler) async { - final fromInput = codeConfig.cCompiler?.envScript; - if (fromInput != null) { - logger?.fine('Using envScript from input: $fromInput'); - return fromInput; + Future> resolveEnvironment(ToolInstance compiler) async { + final envScriptFromConfig = codeConfig.cCompiler?.envScript; + if (envScriptFromConfig != null) { + logger?.fine('Using envScript from input: $envScriptFromConfig'); + final vcvarsArgs = codeConfig.cCompiler?.envScriptArgs; + if (vcvarsArgs != null) { + logger?.fine('Using envScriptArgs from input: $vcvarsArgs'); + } + return await environmentFromBatchFile( + envScriptFromConfig, + arguments: vcvarsArgs ?? [], + ); + } + + if (codeConfig.cCompiler?.compiler != null) { + logger?.fine('Compiler provided without envScript,' + ' assuming environment is already set up.'); + return {}; } final compilerTool = compiler.tool; assert(compilerTool == cl); final vcvarsScript = (await vcvars(compiler).defaultResolver!.resolve(logger: logger)).first; - return vcvarsScript.uri; - } - - List? toolchainEnvironmentScriptArguments() { - final fromInput = codeConfig.cCompiler?.envScriptArgs; - if (fromInput != null) { - logger?.fine('Using envScriptArgs from input: $fromInput'); - return fromInput; - } - - // vcvars above already has x64 or x86 in the script name. - return null; + return await environmentFromBatchFile( + vcvarsScript.uri, + arguments: [ + /* vcvarsScript already has x64 or x86 in the script name. */ + ], + ); } Future resolveLinker() async { diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart index 184bc679c..76afc334d 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart @@ -11,7 +11,6 @@ import '../native_toolchain/msvc.dart'; import '../native_toolchain/tool_likeness.dart'; import '../native_toolchain/xcode.dart'; import '../tool/tool_instance.dart'; -import '../utils/env_from_bat.dart'; import '../utils/run_process.dart'; import 'compiler_resolver.dart'; import 'language.dart'; @@ -334,10 +333,7 @@ class RunCBuilder { } Future runCl({required ToolInstance tool}) async { - final vcvars = (await _resolver.toolchainEnvironmentScript(tool))!; - final vcvarsArgs = _resolver.toolchainEnvironmentScriptArguments(); - final environment = - await environmentFromBatchFile(vcvars, arguments: vcvarsArgs ?? []); + final environment = await _resolver.resolveEnvironment(tool); final isStaticLib = staticLibrary != null; Uri? archiver_; diff --git a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart index f269bb057..9b1190663 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart @@ -42,39 +42,47 @@ void main() { ].firstOrNull?.uri; final targetOS = OS.current; - final buildInputBuilder = BuildInputBuilder() - ..setupShared( - packageName: 'dummy', - packageRoot: tempUri, - outputDirectory: tempUri, - outputDirectoryShared: tempUri2, - ) - ..config.setupBuild( - linkingEnabled: false, - dryRun: false, - ) - ..config.setupShared(buildAssetTypes: [CodeAsset.type]) - ..config.setupCode( - targetOS: targetOS, - macOS: targetOS == OS.macOS - ? MacOSConfig(targetVersion: defaultMacOSVersion) - : null, - targetArchitecture: Architecture.current, - linkModePreference: LinkModePreference.dynamic, - cCompiler: CCompilerConfig( - archiver: ar, - compiler: cc, - linker: ld, - envScript: envScript, - ), - ); - final buildInput = BuildInput(buildInputBuilder.json); - final resolver = - CompilerResolver(codeConfig: buildInput.config.code, logger: logger); - final compiler = await resolver.resolveCompiler(); - final archiver = await resolver.resolveArchiver(); - expect(compiler.uri, buildInput.config.code.cCompiler?.compiler); - expect(archiver.uri, buildInput.config.code.cCompiler?.archiver); + for (final passInEnvScript in [if (targetOS == OS.windows) true, false]) { + final buildInputBuilder = BuildInputBuilder() + ..setupShared( + packageName: 'dummy', + packageRoot: tempUri, + outputDirectory: tempUri, + outputDirectoryShared: tempUri2, + ) + ..config.setupBuild( + linkingEnabled: false, + dryRun: false, + ) + ..config.setupShared(buildAssetTypes: [CodeAsset.type]) + ..config.setupCode( + targetOS: targetOS, + macOS: targetOS == OS.macOS + ? MacOSConfig(targetVersion: defaultMacOSVersion) + : null, + targetArchitecture: Architecture.current, + linkModePreference: LinkModePreference.dynamic, + cCompiler: CCompilerConfig( + archiver: ar, + compiler: cc, + linker: ld, + envScript: passInEnvScript ? envScript : null, + ), + ); + final buildInput = BuildInput(buildInputBuilder.json); + final resolver = + CompilerResolver(codeConfig: buildInput.config.code, logger: logger); + final compiler = await resolver.resolveCompiler(); + final archiver = await resolver.resolveArchiver(); + expect(compiler.uri, buildInput.config.code.cCompiler?.compiler); + expect(archiver.uri, buildInput.config.code.cCompiler?.archiver); + final environment = await resolver.resolveEnvironment(compiler); + if (passInEnvScript) { + expect(environment, isNot(equals({}))); + } else { + expect(environment, equals({})); + } + } }); test('No compiler found', () async {