Skip to content
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

Add Cpp support #362

Merged
merged 1 commit into from May 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/Build/Command.compile(ClangModule).swift
Expand Up @@ -105,6 +105,7 @@ extension Command {
var args = module.basicArgs
args += module.optimizationFlags(conf)
args += ["-L\(prefix)"]
args += module.languageLinkArgs
args += module.linkFlags
args += module.sources.compilePathsForBuildDir(wd).map{$0.object}
args += Xld
Expand Down
39 changes: 39 additions & 0 deletions Sources/Build/misc.swift
Expand Up @@ -43,6 +43,45 @@ extension CModule {
}
}

extension String {
var isCpp: Bool {
guard let ext = self.fileExt else {
return false
}
return Sources.validCppExtensions.contains(ext)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This should reuse the extensions defined elsewhere -- we should break that into C++ and non-C++ groups so we can just reuse the same set here.


extension ClangModule {
// Returns language specific arguments for a ClangModule.
var languageLinkArgs: [String] {
var args = [String]()
// Check if this module contains any cpp file.
var linkCpp = self.containsCppFiles

// Otherwise check if any of its dependencies contains a cpp file.
// FIXME: It is expensive to iterate over all of the dependencies.
// Figure out a way to cache this kind of lookups.
if !linkCpp {
for case let dep as ClangModule in recursiveDependencies {
if dep.containsCppFiles {
linkCpp = true
break
}
}
}
// Link C++ if found any cpp source.
if linkCpp {
args += ["-lc++"]
}
return args
}

var containsCppFiles: Bool {
return sources.paths.contains { $0.isCpp }
}
}

extension ClangModule {

public enum ModuleMapError: ErrorProtocol {
Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageLoading/PackageExtensions.swift
Expand Up @@ -151,7 +151,7 @@ extension Package {
private func modulify(_ path: String, name: String) throws -> Module {
let walked = walk(path, recursing: shouldConsiderDirectory).map{ $0 }

let cSources = walked.filter{ isValidSource($0, validExtensions: Sources.validCExtensions) }
let cSources = walked.filter{ isValidSource($0, validExtensions: Sources.validCFamilyExtensions) }
let swiftSources = walked.filter{ isValidSource($0, validExtensions: Sources.validSwiftExtensions) }

if !cSources.isEmpty {
Expand Down
8 changes: 6 additions & 2 deletions Sources/PackageModel/Sources.swift
Expand Up @@ -30,6 +30,10 @@ public struct Sources {
static public var validSwiftExtensions = Set<String>(["swift"])

static public var validCExtensions = Set<String>(["c", "m"])

static public var validExtensions = { validSwiftExtensions.union(validCExtensions) }()

static public var validCppExtensions = Set<String>(["mm", "cc", "cpp", "cxx"])

static public var validCFamilyExtensions = validCExtensions.union(validCppExtensions)

static public var validExtensions = { validSwiftExtensions.union(validCExtensions).union(validCFamilyExtensions) }()
}