-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage.swift
51 lines (44 loc) · 1.51 KB
/
Package.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// swift-tools-version:4.0
import PackageDescription
#if os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || CYGWIN
import Glibc
#if os(Linux)
var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> { return __environ }
#endif
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
@_silgen_name("_NSGetEnviron")
func _NSGetEnviron() -> UnsafeMutablePointer<UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>>
var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> { return _NSGetEnviron().pointee }
#endif
let packageDir: String = {
let me = CommandLine.arguments[0]
var parts = me.split(separator: "/", omittingEmptySubsequences: false).map(String.init)
parts[parts.endIndex - 1] = ""
return parts.joined(separator: "/")
}()
let package = Package(
name: "CPerl",
pkgConfig: "perl"
)
func env(_ name: String) -> String? {
guard let value = getenv(name) else { return nil }
return String(cString: value)
}
let tmpdir = env("TMPDIR") ?? env("TEMP") ?? env("TMP") ?? "/tmp/"
if packageDir[packageDir.startIndex..<min(packageDir.endIndex, tmpdir.endIndex)] != tmpdir {
let command = packageDir + "prepare"
var pid = pid_t()
command.withCString {
guard posix_spawn(&pid, command, nil, nil, [UnsafeMutablePointer(mutating: $0), nil], environ) == 0 else {
fatalError("Failed to spawn \(command)")
}
}
var status: Int32 = 0
guard waitpid(pid, &status, 0) != -1 else {
fatalError("Failed to waitpid")
}
guard status == 0 else {
fatalError("\(command) terminated with status \(status)")
}
}