Skip to content

Commit b0057e3

Browse files
committed
Add comments for some of the methods
1 parent 6069196 commit b0057e3

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

Sources/Build/Buildable.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ protocol Buildable {
1717
}
1818

1919
extension CModule {
20-
func workingDirectory(prefix: String) -> String {
20+
///Returns the build directory path of a CModule
21+
func buildDirectory(prefix: String) -> String {
2122
return Path.join(prefix, "\(c99name).build")
2223
}
2324
}
@@ -27,24 +28,20 @@ extension Module: Buildable {
2728
return self is TestModule
2829
}
2930

30-
func XccFlagsForPrefix(prefix: String) -> [String] {
31+
func XccFlags(prefix: String) -> [String] {
3132
return recursiveDependencies.flatMap { module -> [String] in
3233
if let module = module as? ClangModule {
33-
var moduleMap: String? = nil
34-
34+
///For ClangModule we check if there is a user provided module map
35+
///otherwise we return with path of generated one.
36+
///We will fail before this is ever called if there is no module map.
37+
///FIXME: The user provided modulemap should be copied to build dir
38+
///but that requires copying the complete include dir because it'll
39+
///mostly likely contain relative paths.
3540
if module.moduleMapPath.isFile {
36-
moduleMap = module.moduleMapPath
41+
return ["-Xcc", "-fmodule-map-file=\(module.moduleMapPath)"]
3742
}
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 []
43+
let genModuleMap = Path.join(module.buildDirectory(prefix), module.moduleMap)
44+
return ["-Xcc", "-fmodule-map-file=\(genModuleMap)"]
4845
} else if let module = module as? CModule {
4946
return ["-Xcc", "-fmodule-map-file=\(module.moduleMapPath)"]
5047
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private extension Sources {
6969
extension Command {
7070
static func compile(clangModule module: ClangModule, externalModules: Set<Module>, configuration conf: Configuration, prefix: String, CC: String) throws -> ([Command], Command) {
7171

72-
let wd = module.workingDirectory(prefix)
72+
let wd = module.buildDirectory(prefix)
7373

7474
if module.type == .Library {
7575
try module.generateModuleMap(inDir: wd)

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.XccFlagsForPrefix(prefix)
17+
let otherArgs = otherArgs + module.XccFlags(prefix)
1818

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

Sources/Build/misc.swift

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ extension ClangModule {
5050

5151
public func generateModuleMap(inDir wd: String) throws {
5252

53-
//Return if module map is already present
53+
///Return if module map is already present
5454
guard !moduleMapPath.isFile else {
5555
return
5656
}
5757

5858
let includeDir = path
5959

60-
//Warn and return if no include directory
60+
///Warn and return if no include directory
6161
guard includeDir.isDirectory else {
6262
print("warning: No include directory found, a library can not be imported without any public headers.")
6363
return
@@ -68,17 +68,31 @@ extension ClangModule {
6868
let files = walked.filter{$0.isFile && $0.hasSuffix(".h")}
6969
let dirs = walked.filter{$0.isDirectory}
7070

71+
///There are three possible cases for which we will generate modulemaps:
72+
///Flat includes dir with only header files, in that case we generate
73+
/// `umbrella "path/to/includes/"`
74+
///Module name dir is the only dir in includes, in that case we generate
75+
/// `umbrella header "path/to/includes/modulename/modulename.h"` if there is a `modulename.h`
76+
///inside that directory, otherwise we generate
77+
/// `umbrella "path/to/includes/modulename"`
78+
7179
if dirs.isEmpty {
7280
guard !files.isEmpty else { throw ModuleMapError.UnsupportedIncludeLayoutForModule(name) }
7381
try createModuleMap(inDir: wd, type: .FlatHeaderLayout)
7482
return
7583
}
7684

77-
guard let moduleHeaderDir = dirs.first where moduleHeaderDir.basename == name && files.isEmpty else {
85+
guard let moduleHeaderDir = dirs.first where moduleHeaderDir.basename == c99name && files.isEmpty else {
7886
throw ModuleMapError.UnsupportedIncludeLayoutForModule(name)
7987
}
8088

81-
let umbrellaHeader = Path.join(moduleHeaderDir, "\(name).h")
89+
let umbrellaHeader = Path.join(moduleHeaderDir, "\(c99name).h")
90+
91+
let invalidUmbrellaHeader = Path.join(moduleHeaderDir, "\(name).h")
92+
if c99name != name && invalidUmbrellaHeader.isFile {
93+
print("warning: \(invalidUmbrellaHeader) should be renamed to \(umbrellaHeader) to be used as Umbrella header")
94+
}
95+
8296
if umbrellaHeader.isFile {
8397
try createModuleMap(inDir: wd, type: .HeaderFile)
8498
} else {
@@ -98,24 +112,22 @@ extension ClangModule {
98112
let moduleMap = try fopen(moduleMapFile, mode: .Write)
99113
defer { fclose(moduleMap) }
100114

101-
try fputs("module \(name) {\n", moduleMap)
102-
try fputs(" umbrella ", moduleMap)
103-
115+
var output = "module \(c99name) {\n"
104116
switch type {
105117
case .FlatHeaderLayout:
106-
try fputs("\"\(path)\"\n", moduleMap)
118+
output += " umbrella \"\(path)\"\n"
107119
case .ModuleNameDir:
108-
let path = Path.join(self.path, name)
109-
try fputs("\"\(path)\"\n", moduleMap)
120+
let path = Path.join(self.path, c99name)
121+
output += " umbrella \"\(path)\"\n"
110122
case .HeaderFile:
111-
let path = Path.join(self.path, name, "\(name).h")
112-
try fputs("header \"\(path)\"\n", moduleMap)
113-
123+
let path = Path.join(self.path, c99name, "\(c99name).h")
124+
output += " umbrella header \"\(path)\"\n"
114125
}
115-
116-
try fputs(" link \"\(name)\"\n", moduleMap)
117-
try fputs(" export *\n", moduleMap)
118-
try fputs("}\n", moduleMap)
126+
output += " link \"\(c99name)\"\n"
127+
output += " export *\n"
128+
output += "}\n"
129+
130+
try fputs(output, moduleMap)
119131
}
120132
}
121133

0 commit comments

Comments
 (0)