Skip to content

[Xcodeproj] Generating clibs targets 2 #235

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 6 commits into from
Apr 6, 2016
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
26 changes: 25 additions & 1 deletion Sources/PackageType/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
TODO should be a protocol
*/

public class Module {
public protocol ModuleProtocol {
var name: String { get }
var c99name: String { get }
var dependencies: [Module] { get set }
var recursiveDependencies: [Module] { get }
}

public class Module: ModuleProtocol {
/**
This name is not the final name in many cases, instead
use c99name if you need uniqueness.
Expand Down Expand Up @@ -56,6 +63,11 @@ extension ModuleTypeProtocol {
}
}


public protocol XcodeModuleProtocol: ModuleProtocol, ModuleTypeProtocol {
var fileType: String { get }
}

extension Module: Hashable, Equatable {
public var hashValue: Int { return c99name.hashValue }
}
Expand All @@ -79,6 +91,12 @@ extension SwiftModule: ModuleTypeProtocol {
}
}

extension SwiftModule: XcodeModuleProtocol {
public var fileType: String {
return "sourcecode.swift"
}
}

public class CModule: Module {
public let path: String

Expand All @@ -98,6 +116,12 @@ public class ClangModule: CModule {
}
}

extension ClangModule: XcodeModuleProtocol {
public var fileType: String {
return "sourcecode.c.c"
}
}

extension ClangModule: ModuleTypeProtocol {
public var mainFile: String {
return "main.c"
Expand Down
21 changes: 12 additions & 9 deletions Sources/Xcodeproj/Module+PBXProj.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let linkPhaseFileRefPrefix = "_LinkFileRef_"
let sourceGroupFileRefPrefix = "__PBXFileRef_"
let compilePhaseFileRefPrefix = "__src_cc_ref_"

extension Module {
extension XcodeModuleProtocol {
var dependencyReference: String { return "__Dependency_\(c99name)" }
var productReference: String { return "_____Product_\(c99name)" }
var targetReference: String { return "______Target_\(c99name)" }
Expand All @@ -52,7 +52,7 @@ extension Module {
var linkPhaseReference: String { return "___LinkPhase_\(c99name)" }
}

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

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

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

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

extension SwiftModule {
extension XcodeModuleProtocol {

private var isLibrary: Bool {
return type == .Library
}

var type: String {
var productType: String {
if self is TestModule {
return "com.apple.product-type.bundle.unit-test"
} else if isLibrary {
Expand All @@ -119,6 +120,8 @@ extension SwiftModule {
return "compiled.mach-o.\(suffix())"
}



var productPath: String {
if self is TestModule {
return "\(c99name).xctest"
Expand All @@ -130,11 +133,11 @@ extension SwiftModule {
}

var linkPhaseFileRefs: String {
return recursiveDependencies.map{ fileRef(forLinkPhaseChild: $0) }.joined(separator: ", ")
return recursiveDependencies.flatMap { $0 as? XcodeModuleProtocol }.map{ fileRef(forLinkPhaseChild: $0) }.joined(separator: ", ")
}

var nativeTargetDependencies: String {
return dependencies.map{ $0.dependencyReference }.joined(separator: ", ")
return dependencies.flatMap { $0 as? XcodeModuleProtocol }.map{ $0.dependencyReference }.joined(separator: ", ")
}

var productName: String {
Expand Down Expand Up @@ -211,7 +214,7 @@ extension SwiftModule {
}


extension SwiftModule {
extension XcodeModuleProtocol {
var blueprintIdentifier: String {
return targetReference
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Xcodeproj/generate().swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import POSIX
Generates an xcodeproj at the specified path.
- Returns: the path to the generated project
*/
public func generate(dstdir dstdir: String, projectName: String, srcroot: String, modules: [SwiftModule], externalModules: [SwiftModule], products: [Product]) throws -> String {
public func generate(dstdir dstdir: String, projectName: String, srcroot: String, modules: [XcodeModuleProtocol], externalModules: [XcodeModuleProtocol], products: [Product]) throws -> String {

let xcodeprojName = "\(projectName).xcodeproj"
let xcodeprojPath = try mkdir(dstdir, xcodeprojName)
Expand Down
9 changes: 5 additions & 4 deletions Sources/Xcodeproj/pbxproj().swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import PackageType
import Utility

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

let rootModulesSet = Set(modules).subtract(Set(externalModules))
// let rootModulesSet = Set(modules).subtract(Set(externalModules))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem is this 2 lines. As I understood, I can't do this for protocols?

let rootModulesSet = modules
let nonTestRootModules = rootModulesSet.filter{ !($0 is TestModule) }
let (tests, nonTests) = modules.partition{ $0 is TestModule }

Expand Down Expand Up @@ -79,7 +80,7 @@ public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [Swif
for (ref, path) in fileRefs(forModuleSources: module, srcroot: srcroot) {
print(" \(ref) = {")
print(" isa = PBXFileReference;")
print(" lastKnownFileType = sourcecode.swift;")
print(" lastKnownFileType = \(module.fileType);")
print(" name = '\(Path(path).relative(to: module.sources.root))';")
print(" sourceTree = '<group>';")
print(" };")
Expand All @@ -95,7 +96,7 @@ public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [Swif
print(" name = \(module.name);")
print(" productName = \(module.c99name);")
print(" productReference = \(module.productReference);")
print(" productType = '\(module.type)';")
print(" productType = '\(module.productType)';")
print(" };")

// the product file reference
Expand Down
2 changes: 1 addition & 1 deletion Sources/Xcodeproj/xcscheme().swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import PackageType

func xcscheme(container container: String, modules: [SwiftModule], printer print: (String) -> Void) {
func xcscheme(container container: String, modules: [XcodeModuleProtocol], printer print: (String) -> Void) {
print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
print("<Scheme LastUpgradeVersion = \"9999\" version = \"1.3\">")
print(" <BuildAction parallelizeBuildables = \"YES\" buildImplicitDependencies = \"YES\">")
Expand Down
6 changes: 3 additions & 3 deletions Sources/swift-build/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ do {
let (rootPackage, externalPackages) = try fetch(dirs.root)
let (modules, externalModules, products) = try transmute(rootPackage, externalPackages: externalPackages)

let swiftModules = modules.flatMap{ $0 as? SwiftModule }
let externalSwiftModules = externalModules.flatMap{ $0 as? SwiftModule }
let xcodeModules = modules.flatMap { $0 as? XcodeModuleProtocol }
let externalXcodeModules = externalModules.flatMap { $0 as? XcodeModuleProtocol }

let projectName: String
let dstdir: String
Expand All @@ -115,7 +115,7 @@ do {
projectName = packageName
}

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

print("generated:", outpath.prettied)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Xcodeproj/TestGeneration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class TestGeneration: XCTestCase {

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

let projectName = "DummyProjectName"
Expand Down