Skip to content

Updates and fixes to pkg-config #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Sources/Build/PkgConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ struct PkgConfigParser {

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

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

private mutating func parseKeyValue(line: String) throws {
if line.hasPrefix("Requires: ") {
dependencies = try parseDependencies(value(line: line))
dependencies = try parseDependencies(resolveVariables(value(line: line)))
} else if line.hasPrefix("Libs: ") {
libs = try resolveVariables(value(line: line)).chomp()
libs = try resolveVariables(value(line: line))
} else if line.hasPrefix("Cflags: ") {
cFlags = try resolveVariables(value(line: line)).chomp()
cFlags = try resolveVariables(value(line: line))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Build/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ extension SystemPackageProvider {
}

var isAvailable: Bool {
guard let platform = Platform.currentPlatform() else { return false }
guard let platform = Platform.currentPlatform else { return false }
switch self {
case .Brew(_):
if case .Darwin = platform {
Expand Down
4 changes: 3 additions & 1 deletion Sources/Utility/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public enum Platform {
case Debian
}

public static func currentPlatform() -> Platform? {
// Lazily return current platform.
public static var currentPlatform = Platform.findCurrentPlatform()
private static func findCurrentPlatform() -> Platform? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use something like:

public static var currentPlatform = {
}()

which is a standard trick I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that I think got some error...I'll retry.

guard let uname = try? popen(["uname"]).chomp().lowercased() else { return nil }
switch uname {
case "darwin":
Expand Down
45 changes: 37 additions & 8 deletions Tests/Build/PkgConfigParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,51 @@ final class PkgConfigParserTests: XCTestCase {

func testGTK3PCFile() {
loadPCFile("gtk+-3.0.pc") { parser in
guard let parser = parser else { XCTFail("Unexpected parsing error"); return}
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"])
XCTAssertEqual(parser.dependencies, ["gdk-3.0", "atk", "cairo", "cairo-gobject", "gdk-pixbuf-2.0", "gio-2.0"])
XCTAssertEqual(parser.cFlags, "-I/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0 ")
XCTAssertEqual(parser.libs, "-L/usr/local/Cellar/gtk+3/3.18.9/lib -lgtk-3 ")
XCTAssertEqual(parser.cFlags, "-I/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0")
XCTAssertEqual(parser.libs, "-L/usr/local/Cellar/gtk+3/3.18.9/lib -lgtk-3")
}
}

private func loadPCFile(_ inputName: String, line: UInt = #line, body: (PkgConfigParser) -> Void) {
func testEmptyCFlags() {
loadPCFile("empty_cflags.pc") { parser in
guard let parser = parser else { XCTFail("Unexpected parsing error"); return}
XCTAssertEqual(parser.variables, ["prefix": "/usr/local/bin", "exec_prefix": "/usr/local/bin"])
XCTAssertEqual(parser.dependencies, ["gdk-3.0", "atk"])
XCTAssertEqual(parser.cFlags, "")
XCTAssertEqual(parser.libs, "-L/usr/local/bin -lgtk-3")
}
}

func testVariableinDependency() {
loadPCFile("deps_variable.pc") { parser in
guard let parser = parser else { XCTFail("Unexpected parsing error"); return}
XCTAssertEqual(parser.variables, ["prefix": "/usr/local/bin", "exec_prefix": "/usr/local/bin", "my_dep": "atk"])
XCTAssertEqual(parser.dependencies, ["gdk-3.0", "atk"])
XCTAssertEqual(parser.cFlags, "-I")
XCTAssertEqual(parser.libs, "-L/usr/local/bin -lgtk-3")
}
}

func testUnresolvablePCFile() {
loadPCFile("failure_case.pc") { parser in
if parser != nil {
XCTFail("parsing should have failed: \(parser)")
}
}
}

private func loadPCFile(_ inputName: String, body: (PkgConfigParser?) -> Void) {
let input = Path.join(#file, "../pkgconfigInputs", inputName).normpath
var parser: PkgConfigParser? = PkgConfigParser(pcFile: input)
do {
let input = Path.join(#file, "../pkgconfigInputs", inputName).normpath
var parser = PkgConfigParser(pcFile: input)
try parser.parse()
body(parser)
try parser?.parse()
} catch {
XCTFail("Unexpected error: \(error)", file: #file, line: line)
parser = nil
}
body(parser)
}
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/Build/pkgconfigInputs/deps_variable.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
prefix=/usr/local/bin
exec_prefix=${prefix}
my_dep=atk
#some comment

Requires: gdk-3.0 >= 1.0.0 ${my_dep}
Libs: -L${prefix} -lgtk-3
Cflags: -I
8 changes: 8 additions & 0 deletions Tests/Build/pkgconfigInputs/empty_cflags.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
prefix=/usr/local/bin
exec_prefix=${prefix}

#some comment

Requires: gdk-3.0 atk
Libs: -L${prefix} -lgtk-3
Cflags:
8 changes: 8 additions & 0 deletions Tests/Build/pkgconfigInputs/failure_case.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
prefix=/usr/local/bin
exec_prefix=${prefix}

#some comment

Requires: gdk-3.0 >= 1.0.0
Libs: -L${prefix} -lgtk-3 ${my_dep}
Cflags: -I