Skip to content

Commit ba34674

Browse files
authored
[native_toolchain_c] Support Clang on Windows 2 (#1910)
Closes: #1892 Using clang on Windows requires being run with the windows dev environment _or_ an MSVC toolchain installed which is discoverable on the system. On the GitHub bots, MSVC comes pre-installed, so we can't reproduce the case where Clang cannot find the MSVC dev environment. I've tested this locally by (1) uninstalling visual studio, (2) seeing that the build with Clang fails, (3) hardcoding in a dev environment (from depot_tools) in `native_toolchain_c`, and (4) seeing that that the tests pass.
1 parent 434db67 commit ba34674

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ class CompilerResolver {
218218
}
219219

220220
final compilerTool = compiler.tool;
221-
assert(compilerTool == cl);
221+
if (compilerTool != cl) {
222+
// If Clang is used on Windows, and we could discover the MSVC
223+
// installation, then Clang should be able to discover it as well.
224+
return {};
225+
}
222226
final vcvarsScript =
223227
(await vcvars(compiler).defaultResolver!.resolve(logger: logger)).first;
224228
return await environmentFromBatchFile(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class RunCBuilder {
131131
}
132132

133133
Future<void> runClangLike({required ToolInstance tool}) async {
134+
// Clang for Windows requires the MSVC Developer Environment.
135+
final environment = await _resolver.resolveEnvironment(tool);
136+
134137
final isStaticLib = staticLibrary != null;
135138
Uri? archiver_;
136139
if (isStaticLib) {
@@ -176,6 +179,7 @@ class RunCBuilder {
176179
targetMacOSVersion,
177180
[sourceFiles[i]],
178181
objectFile,
182+
environment,
179183
);
180184
objectFiles.add(objectFile);
181185
}
@@ -189,6 +193,7 @@ class RunCBuilder {
189193
logger: logger,
190194
captureOutput: false,
191195
throwOnUnexpectedExitCode: true,
196+
environment: environment,
192197
);
193198
} else {
194199
await _compile(
@@ -200,6 +205,7 @@ class RunCBuilder {
200205
targetMacOSVersion,
201206
sourceFiles,
202207
dynamicLibrary != null ? outDir.resolveUri(dynamicLibrary!) : null,
208+
environment,
203209
);
204210
}
205211
}
@@ -214,9 +220,11 @@ class RunCBuilder {
214220
int? targetMacOSVersion,
215221
Iterable<String> sourceFiles,
216222
Uri? outFile,
223+
Map<String, String> environment,
217224
) async {
218225
await runProcess(
219226
executable: toolInstance.uri,
227+
environment: environment,
220228
arguments: [
221229
if (codeConfig.targetOS == OS.android) ...[
222230
'--target='

0 commit comments

Comments
 (0)