Skip to content

Commit 2d8ded1

Browse files
committed
Merge pull request #280 from aciidb0mb3r/pkg_updates
2 parents 22954af + 94ff175 commit 2d8ded1

File tree

7 files changed

+74
-18
lines changed

7 files changed

+74
-18
lines changed

Sources/Build/PkgConfig.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ struct PkgConfigParser {
9595

9696
let file = File(path: self.pcFile)
9797
for line in try file.enumerate() {
98-
// Ignore any commented line.
99-
if line.hasPrefix("#") || line.isEmpty { continue }
100-
// Remove any trailing comment from the line.
101-
let line = removeComment(line: line)
98+
// Remove commented or any trailing comment from the line.
99+
let uncommentedLine = removeComment(line: line)
100+
// Ignore any empty or whitespace line.
101+
guard let line = uncommentedLine.chuzzle() else { continue }
102102

103-
if let colonIndex = line.characters.index(of: ":") where line[colonIndex.successor()] == " " {
103+
if let colonIndex = line.characters.index(of: ":") where
104+
line.endIndex == colonIndex.successor() || line[colonIndex.successor()] == " " {
104105
// Found a key-value pair.
105106
try parseKeyValue(line: line)
106107
} else if let equalsIndex = line.characters.index(of: "=") {
@@ -117,11 +118,11 @@ struct PkgConfigParser {
117118

118119
private mutating func parseKeyValue(line: String) throws {
119120
if line.hasPrefix("Requires: ") {
120-
dependencies = try parseDependencies(value(line: line))
121+
dependencies = try parseDependencies(resolveVariables(value(line: line)))
121122
} else if line.hasPrefix("Libs: ") {
122-
libs = try resolveVariables(value(line: line)).chomp()
123+
libs = try resolveVariables(value(line: line))
123124
} else if line.hasPrefix("Cflags: ") {
124-
cFlags = try resolveVariables(value(line: line)).chomp()
125+
cFlags = try resolveVariables(value(line: line))
125126
}
126127
}
127128

Sources/Build/misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ extension SystemPackageProvider {
189189
}
190190

191191
var isAvailable: Bool {
192-
guard let platform = Platform.currentPlatform() else { return false }
192+
guard let platform = Platform.currentPlatform else { return false }
193193
switch self {
194194
case .Brew(_):
195195
if case .Darwin = platform {

Sources/Utility/Platform.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public enum Platform {
1818
case Debian
1919
}
2020

21-
public static func currentPlatform() -> Platform? {
21+
// Lazily return current platform.
22+
public static var currentPlatform = Platform.findCurrentPlatform()
23+
private static func findCurrentPlatform() -> Platform? {
2224
guard let uname = try? popen(["uname"]).chomp().lowercased() else { return nil }
2325
switch uname {
2426
case "darwin":

Tests/Build/PkgConfigParserTests.swift

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,51 @@ final class PkgConfigParserTests: XCTestCase {
1616

1717
func testGTK3PCFile() {
1818
loadPCFile("gtk+-3.0.pc") { parser in
19+
guard let parser = parser else { XCTFail("Unexpected parsing error"); return}
1920
XCTAssertEqual(parser.variables, ["libdir": "/usr/local/Cellar/gtk+3/3.18.9/lib", "gtk_host": "x86_64-apple-darwin15.3.0", "includedir": "/usr/local/Cellar/gtk+3/3.18.9/include", "prefix": "/usr/local/Cellar/gtk+3/3.18.9", "gtk_binary_version": "3.0.0", "exec_prefix": "/usr/local/Cellar/gtk+3/3.18.9", "targets": "quartz"])
2021
XCTAssertEqual(parser.dependencies, ["gdk-3.0", "atk", "cairo", "cairo-gobject", "gdk-pixbuf-2.0", "gio-2.0"])
21-
XCTAssertEqual(parser.cFlags, "-I/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0 ")
22-
XCTAssertEqual(parser.libs, "-L/usr/local/Cellar/gtk+3/3.18.9/lib -lgtk-3 ")
22+
XCTAssertEqual(parser.cFlags, "-I/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0")
23+
XCTAssertEqual(parser.libs, "-L/usr/local/Cellar/gtk+3/3.18.9/lib -lgtk-3")
2324
}
2425
}
2526

26-
private func loadPCFile(_ inputName: String, line: UInt = #line, body: (PkgConfigParser) -> Void) {
27+
func testEmptyCFlags() {
28+
loadPCFile("empty_cflags.pc") { parser in
29+
guard let parser = parser else { XCTFail("Unexpected parsing error"); return}
30+
XCTAssertEqual(parser.variables, ["prefix": "/usr/local/bin", "exec_prefix": "/usr/local/bin"])
31+
XCTAssertEqual(parser.dependencies, ["gdk-3.0", "atk"])
32+
XCTAssertEqual(parser.cFlags, "")
33+
XCTAssertEqual(parser.libs, "-L/usr/local/bin -lgtk-3")
34+
}
35+
}
36+
37+
func testVariableinDependency() {
38+
loadPCFile("deps_variable.pc") { parser in
39+
guard let parser = parser else { XCTFail("Unexpected parsing error"); return}
40+
XCTAssertEqual(parser.variables, ["prefix": "/usr/local/bin", "exec_prefix": "/usr/local/bin", "my_dep": "atk"])
41+
XCTAssertEqual(parser.dependencies, ["gdk-3.0", "atk"])
42+
XCTAssertEqual(parser.cFlags, "-I")
43+
XCTAssertEqual(parser.libs, "-L/usr/local/bin -lgtk-3")
44+
}
45+
}
46+
47+
func testUnresolvablePCFile() {
48+
loadPCFile("failure_case.pc") { parser in
49+
if parser != nil {
50+
XCTFail("parsing should have failed: \(parser)")
51+
}
52+
}
53+
}
54+
55+
private func loadPCFile(_ inputName: String, body: (PkgConfigParser?) -> Void) {
56+
let input = Path.join(#file, "../pkgconfigInputs", inputName).normpath
57+
var parser: PkgConfigParser? = PkgConfigParser(pcFile: input)
2758
do {
28-
let input = Path.join(#file, "../pkgconfigInputs", inputName).normpath
29-
var parser = PkgConfigParser(pcFile: input)
30-
try parser.parse()
31-
body(parser)
59+
try parser?.parse()
3260
} catch {
33-
XCTFail("Unexpected error: \(error)", file: #file, line: line)
61+
parser = nil
3462
}
63+
body(parser)
3564
}
3665
}
3766

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
prefix=/usr/local/bin
2+
exec_prefix=${prefix}
3+
my_dep=atk
4+
#some comment
5+
6+
Requires: gdk-3.0 >= 1.0.0 ${my_dep}
7+
Libs: -L${prefix} -lgtk-3
8+
Cflags: -I
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
prefix=/usr/local/bin
2+
exec_prefix=${prefix}
3+
4+
#some comment
5+
6+
Requires: gdk-3.0 atk
7+
Libs: -L${prefix} -lgtk-3
8+
Cflags:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
prefix=/usr/local/bin
2+
exec_prefix=${prefix}
3+
4+
#some comment
5+
6+
Requires: gdk-3.0 >= 1.0.0
7+
Libs: -L${prefix} -lgtk-3 ${my_dep}
8+
Cflags: -I

0 commit comments

Comments
 (0)