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 f9d8cfc commit f048d3b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
6 changes: 5 additions & 1 deletion Sources/Build/Command.compile(ClangModule).swift
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
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
Expand Up @@ -29,7 +29,7 @@ public struct Sources {

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

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

static public var validExtensions = { validSwiftExtensions.union(validCExtensions) }()
}
23 changes: 0 additions & 23 deletions Tests/Functional/ClangModuleTests.swift
Expand Up @@ -100,29 +100,6 @@ class ClangModulesTestCase: XCTestCase {
XCTAssertFileExists(prefix, ".build", "debug", "libCDynamicLookup.so")
}
}

#if os(OSX)
func testObjectiveCModule() {
fixture(name: "ClangModules/Objc") { prefix in
XCTAssertBuilds(prefix)

// Check if objc module is present.
XCTAssertFileExists(prefix, ".build", "debug", "libobjc.so")

// Test objc executable (depends on objc module).
let objcExec = ".build/debug/objc_exec"
XCTAssertFileExists(prefix, objcExec)
var output = try popen([Path.join(prefix, objcExec)])
XCTAssertEqual(output, "Hello from Objc 5")

// Test swift executable (depends on objc module).
let swiftExec = ".build/debug/swift-exec"
XCTAssertFileExists(prefix, swiftExec)
output = try popen([Path.join(prefix, swiftExec)])
XCTAssertEqual(output, "5\n")
}
}
#endif
}


Expand Down

0 comments on commit f048d3b

Please sign in to comment.