Skip to content

Commit 75877ea

Browse files
authored
Introduce extraCXXFlags, deprecate extraCPPFlags (#5827)
Usually, CPP is used as an abbreviation for the C preprocessor, while CXX more commonly refers to the C++ compiler. We also use `cxxSettings` in `Package.swift` manifest: https://developer.apple.com/documentation/packagedescription/target/cxxsettings. For this wider consistency, `extraCXXFlags` is introduced as a replacement for `extraCPPFlags`. The latter is then deprecated and is made a computed property that just returns the value of `extraCXXFlags`.
1 parent 2f928e2 commit 75877ea

File tree

6 files changed

+58
-21
lines changed

6 files changed

+58
-21
lines changed

Sources/PackageModel/Destination.swift

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,49 @@ public struct Destination: Encodable, Equatable {
6464

6565
/// Additional flags to be passed to the Swift compiler.
6666
public let extraSwiftCFlags: [String]
67+
68+
/// Additional flags to be passed to the C++ compiler.
69+
@available(*, deprecated, message: "use `extraCXXFlags` instead")
70+
public var extraCPPFlags: [String] {
71+
extraCXXFlags
72+
}
73+
74+
/// Additional flags to be passed to the C++ compiler.
75+
public var extraCXXFlags: [String]
6776

68-
/// Additional flags to be passed when compiling with C++.
69-
public let extraCPPFlags: [String]
70-
77+
/// Creates a compilation destination with the specified properties.
78+
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraCCFlags:extraSwiftCFlags:extraCXXFlags)` instead")
79+
public init(
80+
target: Triple? = nil,
81+
sdk: AbsolutePath?,
82+
binDir: AbsolutePath,
83+
extraCCFlags: [String] = [],
84+
extraSwiftCFlags: [String] = [],
85+
extraCPPFlags: [String]
86+
) {
87+
self.target = target
88+
self.sdk = sdk
89+
self.binDir = binDir
90+
self.extraCCFlags = extraCCFlags
91+
self.extraSwiftCFlags = extraSwiftCFlags
92+
self.extraCXXFlags = extraCPPFlags
93+
}
94+
7195
/// Creates a compilation destination with the specified properties.
7296
public init(
7397
target: Triple? = nil,
7498
sdk: AbsolutePath?,
7599
binDir: AbsolutePath,
76100
extraCCFlags: [String] = [],
77101
extraSwiftCFlags: [String] = [],
78-
extraCPPFlags: [String] = []
102+
extraCXXFlags: [String] = []
79103
) {
80104
self.target = target
81105
self.sdk = sdk
82106
self.binDir = binDir
83107
self.extraCCFlags = extraCCFlags
84108
self.extraSwiftCFlags = extraSwiftCFlags
85-
self.extraCPPFlags = extraCPPFlags
109+
self.extraCXXFlags = extraCXXFlags
86110
}
87111

88112
/// Returns the bin directory for the host.
@@ -156,8 +180,7 @@ public struct Destination: Encodable, Equatable {
156180
sdk: sdkPath,
157181
binDir: binDir,
158182
extraCCFlags: extraCCFlags,
159-
extraSwiftCFlags: extraSwiftCFlags,
160-
extraCPPFlags: []
183+
extraSwiftCFlags: extraSwiftCFlags
161184
)
162185
}
163186

@@ -197,10 +220,7 @@ public struct Destination: Encodable, Equatable {
197220
return Destination(
198221
target: triple,
199222
sdk: wasiSysroot,
200-
binDir: host.binDir,
201-
extraCCFlags: [],
202-
extraSwiftCFlags: [],
203-
extraCPPFlags: []
223+
binDir: host.binDir
204224
)
205225
}
206226
return nil
@@ -223,7 +243,8 @@ extension Destination {
223243
binDir: destination.binDir,
224244
extraCCFlags: destination.extraCCFlags,
225245
extraSwiftCFlags: destination.extraSwiftCFlags,
226-
extraCPPFlags: destination.extraCPPFlags
246+
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
247+
extraCXXFlags: destination.extraCPPFlags
227248
)
228249
}
229250
}

Sources/PackageModel/Toolchain.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ public protocol Toolchain {
3636
/// Additional flags to be passed to the Swift compiler.
3737
var extraSwiftCFlags: [String] { get }
3838

39-
/// Additional flags to be passed when compiling with C++.
39+
/// Additional flags to be passed to the C++ compiler.
40+
@available(*, deprecated, message: "use extraCXXFlags instead")
4041
var extraCPPFlags: [String] { get }
42+
43+
/// Additional flags to be passed to the C++ compiler.
44+
var extraCXXFlags: [String] { get }
4145
}
4246

4347
extension Toolchain {
@@ -57,4 +61,8 @@ extension Toolchain {
5761
return try AbsolutePath(validating: "../../lib", relativeTo: resolveSymlinks(swiftCompilerPath))
5862
}
5963
}
64+
65+
public var extraCPPFlags: [String] {
66+
extraCXXFlags
67+
}
6068
}

Sources/PackageModel/UserToolchain.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ public final class UserToolchain: Toolchain {
3737
public var extraCCFlags: [String]
3838

3939
public let extraSwiftCFlags: [String]
40-
41-
public var extraCPPFlags: [String]
40+
41+
/// Additional flags to be passed to the C++ compiler.
42+
@available(*, deprecated, message: "use extraCXXFlags instead")
43+
public var extraCPPFlags: [String] {
44+
extraCXXFlags
45+
}
46+
47+
/// Additional flags to be passed to the C++ compiler.
48+
public var extraCXXFlags: [String]
4249

4350
/// Path of the `swift` interpreter.
4451
public var swiftInterpreterPath: AbsolutePath {
@@ -396,10 +403,10 @@ public final class UserToolchain: Toolchain {
396403
triple.isDarwin() ? "-isysroot" : "--sysroot", sdk.pathString
397404
] + destination.extraCCFlags
398405

399-
self.extraCPPFlags = destination.extraCPPFlags
406+
self.extraCXXFlags = destination.extraCXXFlags
400407
} else {
401408
self.extraCCFlags = destination.extraCCFlags
402-
self.extraCPPFlags = destination.extraCPPFlags
409+
self.extraCXXFlags = destination.extraCXXFlags
403410
}
404411

405412
if triple.isWindows() {

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ private struct _Toolchain: Encodable {
393393
try container.encode(toolchain.getClangCompiler(), forKey: .clangCompiler)
394394

395395
try container.encode(toolchain.extraCCFlags, forKey: .extraCCFlags)
396-
try container.encode(toolchain.extraCPPFlags, forKey: .extraCPPFlags)
396+
// Maintaining `extraCPPFlags` key for compatibility with older encoding.
397+
try container.encode(toolchain.extraCXXFlags, forKey: .extraCPPFlags)
397398
try container.encode(toolchain.extraSwiftCFlags, forKey: .extraSwiftCFlags)
398399
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
399400
}

Sources/XCBuildSupport/XcodeBuildSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public final class XcodeBuildSystem: SPMBuildCore.BuildSystem {
183183
).joined(separator: " ")
184184
settings["OTHER_CPLUSPLUSFLAGS"] = (
185185
["$(inherited)"]
186-
+ buildParameters.toolchain.extraCPPFlags
186+
+ buildParameters.toolchain.extraCXXFlags
187187
+ buildParameters.flags.cxxCompilerFlags.map { $0.spm_shellEscaped() }
188188
).joined(separator: " ")
189189
settings["OTHER_SWIFT_FLAGS"] = (

Tests/BuildTests/MockBuildTestHelper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ struct MockToolchain: PackageModel.Toolchain {
2020
let extraCCFlags: [String] = []
2121
let extraSwiftCFlags: [String] = []
2222
#if os(macOS)
23-
let extraCPPFlags: [String] = ["-lc++"]
23+
let extraCXXFlags: [String] = ["-lc++"]
2424
#else
25-
let extraCPPFlags: [String] = ["-lstdc++"]
25+
let extraCXXFlags: [String] = ["-lstdc++"]
2626
#endif
2727
func getClangCompiler() throws -> AbsolutePath {
2828
return AbsolutePath(path: "/fake/path/to/clang")

0 commit comments

Comments
 (0)