Skip to content

Stop renaming main symbol for WASI #3804

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 4 commits into from
Oct 18, 2021
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
15 changes: 9 additions & 6 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,16 @@ extension BuildParameters {

/// Computes the linker flags to use in order to rename a module-named main function to 'main' for the target platform, or nil if the linker doesn't support it for the platform.
fileprivate func linkerFlagsForRenamingMainFunction(of target: ResolvedTarget) -> [String]? {
var args: [String] = []
let args: [String]
if self.triple.isDarwin() {
args = ["-alias", "_\(target.c99name)_main", "_main"]
}
else if self.triple.isLinux() {
args = ["--defsym", "main=\(target.c99name)_main"]
}
else {
return nil
}
return args.flatMap { ["-Xlinker", $0] }
}

Expand Down Expand Up @@ -745,10 +748,9 @@ public final class SwiftTargetBuildDescription {
// we can rename the symbol unconditionally.
// No `-` for these flags because the set of Strings in driver.supportedFrontendFlags do
// not have a leading `-`
if SwiftTargetBuildDescription.checkSupportedFrontendFlags(flags: ["entry-point-function-name"], fileSystem: self.fileSystem) {
if buildParameters.linkerFlagsForRenamingMainFunction(of: target) != nil {
args += ["-Xfrontend", "-entry-point-function-name", "-Xfrontend", "\(target.c99name)_main"]
}
if buildParameters.canRenameEntrypointFunctionName,
buildParameters.linkerFlagsForRenamingMainFunction(of: target) != nil {
args += ["-Xfrontend", "-entry-point-function-name", "-Xfrontend", "\(target.c99name)_main"]
}
}

Expand Down Expand Up @@ -1228,7 +1230,8 @@ public final class ProductBuildDescription {
// we will instead have generated a source file containing the redirect.
// Support for linking tests againsts executables is conditional on the tools
// version of the package that defines the executable product.
if product.executableModule.underlyingTarget is SwiftTarget, toolsVersion >= .v5_5 {
if product.executableModule.underlyingTarget is SwiftTarget, toolsVersion >= .v5_5,
buildParameters.canRenameEntrypointFunctionName {
if let flags = buildParameters.linkerFlagsForRenamingMainFunction(of: product.executableModule) {
args += flags
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,9 @@ public class SwiftTool {
xcbuildFlags: options.xcbuildFlags,
jobs: options.jobs ?? UInt32(ProcessInfo.processInfo.activeProcessorCount),
shouldLinkStaticSwiftStdlib: options.shouldLinkStaticSwiftStdlib,
canRenameEntrypointFunctionName: SwiftTargetBuildDescription.checkSupportedFrontendFlags(
flags: ["entry-point-function-name"], fileSystem: localFileSystem
),
sanitizers: options.enabledSanitizers,
enableCodeCoverage: options.shouldEnableCodeCoverage,
indexStoreMode: options.indexStoreMode.indexStoreMode,
Expand Down
5 changes: 5 additions & 0 deletions Sources/SPMBuildCore/BuildParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public struct BuildParameters: Encodable {
/// Whether to create dylibs for dynamic library products.
public var shouldCreateDylibForDynamicProducts: Bool

/// Whether to enable the entry-point-function-name feature.
public var canRenameEntrypointFunctionName: Bool

/// The current build environment.
public var buildEnvironment: BuildEnvironment {
BuildEnvironment(platform: currentPlatform, configuration: configuration)
Expand Down Expand Up @@ -182,6 +185,7 @@ public struct BuildParameters: Encodable {
jobs: UInt32 = UInt32(ProcessInfo.processInfo.activeProcessorCount),
shouldLinkStaticSwiftStdlib: Bool = false,
shouldEnableManifestCaching: Bool = false,
canRenameEntrypointFunctionName: Bool = false,
shouldCreateDylibForDynamicProducts: Bool = true,
sanitizers: EnabledSanitizers = EnabledSanitizers(),
enableCodeCoverage: Bool = false,
Expand Down Expand Up @@ -211,6 +215,7 @@ public struct BuildParameters: Encodable {
self.shouldLinkStaticSwiftStdlib = shouldLinkStaticSwiftStdlib
self.shouldEnableManifestCaching = shouldEnableManifestCaching
self.shouldCreateDylibForDynamicProducts = shouldCreateDylibForDynamicProducts
self.canRenameEntrypointFunctionName = canRenameEntrypointFunctionName
self.sanitizers = sanitizers
self.enableCodeCoverage = enableCodeCoverage
self.indexStoreMode = indexStoreMode
Expand Down
47 changes: 47 additions & 0 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ final class BuildPlanTests: XCTestCase {
toolchain: SPMBuildCore.Toolchain = MockToolchain(),
flags: BuildFlags = BuildFlags(),
shouldLinkStaticSwiftStdlib: Bool = false,
canRenameEntrypointFunctionName: Bool = false,
destinationTriple: TSCUtility.Triple = hostTriple,
indexStoreMode: BuildParameters.IndexStoreMode = .off,
useExplicitModuleBuild: Bool = false
Expand All @@ -76,6 +77,7 @@ final class BuildPlanTests: XCTestCase {
flags: flags,
jobs: 3,
shouldLinkStaticSwiftStdlib: shouldLinkStaticSwiftStdlib,
canRenameEntrypointFunctionName: canRenameEntrypointFunctionName,
indexStoreMode: indexStoreMode,
useExplicitModuleBuild: useExplicitModuleBuild
)
Expand Down Expand Up @@ -1826,6 +1828,51 @@ final class BuildPlanTests: XCTestCase {
XCTAssertEqual(testPathExtension, "wasm")
}

func testEntrypointRenaming() throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Pkg/Sources/exe/main.swift"
)

let observability = ObservabilitySystem.makeForTesting()
let graph = try loadPackageGraph(
fs: fs,
manifests: [
Manifest.createRootManifest(
name: "Pkg",
path: .init("/Pkg"),
toolsVersion: .v5_5,
targets: [
TargetDescription(name: "exe", type: .executable),
]),
],
observabilityScope: observability.topScope
)

func createResult(for triple: TSCUtility.Triple) throws -> BuildPlanResult {
BuildPlanResult(plan: try BuildPlan(
buildParameters: mockBuildParameters(canRenameEntrypointFunctionName: true, destinationTriple: triple),
graph: graph,
fileSystem: fs,
observabilityScope: observability.topScope
))
}
let supportingTriples: [TSCUtility.Triple] = [.x86_64Linux, .macOS]
for triple in supportingTriples {
let result = try createResult(for: triple)
let exe = try result.target(for: "exe").swiftTarget().compileArguments()
XCTAssertMatch(exe, ["-Xfrontend", "-entry-point-function-name", "-Xfrontend", "exe_main"])
let linkExe = try result.buildProduct(for: "exe").linkArguments()
XCTAssertMatch(linkExe, [.contains("exe_main")])
}

let unsupportingTriples: [TSCUtility.Triple] = [.wasi, .windows]
for triple in unsupportingTriples {
let result = try createResult(for: triple)
let exe = try result.target(for: "exe").swiftTarget().compileArguments()
XCTAssertNoMatch(exe, ["-entry-point-function-name"])
}
}

func testIndexStore() throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Pkg/Sources/exe/main.swift",
Expand Down