Skip to content

Handle Spaces in Makefile Parsing #49

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 1 commit into from
Aug 13, 2019
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
62 changes: 62 additions & 0 deletions Sources/ISDBTibs/Makefile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation

public struct Makefile {
public let outputs: [(target: Substring, deps: [Substring])]

public init?(contents: String) {
var makeOutputs: [(target: Substring, deps: [Substring])] = []

let lines = contents.split(whereSeparator: { $0.isNewline })
for line in lines {
var deps: [Substring] = []
let split = line.split(separator: ":", maxSplits: 1)

guard let output = split.first, let depStr = split.last else {
return nil
}

var prev: Character = "."
var it = depStr.startIndex
var strStart = it

while it != depStr.endIndex {
let curr = depStr[it]
if curr == " " && prev != "\\" {
let dep = depStr[strStart..<it]
if !dep.isEmpty {
deps.append(dep)
}
strStart = depStr.index(after: it)
}
prev = curr
it = depStr.index(after: it)
}

let dep = depStr[strStart..<it]
if !dep.isEmpty {
deps.append(dep)
}
makeOutputs.append((target: output, deps: deps))
}
outputs = makeOutputs
}

public init?(path: URL) {
guard let contents = try? String(contentsOf: path) else {
return nil
}
self.init(contents: contents)
}

}
22 changes: 5 additions & 17 deletions Sources/tibs/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,15 @@ var stderr = FileHandle.standardError

func swiftDepsMerge(output: String, _ files: [String]) {
var allDeps: Set<Substring> = []

for file in files {
let content: String
do {
content = try String(contentsOf: URL(fileURLWithPath: file))
} catch {
print("error: could not read dep file '\(file)': \(error)", to: &stderr)
guard let makefile = Makefile(path: URL(fileURLWithPath: file)) else {
print("error: could not read dep file '\(file)'", to: &stderr)
exit(1)
}

let lines = content.split(whereSeparator: { $0.isNewline })
for line in lines {
guard let depStr = line.split(separator: ":", maxSplits: 1).last else {
print("error: malformed dep file '\(file)': expected :", to: &stderr)
exit(1)
}

let deps = depStr.split(whereSeparator: { $0.isWhitespace })
for dep in deps {
allDeps.insert(dep)
}
}
let allOutputs = makefile.outputs.flatMap { $0.deps }
allDeps.formUnion(allOutputs)
}

print("\(output) : \(allDeps.sorted().joined(separator: " "))")
Expand Down
42 changes: 42 additions & 0 deletions Tests/ISDBTibsTests/MiscTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,46 @@ final class MiscTests: XCTestCase {
XCTAssertFalse(try data2.writeIfChanged(to: url))
try FileManager.default.removeItem(at: url)
}

func testMakefile() throws {
typealias Outputs = [(target: Substring, deps: [Substring])]
func outputsEqual(actual: Outputs, expected: Outputs) -> Bool {
return actual.count == expected.count && zip(actual, expected).allSatisfy(==)
}

let makefiles: [String: Outputs] = [
/*simple*/
"target: dep1 dep2 dep3": [
(target: "target", deps: ["dep1", "dep2", "dep3"])
],
/*newlines*/
"target1: dep1 \ntarget2: dep1 dep2 dep3": [
(target: "target1", deps: ["dep1"]),
(target: "target2", deps: ["dep1", "dep2", "dep3"])
],
/*nodeps*/
"target: ": [
(target: "target", deps: [])
],
/*spaces*/
"target: Dep\\ with\\ spaces" : [
(target: "target", deps: ["Dep\\ with\\ spaces"])
],
"target\\ with\\ spaces: Dep" : [
(target: "target\\ with\\ spaces", deps: ["Dep"])
],
/*paths*/
"target: Dep/with\\slashes" : [
(target: "target", deps: ["Dep/with\\slashes"])
],
]

for (makefile, exp) in makefiles {
guard let parsed = Makefile(contents: makefile) else {
XCTFail("Could not parse: \(makefile)")
return
}
XCTAssertTrue(outputsEqual(actual: parsed.outputs, expected: exp), "Makefile parse did not match!\nExpected: \(exp)\nAcutal: \(parsed.outputs)")
}
}
}
1 change: 1 addition & 0 deletions Tests/ISDBTibsTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extension MiscTests {
// to regenerate.
static let __allTests__MiscTests = [
("testDataWriteIfChanged", testDataWriteIfChanged),
("testMakefile", testMakefile),
]
}

Expand Down