Skip to content

Commit 95b91f3

Browse files
authored
Ensure prebuilt Modules include path added to generated modules (#8485)
1 parent 01e7c31 commit 95b91f3

File tree

3 files changed

+133
-5
lines changed

3 files changed

+133
-5
lines changed

Sources/Build/BuildPlan/BuildPlan+Test.swift

+32-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import struct PackageGraph.ResolvedProduct
2727
import struct PackageGraph.ResolvedModule
2828

2929
import struct PackageModel.Sources
30+
import enum PackageModel.BuildSettings
3031
import class PackageModel.SwiftModule
3132
import class PackageModel.Module
3233
import struct SPMBuildCore.BuildParameters
@@ -80,18 +81,30 @@ extension BuildPlan {
8081
let discoveryMainFile = discoveryDerivedDir.appending(component: TestDiscoveryTool.mainFileName)
8182

8283
var discoveryPaths: [AbsolutePath] = []
84+
var discoveryBuildSettings: BuildSettings.AssignmentTable = .init()
8385
discoveryPaths.append(discoveryMainFile)
8486
for testTarget in testProduct.modules {
8587
let path = discoveryDerivedDir.appending(components: testTarget.name + ".swift")
8688
discoveryPaths.append(path)
89+
// Add in the include path from the test targets to ensure this module builds
90+
if let flags = testTarget.underlying.buildSettings.assignments[.OTHER_SWIFT_FLAGS] {
91+
for assignment in flags {
92+
let values = assignment.values.filter({ $0.hasPrefix("-I") })
93+
if !values.isEmpty {
94+
discoveryBuildSettings.add(.init(values: values, conditions: []), for: .OTHER_SWIFT_FLAGS)
95+
}
96+
}
97+
}
8798
}
8899

89100
let discoveryTarget = SwiftModule(
90101
name: discoveryTargetName,
91102
dependencies: testProduct.underlying.modules.map { .module($0, conditions: []) },
92103
packageAccess: true, // test target is allowed access to package decls by default
93-
testDiscoverySrc: Sources(paths: discoveryPaths, root: discoveryDerivedDir)
104+
testDiscoverySrc: Sources(paths: discoveryPaths, root: discoveryDerivedDir),
105+
buildSettings: discoveryBuildSettings
94106
)
107+
95108
let discoveryResolvedModule = ResolvedModule(
96109
packageIdentity: testProduct.packageIdentity,
97110
underlying: discoveryTarget,
@@ -127,12 +140,26 @@ extension BuildPlan {
127140
let entryPointMainFile = entryPointDerivedDir.appending(component: entryPointMainFileName)
128141
let entryPointSources = Sources(paths: [entryPointMainFile], root: entryPointDerivedDir)
129142

143+
var entryPointBuildSettings: BuildSettings.AssignmentTable = .init()
144+
for testTarget in testProduct.modules {
145+
// Add in the include path from the test targets to ensure this module builds
146+
if let flags = testTarget.underlying.buildSettings.assignments[.OTHER_SWIFT_FLAGS] {
147+
for assignment in flags {
148+
let values = assignment.values.filter({ $0.hasPrefix("-I") })
149+
if !values.isEmpty {
150+
entryPointBuildSettings.add(.init(values: values, conditions: []), for: .OTHER_SWIFT_FLAGS)
151+
}
152+
}
153+
}
154+
}
155+
130156
let entryPointTarget = SwiftModule(
131157
name: testProduct.name,
132158
type: .library,
133159
dependencies: testProduct.underlying.modules.map { .module($0, conditions: []) } + swiftTargetDependencies,
134160
packageAccess: true, // test target is allowed access to package decls
135-
testEntryPointSources: entryPointSources
161+
testEntryPointSources: entryPointSources,
162+
buildSettings: entryPointBuildSettings
136163
)
137164
let entryPointResolvedTarget = ResolvedModule(
138165
packageIdentity: testProduct.packageIdentity,
@@ -249,7 +276,8 @@ private extension PackageModel.SwiftModule {
249276
type: PackageModel.Module.Kind? = nil,
250277
dependencies: [PackageModel.Module.Dependency],
251278
packageAccess: Bool,
252-
testEntryPointSources sources: Sources
279+
testEntryPointSources sources: Sources,
280+
buildSettings: BuildSettings.AssignmentTable = .init()
253281
) {
254282
self.init(
255283
name: name,
@@ -258,6 +286,7 @@ private extension PackageModel.SwiftModule {
258286
sources: sources,
259287
dependencies: dependencies,
260288
packageAccess: packageAccess,
289+
buildSettings: buildSettings,
261290
usesUnsafeFlags: false
262291
)
263292
}

Sources/PackageModel/Module/SwiftModule.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ public final class SwiftModule: Module {
2828
[defaultTestEntryPointName, "LinuxMain.swift"]
2929
}
3030

31-
public init(name: String, dependencies: [Module.Dependency], packageAccess: Bool, testDiscoverySrc: Sources) {
31+
public init(
32+
name: String,
33+
dependencies: [Module.Dependency],
34+
packageAccess: Bool,
35+
testDiscoverySrc: Sources,
36+
buildSettings: BuildSettings.AssignmentTable = .init()) {
3237
self.declaredSwiftVersions = []
3338

3439
super.init(
@@ -38,7 +43,7 @@ public final class SwiftModule: Module {
3843
sources: testDiscoverySrc,
3944
dependencies: dependencies,
4045
packageAccess: packageAccess,
41-
buildSettings: .init(),
46+
buildSettings: buildSettings,
4247
buildSettingsDescription: [],
4348
pluginUsages: [],
4449
usesUnsafeFlags: false

Tests/BuildTests/BuildPlanTests.swift

+94
Original file line numberDiff line numberDiff line change
@@ -4571,6 +4571,100 @@ final class BuildPlanTests: XCTestCase {
45714571
}
45724572
}
45734573

4574+
func testPrebuiltsFlags() async throws {
4575+
// Make sure the include path for the prebuilts get passed to the
4576+
// generated test entry point and discover targets on Linux/Windows
4577+
let observability = ObservabilitySystem.makeForTesting()
4578+
4579+
let prebuiltLibrary = PrebuiltLibrary(
4580+
identity: .plain("swift-syntax"),
4581+
libraryName: "MacroSupport",
4582+
path: "/MyPackage/.build/prebuilts/swift-syntax/600.0.1/6.1-MacroSupport-macos_aarch64",
4583+
products: [
4584+
"SwiftBasicFormat",
4585+
"SwiftCompilerPlugin",
4586+
"SwiftDiagnostics",
4587+
"SwiftIDEUtils",
4588+
"SwiftOperators",
4589+
"SwiftParser",
4590+
"SwiftParserDiagnostics",
4591+
"SwiftRefactor",
4592+
"SwiftSyntax",
4593+
"SwiftSyntaxBuilder",
4594+
"SwiftSyntaxMacros",
4595+
"SwiftSyntaxMacroExpansion",
4596+
"SwiftSyntaxMacrosTestSupport",
4597+
"SwiftSyntaxMacrosGenericTestSupport",
4598+
"_SwiftCompilerPluginMessageHandling",
4599+
"_SwiftLibraryPluginProvider"
4600+
],
4601+
cModules: ["_SwiftSyntaxCShims"]
4602+
)
4603+
4604+
let fs = InMemoryFileSystem(
4605+
emptyFiles: [
4606+
"/MyPackage/Sources/MyMacroMacros/MyMacroMacros.swift",
4607+
"/MyPackage/Sources/MyMacros/MyMacros.swift",
4608+
"/MyPackage/Sources/MyMacroTests/MyMacroTests.swift"
4609+
]
4610+
)
4611+
4612+
let graph = try loadModulesGraph(
4613+
fileSystem: fs,
4614+
manifests: [
4615+
Manifest.createRootManifest(
4616+
displayName: "MyPackage",
4617+
path: "/MyPackage",
4618+
targets: [
4619+
TargetDescription(name: "MyMacroMacros", type: .macro),
4620+
TargetDescription(
4621+
name: "MyMacros",
4622+
dependencies: [
4623+
"MyMacroMacros",
4624+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
4625+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
4626+
]
4627+
),
4628+
TargetDescription(
4629+
name: "MyMacroTests",
4630+
dependencies: [
4631+
"MyMacroMacros",
4632+
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
4633+
],
4634+
type: .test
4635+
)
4636+
]
4637+
)
4638+
],
4639+
prebuilts: [prebuiltLibrary.identity: prebuiltLibrary.products.reduce(into: [:]) {
4640+
$0[$1] = prebuiltLibrary
4641+
}],
4642+
observabilityScope: observability.topScope
4643+
)
4644+
4645+
func checkTriple(triple: Basics.Triple) async throws {
4646+
let result = try await BuildPlanResult(
4647+
plan: mockBuildPlan(
4648+
triple: triple,
4649+
graph: graph,
4650+
fileSystem: fs,
4651+
observabilityScope: observability.topScope
4652+
)
4653+
)
4654+
4655+
let modulesDir = "-I\(prebuiltLibrary.path.pathString)/Modules"
4656+
let mytest = try XCTUnwrap(result.allTargets(named: "MyMacroTests").first)
4657+
XCTAssert(try mytest.swift().compileArguments().contains(modulesDir))
4658+
let entryPoint = try XCTUnwrap(result.allTargets(named: "MyPackagePackageTests").first)
4659+
XCTAssert(try entryPoint.swift().compileArguments().contains(modulesDir))
4660+
let discovery = try XCTUnwrap(result.allTargets(named: "MyPackagePackageDiscoveredTests").first)
4661+
XCTAssert(try discovery.swift().compileArguments().contains(modulesDir))
4662+
}
4663+
4664+
try await checkTriple(triple: .x86_64Linux)
4665+
try await checkTriple(triple: .x86_64Windows)
4666+
}
4667+
45744668
func testExtraBuildFlags() async throws {
45754669
let fs = InMemoryFileSystem(
45764670
emptyFiles:

0 commit comments

Comments
 (0)