Fix build for windows (#24)

This commit is contained in:
Kila2 2024-10-10 08:36:22 +08:00 committed by GitHub
parent 8f050f5938
commit e6f6aa818f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 13 deletions

View File

@ -16,7 +16,7 @@
import Foundation import Foundation
#if os(Linux) #if os(Linux) || os(Windows)
let NSEC_PER_SEC: UInt64 = 1_000_000_000 let NSEC_PER_SEC: UInt64 = 1_000_000_000
#endif #endif

View File

@ -17,7 +17,14 @@
import Foundation import Foundation
import MessagePack import MessagePack
import SemanticVersion import SemanticVersion
#if os(Windows)
import WinSDK.System
let ENV_SEPARATOR=";"
let PKL_EXEC_NAME="pkl.exe"
#else
let ENV_SEPARATOR=":"
let PKL_EXEC_NAME="pkl"
#endif
/// Perfoms `action`, returns its result and then closes the manager. /// Perfoms `action`, returns its result and then closes the manager.
/// ///
/// - Parameter action: The action to perform /// - Parameter action: The action to perform
@ -38,21 +45,32 @@ public func withEvaluatorManager<T>(_ action: (EvaluatorManager) async throws ->
} }
} }
func getenv(_ key: String) -> String? {
#if os(Windows)
let key = key.lowercased()
return ProcessInfo.processInfo.environment.first { (envKey: String, value: String) in
return key == envKey.lowercased()
}?.value
#else
return ProcessInfo.processInfo.environment[key]
#endif
}
/// Resolve the (CLI) command to invoke Pkl. /// Resolve the (CLI) command to invoke Pkl.
/// ///
/// First, checks the `PKL_EXEC` environment variable. If that is not set, searches the `PATH` for a directory /// First, checks the `PKL_EXEC` environment variable. If that is not set, searches the `PATH` for a directory
/// containing `pkl`. /// containing `pkl`.
func getPklCommand() throws -> [String] { func getPklCommand() throws -> [String] {
if let exec = ProcessInfo.processInfo.environment["PKL_EXEC"] { if let exec = getenv("PKL_EXEC") {
return exec.components(separatedBy: " ") return exec.components(separatedBy: " ")
} }
guard let path = ProcessInfo.processInfo.environment["PATH"] else { guard let path = getenv("PATH") else {
throw PklError("Unable to read PATH environment variable.") throw PklError("Unable to read PATH environment variable.")
} }
for dir in path.components(separatedBy: ":") { for dir in path.components(separatedBy: ENV_SEPARATOR) {
do { do {
let contents = try FileManager.default.contentsOfDirectory(atPath: dir) let contents = try FileManager.default.contentsOfDirectory(atPath: dir)
if let pkl = contents.first(where: { $0 == "pkl" }) { if let pkl = contents.first(where: { $0 == PKL_EXEC_NAME }) {
let file = NSString.path(withComponents: [dir, pkl]) let file = NSString.path(withComponents: [dir, pkl])
if FileManager.default.isExecutableFile(atPath: file) { if FileManager.default.isExecutableFile(atPath: file) {
return [file] return [file]

View File

@ -36,7 +36,7 @@ extension ModuleSource {
/// - Parameter path: The file path component. /// - Parameter path: The file path component.
public static func path(_ path: String) -> ModuleSource { public static func path(_ path: String) -> ModuleSource {
let path = resolvePaths(path) let path = resolvePaths(path)
return ModuleSource(uri: URL(string: "file://\(path)")!, text: nil) return ModuleSource(uri: URL(fileURLWithPath: path), text: nil)
} }
/// Creates a synthetic ``ModuleSource`` with the given text. /// Creates a synthetic ``ModuleSource`` with the given text.

View File

@ -15,11 +15,6 @@
// ===----------------------------------------------------------------------===// // ===----------------------------------------------------------------------===//
import Foundation import Foundation
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
let pklDebug = ProcessInfo.processInfo.environment["PKL_DEBUG"] let pklDebug = ProcessInfo.processInfo.environment["PKL_DEBUG"]
@ -80,7 +75,7 @@ public func mapEquals<K>(map1: [K: any DynamicallyEquatable], map2: [K: any Dyna
public func resolvePaths(_ paths: String...) -> String { public func resolvePaths(_ paths: String...) -> String {
var result = FileManager.default.currentDirectoryPath var result = FileManager.default.currentDirectoryPath
for path in paths { for path in paths {
if path.starts(with: "/") { if path.starts(with: "/") || URL(fileURLWithPath: path).path == path {
result = path result = path
} else { } else {
result = NSString.path(withComponents: [result, path]) result = NSString.path(withComponents: [result, path])