Skip to content

Commit 9a5ead0

Browse files
committed
Merge pull request #195 from aciidb0mb3r/patch-23
Improve external dependency discovery logic
2 parents e0b5aed + 96dff51 commit 9a5ead0

File tree

15 files changed

+48
-15
lines changed

15 files changed

+48
-15
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import PackageDescription
2+
3+
let package = Package(
4+
name: "Bar",
5+
dependencies: [
6+
.Package(url: "../Foo", majorVersion: 1)
7+
]
8+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <Foo/Foo.h>
2+
3+
void cool() {
4+
foo();
5+
}

Fixtures/DependencyResolution/External/CUsingCDep2/Bar/Sources/SeaLover/include/Sea.h

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module SeaLover {
2+
header "Sea/Sea.h"
3+
link "SeaLover"
4+
export *
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Foo
2+
3+
foo()

Fixtures/DependencyResolution/External/CUsingCDep2/Foo/Package.swift

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void foo() {
2+
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void foo();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Foo {
2+
header "Foo/Foo.h"
3+
link "Foo"
4+
export *
5+
}

Sources/Build/Command.compile(ClangModule).swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import POSIX
1515
//FIXME: Incremental builds
1616

1717
extension Command {
18-
static func compile(clangModule module: ClangModule, configuration conf: Configuration, prefix: String) -> (Command, Command) {
18+
static func compile(clangModule module: ClangModule, externalModules: Set<Module>, configuration conf: Configuration, prefix: String) -> (Command, Command) {
1919

2020
let wd = Path.join(prefix, "\(module.c99name).build")
2121
let mkdir = Command.createDirectory(wd)
@@ -36,14 +36,8 @@ extension Command {
3636
//transitive closure of the target being built allowing the use of `#include "..."`
3737
//add `-I` argument to the include directory of every target outside the package in the
3838
//transitive closure of the target being built allowing the use of `#include <...>`
39-
//FIXME: To detect external deps we're checking if their path's parent.parent directory
40-
//is `Packages` as external deps will get copied to `Packages` dir. There should be a
41-
//better way to do this.
42-
if dep.path.parentDirectory.parentDirectory.basename == "Packages" {
43-
includeFlag = "-I"
44-
} else {
45-
includeFlag = "-iquote"
46-
}
39+
40+
includeFlag = externalModules.contains(dep) ? "-I" : "-iquote"
4741
args += [includeFlag, dep.path]
4842
args += ["-l\(dep.c99name)"] //FIXME: giving path to other module's -fmodule-map-file is not linking that module
4943
}

Sources/Build/describe().swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Utility
1818
/**
1919
- Returns: path to generated YAML for consumption by the llbuild based swift-build-tool
2020
*/
21-
public func describe(prefix: String, _ conf: Configuration, _ modules: [Module], _ products: [Product], Xcc: [String], Xld: [String], Xswiftc: [String]) throws -> String {
21+
public func describe(prefix: String, _ conf: Configuration, _ modules: [Module], _ externalModules: Set<Module> , _ products: [Product], Xcc: [String], Xld: [String], Xswiftc: [String]) throws -> String {
2222

2323
guard modules.count > 0 else {
2424
throw Error.NoModules
@@ -50,7 +50,7 @@ public func describe(prefix: String, _ conf: Configuration, _ modules: [Module],
5050
}
5151
}
5252

53-
let (compile, mkdir) = Command.compile(clangModule: module, configuration: conf, prefix: prefix)
53+
let (compile, mkdir) = Command.compile(clangModule: module, externalModules: externalModules, configuration: conf, prefix: prefix)
5454
commands.append(compile)
5555
commands.append(mkdir)
5656
targets.main.cmds.append(compile)

Sources/swift-build/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ do {
6868
let dirs = try directories()
6969
let (rootPackage, externalPackages) = try fetch(dirs.root)
7070
let (modules, externalModules, products) = try transmute(rootPackage, externalPackages: externalPackages)
71-
let yaml = try describe(dirs.build, conf, modules, products, Xcc: opts.Xcc, Xld: opts.Xld, Xswiftc: opts.Xswiftc)
71+
let yaml = try describe(dirs.build, conf, modules, Set<Module>(externalModules), products, Xcc: opts.Xcc, Xld: opts.Xld, Xswiftc: opts.Xswiftc)
7272
try build(YAMLPath: yaml, target: "default")
7373

7474
case .Init(let initMode):

Sources/swift-build/xp.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import Build
1414
import POSIX
1515

1616
#if os(Linux)
17-
public func describe(prefix: String, _ conf: Configuration, _ modules: [Module], _ products: [Product], Xcc: [String], Xld: [String], Xswiftc: [String]) throws -> String {
17+
public func describe(prefix: String, _ conf: Configuration, _ modules: [Module], _ externalModules: Set<Module>, _ products: [Product], Xcc: [String], Xld: [String], Xswiftc: [String]) throws -> String {
1818
do {
19-
return try Build.describe(prefix, conf, modules, products, Xcc: Xcc, Xld: Xld, Xswiftc: Xswiftc)
19+
return try Build.describe(prefix, conf, modules, externalModules, products, Xcc: Xcc, Xld: Xld, Xswiftc: Xswiftc)
2020
} catch {
2121

2222
// it is a common error on Linux for clang++ to not be installed, but

Tests/Build/DescribeTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import XCTest
1414
final class DescribeTests: XCTestCase {
1515
func testDescribingNoModulesThrows() {
1616
do {
17-
let _ = try describe("foo", .Debug, [], [], Xcc: [], Xld: [], Xswiftc: [])
17+
let _ = try describe("foo", .Debug, [], [], [], Xcc: [], Xld: [], Xswiftc: [])
1818
XCTFail("This call should throw")
1919
} catch Build.Error.NoModules {
2020
XCTAssert(true, "This error should be throw")

Tests/Functional/TestClangModules.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ class TestClangModulesTestCase: XCTestCase {
7373
XCTAssertEqual(output, "hello 5")
7474
}
7575
}
76+
77+
func testCUsingCDep2() {
78+
//The C dependency "Foo" has different layout
79+
fixture(name: "DependencyResolution/External/CUsingCDep2") { prefix in
80+
XCTAssertBuilds(prefix, "Bar")
81+
XCTAssertFileExists(prefix, "Bar/.build/debug/libFoo.so")
82+
XCTAssertDirectoryExists(prefix, "Bar/Packages/Foo-1.2.3")
83+
}
84+
}
7685
}
7786

7887

0 commit comments

Comments
 (0)