Skip to content

Introduce extraCXXFlags, deprecate extraCPPFlags #5827

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

Merged
merged 1 commit into from
Oct 20, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions Sources/PackageModel/Destination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,49 @@ public struct Destination: Encodable, Equatable {

/// Additional flags to be passed to the Swift compiler.
public let extraSwiftCFlags: [String]

/// Additional flags to be passed to the C++ compiler.
@available(*, deprecated, message: "use `extraCXXFlags` instead")
public var extraCPPFlags: [String] {
extraCXXFlags
}

/// Additional flags to be passed to the C++ compiler.
public var extraCXXFlags: [String]

/// Additional flags to be passed when compiling with C++.
public let extraCPPFlags: [String]

/// Creates a compilation destination with the specified properties.
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraCCFlags:extraSwiftCFlags:extraCXXFlags)` instead")
public init(
target: Triple? = nil,
sdk: AbsolutePath?,
binDir: AbsolutePath,
extraCCFlags: [String] = [],
extraSwiftCFlags: [String] = [],
extraCPPFlags: [String]
) {
self.target = target
self.sdk = sdk
self.binDir = binDir
self.extraCCFlags = extraCCFlags
self.extraSwiftCFlags = extraSwiftCFlags
self.extraCXXFlags = extraCPPFlags
}

/// Creates a compilation destination with the specified properties.
public init(
target: Triple? = nil,
sdk: AbsolutePath?,
binDir: AbsolutePath,
extraCCFlags: [String] = [],
extraSwiftCFlags: [String] = [],
extraCPPFlags: [String] = []
extraCXXFlags: [String] = []
) {
self.target = target
self.sdk = sdk
self.binDir = binDir
self.extraCCFlags = extraCCFlags
self.extraSwiftCFlags = extraSwiftCFlags
self.extraCPPFlags = extraCPPFlags
self.extraCXXFlags = extraCXXFlags
}

/// Returns the bin directory for the host.
Expand Down Expand Up @@ -156,8 +180,7 @@ public struct Destination: Encodable, Equatable {
sdk: sdkPath,
binDir: binDir,
extraCCFlags: extraCCFlags,
extraSwiftCFlags: extraSwiftCFlags,
extraCPPFlags: []
extraSwiftCFlags: extraSwiftCFlags
)
}

Expand Down Expand Up @@ -197,10 +220,7 @@ public struct Destination: Encodable, Equatable {
return Destination(
target: triple,
sdk: wasiSysroot,
binDir: host.binDir,
extraCCFlags: [],
extraSwiftCFlags: [],
extraCPPFlags: []
binDir: host.binDir
)
}
return nil
Expand All @@ -223,7 +243,8 @@ extension Destination {
binDir: destination.binDir,
extraCCFlags: destination.extraCCFlags,
extraSwiftCFlags: destination.extraSwiftCFlags,
extraCPPFlags: destination.extraCPPFlags
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
extraCXXFlags: destination.extraCPPFlags
)
}
}
Expand Down
10 changes: 9 additions & 1 deletion Sources/PackageModel/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public protocol Toolchain {
/// Additional flags to be passed to the Swift compiler.
var extraSwiftCFlags: [String] { get }

/// Additional flags to be passed when compiling with C++.
/// Additional flags to be passed to the C++ compiler.
@available(*, deprecated, message: "use extraCXXFlags instead")
var extraCPPFlags: [String] { get }

/// Additional flags to be passed to the C++ compiler.
var extraCXXFlags: [String] { get }
}

extension Toolchain {
Expand All @@ -53,4 +57,8 @@ extension Toolchain {
// FIXME: Not sure if it's better to base this off of Swift compiler or our own binary.
return AbsolutePath("../../lib", relativeTo: resolveSymlinks(swiftCompilerPath))
}

public var extraCPPFlags: [String] {
extraCXXFlags
}
}
15 changes: 11 additions & 4 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ public final class UserToolchain: Toolchain {
public var extraCCFlags: [String]

public let extraSwiftCFlags: [String]

public var extraCPPFlags: [String]

/// Additional flags to be passed to the C++ compiler.
@available(*, deprecated, message: "use extraCXXFlags instead")
public var extraCPPFlags: [String] {
extraCXXFlags
}

/// Additional flags to be passed to the C++ compiler.
public var extraCXXFlags: [String]

/// Path of the `swift` interpreter.
public var swiftInterpreterPath: AbsolutePath {
Expand Down Expand Up @@ -397,10 +404,10 @@ public final class UserToolchain: Toolchain {
triple.isDarwin() ? "-isysroot" : "--sysroot", sdk.pathString
] + destination.extraCCFlags

self.extraCPPFlags = destination.extraCPPFlags
self.extraCXXFlags = destination.extraCXXFlags
} else {
self.extraCCFlags = destination.extraCCFlags
self.extraCPPFlags = destination.extraCPPFlags
self.extraCXXFlags = destination.extraCXXFlags
}

if triple.isWindows() {
Expand Down
3 changes: 2 additions & 1 deletion Sources/SPMBuildCore/BuildParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ private struct _Toolchain: Encodable {
try container.encode(toolchain.getClangCompiler(), forKey: .clangCompiler)

try container.encode(toolchain.extraCCFlags, forKey: .extraCCFlags)
try container.encode(toolchain.extraCPPFlags, forKey: .extraCPPFlags)
// Maintaining `extraCPPFlags` key for compatibility with older encoding.
try container.encode(toolchain.extraCXXFlags, forKey: .extraCPPFlags)
try container.encode(toolchain.extraSwiftCFlags, forKey: .extraSwiftCFlags)
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/XCBuildSupport/XcodeBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public final class XcodeBuildSystem: SPMBuildCore.BuildSystem {
).joined(separator: " ")
settings["OTHER_CPLUSPLUSFLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraCPPFlags
+ buildParameters.toolchain.extraCXXFlags
+ buildParameters.flags.cxxCompilerFlags.map { $0.spm_shellEscaped() }
).joined(separator: " ")
settings["OTHER_SWIFT_FLAGS"] = (
Expand Down
4 changes: 2 additions & 2 deletions Tests/BuildTests/MockBuildTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct MockToolchain: PackageModel.Toolchain {
let extraCCFlags: [String] = []
let extraSwiftCFlags: [String] = []
#if os(macOS)
let extraCPPFlags: [String] = ["-lc++"]
let extraCXXFlags: [String] = ["-lc++"]
#else
let extraCPPFlags: [String] = ["-lstdc++"]
let extraCXXFlags: [String] = ["-lstdc++"]
#endif
func getClangCompiler() throws -> AbsolutePath {
return AbsolutePath("/fake/path/to/clang")
Expand Down