Skip to content

[Xcodeproj] Generating clibs targets, beginning #205

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

Closed
wants to merge 7 commits into from
Closed
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
34 changes: 30 additions & 4 deletions Sources/PackageType/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public func ==(lhs: Module, rhs: Module) -> Bool {
return lhs.c99name == rhs.c99name
}

public enum ModuleType {
case Library, Executable
}

public class SwiftModule: Module {
public let sources: Sources

Expand All @@ -53,10 +57,6 @@ public class SwiftModule: Module {
super.init(name: name)
}

public enum ModuleType {
case Library, Executable
}

public var type: ModuleType {
let isLibrary = !sources.relativePaths.contains { path in
path.basename.lowercased() == "main.swift"
Expand Down Expand Up @@ -99,6 +99,32 @@ public class TestModule: SwiftModule {
}
}

public class XcodeModule: Module {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there should be a XcodeModule since Modules here are things which can be built.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a protocol would be better, XCodeRepresentable or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to make a protocol. But -G use all Module fields. I made a protocol which copies all Module properties and stopped in

public var dependencies: [Module]  /// in build order

Don't remember an error. Also I'm not very good in swift, maybe it possible.

public var sources: Sources
public var type: ModuleType
public var fileType: String
public var modulemap: String?
public init?(module: Module){
switch module {
case let swiftModule as SwiftModule:
sources = swiftModule.sources
type = swiftModule.type
fileType = "sourcecode.swift"
modulemap = nil
case let clangModule as ClangModule:
sources = clangModule.sources
type = .Library
fileType = "sourcecode.c.c"
modulemap = clangModule.path+"/module.modulemap"
default:
return nil
}
super.init(name: module.name)
dependencies = module.dependencies
// dependencies = module.dependencies.map { XcodeModule(module: $0) }
}
}


extension Module: CustomStringConvertible {
public var description: String {
Expand Down
27 changes: 13 additions & 14 deletions Sources/Xcodeproj/Module+PBXProj.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ private func fileRef(suffixForModuleSourceFile path: String, srcroot: String) ->
}.joined(separator: "")
}

func fileRefs(forModuleSources module: SwiftModule, srcroot: String) -> [(String, String)] {
func fileRefs(forModuleSources module: XcodeModule, 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: XcodeModule, 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 XcodeModule {
private var isLibrary: Bool {
return type == .Library
}
Expand All @@ -95,7 +95,7 @@ extension SwiftModule {
if self is TestModule {
return "com.apple.product-type.bundle.unit-test"
} else if isLibrary {
return "com.apple.product-type.library.dynamic"
return "com.apple.product-type.framework"
} else {
return "com.apple.product-type.tool"
}
Expand All @@ -106,7 +106,7 @@ extension SwiftModule {
if self is TestModule {
return "wrapper.cfbundle"
} else if isLibrary {
return "dylib"
return "framework"
} else {
return "executable"
}
Expand All @@ -118,7 +118,7 @@ extension SwiftModule {
if self is TestModule {
return "\(c99name).xctest"
} else if isLibrary {
return "\(c99name).dylib"
return "\(c99name).framework"
} else {
return name
}
Expand All @@ -132,13 +132,8 @@ extension SwiftModule {
return dependencies.map{ $0.dependencyReference }.joined(separator: ", ")
}

var productName: String {
if isLibrary && !(self is TestModule) {
// you can go without a lib prefix, but something unexpected will break
return "'lib$(TARGET_NAME)'"
} else {
return "'$(TARGET_NAME)'"
}
var productName: String {
return "'$(TARGET_NAME)'"
}

var debugBuildSettings: String {
Expand Down Expand Up @@ -182,6 +177,10 @@ extension SwiftModule {
if isLibrary {
buildSettings["ENABLE_TESTABILITY"] = "YES"
buildSettings["DYLIB_INSTALL_NAME_BASE"] = "'$(CONFIGURATION_BUILD_DIR)'"
if let modulemap = modulemap {
buildSettings["DEFINES_MODULE"] = "YES"
buildSettings["MODULEMAP_FILE"] = modulemap
}
} else {
// override default behavior, instead link dynamically
buildSettings["SWIFT_FORCE_STATIC_LINK_STDLIB"] = "NO"
Expand All @@ -194,7 +193,7 @@ extension SwiftModule {
}


extension SwiftModule {
extension XcodeModule {
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], products: [Product]) throws -> String {
public func generate(dstdir dstdir: String, projectName: String, srcroot: String, modules: [XcodeModule], products: [Product]) throws -> String {

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

public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [SwiftModule], products _: [Product], printer print: (String) -> Void) {
public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [XcodeModule], products _: [Product], printer print: (String) -> Void) {
let nontests = modules.filter{ !($0 is TestModule) }
let tests = modules.filter{ $0 is TestModule }

Expand Down Expand Up @@ -67,7 +67,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 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: [XcodeModule], 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
4 changes: 2 additions & 2 deletions Sources/swift-build/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ do {
let dirs = try directories()
let packages = try fetch(dirs.root)
let (modules, products) = try transmute(packages, rootdir: dirs.root)
let swiftModules = modules.flatMap{ $0 as? SwiftModule }
let xcodeModules = modules.flatMap{ XcodeModule(module: $0) }

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

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

print("generated:", outpath.prettied)
}
Expand Down