Skip to content

Add support for testable executables on Windows #8515

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 1 commit into from
Apr 23, 2025
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
20 changes: 20 additions & 0 deletions Sources/Build/BuildPlan/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,32 @@ extension BuildParameters {
args = ["-alias", "_\(target.c99name)_main", "_main"]
case .elf:
args = ["--defsym", "main=\(target.c99name)_main"]
case .coff:
// If the user is specifying a custom entry point name that isn't "main", assume they may be setting WinMain or wWinMain
// and don't do any modifications ourselves. In that case the linker will infer the WINDOWS subsystem and call WinMainCRTStartup,
// which will then call the custom entry point. And WinMain/wWinMain != main, so this still won't run into duplicate symbol
// issues when called from a test target, which always uses main.
if let customEntryPointFunctionName = findCustomEntryPointFunctionName(of: target), customEntryPointFunctionName != "main" {
return nil
}
args = ["/ALTERNATENAME:main=\(target.c99name)_main", "/SUBSYSTEM:CONSOLE"]
default:
return nil
}
return args.asSwiftcLinkerFlags()
}

private func findCustomEntryPointFunctionName(of target: ResolvedModule) -> String? {
let flags = createScope(for: target).evaluate(.OTHER_SWIFT_FLAGS)
var it = flags.makeIterator()
while let value = it.next() {
if value == "-Xfrontend" && it.next() == "-entry-point-function-name" && it.next() == "-Xfrontend" {
return it.next()
}
}
return nil
}

/// Returns the scoped view of build settings for a given target.
func createScope(for target: ResolvedModule) -> BuildSettings.Scope {
BuildSettings.Scope(target.underlying.buildSettings, environment: buildEnvironment)
Expand Down
4 changes: 2 additions & 2 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3875,7 +3875,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
observabilityScope: observability.topScope
))
}
let supportingTriples: [Basics.Triple] = [.x86_64Linux, .x86_64MacOS]
let supportingTriples: [Basics.Triple] = [.x86_64Linux, .x86_64MacOS, .x86_64Windows]
for triple in supportingTriples {
let result = try await createResult(for: triple)
let exe = try result.moduleBuildDescription(for: "exe").swift().compileArguments()
Expand All @@ -3884,7 +3884,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
XCTAssertMatch(linkExe, [.contains("exe_main")])
}

let unsupportingTriples: [Basics.Triple] = [.wasi, .windows]
let unsupportingTriples: [Basics.Triple] = [.wasi]
for triple in unsupportingTriples {
let result = try await createResult(for: triple)
let exe = try result.moduleBuildDescription(for: "exe").swift().compileArguments()
Expand Down