Skip to content

Commit a46c362

Browse files
goloveychukmxcl
authored andcommitted
[xcodeproj] generate C targets
Adds `CModules` to generated Xcode projects;
1 parent e93a62d commit a46c362

File tree

7 files changed

+49
-21
lines changed

7 files changed

+49
-21
lines changed

Sources/PackageType/Module.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
TODO should be a protocol
1616
*/
1717

18-
public class Module {
18+
public protocol ModuleProtocol {
19+
var name: String { get }
20+
var c99name: String { get }
21+
var dependencies: [Module] { get set }
22+
var recursiveDependencies: [Module] { get }
23+
}
24+
25+
public class Module: ModuleProtocol {
1926
/**
2027
This name is not the final name in many cases, instead
2128
use c99name if you need uniqueness.
@@ -56,6 +63,11 @@ extension ModuleTypeProtocol {
5663
}
5764
}
5865

66+
67+
public protocol XcodeModuleProtocol: ModuleProtocol, ModuleTypeProtocol {
68+
var fileType: String { get }
69+
}
70+
5971
extension Module: Hashable, Equatable {
6072
public var hashValue: Int { return c99name.hashValue }
6173
}
@@ -79,6 +91,12 @@ extension SwiftModule: ModuleTypeProtocol {
7991
}
8092
}
8193

94+
extension SwiftModule: XcodeModuleProtocol {
95+
public var fileType: String {
96+
return "sourcecode.swift"
97+
}
98+
}
99+
82100
public class CModule: Module {
83101
public let path: String
84102

@@ -97,6 +115,12 @@ public class ClangModule: CModule {
97115
}
98116
}
99117

118+
extension ClangModule: XcodeModuleProtocol {
119+
public var fileType: String {
120+
return "sourcecode.c.c"
121+
}
122+
}
123+
100124
extension ClangModule: ModuleTypeProtocol {
101125
public var mainFile: String {
102126
return "main.c"

Sources/Xcodeproj/Module+PBXProj.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let linkPhaseFileRefPrefix = "_LinkFileRef_"
4040
let sourceGroupFileRefPrefix = "__PBXFileRef_"
4141
let compilePhaseFileRefPrefix = "__src_cc_ref_"
4242

43-
extension Module {
43+
extension XcodeModuleProtocol {
4444
var dependencyReference: String { return "__Dependency_\(c99name)" }
4545
var productReference: String { return "_____Product_\(c99name)" }
4646
var targetReference: String { return "______Target_\(c99name)" }
@@ -52,7 +52,7 @@ extension Module {
5252
var linkPhaseReference: String { return "___LinkPhase_\(c99name)" }
5353
}
5454

55-
func fileRef(forLinkPhaseChild module: Module) -> String {
55+
func fileRef(forLinkPhaseChild module: XcodeModuleProtocol) -> String {
5656
return linkPhaseFileRefPrefix + module.c99name
5757
}
5858

@@ -75,28 +75,29 @@ func fileRef(inProjectRoot name: String, srcroot: String) -> (String, String, St
7575
return ("'\(sourceGroupFileRefPrefix)\(suffix)'", name, Path.join(srcroot, name))
7676
}
7777

78-
func fileRefs(forModuleSources module: SwiftModule, srcroot: String) -> [(String, String)] {
78+
func fileRefs(forModuleSources module: XcodeModuleProtocol, srcroot: String) -> [(String, String)] {
7979
return module.sources.relativePaths.map { relativePath in
8080
let path = Path.join(module.sources.root, relativePath)
8181
let suffix = fileRef(suffixForModuleSourceFile: path, srcroot: srcroot)
8282
return ("'\(sourceGroupFileRefPrefix)\(suffix)'", relativePath)
8383
}
8484
}
8585

86-
func fileRefs(forCompilePhaseSourcesInModule module: SwiftModule, srcroot: String) -> [(String, String)] {
86+
func fileRefs(forCompilePhaseSourcesInModule module: XcodeModuleProtocol, srcroot: String) -> [(String, String)] {
8787
return fileRefs(forModuleSources: module, srcroot: srcroot).map { ref1, relativePath in
8888
let path = Path.join(module.sources.root, relativePath)
8989
let suffix = fileRef(suffixForModuleSourceFile: path, srcroot: srcroot)
9090
return (ref1, "'\(compilePhaseFileRefPrefix)\(suffix)'")
9191
}
9292
}
9393

94-
extension SwiftModule {
94+
extension XcodeModuleProtocol {
95+
9596
private var isLibrary: Bool {
9697
return type == .Library
9798
}
9899

99-
var type: String {
100+
var productType: String {
100101
if self is TestModule {
101102
return "com.apple.product-type.bundle.unit-test"
102103
} else if isLibrary {
@@ -119,6 +120,8 @@ extension SwiftModule {
119120
return "compiled.mach-o.\(suffix())"
120121
}
121122

123+
124+
122125
var productPath: String {
123126
if self is TestModule {
124127
return "\(c99name).xctest"
@@ -130,11 +133,11 @@ extension SwiftModule {
130133
}
131134

132135
var linkPhaseFileRefs: String {
133-
return recursiveDependencies.map{ fileRef(forLinkPhaseChild: $0) }.joined(separator: ", ")
136+
return recursiveDependencies.flatMap { $0 as? XcodeModuleProtocol }.map{ fileRef(forLinkPhaseChild: $0) }.joined(separator: ", ")
134137
}
135138

136139
var nativeTargetDependencies: String {
137-
return dependencies.map{ $0.dependencyReference }.joined(separator: ", ")
140+
return dependencies.flatMap { $0 as? XcodeModuleProtocol }.map{ $0.dependencyReference }.joined(separator: ", ")
138141
}
139142

140143
var productName: String {
@@ -211,7 +214,7 @@ extension SwiftModule {
211214
}
212215

213216

214-
extension SwiftModule {
217+
extension XcodeModuleProtocol {
215218
var blueprintIdentifier: String {
216219
return targetReference
217220
}

Sources/Xcodeproj/generate().swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import POSIX
1616
Generates an xcodeproj at the specified path.
1717
- Returns: the path to the generated project
1818
*/
19-
public func generate(dstdir dstdir: String, projectName: String, srcroot: String, modules: [SwiftModule], externalModules: [SwiftModule], products: [Product]) throws -> String {
19+
public func generate(dstdir dstdir: String, projectName: String, srcroot: String, modules: [XcodeModuleProtocol], externalModules: [XcodeModuleProtocol], products: [Product]) throws -> String {
2020

2121
let xcodeprojName = "\(projectName).xcodeproj"
2222
let xcodeprojPath = try mkdir(dstdir, xcodeprojName)

Sources/Xcodeproj/pbxproj().swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
import PackageType
1616
import Utility
1717

18-
public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [SwiftModule], externalModules: [SwiftModule], products _: [Product], printer print: (String) -> Void) {
18+
public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [XcodeModuleProtocol], externalModules: [XcodeModuleProtocol], products _: [Product], printer print: (String) -> Void) {
1919

20-
let rootModulesSet = Set(modules).subtract(Set(externalModules))
20+
// let rootModulesSet = Set(modules).subtract(Set(externalModules))
21+
let rootModulesSet = modules
2122
let nonTestRootModules = rootModulesSet.filter{ !($0 is TestModule) }
2223
let (tests, nonTests) = modules.partition{ $0 is TestModule }
2324

@@ -79,7 +80,7 @@ public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [Swif
7980
for (ref, path) in fileRefs(forModuleSources: module, srcroot: srcroot) {
8081
print(" \(ref) = {")
8182
print(" isa = PBXFileReference;")
82-
print(" lastKnownFileType = sourcecode.swift;")
83+
print(" lastKnownFileType = \(module.fileType);")
8384
print(" name = '\(Path(path).relative(to: module.sources.root))';")
8485
print(" sourceTree = '<group>';")
8586
print(" };")
@@ -95,7 +96,7 @@ public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [Swif
9596
print(" name = \(module.name);")
9697
print(" productName = \(module.c99name);")
9798
print(" productReference = \(module.productReference);")
98-
print(" productType = '\(module.type)';")
99+
print(" productType = '\(module.productType)';")
99100
print(" };")
100101

101102
// the product file reference

Sources/Xcodeproj/xcscheme().swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import PackageType
1212

13-
func xcscheme(container container: String, modules: [SwiftModule], printer print: (String) -> Void) {
13+
func xcscheme(container container: String, modules: [XcodeModuleProtocol], printer print: (String) -> Void) {
1414
print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
1515
print("<Scheme LastUpgradeVersion = \"9999\" version = \"1.3\">")
1616
print(" <BuildAction parallelizeBuildables = \"YES\" buildImplicitDependencies = \"YES\">")

Sources/swift-build/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ do {
9090
let (rootPackage, externalPackages) = try fetch(dirs.root)
9191
let (modules, externalModules, products) = try transmute(rootPackage, externalPackages: externalPackages)
9292

93-
let swiftModules = modules.flatMap{ $0 as? SwiftModule }
94-
let externalSwiftModules = externalModules.flatMap{ $0 as? SwiftModule }
93+
let xcodeModules = modules.flatMap { $0 as? XcodeModuleProtocol }
94+
let externalXcodeModules = externalModules.flatMap { $0 as? XcodeModuleProtocol }
9595

9696
let projectName: String
9797
let dstdir: String
@@ -110,7 +110,7 @@ do {
110110
projectName = packageName
111111
}
112112

113-
let outpath = try Xcodeproj.generate(dstdir: dstdir, projectName: projectName, srcroot: dirs.root, modules: swiftModules, externalModules: externalSwiftModules, products: products)
113+
let outpath = try Xcodeproj.generate(dstdir: dstdir, projectName: projectName, srcroot: dirs.root, modules: xcodeModules, externalModules: externalXcodeModules, products: products)
114114

115115
print("generated:", outpath.prettied)
116116
}

Tests/Xcodeproj/TestGeneration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class TestGeneration: XCTestCase {
4242

4343
func testXcodeBuildCanParseIt() {
4444
mktmpdir { dstdir in
45-
func dummy() -> [SwiftModule] {
46-
return [SwiftModule(name: "DummyModuleName", sources: Sources(paths: [], root: dstdir))]
45+
func dummy() -> [XcodeModuleProtocol] {
46+
return [SwiftModule(name: "DummyModuleName", sources: Sources(paths: [], root: dstdir)) as! XcodeModuleProtocol]
4747
}
4848

4949
let projectName = "DummyProjectName"

0 commit comments

Comments
 (0)