Skip to content

Commit c77845e

Browse files
Fix handle spaces in the path to the cl.exe compiler
When the Visual Studio C++ compiler (cl.exe) is installed in a path containing spaces (e.g., the default "C:\Program Files\Microsoft Visual Studio\2022\..."), the build system failed to execute it correctly. The command was invoked without proper quoting, leading to an error: 'C:\Program' is not recognized as an internal or external command Signed-off-by: Dee HY <[email protected]>
1 parent 4341e37 commit c77845e

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class CBuilder extends CTool implements Builder {
7979
super.linkModePreference,
8080
super.optimizationLevel = OptimizationLevel.o3,
8181
this.buildMode = BuildMode.release,
82+
super.runInShell,
8283
}) : super(type: OutputType.library);
8384

8485
CBuilder.executable({
@@ -105,6 +106,7 @@ class CBuilder extends CTool implements Builder {
105106
super.cppLinkStdLib,
106107
super.optimizationLevel = OptimizationLevel.o3,
107108
this.buildMode = BuildMode.release,
109+
super.runInShell,
108110
}) : super(
109111
type: OutputType.executable,
110112
assetName: null,
@@ -203,6 +205,7 @@ class CBuilder extends CTool implements Builder {
203205
language: language,
204206
cppLinkStdLib: cppLinkStdLib,
205207
optimizationLevel: optimizationLevel,
208+
runInShell: runInShell,
206209
);
207210
await task.run();
208211

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CLinker extends CTool implements Linker {
4141
super.cppLinkStdLib,
4242
super.linkModePreference,
4343
super.optimizationLevel = OptimizationLevel.o3,
44+
super.runInShell,
4445
}) : super(type: OutputType.library);
4546

4647
/// Runs the C Linker with on this C build spec.
@@ -94,6 +95,7 @@ class CLinker extends CTool implements Linker {
9495
language: language,
9596
cppLinkStdLib: cppLinkStdLib,
9697
optimizationLevel: optimizationLevel,
98+
runInShell: runInShell,
9799
);
98100
await task.run();
99101

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ abstract class CTool {
166166
/// What optimization level should be used for compiling.
167167
final OptimizationLevel optimizationLevel;
168168

169+
/// Whether to run the process in a shell.
170+
///
171+
/// If the path to cl.exe contains spaces, this option needs to be set to
172+
/// false.
173+
final bool? runInShell;
174+
169175
CTool({
170176
required this.name,
171177
required this.packageName,
@@ -186,5 +192,6 @@ abstract class CTool {
186192
required this.linkModePreference,
187193
required this.type,
188194
required this.optimizationLevel,
195+
this.runInShell,
189196
});
190197
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class RunCBuilder {
5050
final Language language;
5151
final String? cppLinkStdLib;
5252
final OptimizationLevel optimizationLevel;
53+
final bool? runInShell;
5354

5455
RunCBuilder({
5556
required this.input,
@@ -73,6 +74,7 @@ class RunCBuilder {
7374
this.language = Language.c,
7475
this.cppLinkStdLib,
7576
required this.optimizationLevel,
77+
this.runInShell,
7678
}) : outDir = input.outputDirectory,
7779
assert(
7880
[executable, dynamicLibrary, staticLibrary].whereType<Uri>().length ==
@@ -196,6 +198,7 @@ class RunCBuilder {
196198
captureOutput: false,
197199
throwOnUnexpectedExitCode: true,
198200
environment: environment,
201+
runInShell: runInShell,
199202
);
200203
} else {
201204
await _compile(
@@ -343,6 +346,7 @@ class RunCBuilder {
343346
logger: logger,
344347
captureOutput: false,
345348
throwOnUnexpectedExitCode: true,
349+
runInShell: runInShell,
346350
);
347351
}
348352

@@ -401,6 +405,7 @@ class RunCBuilder {
401405
captureOutput: false,
402406
stdoutLogLevel: Level.INFO,
403407
throwOnUnexpectedExitCode: true,
408+
runInShell: runInShell,
404409
);
405410

406411
if (staticLibrary != null) {

pkgs/native_toolchain_c/lib/src/utils/run_process.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Future<RunProcessResult> runProcess({
2222
Level stdoutLogLevel = Level.FINE,
2323
int expectedExitCode = 0,
2424
bool throwOnUnexpectedExitCode = false,
25+
bool? runInShell,
2526
}) async {
2627
final printWorkingDir =
2728
workingDirectory != null && workingDirectory != Directory.current.uri;
@@ -41,7 +42,7 @@ Future<RunProcessResult> runProcess({
4142
arguments,
4243
workingDirectory: workingDirectory?.toFilePath(),
4344
environment: environment,
44-
runInShell: Platform.isWindows && workingDirectory != null,
45+
runInShell: runInShell ?? Platform.isWindows && workingDirectory != null,
4546
);
4647

4748
final stdoutSub = process.stdout.listen((List<int> data) {

0 commit comments

Comments
 (0)