Skip to content

[5.5] SwiftPM complains about @main in non main.swift file when integrated driver is not enabled #3575

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
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
14 changes: 8 additions & 6 deletions Fixtures/Miscellaneous/AtMainSupport/Package.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// swift-tools-version:5.4
// swift-tools-version:5.5
import PackageDescription

let package = Package(
name: "AtMainSupport",
products: [
.executable(name: "ClangExec", targets: ["ClangExec"]),
.executable(name: "SwiftExec", targets: ["SwiftExec"]),
.executable(name: "ClangExecSingleFile", targets: ["ClangExecSingleFile"]),
.executable(name: "SwiftExecSingleFile", targets: ["SwiftExecSingleFile"]),
.executable(name: "SwiftExecMultiFile", targets: ["SwiftExecMultiFile"]),
],
targets: [
.executableTarget(name: "ClangExec"),
.executableTarget(name: "SwiftExec"),
.executableTarget(name: "ClangExecSingleFile"),
.executableTarget(name: "SwiftExecSingleFile"),
.executableTarget(name: "SwiftExecMultiFile"),
]
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@main
struct MyProgram {
static func main() {
print("Hello, Swift.")
}
}
16 changes: 5 additions & 11 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@ public final class SwiftTargetBuildDescription {
}
}
}

// If the target needs to be parsed without any special semantics involving "main.swift", do so now.
if self.needsToBeParsedAsLibrary {
args += ["-parse-as-library"]
}

// Only add the build path to the framework search path if there are binary frameworks to link against.
if !libraryBinaryPaths.isEmpty {
Expand Down Expand Up @@ -809,10 +814,6 @@ public final class SwiftTargetBuildDescription {
// FIXME: Eliminate side effect.
result.append(try writeOutputFileMap().pathString)

if self.needsToBeParsedAsLibrary {
result.append("-parse-as-library")
}

if buildParameters.useWholeModuleOptimization {
result.append("-whole-module-optimization")
result.append("-num-threads")
Expand Down Expand Up @@ -849,10 +850,6 @@ public final class SwiftTargetBuildDescription {
result.append("-experimental-skip-non-inlinable-function-bodies")
result.append("-force-single-frontend-invocation")

if self.needsToBeParsedAsLibrary {
result.append("-parse-as-library")
}

// FIXME: Handle WMO

for source in target.sources.paths {
Expand Down Expand Up @@ -896,9 +893,6 @@ public final class SwiftTargetBuildDescription {
// FIXME: Eliminate side effect.
result.append(try writeOutputFileMap().pathString)

if self.needsToBeParsedAsLibrary {
result.append("-parse-as-library")
}
// FIXME: Handle WMO

result.append("-c")
Expand Down
15 changes: 11 additions & 4 deletions Tests/CommandsTests/BuildToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,22 @@ final class BuildToolTests: XCTestCase {
fixture(name: "Miscellaneous/AtMainSupport") { path in
let fullPath = resolveSymlinks(path)
do {
let result = try build(["--product", "ClangExec"], packagePath: fullPath)
XCTAssert(result.binContents.contains("ClangExec"))
let result = try build(["--product", "ClangExecSingleFile"], packagePath: fullPath)
XCTAssert(result.binContents.contains("ClangExecSingleFile"))
} catch SwiftPMProductError.executionFailure(_, let stdout, let stderr) {
XCTFail(stdout + "\n" + stderr)
}

do {
let result = try build(["--product", "SwiftExec"], packagePath: fullPath)
XCTAssert(result.binContents.contains("SwiftExec"))
let result = try build(["--product", "SwiftExecSingleFile"], packagePath: fullPath)
XCTAssert(result.binContents.contains("SwiftExecSingleFile"))
} catch SwiftPMProductError.executionFailure(_, let stdout, let stderr) {
XCTFail(stdout + "\n" + stderr)
}

do {
let result = try build(["--product", "SwiftExecMultiFile"], packagePath: fullPath)
XCTAssert(result.binContents.contains("SwiftExecMultiFile"))
} catch SwiftPMProductError.executionFailure(_, let stdout, let stderr) {
XCTFail(stdout + "\n" + stderr)
}
Expand Down