Skip to content

[5.5] Check what compiler flags are available (#3398) #3525

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
Jun 8, 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ let package = Package(
.target(
/** SwiftPM test support library */
name: "SPMTestSupport",
dependencies: ["SwiftToolsSupport-auto", "Basics", "TSCTestSupport", "PackageGraph", "PackageLoading", "SourceControl", "Workspace", "Xcodeproj", "XCBuildSupport"]),
dependencies: ["SwiftToolsSupport-auto", "Basics", "Build", "TSCTestSupport", "PackageGraph", "PackageLoading", "SourceControl", "Workspace", "Xcodeproj", "XCBuildSupport"]),

// MARK: SwiftPM tests

Expand Down
23 changes: 19 additions & 4 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PackageGraph
import PackageLoading
import Foundation
import SPMBuildCore
@_implementationOnly import SwiftDriver

extension String {
fileprivate var asSwiftStringLiteralConstant: String {
Expand Down Expand Up @@ -690,7 +691,17 @@ public final class SwiftTargetBuildDescription {
let path = derivedSources.root.appending(subpath)
try fs.writeIfChanged(path: path, bytes: stream.bytes)
}


public static func checkSupportedFrontendFlags(flags: Set<String>, fs: FileSystem) -> Bool {
do {
let executor = try SPMSwiftDriverExecutor(resolver: ArgsResolver(fileSystem: fs), fileSystem: fs, env: [:])
let driver = try Driver(args: ["swiftc"], executor: executor)
return driver.supportedFrontendFlags.intersection(flags) == flags
} catch {
return false
}
}

/// The arguments needed to compile this target.
public func compileArguments() -> [String] {
var args = [String]()
Expand Down Expand Up @@ -733,8 +744,12 @@ public final class SwiftTargetBuildDescription {
// can construct the linker flags. In the future we will use a generated
// code stub for the cases in which the linker doesn't support it, so that
// we can rename the symbol unconditionally.
if buildParameters.linkerFlagsForRenamingMainFunction(of: target) != nil {
args += ["-Xfrontend", "-entry-point-function-name", "-Xfrontend", "\(target.c99name)_main"]
// 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"], fs: self.fs) {
if buildParameters.linkerFlagsForRenamingMainFunction(of: target) != nil {
args += ["-Xfrontend", "-entry-point-function-name", "-Xfrontend", "\(target.c99name)_main"]
}
}
}

Expand Down Expand Up @@ -2080,7 +2095,7 @@ private func generateResourceInfoPlist(
return true
}

fileprivate extension Triple {
fileprivate extension TSCUtility.Triple {
var isSupportingStaticStdlib: Bool {
isLinux() || arch == .wasm32
}
Expand Down
9 changes: 2 additions & 7 deletions Sources/SPMTestSupport/Resources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SPMBuildCore
import Foundation
import PackageLoading
import Workspace
import Build

#if os(macOS)
private func bundleRoot() -> AbsolutePath {
Expand Down Expand Up @@ -66,11 +67,5 @@ public class Resources: ManifestResourceProvider {
return Resources.default.binDir == nil
}

public var swiftCompilerSupportsRenamingMainSymbol: Bool {
return (try? withTemporaryDirectory { tmpDir in
FileManager.default.createFile(atPath: "\(tmpDir)/foo.swift", contents: Data())
let result = try Process.popen(args: self.swiftCompiler.pathString, "-c", "-Xfrontend", "-entry-point-function-name", "-Xfrontend", "foo", "\(tmpDir)/foo.swift", "-o", "\(tmpDir)/foo.o")
return try !result.utf8stderrOutput().contains("unknown argument: '-entry-point-function-name'")
}) ?? false
}
public let swiftCompilerSupportsRenamingMainSymbol = SwiftTargetBuildDescription.checkSupportedFrontendFlags(flags: ["entry-point-function-name"], fs: localFileSystem)
}