Skip to content

PackageModel: add struct BuildFlags, reduce reliance on TSC #5837

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 2 commits into from
Oct 25, 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
12 changes: 6 additions & 6 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public final class ClangTargetBuildDescription {
args += ["-include", resourceAccessorHeaderFile.pathString]
}

args += buildParameters.toolchain.extraCCFlags
args += buildParameters.toolchain.extraFlags.cCompilerFlags
// User arguments (from -Xcc and -Xcxx below) should follow generated arguments to allow user overrides
args += buildParameters.flags.cCompilerFlags

Expand Down Expand Up @@ -944,7 +944,7 @@ public final class SwiftTargetBuildDescription {
args += ["-emit-module-interface-path", parseableModuleInterfaceOutputPath.pathString]
}

args += buildParameters.toolchain.extraSwiftCFlags
args += buildParameters.toolchain.extraFlags.swiftCompilerFlags
// User arguments (from -Xswiftc) should follow generated arguments to allow user overrides
args += buildParameters.swiftCompilerFlags

Expand Down Expand Up @@ -1014,7 +1014,7 @@ public final class SwiftTargetBuildDescription {
result.append("-emit-module")
result.append("-emit-module-path")
result.append(moduleOutputPath.pathString)
result += buildParameters.toolchain.extraSwiftCFlags
result += buildParameters.toolchain.extraFlags.swiftCompilerFlags

result.append("-Xfrontend")
result.append("-experimental-skip-non-inlinable-function-bodies")
Expand Down Expand Up @@ -1090,7 +1090,7 @@ public final class SwiftTargetBuildDescription {
result += buildParameters.sanitizers.compileSwiftFlags()
result += ["-parseable-output"]
result += try self.buildSettingsFlags()
result += buildParameters.toolchain.extraSwiftCFlags
result += buildParameters.toolchain.extraFlags.swiftCompilerFlags
result += buildParameters.swiftCompilerFlags
return result
}
Expand Down Expand Up @@ -1537,7 +1537,7 @@ public final class ProductBuildDescription {
// building for Darwin in debug configuration.
args += swiftASTs.flatMap{ ["-Xlinker", "-add_ast_path", "-Xlinker", $0.pathString] }

args += buildParameters.toolchain.extraSwiftCFlags
args += buildParameters.toolchain.extraFlags.swiftCompilerFlags
// User arguments (from -Xlinker and -Xswiftc) should follow generated arguments to allow user overrides
args += buildParameters.linkerFlags
args += stripInvalidArguments(buildParameters.swiftCompilerFlags)
Expand Down Expand Up @@ -2341,7 +2341,7 @@ public class BuildPlan {
let buildPath = buildParameters.buildPath.pathString
var arguments = ["-I", buildPath]

var extraSwiftCFlags = buildParameters.toolchain.extraSwiftCFlags
var extraSwiftCFlags = buildParameters.toolchain.extraFlags.swiftCompilerFlags
if !includeLibrarySearchPaths {
for index in extraSwiftCFlags.indices.dropLast().reversed() {
if extraSwiftCFlags[index] == "-L" {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Commands/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import PackageModel
import SPMBuildCore
import Build

import struct TSCUtility.BuildFlags
import struct TSCUtility.Triple

struct GlobalOptions: ParsableArguments {
Expand Down Expand Up @@ -279,10 +278,11 @@ struct BuildOptions: ParsableArguments {

var buildFlags: BuildFlags {
BuildFlags(
xcc: cCompilerFlags,
xcxx: cxxCompilerFlags,
xswiftc: swiftCompilerFlags,
xlinker: linkerFlags)
cCompilerFlags: cCompilerFlags,
cxxCompilerFlags: cxxCompilerFlags,
swiftCompilerFlags: swiftCompilerFlags,
linkerFlags: linkerFlags
)
}

/// The compilation destination’s target triple.
Expand Down
37 changes: 37 additions & 0 deletions Sources/PackageModel/BuildFlags.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

/// Build-tool independent flags.
public struct BuildFlags: Equatable, Encodable {

/// Flags to pass to the C compiler.
public var cCompilerFlags: [String]

/// Flags to pass to the C++ compiler.
public var cxxCompilerFlags: [String]

/// Flags to pass to the Swift compiler.
public var swiftCompilerFlags: [String]

/// Flags to pass to the linker.
public var linkerFlags: [String]

public init(
cCompilerFlags: [String]? = nil,
cxxCompilerFlags: [String]? = nil,
swiftCompilerFlags: [String]? = nil,
linkerFlags: [String]? = nil
) {
self.cCompilerFlags = cCompilerFlags ?? []
self.cxxCompilerFlags = cxxCompilerFlags ?? []
self.swiftCompilerFlags = swiftCompilerFlags ?? []
self.linkerFlags = linkerFlags ?? []
}
}
1 change: 1 addition & 0 deletions Sources/PackageModel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
add_library(PackageModel
BuildConfiguration.swift
BuildEnvironment.swift
BuildFlags.swift
BuildSettings.swift
Destination.swift
Diagnostics.swift
Expand Down
49 changes: 27 additions & 22 deletions Sources/PackageModel/Destination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,28 @@ public struct Destination: Encodable, Equatable {
public var binDir: AbsolutePath

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

/// Additional flags to be passed to the Swift compiler.
public let extraSwiftCFlags: [String]
@available(*, deprecated, message: "use `extraFlags.swiftCompilerFlags` instead")
public var extraSwiftCFlags: [String] {
extraFlags.swiftCompilerFlags
}

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

/// Additional flags to be passed to the C++ compiler.
public var extraCXXFlags: [String]
/// Additional flags to be passed to the build tools.
public let extraFlags: BuildFlags

/// Creates a compilation destination with the specified properties.
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraCCFlags:extraSwiftCFlags:extraCXXFlags)` instead")
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraFlags)` instead")
public init(
target: Triple? = nil,
sdk: AbsolutePath?,
Expand All @@ -87,26 +93,24 @@ public struct Destination: Encodable, Equatable {
self.target = target
self.sdk = sdk
self.binDir = binDir
self.extraCCFlags = extraCCFlags
self.extraSwiftCFlags = extraSwiftCFlags
self.extraCXXFlags = extraCPPFlags
self.extraFlags = BuildFlags(
cCompilerFlags: extraCCFlags,
cxxCompilerFlags: extraCPPFlags,
swiftCompilerFlags: extraSwiftCFlags
)
}

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

/// Returns the bin directory for the host.
Expand Down Expand Up @@ -179,8 +183,7 @@ public struct Destination: Encodable, Equatable {
target: nil,
sdk: sdkPath,
binDir: binDir,
extraCCFlags: extraCCFlags,
extraSwiftCFlags: extraSwiftCFlags
extraFlags: BuildFlags(cCompilerFlags: extraCCFlags, swiftCompilerFlags: extraSwiftCFlags)
)
}

Expand Down Expand Up @@ -241,10 +244,12 @@ extension Destination {
target: destination.target.map{ try Triple($0) },
sdk: destination.sdk,
binDir: destination.binDir,
extraCCFlags: destination.extraCCFlags,
extraSwiftCFlags: destination.extraSwiftCFlags,
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
extraCXXFlags: destination.extraCPPFlags
extraFlags: BuildFlags(
cCompilerFlags: destination.extraCCFlags,
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
cxxCompilerFlags: destination.extraCPPFlags,
swiftCompilerFlags: destination.extraSwiftCFlags
)
)
}
}
Expand Down
20 changes: 15 additions & 5 deletions Sources/PackageModel/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ public protocol Toolchain {
// the OSS clang compiler. This API should not used for any other purpose.
/// Returns true if clang compiler's vendor is Apple and nil if unknown.
func _isClangCompilerVendorApple() throws -> Bool?

/// Additional flags to be passed to the build tools.
var extraFlags: BuildFlags { get }

/// Additional flags to be passed to the C compiler.
@available(*, deprecated, message: "use extraFlags.cCompilerFlags instead")
var extraCCFlags: [String] { get }

/// Additional flags to be passed to the Swift compiler.
@available(*, deprecated, message: "use extraFlags.swiftCompilerFlags instead")
var extraSwiftCFlags: [String] { get }

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

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

extension Toolchain {
Expand All @@ -62,7 +64,15 @@ extension Toolchain {
}
}

public var extraCCFlags: [String] {
extraFlags.cCompilerFlags
}

public var extraCPPFlags: [String] {
extraCXXFlags
extraFlags.cxxCompilerFlags
}

public var extraSwiftCFlags: [String] {
extraFlags.swiftCompilerFlags
}
}
41 changes: 16 additions & 25 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,10 @@ public final class UserToolchain: Toolchain {

/// Path of the `swiftc` compiler.
public let swiftCompilerPath: AbsolutePath

public var extraCCFlags: [String]

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 build tools.
public var extraFlags: BuildFlags

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

/// Path of the `swift` interpreter.
public var swiftInterpreterPath: AbsolutePath {
return self.swiftCompilerPath.parentDirectory.appending(component: "swift" + hostExecutableSuffix)
Expand Down Expand Up @@ -342,13 +332,13 @@ public final class UserToolchain: Toolchain {
}
}

return destination.extraSwiftCFlags
return destination.extraFlags.swiftCompilerFlags
}

return (triple.isDarwin() || triple.isAndroid() || triple.isWASI() || triple.isWindows()
? ["-sdk", sdk.pathString]
: [])
+ destination.extraSwiftCFlags
+ destination.extraFlags.swiftCompilerFlags
}

// MARK: - initializer
Expand Down Expand Up @@ -395,18 +385,19 @@ public final class UserToolchain: Toolchain {
}

self.triple = triple
self.extraFlags = BuildFlags()

self.extraSwiftCFlags = try Self.deriveSwiftCFlags(triple: triple, destination: destination, environment: environment)
self.extraFlags.swiftCompilerFlags = try Self.deriveSwiftCFlags(triple: triple, destination: destination, environment: environment)

if let sdk = destination.sdk {
self.extraCCFlags = [
self.extraFlags.cCompilerFlags = [
triple.isDarwin() ? "-isysroot" : "--sysroot", sdk.pathString
] + destination.extraCCFlags
] + destination.extraFlags.cCompilerFlags

self.extraCXXFlags = destination.extraCXXFlags
self.extraFlags.cxxCompilerFlags = destination.extraFlags.cxxCompilerFlags
} else {
self.extraCCFlags = destination.extraCCFlags
self.extraCXXFlags = destination.extraCXXFlags
self.extraFlags.cCompilerFlags = destination.extraFlags.cCompilerFlags
self.extraFlags.cxxCompilerFlags = destination.extraFlags.cxxCompilerFlags
}

if triple.isWindows() {
Expand All @@ -417,22 +408,22 @@ public final class UserToolchain: Toolchain {
case .multithreadedDebugDLL:
// Defines _DEBUG, _MT, and _DLL
// Linker uses MSVCRTD.lib
self.extraCCFlags += ["-D_DEBUG", "-D_MT", "-D_DLL", "-Xclang", "--dependent-lib=msvcrtd"]
self.extraFlags.cCompilerFlags += ["-D_DEBUG", "-D_MT", "-D_DLL", "-Xclang", "--dependent-lib=msvcrtd"]

case .multithreadedDLL:
// Defines _MT, and _DLL
// Linker uses MSVCRT.lib
self.extraCCFlags += ["-D_MT", "-D_DLL", "-Xclang", "--dependent-lib=msvcrt"]
self.extraFlags.cCompilerFlags += ["-D_MT", "-D_DLL", "-Xclang", "--dependent-lib=msvcrt"]

case .multithreadedDebug:
// Defines _DEBUG, and _MT
// Linker uses LIBCMTD.lib
self.extraCCFlags += ["-D_DEBUG", "-D_MT", "-Xclang", "--dependent-lib=libcmtd"]
self.extraFlags.cCompilerFlags += ["-D_DEBUG", "-D_MT", "-Xclang", "--dependent-lib=libcmtd"]

case .multithreaded:
// Defines _MT
// Linker uses LIBCMT.lib
self.extraCCFlags += ["-D_MT", "-Xclang", "--dependent-lib=libcmt"]
self.extraFlags.cCompilerFlags += ["-D_MT", "-Xclang", "--dependent-lib=libcmt"]
}
}
}
Expand All @@ -450,7 +441,7 @@ public final class UserToolchain: Toolchain {
self.configuration = .init(
librarianPath: librarianPath,
swiftCompilerPath: swiftCompilers.manifest,
swiftCompilerFlags: self.extraSwiftCFlags,
swiftCompilerFlags: self.extraFlags.swiftCompilerFlags,
swiftCompilerEnvironment: environment,
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
sdkRootPath: self.destination.sdk,
Expand Down
7 changes: 3 additions & 4 deletions Sources/SPMBuildCore/BuildParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import TSCBasic
import PackageModel
import PackageGraph

import struct TSCUtility.BuildFlags
import struct TSCUtility.Triple

public struct BuildParameters: Encodable {
Expand Down Expand Up @@ -392,10 +391,10 @@ private struct _Toolchain: Encodable {
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
try container.encode(toolchain.getClangCompiler(), forKey: .clangCompiler)

try container.encode(toolchain.extraCCFlags, forKey: .extraCCFlags)
try container.encode(toolchain.extraFlags.cCompilerFlags, forKey: .extraCCFlags)
// 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.extraFlags.cxxCompilerFlags, forKey: .extraCPPFlags)
try container.encode(toolchain.extraFlags.swiftCompilerFlags, forKey: .extraSwiftCFlags)
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
}
}
Expand Down
Loading