Skip to content

Commit 982230f

Browse files
committed
[native_toolchain_c] Support environment for cl.exe already set up
1 parent a646680 commit 982230f

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:io';
77
import 'package:logging/logging.dart';
88
import 'package:native_assets_cli/code_assets.dart';
99

10+
import '../../native_toolchain_c.dart';
1011
import '../native_toolchain/android_ndk.dart';
1112
import '../native_toolchain/apple_clang.dart';
1213
import '../native_toolchain/clang.dart';
@@ -196,29 +197,36 @@ class CompilerResolver {
196197
return null;
197198
}
198199

199-
Future<Uri?> toolchainEnvironmentScript(ToolInstance compiler) async {
200-
final fromInput = codeConfig.cCompiler?.envScript;
201-
if (fromInput != null) {
202-
logger?.fine('Using envScript from input: $fromInput');
203-
return fromInput;
200+
Future<Map<String, String>> resolveEnvironment(ToolInstance compiler) async {
201+
final envScriptFromConfig = codeConfig.cCompiler?.envScript;
202+
if (envScriptFromConfig != null) {
203+
logger?.fine('Using envScript from input: $envScriptFromConfig');
204+
final vcvarsArgs = codeConfig.cCompiler?.envScriptArgs;
205+
if (vcvarsArgs != null) {
206+
logger?.fine('Using envScriptArgs from input: $vcvarsArgs');
207+
}
208+
return await environmentFromBatchFile(
209+
envScriptFromConfig,
210+
arguments: vcvarsArgs ?? [],
211+
);
212+
}
213+
214+
if (codeConfig.cCompiler?.compiler != null) {
215+
logger?.fine('Compiler provided without envScript,'
216+
' assuming environment is already set up.');
217+
return {};
204218
}
205219

206220
final compilerTool = compiler.tool;
207221
assert(compilerTool == cl);
208222
final vcvarsScript =
209223
(await vcvars(compiler).defaultResolver!.resolve(logger: logger)).first;
210-
return vcvarsScript.uri;
211-
}
212-
213-
List<String>? toolchainEnvironmentScriptArguments() {
214-
final fromInput = codeConfig.cCompiler?.envScriptArgs;
215-
if (fromInput != null) {
216-
logger?.fine('Using envScriptArgs from input: $fromInput');
217-
return fromInput;
218-
}
219-
220-
// vcvars above already has x64 or x86 in the script name.
221-
return null;
224+
return await environmentFromBatchFile(
225+
vcvarsScript.uri,
226+
arguments: [
227+
/* vcvarsScript already has x64 or x86 in the script name. */
228+
],
229+
);
222230
}
223231

224232
Future<ToolInstance> resolveLinker() async {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import '../native_toolchain/msvc.dart';
1111
import '../native_toolchain/tool_likeness.dart';
1212
import '../native_toolchain/xcode.dart';
1313
import '../tool/tool_instance.dart';
14-
import '../utils/env_from_bat.dart';
1514
import '../utils/run_process.dart';
1615
import 'compiler_resolver.dart';
1716
import 'language.dart';
@@ -334,10 +333,7 @@ class RunCBuilder {
334333
}
335334

336335
Future<void> runCl({required ToolInstance tool}) async {
337-
final vcvars = (await _resolver.toolchainEnvironmentScript(tool))!;
338-
final vcvarsArgs = _resolver.toolchainEnvironmentScriptArguments();
339-
final environment =
340-
await environmentFromBatchFile(vcvars, arguments: vcvarsArgs ?? []);
336+
final environment = await _resolver.resolveEnvironment(tool);
341337

342338
final isStaticLib = staticLibrary != null;
343339
Uri? archiver_;

0 commit comments

Comments
 (0)