forked from Longhanks/qlift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPackage.swift
71 lines (61 loc) · 2.31 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// swift-tools-version:5.6
import PackageDescription
var swiftSettings: [SwiftSetting]? = nil
if let buildFlag = try? getQTBuildFlag() {
swiftSettings = [.define(buildFlag)]
}
let package = Package(
name: "Qlift",
products: [
.library(name: "Qlift", targets: ["Qlift"]),
],
targets: [
.target(name: "Qlift",
dependencies: ["CQlift"],
swiftSettings: swiftSettings),
.target(name: "CQlift",
dependencies: ["CQt5Widgets", "CQt5Quick"],
cxxSettings: [.headerSearchPath("private")]),
.systemLibrary(name: "CQt5Widgets", pkgConfig: "Qt5Widgets"),
.systemLibrary(name: "CQt5Quick", pkgConfig: "Qt5Quick"),
],
cxxLanguageStandard: .cxx17
)
import Foundation
private func getURL(for app: String) -> URL? {
let pathList = ProcessInfo.processInfo.environment["PATH"] ?? "/usr/bin"
for path in pathList.split(separator: ":") {
let url = URL(fileURLWithPath: String(path)).appendingPathComponent(app)
if let res = try? url.resourceValues(forKeys: [.isExecutableKey]),
res.isExecutable ?? false
{
return url
}
}
return nil
}
private func runCmd(url: URL, command: String) throws -> (exitCode: Int32, output: String) {
let process = Process()
process.executableURL = url
let stdinPipe = Pipe()
let stdErrPipe = Pipe()
process.standardOutput = stdinPipe
process.standardError = stdErrPipe
process.arguments = command.split(separator: " ").map { String($0) }
try process.run()
process.waitUntilExit()
let status = process.terminationStatus
let data = try stdinPipe.fileHandleForReading.readToEnd() ?? Data()
let output = (String(data: data, encoding: .utf8) ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
return (exitCode: status, output: output)
}
private func getQTBuildFlag() throws -> String? {
guard let url = getURL(for: "pkg-config") else { return "QT5_15" }
let (exitCode, version) = try runCmd(url: url, command: "--modversion Qt5Widgets")
guard exitCode == 0, !version.isEmpty else { return "QT5_15" }
let compare = version.compare("5.15.0", options: .numeric)
if compare == .orderedSame || compare == .orderedDescending {
return "QT5_15"
}
return nil
}