Skip to content

Commit 57ff0a4

Browse files
committed
Merge branch 'maxd/platform-version-provider' of github.com:apple/swift-package-manager into maxd/packagegraph-value-types
# Conflicts: # Sources/Build/BuildDescription/TargetBuildDescription.swift # Sources/Build/BuildPlan/BuildPlan+Clang.swift # Sources/Build/BuildPlan/BuildPlan+Product.swift # Sources/Build/BuildPlan/BuildPlan+Swift.swift # Sources/PackageGraph/Resolution/ResolvedProduct.swift # Sources/PackageGraph/Resolution/ResolvedTarget.swift
2 parents 477935a + 5b6670e commit 57ff0a4

File tree

56 files changed

+1226
-554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1226
-554
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// swift-tools-version: 5.9
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "SwiftAndCTargets",
7+
targets: [
8+
.target(name: "cLib"),
9+
.executableTarget(name: "exe", dependencies: ["cLib", "swiftLib"]),
10+
.target(name: "swiftLib"),
11+
]
12+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
3+
#include "include/cLib.h"
4+
5+
void cPrint(int value) {
6+
printf("c value: %i\n", value);
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
extern void cPrint(int);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import swiftLib
2+
import cLib
3+
4+
cPrint(1)
5+
swiftPrint(value: 2)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public func swiftPrint(value: Int) {
2+
print("swift value: \(value)")
3+
}

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public final class ClangTargetBuildDescription {
215215

216216
var args = [String]()
217217
// Only enable ARC on macOS.
218-
if buildParameters.targetTriple.isDarwin() {
218+
if self.buildParameters.triple.isDarwin() {
219219
args += ["-fobjc-arc"]
220220
}
221221
args += try buildParameters.targetTripleArgs(for: target)
@@ -224,32 +224,33 @@ public final class ClangTargetBuildDescription {
224224
args += activeCompilationConditions
225225
args += ["-fblocks"]
226226

227+
let buildTriple = self.buildParameters.triple
227228
// Enable index store, if appropriate.
228229
//
229230
// This feature is not widely available in OSS clang. So, we only enable
230231
// index store for Apple's clang or if explicitly asked to.
231232
if ProcessEnv.vars.keys.contains("SWIFTPM_ENABLE_CLANG_INDEX_STORE") {
232-
args += buildParameters.indexStoreArguments(for: target)
233-
} else if buildParameters.targetTriple.isDarwin(),
234-
(try? buildParameters.toolchain._isClangCompilerVendorApple()) == true
233+
args += self.buildParameters.indexStoreArguments(for: target)
234+
} else if buildTriple.isDarwin(),
235+
(try? self.buildParameters.toolchain._isClangCompilerVendorApple()) == true
235236
{
236-
args += buildParameters.indexStoreArguments(for: target)
237+
args += self.buildParameters.indexStoreArguments(for: target)
237238
}
238239

239240
// Enable Clang module flags, if appropriate.
240241
let enableModules: Bool
242+
let triple = self.buildParameters.triple
241243
if toolsVersion < .v5_8 {
242244
// For version < 5.8, we enable them except in these cases:
243245
// 1. on Darwin when compiling for C++, because C++ modules are disabled on Apple-built Clang releases
244246
// 2. on Windows when compiling for any language, because of issues with the Windows SDK
245247
// 3. on Android when compiling for any language, because of issues with the Android SDK
246-
enableModules = !(buildParameters.targetTriple.isDarwin() && isCXX) && !buildParameters.targetTriple
247-
.isWindows() && !buildParameters.targetTriple.isAndroid()
248+
enableModules = !(triple.isDarwin() && isCXX) && !triple.isWindows() && !triple.isAndroid()
248249
} else {
249250
// For version >= 5.8, we disable them when compiling for C++ regardless of platforms, see:
250251
// https://github.com/llvm/llvm-project/issues/55980 for clang frontend crash when module
251252
// enabled for C++ on c++17 standard and above.
252-
enableModules = !isCXX && !buildParameters.targetTriple.isWindows() && !buildParameters.targetTriple.isAndroid()
253+
enableModules = !isCXX && !triple.isWindows() && !triple.isAndroid()
253254
}
254255

255256
if enableModules {

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,16 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
110110
return []
111111
}
112112

113+
let triple = self.buildParameters.triple
113114
switch self.buildParameters.configuration {
114115
case .debug:
115116
return []
116117
case .release:
117-
if self.buildParameters.targetTriple.isApple() {
118+
if triple.isApple() {
118119
return ["-Xlinker", "-dead_strip"]
119-
} else if self.buildParameters.targetTriple.isWindows() {
120+
} else if triple.isWindows() {
120121
return ["-Xlinker", "/OPT:REF"]
121-
} else if self.buildParameters.targetTriple.arch == .wasm32 {
122+
} else if triple.arch == .wasm32 {
122123
// FIXME: wasm-ld strips data segments referenced through __start/__stop symbols
123124
// during GC, and it removes Swift metadata sections like swift5_protocols
124125
// We should add support of SHF_GNU_RETAIN-like flag for __attribute__((retain))
@@ -136,7 +137,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
136137
/// The arguments to the librarian to create a static library.
137138
public func archiveArguments() throws -> [String] {
138139
let librarian = self.buildParameters.toolchain.librarianPath.pathString
139-
let triple = self.buildParameters.targetTriple
140+
let triple = self.buildParameters.triple
140141
if triple.isWindows(), librarian.hasSuffix("link") || librarian.hasSuffix("link.exe") {
141142
return try [librarian, "/LIB", "/OUT:\(binaryPath.pathString)", "@\(self.linkFileListPath.pathString)"]
142143
}
@@ -187,6 +188,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
187188
}
188189

189190
var isLinkingStaticStdlib = false
191+
let triple = self.buildParameters.triple
190192
switch derivedProductType {
191193
case .macro:
192194
throw InternalError("macro not supported") // should never be reached
@@ -206,7 +208,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
206208
args += self.deadStripArguments
207209
case .library(.dynamic):
208210
args += ["-emit-library"]
209-
if self.buildParameters.targetTriple.isDarwin() {
211+
if triple.isDarwin() {
210212
let relativePath = try "@rpath/\(buildParameters.binaryRelativePath(for: self.product).pathString)"
211213
args += ["-Xlinker", "-install_name", "-Xlinker", relativePath]
212214
}
@@ -215,9 +217,9 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
215217
// Link the Swift stdlib statically, if requested.
216218
// TODO: unify this logic with SwiftTargetBuildDescription.stdlibArguments
217219
if self.buildParameters.linkingParameters.shouldLinkStaticSwiftStdlib {
218-
if self.buildParameters.targetTriple.isDarwin() {
220+
if triple.isDarwin() {
219221
self.observabilityScope.emit(.swiftBackDeployError)
220-
} else if self.buildParameters.targetTriple.isSupportingStaticStdlib {
222+
} else if triple.isSupportingStaticStdlib {
221223
args += ["-static-stdlib"]
222224
isLinkingStaticStdlib = true
223225
}
@@ -260,9 +262,9 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
260262
// Set rpath such that dynamic libraries are looked up
261263
// adjacent to the product, unless overridden.
262264
if !self.buildParameters.linkingParameters.shouldDisableLocalRpath {
263-
if self.buildParameters.targetTriple.isLinux() {
265+
if triple.isLinux() {
264266
args += ["-Xlinker", "-rpath=$ORIGIN"]
265-
} else if self.buildParameters.targetTriple.isDarwin() {
267+
} else if triple.isDarwin() {
266268
let rpath = self.product.type == .test ? "@loader_path/../../../" : "@loader_path"
267269
args += ["-Xlinker", "-rpath", "-Xlinker", rpath]
268270
}
@@ -283,8 +285,9 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
283285

284286
// When deploying to macOS prior to macOS 12, add an rpath to the
285287
// back-deployed concurrency libraries.
286-
if useStdlibRpath, self.buildParameters.targetTriple.isMacOSX {
288+
if useStdlibRpath, triple.isMacOSX {
287289
let macOSSupportedPlatform = self.package.getSupportedPlatform(for: .macOS, usingXCTest: product.isLinkingXCTest)
290+
288291
if macOSSupportedPlatform.version.major < 12 {
289292
let backDeployedStdlib = try buildParameters.toolchain.macosSwiftStdlib
290293
.parentDirectory
@@ -325,6 +328,16 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
325328
// User arguments (from -Xlinker) should follow generated arguments to allow user overrides
326329
args += self.buildParameters.flags.linkerFlags.asSwiftcLinkerFlags()
327330

331+
// Enable the correct lto mode if requested.
332+
switch self.buildParameters.linkingParameters.linkTimeOptimizationMode {
333+
case nil:
334+
break
335+
case .full:
336+
args += ["-lto=llvm-full"]
337+
case .thin:
338+
args += ["-lto=llvm-thin"]
339+
}
340+
328341
// Pass default library paths from the toolchain.
329342
for librarySearchPath in self.buildParameters.toolchain.librarySearchPaths {
330343
args += ["-L", librarySearchPath.pathString]
@@ -351,7 +364,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
351364
flags += libraries.map { "-l" + $0 }
352365

353366
// Linked frameworks.
354-
if self.buildParameters.targetTriple.supportsFrameworks {
367+
if self.buildParameters.triple.supportsFrameworks {
355368
let frameworks = OrderedSet(self.staticTargets.reduce([]) {
356369
$0 + self.buildParameters.createScope(for: $1).evaluate(.LINK_FRAMEWORKS)
357370
})

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,16 @@ public final class SwiftTargetBuildDescription {
105105
}
106106
}
107107

108+
var modulesPath: AbsolutePath {
109+
return self.buildParameters.buildPath.appending(component: "Modules")
110+
}
111+
108112
/// The path to the swiftmodule file after compilation.
109-
var moduleOutputPath: AbsolutePath {
113+
public var moduleOutputPath: AbsolutePath { // note: needs to be public because of sourcekit-lsp
110114
// If we're an executable and we're not allowing test targets to link against us, we hide the module.
111-
let allowLinkingAgainstExecutables = (buildParameters.targetTriple.isDarwin() || self.buildParameters.targetTriple
112-
.isLinux() || self.buildParameters.targetTriple.isWindows()) && self.toolsVersion >= .v5_5
113-
let dirPath = (target.type == .executable && !allowLinkingAgainstExecutables) ? self.tempsPath : self
114-
.buildParameters.buildPath
115+
let triple = buildParameters.triple
116+
let allowLinkingAgainstExecutables = (triple.isDarwin() || triple.isLinux() || triple.isWindows()) && self.toolsVersion >= .v5_5
117+
let dirPath = (target.type == .executable && !allowLinkingAgainstExecutables) ? self.tempsPath : self.modulesPath
115118
return dirPath.appending(component: self.target.c99name + ".swiftmodule")
116119
}
117120

@@ -124,7 +127,7 @@ public final class SwiftTargetBuildDescription {
124127

125128
/// The path to the swifinterface file after compilation.
126129
var parseableModuleInterfaceOutputPath: AbsolutePath {
127-
self.buildParameters.buildPath.appending(component: self.target.c99name + ".swiftinterface")
130+
self.modulesPath.appending(component: self.target.c99name + ".swiftinterface")
128131
}
129132

130133
/// Path to the resource Info.plist file, if generated.
@@ -320,7 +323,7 @@ public final class SwiftTargetBuildDescription {
320323
return
321324
}
322325

323-
guard buildParameters.targetTriple.isDarwin(), buildParameters.testingParameters.experimentalTestOutput else {
326+
guard self.buildParameters.triple.isDarwin(), self.buildParameters.testingParameters.experimentalTestOutput else {
324327
return
325328
}
326329

@@ -364,7 +367,7 @@ public final class SwiftTargetBuildDescription {
364367
guard let bundlePath else { return }
365368

366369
let mainPathSubstitution: String
367-
if self.buildParameters.targetTriple.isWASI() {
370+
if self.buildParameters.triple.isWASI() {
368371
// We prefer compile-time evaluation of the bundle path here for WASI. There's no benefit in evaluating this
369372
// at runtime, especially as `Bundle` support in WASI Foundation is partial. We expect all resource paths to
370373
// evaluate to `/\(resourceBundleName)/\(resourcePath)`, which allows us to pass this path to JS APIs like
@@ -641,15 +644,15 @@ public final class SwiftTargetBuildDescription {
641644
result.append(contentsOf: self.sources.map(\.pathString))
642645

643646
result.append("-I")
644-
result.append(self.buildParameters.buildPath.pathString)
647+
result.append(self.modulesPath.pathString)
645648

646649
result += try self.compileArguments()
647650
return result
648651
}
649652

650653
/// Returns true if ObjC compatibility header should be emitted.
651654
private var shouldEmitObjCCompatibilityHeader: Bool {
652-
self.buildParameters.targetTriple.isDarwin() && self.target.type == .library
655+
self.buildParameters.triple.isDarwin() && self.target.type == .library
653656
}
654657

655658
func writeOutputFileMap() throws -> AbsolutePath {
@@ -852,7 +855,7 @@ public final class SwiftTargetBuildDescription {
852855
var arguments: [String] = []
853856

854857
let isLinkingStaticStdlib = self.buildParameters.linkingParameters.shouldLinkStaticSwiftStdlib
855-
&& self.buildParameters.targetTriple.isSupportingStaticStdlib
858+
&& self.buildParameters.triple.isSupportingStaticStdlib
856859
if isLinkingStaticStdlib {
857860
arguments += ["-static-stdlib"]
858861
}

Sources/Build/BuildDescription/TargetBuildDescription.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Basics
1414
import struct PackageGraph.ResolvedTarget
1515
import struct PackageModel.Resource
1616
import struct SPMBuildCore.BuildToolPluginInvocationResult
17+
import struct SPMBuildCore.BuildParameters
1718

1819
/// A target description which can either be for a Swift or Clang target.
1920
public enum TargetBuildDescription {
@@ -91,4 +92,13 @@ public enum TargetBuildDescription {
9192
return target.buildToolPluginInvocationResults
9293
}
9394
}
95+
96+
var buildParameters: BuildParameters {
97+
switch self {
98+
case .swift(let swiftTargetBuildDescription):
99+
return swiftTargetBuildDescription.buildParameters
100+
case .clang(let clangTargetBuildDescription):
101+
return clangTargetBuildDescription.buildParameters
102+
}
103+
}
94104
}

Sources/Build/BuildManifest/LLBuildManifestBuilder+Clang.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension LLBuildManifestBuilder {
4242
}
4343
}
4444

45-
for dependency in target.target.dependencies(satisfying: self.buildEnvironment) {
45+
for dependency in target.target.dependencies(satisfying: target.buildEnvironment) {
4646
switch dependency {
4747
case .target(let target, _):
4848
addStaticTargetInputs(target)
@@ -68,7 +68,7 @@ extension LLBuildManifestBuilder {
6868
}
6969

7070
for binaryPath in target.libraryBinaryPaths {
71-
let path = destinationPath(forBinaryAt: binaryPath)
71+
let path = target.buildParameters.destinationPath(forBinaryAt: binaryPath)
7272
if self.fileSystem.isDirectory(binaryPath) {
7373
inputs.append(directory: path)
7474
} else {
@@ -97,7 +97,7 @@ extension LLBuildManifestBuilder {
9797

9898
args += ["-c", path.source.pathString, "-o", path.object.pathString]
9999

100-
let clangCompiler = try buildParameters.toolchain.getClangCompiler().pathString
100+
let clangCompiler = try target.buildParameters.toolchain.getClangCompiler().pathString
101101
args.insert(clangCompiler, at: 0)
102102

103103
let objectFileNode: Node = .file(path.object)
@@ -116,7 +116,7 @@ extension LLBuildManifestBuilder {
116116
try addBuildToolPlugins(.clang(target))
117117

118118
// Create a phony node to represent the entire target.
119-
let targetName = target.target.getLLBuildTargetName(config: self.buildConfig)
119+
let targetName = target.target.getLLBuildTargetName(config: target.buildParameters.buildConfig)
120120
let output: Node = .virtual(targetName)
121121

122122
self.manifest.addNode(output, toTarget: targetName)
@@ -126,7 +126,7 @@ extension LLBuildManifestBuilder {
126126
outputs: [output]
127127
)
128128

129-
if self.plan.graph.isInRootPackages(target.target, satisfying: self.buildEnvironment) {
129+
if self.plan.graph.isInRootPackages(target.target, satisfying: target.buildParameters.buildEnvironment) {
130130
if !target.isTestTarget {
131131
self.addNode(output, toTarget: .main)
132132
}

Sources/Build/BuildManifest/LLBuildManifestBuilder+Product.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import struct LLBuildManifest.Node
1515

1616
extension LLBuildManifestBuilder {
1717
func createProductCommand(_ buildProduct: ProductBuildDescription) throws {
18-
let cmdName = try buildProduct.product.getCommandName(config: self.buildConfig)
18+
let cmdName = try buildProduct.product.getCommandName(config: buildProduct.buildParameters.buildConfig)
1919

2020
// Add dependency on Info.plist generation on Darwin platforms.
2121
let testInputs: [AbsolutePath]
2222
if buildProduct.product.type == .test
23-
&& buildProduct.buildParameters.targetTriple.isDarwin()
23+
&& buildProduct.buildParameters.triple.isDarwin()
2424
&& buildProduct.buildParameters.testingParameters.experimentalTestOutput {
2525
let testBundleInfoPlistPath = try buildProduct.binaryPath.parentDirectory.parentDirectory.appending(component: "Info.plist")
2626
testInputs = [testBundleInfoPlistPath]
@@ -34,7 +34,7 @@ extension LLBuildManifestBuilder {
3434
}
3535

3636
// Create a phony node to represent the entire target.
37-
let targetName = try buildProduct.product.getLLBuildTargetName(config: self.buildConfig)
37+
let targetName = try buildProduct.product.getLLBuildTargetName(config: buildProduct.buildParameters.buildConfig)
3838
let output: Node = .virtual(targetName)
3939

4040
let finalProductNode: Node
@@ -59,8 +59,8 @@ extension LLBuildManifestBuilder {
5959
let linkedBinaryNode: Node
6060
let linkedBinaryPath = try buildProduct.binaryPath
6161
if case .executable = buildProduct.product.type,
62-
buildParameters.targetTriple.isMacOSX,
63-
buildParameters.debuggingParameters.shouldEnableDebuggingEntitlement {
62+
buildProduct.buildParameters.triple.isMacOSX,
63+
buildProduct.buildParameters.debuggingParameters.shouldEnableDebuggingEntitlement {
6464
shouldCodeSign = true
6565
linkedBinaryNode = try .file(buildProduct.binaryPath, isMutated: true)
6666
} else {
@@ -85,7 +85,7 @@ extension LLBuildManifestBuilder {
8585
outputPath: plistPath
8686
)
8787

88-
let cmdName = try buildProduct.product.getCommandName(config: self.buildConfig)
88+
let cmdName = try buildProduct.product.getCommandName(config: buildProduct.buildParameters.buildConfig)
8989
let codeSigningOutput = Node.virtual(targetName + "-CodeSigning")
9090
try self.manifest.addShellCmd(
9191
name: "\(cmdName)-entitlements",

Sources/Build/BuildManifest/LLBuildManifestBuilder+Resources.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension LLBuildManifestBuilder {
4545
outputs.append(output)
4646
}
4747

48-
let cmdName = target.target.getLLBuildResourcesCmdName(config: self.buildConfig)
48+
let cmdName = target.target.getLLBuildResourcesCmdName(config: target.buildParameters.buildConfig)
4949
self.manifest.addPhonyCmd(name: cmdName, inputs: outputs, outputs: [.virtual(cmdName)])
5050

5151
return .virtual(cmdName)

0 commit comments

Comments
 (0)