Skip to content

Commit f3a36f6

Browse files
committed
[Swift Build] Correct -parse-as-library behavior for executable targets
1 parent a9e72ad commit f3a36f6

File tree

16 files changed

+115
-43
lines changed

16 files changed

+115
-43
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// swift-tools-version: 6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "ParseAsLibrary",
7+
products: [],
8+
targets: [
9+
.executableTarget(name: "ExecutableTargetOneFileNamedMainMainAttr"),
10+
.executableTarget(name: "ExecutableTargetOneFileNamedMainNoMainAttr"),
11+
.executableTarget(name: "ExecutableTargetOneFileNotNamedMainMainAttr"),
12+
.executableTarget(name: "ExecutableTargetOneFileNotNamedMainNoMainAttr"),
13+
.executableTarget(name: "ExecutableTargetTwoFiles"),
14+
]
15+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@main struct Entry {
2+
static func main() {}
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(42)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@main struct Entry {
2+
static func main() {}
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(42)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@main struct Entry {
2+
static func main() {}
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
func foo() {}

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ let package = Package(
534534
.target(
535535
name: "SwiftBuildSupport",
536536
dependencies: [
537-
"Build",
538537
"SPMBuildCore",
539538
"PackageGraph",
540539
],

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,6 @@ import struct TSCBasic.ByteString
3434
@available(*, deprecated, renamed: "SwiftModuleBuildDescription")
3535
public typealias SwiftTargetBuildDescription = SwiftModuleBuildDescription
3636

37-
// looking into the file content to see if it is using the @main annotation
38-
// this is not bullet-proof since theoretically the file can contain the @main string for other reasons
39-
// but it is the closest to accurate we can do at this point
40-
package func containsAtMain(fileSystem: FileSystem, path: AbsolutePath) throws -> Bool {
41-
let content: String = try fileSystem.readFileContents(path)
42-
let lines = content.split(whereSeparator: { $0.isNewline }).map { $0.trimmingCharacters(in: .whitespaces) }
43-
44-
var multilineComment = false
45-
for line in lines {
46-
if line.hasPrefix("//") {
47-
continue
48-
}
49-
if line.hasPrefix("/*") {
50-
multilineComment = true
51-
}
52-
if line.hasSuffix("*/") {
53-
multilineComment = false
54-
}
55-
if multilineComment {
56-
continue
57-
}
58-
if line.hasPrefix("@main") {
59-
return true
60-
}
61-
}
62-
return false
63-
}
64-
6537
/// Build description for a Swift module.
6638
public final class SwiftModuleBuildDescription {
6739
/// The package this target belongs to.

Sources/SPMBuildCore/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_library(SPMBuildCore
2626
Plugins/PluginMessages.swift
2727
Plugins/PluginScriptRunner.swift
2828
CommandPluginResult.swift
29+
MainAttrDetection.swift
2930
ResolvedPackage+Extensions.swift
3031
Triple+Extensions.swift
3132
XCFrameworkMetadata.swift

0 commit comments

Comments
 (0)