From 5ec54ab00e4c14e1fd9df6b2e140e45bde0acdd5 Mon Sep 17 00:00:00 2001 From: Ankit Aggarwal Date: Sat, 21 May 2016 02:55:00 +0530 Subject: [PATCH] Add C++ support in ClangModule --- .../Build/Command.compile(ClangModule).swift | 6 +++- Sources/Build/misc.swift | 36 +++++++++++++++++++ Sources/PackageModel/Sources.swift | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Sources/Build/Command.compile(ClangModule).swift b/Sources/Build/Command.compile(ClangModule).swift index 5713f105efb..83efc7c9574 100644 --- a/Sources/Build/Command.compile(ClangModule).swift +++ b/Sources/Build/Command.compile(ClangModule).swift @@ -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], @@ -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 diff --git a/Sources/Build/misc.swift b/Sources/Build/misc.swift index 35d95ec0cd0..4c47738869b 100644 --- a/Sources/Build/misc.swift +++ b/Sources/Build/misc.swift @@ -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 { diff --git a/Sources/PackageModel/Sources.swift b/Sources/PackageModel/Sources.swift index 2b8e0d2a349..6d093031343 100644 --- a/Sources/PackageModel/Sources.swift +++ b/Sources/PackageModel/Sources.swift @@ -29,7 +29,7 @@ public struct Sources { static public var validSwiftExtensions = Set(["swift"]) - static public var validCExtensions = Set(["c", "m"]) + static public var validCExtensions = Set(["c", "m", "cc", "cpp", "cxx"]) static public var validExtensions = { validSwiftExtensions.union(validCExtensions) }() }