Skip to content

Commit 7252d64

Browse files
committed
Make the cmake-smoke-test work on Windows
1 parent e8c39e8 commit 7252d64

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

Plugins/cmake-smoke-test/cmake-smoke-test.swift

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ struct CMakeSmokeTest: CommandPlugin {
3737
let extraCMakeArgs = args.extractOption(named: "extra-cmake-arg")
3838
Diagnostics.progress("Extra cmake args: \(extraCMakeArgs.joined(separator: " "))")
3939

40-
let moduleCachePath = context.pluginWorkDirectoryURL.appending(component: "module-cache").path()
40+
let moduleCachePath = try context.pluginWorkDirectoryURL.appending(component: "module-cache").filePath
4141

4242
let swiftBuildURL = context.package.directoryURL
4343
let swiftBuildBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-build")
44-
Diagnostics.progress("swift-build: \(swiftBuildURL.path())")
44+
try Diagnostics.progress("swift-build: \(swiftBuildURL.filePath)")
4545

4646
let swiftToolsSupportCoreURL = try findDependency("swift-tools-support-core", pluginContext: context)
4747
let swiftToolsSupportCoreBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-tools-support-core")
@@ -70,12 +70,12 @@ struct CMakeSmokeTest: CommandPlugin {
7070
sharedSwiftFlags += ["-sdk", sysrootPath]
7171
}
7272

73-
let cMakeProjectArgs = [
74-
"-DArgumentParser_DIR=\(swiftArgumentParserBuildURL.appending(components: "cmake", "modules").path())",
75-
"-DLLBuild_DIR=\(llbuildBuildURL.appending(components: "cmake", "modules").path())",
76-
"-DTSC_DIR=\(swiftToolsSupportCoreBuildURL.appending(components: "cmake", "modules").path())",
77-
"-DSwiftDriver_DIR=\(swiftDriverBuildURL.appending(components: "cmake", "modules").path())",
78-
"-DSwiftSystem_DIR=\(swiftSystemBuildURL.appending(components: "cmake", "modules").path())"
73+
let cMakeProjectArgs = try [
74+
"-DArgumentParser_DIR=\(swiftArgumentParserBuildURL.appending(components: "cmake", "modules").filePath)",
75+
"-DLLBuild_DIR=\(llbuildBuildURL.appending(components: "cmake", "modules").filePath)",
76+
"-DTSC_DIR=\(swiftToolsSupportCoreBuildURL.appending(components: "cmake", "modules").filePath)",
77+
"-DSwiftDriver_DIR=\(swiftDriverBuildURL.appending(components: "cmake", "modules").filePath)",
78+
"-DSwiftSystem_DIR=\(swiftSystemBuildURL.appending(components: "cmake", "modules").filePath)"
7979
]
8080

8181
let sharedCMakeArgs = [
@@ -86,34 +86,34 @@ struct CMakeSmokeTest: CommandPlugin {
8686
] + cMakeProjectArgs + extraCMakeArgs
8787

8888
Diagnostics.progress("Building swift-tools-support-core")
89-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftToolsSupportCoreURL.path()], workingDirectory: swiftToolsSupportCoreBuildURL)
89+
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftToolsSupportCoreURL.filePath], workingDirectory: swiftToolsSupportCoreBuildURL)
9090
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftToolsSupportCoreBuildURL)
9191
Diagnostics.progress("Built swift-tools-support-core")
9292

9393
if hostOS != .macOS {
9494
Diagnostics.progress("Building swift-system")
95-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftSystemURL.path()], workingDirectory: swiftSystemBuildURL)
95+
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftSystemURL.filePath], workingDirectory: swiftSystemBuildURL)
9696
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftSystemBuildURL)
9797
Diagnostics.progress("Built swift-system")
9898
}
9999

100100
Diagnostics.progress("Building llbuild")
101-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + ["-DLLBUILD_SUPPORT_BINDINGS:=Swift", llbuildURL.path()], workingDirectory: llbuildBuildURL)
101+
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + ["-DLLBUILD_SUPPORT_BINDINGS:=Swift", llbuildURL.filePath], workingDirectory: llbuildBuildURL)
102102
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: llbuildBuildURL)
103103
Diagnostics.progress("Built llbuild")
104104

105105
Diagnostics.progress("Building swift-argument-parser")
106-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + ["-DBUILD_TESTING=NO", "-DBUILD_EXAMPLES=NO", swiftArgumentParserURL.path()], workingDirectory: swiftArgumentParserBuildURL)
106+
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + ["-DBUILD_TESTING=NO", "-DBUILD_EXAMPLES=NO", swiftArgumentParserURL.filePath], workingDirectory: swiftArgumentParserBuildURL)
107107
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftArgumentParserBuildURL)
108108
Diagnostics.progress("Built swift-argument-parser")
109109

