Skip to content

[6.1.1] Ensure prebuilt Modules include path added to generated modules (#8485) #8526

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
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
2 changes: 1 addition & 1 deletion IntegrationTests/Tests/IntegrationTests/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ let lldb: AbsolutePath = {
}()

let swiftpmBinaryDirectory: AbsolutePath = {
if let environmentPath = ProcessInfo.processInfo.environment["SWIFTPM_BIN_DIR"] {
if let environmentPath = ProcessInfo.processInfo.environment["SWIFTPM_CUSTOM_BIN_DIR"] {
return try! AbsolutePath(validating: environmentPath)
}

Expand Down
35 changes: 32 additions & 3 deletions Sources/Build/BuildPlan/BuildPlan+Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import struct PackageGraph.ResolvedProduct
import struct PackageGraph.ResolvedModule

import struct PackageModel.Sources
import enum PackageModel.BuildSettings
import class PackageModel.SwiftModule
import class PackageModel.Module
import struct SPMBuildCore.BuildParameters
Expand Down Expand Up @@ -80,18 +81,30 @@ extension BuildPlan {
let discoveryMainFile = discoveryDerivedDir.appending(component: TestDiscoveryTool.mainFileName)

var discoveryPaths: [AbsolutePath] = []
var discoveryBuildSettings: BuildSettings.AssignmentTable = .init()
discoveryPaths.append(discoveryMainFile)
for testTarget in testProduct.modules {
let path = discoveryDerivedDir.appending(components: testTarget.name + ".swift")
discoveryPaths.append(path)
// Add in the include path from the test targets to ensure this module builds
if let flags = testTarget.underlying.buildSettings.assignments[.OTHER_SWIFT_FLAGS] {
for assignment in flags {
let values = assignment.values.filter({ $0.hasPrefix("-I") })
if !values.isEmpty {
discoveryBuildSettings.add(.init(values: values, conditions: []), for: .OTHER_SWIFT_FLAGS)
}
}
}
}

let discoveryTarget = SwiftModule(
name: discoveryTargetName,
dependencies: testProduct.underlying.modules.map { .module($0, conditions: []) },
packageAccess: true, // test target is allowed access to package decls by default
testDiscoverySrc: Sources(paths: discoveryPaths, root: discoveryDerivedDir)
testDiscoverySrc: Sources(paths: discoveryPaths, root: discoveryDerivedDir),
buildSettings: discoveryBuildSettings
)

let discoveryResolvedModule = ResolvedModule(
packageIdentity: testProduct.packageIdentity,
underlying: discoveryTarget,
Expand Down Expand Up @@ -127,12 +140,26 @@ extension BuildPlan {
let entryPointMainFile = entryPointDerivedDir.appending(component: entryPointMainFileName)
let entryPointSources = Sources(paths: [entryPointMainFile], root: entryPointDerivedDir)

var entryPointBuildSettings: BuildSettings.AssignmentTable = .init()
for testTarget in testProduct.modules {
// Add in the include path from the test targets to ensure this module builds
if let flags = testTarget.underlying.buildSettings.assignments[.OTHER_SWIFT_FLAGS] {
for assignment in flags {
let values = assignment.values.filter({ $0.hasPrefix("-I") })
if !values.isEmpty {
entryPointBuildSettings.add(.init(values: values, conditions: []), for: .OTHER_SWIFT_FLAGS)
}
}
}
}

let entryPointTarget = SwiftModule(
name: testProduct.name,
type: .library,
dependencies: testProduct.underlying.modules.map { .module($0, conditions: []) } + swiftTargetDependencies,
packageAccess: true, // test target is allowed access to package decls
testEntryPointSources: entryPointSources
testEntryPointSources: entryPointSources,
buildSettings: entryPointBuildSettings
)
let entryPointResolvedTarget = ResolvedModule(
packageIdentity: testProduct.packageIdentity,
Expand Down Expand Up @@ -249,7 +276,8 @@ private extension PackageModel.SwiftModule {
type: PackageModel.Module.Kind? = nil,
dependencies: [PackageModel.Module.Dependency],
packageAccess: Bool,
testEntryPointSources sources: Sources
testEntryPointSources sources: Sources,
buildSettings: BuildSettings.AssignmentTable = .init()
) {
self.init(
name: name,
Expand All @@ -258,6 +286,7 @@ private extension PackageModel.SwiftModule {
sources: sources,
dependencies: dependencies,
packageAccess: packageAccess,
buildSettings: buildSettings,
usesUnsafeFlags: false
)
}
Expand Down
9 changes: 7 additions & 2 deletions Sources/PackageModel/Module/SwiftModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public final class SwiftModule: Module {
[defaultTestEntryPointName, "LinuxMain.swift"]
}

public init(name: String, dependencies: [Module.Dependency], packageAccess: Bool, testDiscoverySrc: Sources) {
public init(
name: String,
dependencies: [Module.Dependency],
packageAccess: Bool,
testDiscoverySrc: Sources,
buildSettings: BuildSettings.AssignmentTable = .init()) {
self.declaredSwiftVersions = []

super.init(
Expand All @@ -38,7 +43,7 @@ public final class SwiftModule: Module {
sources: testDiscoverySrc,
dependencies: dependencies,
packageAccess: packageAccess,
buildSettings: .init(),
buildSettings: buildSettings,
buildSettingsDescription: [],
pluginUsages: [],
usesUnsafeFlags: false
Expand Down
94 changes: 94 additions & 0 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4571,6 +4571,100 @@ final class BuildPlanTests: XCTestCase {
}
}

func testPrebuiltsFlags() async throws {
// Make sure the include path for the prebuilts get passed to the
// generated test entry point and discover targets on Linux/Windows
let observability = ObservabilitySystem.makeForTesting()

let prebuiltLibrary = PrebuiltLibrary(
identity: .plain("swift-syntax"),
libraryName: "MacroSupport",
path: "/MyPackage/.build/prebuilts/swift-syntax/600.0.1/6.1-MacroSupport-macos_aarch64",
products: [
"SwiftBasicFormat",
"SwiftCompilerPlugin",
"SwiftDiagnostics",
"SwiftIDEUtils",
"SwiftOperators",
"SwiftParser",
"SwiftParserDiagnostics",
"SwiftRefactor",
"SwiftSyntax",
"SwiftSyntaxBuilder",
"SwiftSyntaxMacros",
"SwiftSyntaxMacroExpansion",
"SwiftSyntaxMacrosTestSupport",
"SwiftSyntaxMacrosGenericTestSupport",
"_SwiftCompilerPluginMessageHandling",
"_SwiftLibraryPluginProvider"
],
cModules: ["_SwiftSyntaxCShims"]
)

let fs = InMemoryFileSystem(
emptyFiles: [
"/MyPackage/Sources/MyMacroMacros/MyMacroMacros.swift",
"/MyPackage/Sources/MyMacros/MyMacros.swift",
"/MyPackage/Sources/MyMacroTests/MyMacroTests.swift"
]
)

let graph = try loadModulesGraph(
fileSystem: fs,
manifests: [
Manifest.createRootManifest(
displayName: "MyPackage",
path: "/MyPackage",
targets: [
TargetDescription(name: "MyMacroMacros", type: .macro),
TargetDescription(
name: "MyMacros",
dependencies: [
"MyMacroMacros",
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
TargetDescription(
name: "MyMacroTests",
dependencies: [
"MyMacroMacros",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
],
type: .test
)
]
)
],
prebuilts: [prebuiltLibrary.identity: prebuiltLibrary.products.reduce(into: [:]) {
$0[$1] = prebuiltLibrary
}],
observabilityScope: observability.topScope
)

func checkTriple(triple: Basics.Triple) async throws {
let result = try await BuildPlanResult(
plan: mockBuildPlan(
triple: triple,
graph: graph,
fileSystem: fs,
observabilityScope: observability.topScope
)
)

let modulesDir = "-I\(prebuiltLibrary.path.pathString)/Modules"
let mytest = try XCTUnwrap(result.allTargets(named: "MyMacroTests").first)
XCTAssert(try mytest.swift().compileArguments().contains(modulesDir))
let entryPoint = try XCTUnwrap(result.allTargets(named: "MyPackagePackageTests").first)
XCTAssert(try entryPoint.swift().compileArguments().contains(modulesDir))
let discovery = try XCTUnwrap(result.allTargets(named: "MyPackagePackageDiscoveredTests").first)
XCTAssert(try discovery.swift().compileArguments().contains(modulesDir))
}

try await checkTriple(triple: .x86_64Linux)
try await checkTriple(triple: .x86_64Windows)
}

func testExtraBuildFlags() async throws {
let fs = InMemoryFileSystem(
emptyFiles:
Expand Down
Loading