Skip to content

Commit afa3141

Browse files
authored
Merge pull request #45 from gmittert/WhichWhichWhyWhoWhenWhat
Use where.exe on Windows instead of which
2 parents 1e3e71d + 14334fc commit afa3141

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Sources/ISDBTibs/TibsToolchain.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
#if os(Windows)
15+
import WinSDK
16+
#endif
1417

1518
/// The set of commandline tools used to build a tibs project.
1619
public final class TibsToolchain {
@@ -86,7 +89,11 @@ public final class TibsToolchain {
8689
let fsec = Float(usec) / 1_000_000
8790
fputs("warning: waiting \(fsec) second\(fsec == 1.0 ? "" : "s") to ensure file timestamp " +
8891
"differs; \(reason)\n", stderr)
92+
#if os(Windows)
93+
Sleep(usec / 1000)
94+
#else
8995
usleep(usec)
96+
#endif
9097
}
9198
}
9299
}
@@ -168,15 +175,23 @@ extension TibsToolchain {
168175
public func findTool(name: String) -> URL? {
169176
#if os(macOS)
170177
let cmd = ["/usr/bin/xcrun", "--find", name]
178+
#elseif os(Windows)
179+
var buf = [WCHAR](repeating: 0, count: Int(MAX_PATH))
180+
GetWindowsDirectoryW(&buf, DWORD(MAX_PATH))
181+
var wherePath = String(decodingCString: &buf, as: UTF16.self)
182+
.appendingPathComponent("system32")
183+
.appendingPathComponent("where.exe")
184+
let cmd = [wherePath, name]
171185
#else
172186
let cmd = ["/usr/bin/which", name]
173187
#endif
174188

175189
guard var path = try? Process.tibs_checkNonZeroExit(arguments: cmd) else {
176190
return nil
177191
}
178-
if path.last == "\n" {
179-
path = String(path.dropLast())
180-
}
192+
#if os(Windows)
193+
path = String((path.split { $0.isNewline })[0])
194+
#endif
195+
path = path.trimmingCharacters(in: .whitespacesAndNewlines)
181196
return URL(fileURLWithPath: path, isDirectory: false)
182197
}

0 commit comments

Comments
 (0)