Skip to content

Commit

Permalink
Add C++ support in ClangModule
Browse files Browse the repository at this point in the history
  • Loading branch information
aciidgh committed May 21, 2016
1 parent 8a4372b commit 5ec54ab
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Sources/Build/Command.compile(ClangModule).swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ extension Command {
var args = basicArgs
args += ["-MD", "-MT", "dependencies", "-MF", path.deps]
args += ["-c", path.source, "-o", path.object]

// Append -std flag if file is cpp.
if path.source.isCpp {
args += ["-std=c++14"]
}
let clang = ClangTool(desc: "Compile \(module.name) \(path.filename)",
inputs: dependencies + [path.source],
outputs: [path.object],
Expand All @@ -105,6 +108,7 @@ extension Command {
var args = module.basicArgs
args += module.optimizationFlags(conf)
args += ["-L\(prefix)"]
args += module.languageArgs
args += module.linkFlags
args += module.sources.compilePathsForBuildDir(wd).map{$0.object}
args += Xld
Expand Down
36 changes: 36 additions & 0 deletions Sources/Build/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ extension CModule {
}
}

extension String {
var isCpp: Bool {
guard let ext = self.fileExt else {
return false
}
return ext == "cpp"
}
}

extension ClangModule {
// Returns language specific arguments for a ClangModule.
var languageArgs: [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.
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/PackageModel/Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct Sources {

static public var validSwiftExtensions = Set<String>(["swift"])

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

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

0 comments on commit 5ec54ab

Please sign in to comment.