Skip to content

Commit 060204b

Browse files
committed
Generate module maps in build dir
1 parent 26b56cf commit 060204b

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

Sources/Build/Buildable.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,43 @@
99
*/
1010

1111
import PackageType
12+
import struct Utility.Path
1213

1314
protocol Buildable {
1415
var targetName: String { get }
1516
var isTest: Bool { get }
1617
}
1718

19+
extension CModule {
20+
func workingDirectory(prefix: String) -> String {
21+
return Path.join(prefix, "\(c99name).build")
22+
}
23+
}
24+
1825
extension Module: Buildable {
1926
var isTest: Bool {
2027
return self is TestModule
2128
}
2229

23-
var Xcc: [String] {
30+
func XccFlagsForPrefix(prefix: String) -> [String] {
2431
return recursiveDependencies.flatMap { module -> [String] in
25-
if let module = module as? CModule {
32+
if let module = module as? ClangModule {
33+
var moduleMap: String? = nil
34+
35+
if module.moduleMapPath.isFile {
36+
moduleMap = module.moduleMapPath
37+
}
38+
39+
let genModuleMap = Path.join(module.workingDirectory(prefix), module.moduleMap)
40+
if genModuleMap.isFile {
41+
moduleMap = genModuleMap
42+
}
43+
//No module map found, return with no args
44+
if let moduleMap = moduleMap {
45+
return ["-Xcc", "-fmodule-map-file=\(moduleMap)"]
46+
}
47+
return []
48+
} else if let module = module as? CModule {
2649
return ["-Xcc", "-fmodule-map-file=\(module.moduleMapPath)"]
2750
} else {
2851
return []

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ private extension Sources {
6767
}
6868

6969
extension Command {
70-
static func compile(clangModule module: ClangModule, externalModules: Set<Module>, configuration conf: Configuration, prefix: String, CC: String) -> ([Command], Command) {
70+
static func compile(clangModule module: ClangModule, externalModules: Set<Module>, configuration conf: Configuration, prefix: String, CC: String) throws -> ([Command], Command) {
7171

72-
let wd = Path.join(prefix, "\(module.c99name).build")
72+
let wd = module.workingDirectory(prefix)
73+
74+
if module.type == .Library {
75+
try module.generateModuleMap(inDir: wd)
76+
}
77+
7378
let mkdir = Command.createDirectory(wd)
7479

7580
///------------------------------ Compile -----------------------------------------

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Utility
1414
extension Command {
1515
static func compile(swiftModule module: SwiftModule, configuration conf: Configuration, prefix: String, otherArgs: [String], SWIFT_EXEC: String) throws -> (Command, [Command]) {
1616

17-
let otherArgs = otherArgs + module.Xcc
17+
let otherArgs = otherArgs + module.XccFlagsForPrefix(prefix)
1818

1919
func cmd(tool: ToolProtocol) -> Command {
2020
return Command(node: module.targetName, tool: tool)

Sources/Build/describe().swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ public func describe(prefix: String, _ conf: Configuration, _ modules: [Module],
4343
targets.append(compile, for: module)
4444

4545
case let module as ClangModule:
46-
if module.type == .Library {
47-
try module.generateModuleMap()
48-
}
49-
50-
let (compile, mkdir) = Command.compile(clangModule: module, externalModules: externalModules, configuration: conf, prefix: prefix, CC: CC)
46+
let (compile, mkdir) = try Command.compile(clangModule: module, externalModules: externalModules, configuration: conf, prefix: prefix, CC: CC)
5147
commands += compile
5248
commands.append(mkdir)
5349
targets.main.cmds += compile

Sources/Build/misc.swift

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ func platformFrameworksPath() throws -> String {
3232
}
3333

3434
extension CModule {
35+
36+
var moduleMap: String {
37+
return "module.modulemap"
38+
}
39+
3540
var moduleMapPath: String {
36-
return Path.join(path, "module.modulemap")
41+
return Path.join(path, moduleMap)
3742
}
3843
}
3944

@@ -43,7 +48,7 @@ extension ClangModule {
4348
case UnsupportedIncludeLayoutForModule(String)
4449
}
4550

46-
public func generateModuleMap() throws {
51+
public func generateModuleMap(inDir wd: String) throws {
4752

4853
//Return if module map is already present
4954
guard !moduleMapPath.isFile else {
@@ -52,13 +57,9 @@ extension ClangModule {
5257

5358
let includeDir = path
5459

55-
//Generate empty module map if include dir is not present
60+
//Warn and return if no include directory
5661
guard includeDir.isDirectory else {
57-
print("warning: No include directory, generating empty module map")
58-
try POSIX.mkdir(includeDir)
59-
try fopen(moduleMapPath, mode: .Write) { fp in
60-
try fputs("\n", fp)
61-
}
62+
print("warning: No include directory found, a library can not be imported without any public headers.")
6263
return
6364
}
6465

@@ -69,7 +70,7 @@ extension ClangModule {
6970

7071
if dirs.isEmpty {
7172
guard !files.isEmpty else { throw ModuleMapError.UnsupportedIncludeLayoutForModule(name) }
72-
try createModuleMap(.FlatHeaderLayout)
73+
try createModuleMap(inDir: wd, type: .FlatHeaderLayout)
7374
return
7475
}
7576

@@ -79,9 +80,9 @@ extension ClangModule {
7980

8081
let umbrellaHeader = Path.join(moduleHeaderDir, "\(name).h")
8182
if umbrellaHeader.isFile {
82-
try createModuleMap(.HeaderFile)
83+
try createModuleMap(inDir: wd, type: .HeaderFile)
8384
} else {
84-
try createModuleMap(.ModuleNameDir)
85+
try createModuleMap(inDir: wd, type: .ModuleNameDir)
8586
}
8687
}
8788

@@ -91,20 +92,24 @@ extension ClangModule {
9192
case HeaderFile
9293
}
9394

94-
private func createModuleMap(type: UmbrellaType) throws {
95-
let moduleMap = try fopen(moduleMapPath, mode: .Write)
95+
private func createModuleMap(inDir wd: String, type: UmbrellaType) throws {
96+
try POSIX.mkdir(wd)
97+
let moduleMapFile = Path.join(wd, self.moduleMap)
98+
let moduleMap = try fopen(moduleMapFile, mode: .Write)
9699
defer { fclose(moduleMap) }
97100

98101
try fputs("module \(name) {\n", moduleMap)
99102
try fputs(" umbrella ", moduleMap)
100103

101104
switch type {
102105
case .FlatHeaderLayout:
103-
try fputs("\".\"\n", moduleMap)
106+
try fputs("\"\(path)\"\n", moduleMap)
104107
case .ModuleNameDir:
105-
try fputs("\"\(name)\"\n", moduleMap)
108+
let path = Path.join(self.path, name)
109+
try fputs("\"\(path)\"\n", moduleMap)
106110
case .HeaderFile:
107-
try fputs("header \"\(name)/\(name).h\"\n", moduleMap)
111+
let path = Path.join(self.path, name, "\(name).h")
112+
try fputs("header \"\(path)\"\n", moduleMap)
108113

109114
}
110115

0 commit comments

Comments
 (0)