110110
Diagnostics.progress("Building swift-driver")
111-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftDriverURL.path()], workingDirectory: swiftDriverBuildURL)
111+
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftDriverURL.filePath], workingDirectory: swiftDriverBuildURL)
112112
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftDriverBuildURL)
113113
Diagnostics.progress("Built swift-driver")
114114

115115
Diagnostics.progress("Building swift-build in \(swiftBuildBuildURL)")
116-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftBuildURL.path()], workingDirectory: swiftBuildBuildURL)
116+
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftBuildURL.filePath], workingDirectory: swiftBuildBuildURL)
117117
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftBuildBuildURL)
118118
Diagnostics.progress("Built swift-build")
119119
}
@@ -135,9 +135,10 @@ struct CMakeSmokeTest: CommandPlugin {
135135
throw Errors.missingRepository(name)
136136
}
137137
let dependencyURL = dependency.directoryURL
138-
Diagnostics.progress("\(name): \(dependencyURL.path())")
139-
guard FileManager.default.fileExists(atPath: dependencyURL.path()) else {
140-
throw Errors.missingRepository(dependencyURL.path())
138+
let dependencyFilePath = try dependencyURL.filePath
139+
Diagnostics.progress("\(name): \(dependencyFilePath)")
140+
guard FileManager.default.fileExists(atPath: dependencyFilePath) else {
141+
throw Errors.missingRepository(dependencyFilePath)
141142
}
142143
return dependencyURL
143144
}
@@ -169,6 +170,19 @@ enum OS {
169170
}
170171
}
171172

173+
extension URL {
174+
var filePath: String {
175+
get throws {
176+
try withUnsafeFileSystemRepresentation { path in
177+
guard let path else {
178+
throw Errors.miscError("cannot get file path for URL: \(self)")
179+
}
180+
return String(cString: path)
181+
}
182+
}
183+
}
184+
}
185+
172186
extension Process {
173187
func run() async throws {
174188
try await withCheckedThrowingContinuation { continuation in
@@ -186,8 +200,8 @@ extension Process {
186200
}
187201

188202
static func checkNonZeroExit(url: URL, arguments: [String], workingDirectory: URL, environment: [String: String]? = nil) async throws {
189-
Diagnostics.progress("\(url.path()) \(arguments.joined(separator: " "))")
190-
#if USE_PROCESS_SPAWNING_WORKAROUND
203+
try Diagnostics.progress("\(url.filePath) \(arguments.joined(separator: " "))")
204+
#if USE_PROCESS_SPAWNING_WORKAROUND && !os(Windows)
191205
Diagnostics.progress("Using process spawning workaround")
192206
// Linux workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/4772
193207
// Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, creating processes that even have SIGTERM blocked
@@ -197,7 +211,7 @@ extension Process {
197211
var attrs: posix_spawnattr_t = posix_spawnattr_t()
198212
defer { posix_spawnattr_destroy(&attrs) }
199213
posix_spawn_file_actions_init(&fileActions)
200-
posix_spawn_file_actions_addchdir_np(&fileActions, workingDirectory.path())
214+
try posix_spawn_file_actions_addchdir_np(&fileActions, workingDirectory.filePath)
201215

202216
posix_spawnattr_init(&attrs)
203217
posix_spawnattr_setpgroup(&attrs, 0)
@@ -216,9 +230,9 @@ extension Process {
216230
posix_spawnattr_setsigdefault(&attrs, &mostSignals)
217231
posix_spawnattr_setflags(&attrs, numericCast(POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK))
218232
var pid: pid_t = -1
219-
try withArrayOfCStrings([url.path()] + arguments) { arguments in
233+
try withArrayOfCStrings([url.filePath] + arguments) { arguments in
220234
try withArrayOfCStrings((environment ?? [:]).map { key, value in "\(key)=\(value)" }) { environment in
221-
let spawnResult = posix_spawn(&pid, url.path(), /*file_actions=*/&fileActions, /*attrp=*/&attrs, arguments, nil);
235+
let spawnResult = try posix_spawn(&pid, url.filePath, /*file_actions=*/&fileActions, /*attrp=*/&attrs, arguments, nil);
222236
var exitCode: Int32 = -1
223237
var result = wait4(pid, &exitCode, 0, nil);
224238
while (result == -1 && errno == EINTR) {
@@ -246,6 +260,7 @@ extension Process {
246260
}
247261
}
248262

263+
#if USE_PROCESS_SPAWNING_WORKAROUND && !os(Windows)
249264
func scan<S: Sequence, U>(_ seq: S, _ initial: U, _ combine: (U, S.Element) -> U) -> [U] {
250265
var result: [U] = []
251266
result.reserveCapacity(seq.underestimatedCount)
@@ -283,3 +298,4 @@ func withArrayOfCStrings<T>(
283298
}
284299
}
285300
}
301+
#endif

0 commit comments

Comments
 (0)