Skip to content

Commit f94e5f8

Browse files
authored
PackageModel: add struct BuildFlags, reduce reliance on TSC (#5837)
Added `BuildFlags` to `PackageModel`, mostly a copy of the declaration in TSC, but with a cleaned up initializer arguments naming. Modified uses of build tools flags to use this new struct instead of passing arounds properties separately.
1 parent 25c671e commit f94e5f8

File tree

11 files changed

+125
-80
lines changed

11 files changed

+125
-80
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public final class ClangTargetBuildDescription {
423423
args += ["-include", resourceAccessorHeaderFile.pathString]
424424
}
425425

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

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

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

@@ -1014,7 +1014,7 @@ public final class SwiftTargetBuildDescription {
10141014
result.append("-emit-module")
10151015
result.append("-emit-module-path")
10161016
result.append(moduleOutputPath.pathString)
1017-
result += buildParameters.toolchain.extraSwiftCFlags
1017+
result += buildParameters.toolchain.extraFlags.swiftCompilerFlags
10181018

10191019
result.append("-Xfrontend")
10201020
result.append("-experimental-skip-non-inlinable-function-bodies")
@@ -1090,7 +1090,7 @@ public final class SwiftTargetBuildDescription {
10901090
result += buildParameters.sanitizers.compileSwiftFlags()
10911091
result += ["-parseable-output"]
10921092
result += try self.buildSettingsFlags()
1093-
result += buildParameters.toolchain.extraSwiftCFlags
1093+
result += buildParameters.toolchain.extraFlags.swiftCompilerFlags
10941094
result += buildParameters.swiftCompilerFlags
10951095
return result
10961096
}
@@ -1537,7 +1537,7 @@ public final class ProductBuildDescription {
15371537
// building for Darwin in debug configuration.
15381538
args += swiftASTs.flatMap{ ["-Xlinker", "-add_ast_path", "-Xlinker", $0.pathString] }
15391539

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

2344-
var extraSwiftCFlags = buildParameters.toolchain.extraSwiftCFlags
2344+
var extraSwiftCFlags = buildParameters.toolchain.extraFlags.swiftCompilerFlags
23452345
if !includeLibrarySearchPaths {
23462346
for index in extraSwiftCFlags.indices.dropLast().reversed() {
23472347
if extraSwiftCFlags[index] == "-L" {

Sources/Commands/Options.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import PackageModel
1717
import SPMBuildCore
1818
import Build
1919

20-
import struct TSCUtility.BuildFlags
2120
import struct TSCUtility.Triple
2221

2322
struct GlobalOptions: ParsableArguments {
@@ -279,10 +278,11 @@ struct BuildOptions: ParsableArguments {
279278

280279
var buildFlags: BuildFlags {
281280
BuildFlags(
282-
xcc: cCompilerFlags,
283-
xcxx: cxxCompilerFlags,
284-
xswiftc: swiftCompilerFlags,
285-
xlinker: linkerFlags)
281+
cCompilerFlags: cCompilerFlags,
282+
cxxCompilerFlags: cxxCompilerFlags,
283+
swiftCompilerFlags: swiftCompilerFlags,
284+
linkerFlags: linkerFlags
285+
)
286286
}
287287

288288
/// The compilation destination’s target triple.

Sources/PackageModel/BuildFlags.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
/// Build-tool independent flags.
12+
public struct BuildFlags: Equatable, Encodable {
13+
14+
/// Flags to pass to the C compiler.
15+
public var cCompilerFlags: [String]
16+
17+
/// Flags to pass to the C++ compiler.
18+
public var cxxCompilerFlags: [String]
19+
20+
/// Flags to pass to the Swift compiler.
21+
public var swiftCompilerFlags: [String]
22+
23+
/// Flags to pass to the linker.
24+
public var linkerFlags: [String]
25+
26+
public init(
27+
cCompilerFlags: [String]? = nil,
28+
cxxCompilerFlags: [String]? = nil,
29+
swiftCompilerFlags: [String]? = nil,
30+
linkerFlags: [String]? = nil
31+
) {
32+
self.cCompilerFlags = cCompilerFlags ?? []
33+
self.cxxCompilerFlags = cxxCompilerFlags ?? []
34+
self.swiftCompilerFlags = swiftCompilerFlags ?? []
35+
self.linkerFlags = linkerFlags ?? []
36+
}
37+
}

Sources/PackageModel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
add_library(PackageModel
1010
BuildConfiguration.swift
1111
BuildEnvironment.swift
12+
BuildFlags.swift
1213
BuildSettings.swift
1314
Destination.swift
1415
Diagnostics.swift

Sources/PackageModel/Destination.swift

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,28 @@ public struct Destination: Encodable, Equatable {
6060
public var binDir: AbsolutePath
6161

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

6568
/// Additional flags to be passed to the Swift compiler.
66-
public let extraSwiftCFlags: [String]
69+
@available(*, deprecated, message: "use `extraFlags.swiftCompilerFlags` instead")
70+
public var extraSwiftCFlags: [String] {
71+
extraFlags.swiftCompilerFlags
72+
}
6773

6874
/// Additional flags to be passed to the C++ compiler.
69-
@available(*, deprecated, message: "use `extraCXXFlags` instead")
75+
@available(*, deprecated, message: "use `extraFlags.cxxCompilerFlags` instead")
7076
public var extraCPPFlags: [String] {
71-
extraCXXFlags
77+
extraFlags.cxxCompilerFlags
7278
}
7379

74-
/// Additional flags to be passed to the C++ compiler.
75-
public var extraCXXFlags: [String]
80+
/// Additional flags to be passed to the build tools.
81+
public let extraFlags: BuildFlags
7682

7783
/// Creates a compilation destination with the specified properties.
78-
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraCCFlags:extraSwiftCFlags:extraCXXFlags)` instead")
84+
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraFlags)` instead")
7985
public init(
8086
target: Triple? = nil,
8187
sdk: AbsolutePath?,
@@ -87,26 +93,24 @@ public struct Destination: Encodable, Equatable {
8793
self.target = target
8894
self.sdk = sdk
8995
self.binDir = binDir
90-
self.extraCCFlags = extraCCFlags
91-
self.extraSwiftCFlags = extraSwiftCFlags
92-
self.extraCXXFlags = extraCPPFlags
96+
self.extraFlags = BuildFlags(
97+
cCompilerFlags: extraCCFlags,
98+
cxxCompilerFlags: extraCPPFlags,
99+
swiftCompilerFlags: extraSwiftCFlags
100+
)
93101
}
94102

95103
/// Creates a compilation destination with the specified properties.
96104
public init(
97105
target: Triple? = nil,
98106
sdk: AbsolutePath?,
99107
binDir: AbsolutePath,
100-
extraCCFlags: [String] = [],
101-
extraSwiftCFlags: [String] = [],
102-
extraCXXFlags: [String] = []
108+
extraFlags: BuildFlags = BuildFlags()
103109
) {
104110
self.target = target
105111
self.sdk = sdk
106112
self.binDir = binDir
107-
self.extraCCFlags = extraCCFlags
108-
self.extraSwiftCFlags = extraSwiftCFlags
109-
self.extraCXXFlags = extraCXXFlags
113+
self.extraFlags = extraFlags
110114
}
111115

112116
/// Returns the bin directory for the host.
@@ -179,8 +183,7 @@ public struct Destination: Encodable, Equatable {
179183
target: nil,
180184
sdk: sdkPath,
181185
binDir: binDir,
182-
extraCCFlags: extraCCFlags,
183-
extraSwiftCFlags: extraSwiftCFlags
186+
extraFlags: BuildFlags(cCompilerFlags: extraCCFlags, swiftCompilerFlags: extraSwiftCFlags)
184187
)
185188
}
186189

@@ -241,10 +244,12 @@ extension Destination {
241244
target: destination.target.map{ try Triple($0) },
242245
sdk: destination.sdk,
243246
binDir: destination.binDir,
244-
extraCCFlags: destination.extraCCFlags,
245-
extraSwiftCFlags: destination.extraSwiftCFlags,
246-
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
247-
extraCXXFlags: destination.extraCPPFlags
247+
extraFlags: BuildFlags(
248+
cCompilerFlags: destination.extraCCFlags,
249+
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
250+
cxxCompilerFlags: destination.extraCPPFlags,
251+
swiftCompilerFlags: destination.extraSwiftCFlags
252+
)
248253
)
249254
}
250255
}

Sources/PackageModel/Toolchain.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,21 @@ public protocol Toolchain {
2929
// the OSS clang compiler. This API should not used for any other purpose.
3030
/// Returns true if clang compiler's vendor is Apple and nil if unknown.
3131
func _isClangCompilerVendorApple() throws -> Bool?
32+
33+
/// Additional flags to be passed to the build tools.
34+
var extraFlags: BuildFlags { get }
3235

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

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

3944
/// Additional flags to be passed to the C++ compiler.
40-
@available(*, deprecated, message: "use extraCXXFlags instead")
45+
@available(*, deprecated, message: "use extraFlags.cxxCompilerFlags instead")
4146
var extraCPPFlags: [String] { get }
42-
43-
/// Additional flags to be passed to the C++ compiler.
44-
var extraCXXFlags: [String] { get }
4547
}
4648

4749
extension Toolchain {
@@ -62,7 +64,15 @@ extension Toolchain {
6264
}
6365
}
6466

67+
public var extraCCFlags: [String] {
68+
extraFlags.cCompilerFlags
69+
}
70+
6571
public var extraCPPFlags: [String] {
66-
extraCXXFlags
72+
extraFlags.cxxCompilerFlags
73+
}
74+
75+
public var extraSwiftCFlags: [String] {
76+
extraFlags.swiftCompilerFlags
6777
}
6878
}

Sources/PackageModel/UserToolchain.swift

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,10 @@ public final class UserToolchain: Toolchain {
3333

3434
/// Path of the `swiftc` compiler.
3535
public let swiftCompilerPath: AbsolutePath
36-
37-
public var extraCCFlags: [String]
38-
39-
public let extraSwiftCFlags: [String]
4036

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-
}
37+
/// Additional flags to be passed to the build tools.
38+
public var extraFlags: BuildFlags
4639

47-
/// Additional flags to be passed to the C++ compiler.
48-
public var extraCXXFlags: [String]
49-
5040
/// Path of the `swift` interpreter.
5141
public var swiftInterpreterPath: AbsolutePath {
5242
return self.swiftCompilerPath.parentDirectory.appending(component: "swift" + hostExecutableSuffix)
@@ -342,13 +332,13 @@ public final class UserToolchain: Toolchain {
342332
}
343333
}
344334

345-
return destination.extraSwiftCFlags
335+
return destination.extraFlags.swiftCompilerFlags
346336
}
347337

348338
return (triple.isDarwin() || triple.isAndroid() || triple.isWASI() || triple.isWindows()
349339
? ["-sdk", sdk.pathString]
350340
: [])
351-
+ destination.extraSwiftCFlags
341+
+ destination.extraFlags.swiftCompilerFlags
352342
}
353343

354344
// MARK: - initializer
@@ -395,18 +385,19 @@ public final class UserToolchain: Toolchain {
395385
}
396386

397387
self.triple = triple
388+
self.extraFlags = BuildFlags()
398389

399-
self.extraSwiftCFlags = try Self.deriveSwiftCFlags(triple: triple, destination: destination, environment: environment)
390+
self.extraFlags.swiftCompilerFlags = try Self.deriveSwiftCFlags(triple: triple, destination: destination, environment: environment)
400391

401392
if let sdk = destination.sdk {
402-
self.extraCCFlags = [
393+
self.extraFlags.cCompilerFlags = [
403394
triple.isDarwin() ? "-isysroot" : "--sysroot", sdk.pathString
404-
] + destination.extraCCFlags
395+
] + destination.extraFlags.cCompilerFlags
405396

406-
self.extraCXXFlags = destination.extraCXXFlags
397+
self.extraFlags.cxxCompilerFlags = destination.extraFlags.cxxCompilerFlags
407398
} else {
408-
self.extraCCFlags = destination.extraCCFlags
409-
self.extraCXXFlags = destination.extraCXXFlags
399+
self.extraFlags.cCompilerFlags = destination.extraFlags.cCompilerFlags
400+
self.extraFlags.cxxCompilerFlags = destination.extraFlags.cxxCompilerFlags
410401
}
411402

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

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

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

432423
case .multithreaded:
433424
// Defines _MT
434425
// Linker uses LIBCMT.lib
435-
self.extraCCFlags += ["-D_MT", "-Xclang", "--dependent-lib=libcmt"]
426+
self.extraFlags.cCompilerFlags += ["-D_MT", "-Xclang", "--dependent-lib=libcmt"]
436427
}
437428
}
438429
}
@@ -450,7 +441,7 @@ public final class UserToolchain: Toolchain {
450441
self.configuration = .init(
451442
librarianPath: librarianPath,
452443
swiftCompilerPath: swiftCompilers.manifest,
453-
swiftCompilerFlags: self.extraSwiftCFlags,
444+
swiftCompilerFlags: self.extraFlags.swiftCompilerFlags,
454445
swiftCompilerEnvironment: environment,
455446
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
456447
sdkRootPath: self.destination.sdk,

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import TSCBasic
1616
import PackageModel
1717
import PackageGraph
1818

19-
import struct TSCUtility.BuildFlags
2019
import struct TSCUtility.Triple
2120

2221
public struct BuildParameters: Encodable {
@@ -392,10 +391,10 @@ private struct _Toolchain: Encodable {
392391
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
393392
try container.encode(toolchain.getClangCompiler(), forKey: .clangCompiler)
394393

395-
try container.encode(toolchain.extraCCFlags, forKey: .extraCCFlags)
394+
try container.encode(toolchain.extraFlags.cCompilerFlags, forKey: .extraCCFlags)
396395
// Maintaining `extraCPPFlags` key for compatibility with older encoding.
397-
try container.encode(toolchain.extraCXXFlags, forKey: .extraCPPFlags)
398-
try container.encode(toolchain.extraSwiftCFlags, forKey: .extraSwiftCFlags)
396+
try container.encode(toolchain.extraFlags.cxxCompilerFlags, forKey: .extraCPPFlags)
397+
try container.encode(toolchain.extraFlags.swiftCompilerFlags, forKey: .extraSwiftCFlags)
399398
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
400399
}
401400
}

0 commit comments

Comments
 (0)