Skip to content

Commit f44a2cd

Browse files
committed
Fix for linking tests with swiftbuild on windows
- need library search path to ExecutableModules for windows
1 parent 84b3e6e commit f44a2cd

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// swift-tools-version: 6.2
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "BasicExecutable",
7+
targets: [
8+
.executableTarget(
9+
name: "Executable",
10+
),
11+
]
12+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
@main
5+
struct BasicExecutable {
6+
static func main() {
7+
print("Hello, world!")
8+
}
9+
}

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Modules.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ extension PackagePIFProjectBuilder {
440440
// Redirect the built executable into a separate directory so it won't conflict with the real one.
441441
settings[.TARGET_BUILD_DIR] = "$(TARGET_BUILD_DIR)/ExecutableModules"
442442

443+
// on windows modules are libraries, so we need to add a search path so the linker finds them
444+
impartedSettings[.LIBRARY_SEARCH_PATHS, .windows] = ["$(inherited)", "$(TARGET_BUILD_DIR)/ExecutableModules"]
445+
443446
// Don't install the Swift module of the testable side-built artifact, lest it conflict with the regular
444447
// one.
445448
// The modules should have compatible contents in any case — only the entry point function name is

Tests/SwiftBuildSupportTests/PIFBuilderTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ extension SwiftBuild.ProjectModel.BaseTarget {
150150

151151
@Suite
152152
struct PIFBuilderTests {
153+
154+
@Test func platformExecutableModuleLibrarySearchPath() async throws {
155+
try await withGeneratedPIF(fromFixture: "PIFBuilder/BasicExecutable") { pif, observabilitySystem in
156+
let releaseConfig = try pif.workspace
157+
.project(named: "BasicExecutable")
158+
.target(named: "Executable")
159+
.buildConfig(named: "Release")
160+
161+
for platform in ProjectModel.BuildSettings.Platform.allCases {
162+
let search_paths = releaseConfig.impartedBuildProperties.settings[.LIBRARY_SEARCH_PATHS, platform]
163+
switch platform {
164+
case .macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .xrOS, .driverKit, .freebsd, .android, .linux, .wasi, .openbsd, ._iOSDevice:
165+
#expect(search_paths == nil, "for platform \(platform)")
166+
case .windows:
167+
#expect(search_paths == ["$(inherited)", "$(TARGET_BUILD_DIR)/ExecutableModules"], "for platform \(platform)")
168+
}
169+
}
170+
}
171+
}
172+
153173
@Test func platformConditionBasics() async throws {
154174
try await withGeneratedPIF(fromFixture: "PIFBuilder/UnknownPlatforms") { pif, observabilitySystem in
155175
// We should emit a warning to the PIF log about the unknown platform
@@ -182,7 +202,7 @@ struct PIFBuilderTests {
182202
case .macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .xrOS, .driverKit, .freebsd:
183203
#expect(ld_flags == ["-lc++", "$(inherited)"], "for platform \(platform)")
184204
case .android, .linux, .wasi, .openbsd:
185-
#expect(ld_flags == ["-lstdc++", "$(inherited)"], "for platform \(platform)")
205+
#expect(ld_flags == ["-lstdc++", "$(inherited)"], "for platform \(platform)")
186206
case .windows, ._iOSDevice:
187207
#expect(ld_flags == nil, "for platform \(platform)")
188208
}

0 commit comments

Comments
 (0)