Skip to content

Commit 59f2c1b

Browse files
miggs597Miguel Perez
andauthored
Check what compiler flags are available (#3398) (#3525)
* Check what compiler flags are available * Throwing removed * Consolidation * Touch ups * Removed missed try Co-authored-by: Miguel Perez <[email protected]> (cherry picked from commit b2e73ca)
1 parent ef65666 commit 59f2c1b

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ let package = Package(
252252
.target(
253253
/** SwiftPM test support library */
254254
name: "SPMTestSupport",
255-
dependencies: ["SwiftToolsSupport-auto", "Basics", "TSCTestSupport", "PackageGraph", "PackageLoading", "SourceControl", "Workspace", "Xcodeproj", "XCBuildSupport"]),
255+
dependencies: ["SwiftToolsSupport-auto", "Basics", "Build", "TSCTestSupport", "PackageGraph", "PackageLoading", "SourceControl", "Workspace", "Xcodeproj", "XCBuildSupport"]),
256256

257257
// MARK: SwiftPM tests
258258

Sources/Build/BuildPlan.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import PackageGraph
1616
import PackageLoading
1717
import Foundation
1818
import SPMBuildCore
19+
@_implementationOnly import SwiftDriver
1920

2021
extension String {
2122
fileprivate var asSwiftStringLiteralConstant: String {
@@ -690,7 +691,17 @@ public final class SwiftTargetBuildDescription {
690691
let path = derivedSources.root.appending(subpath)
691692
try fs.writeIfChanged(path: path, bytes: stream.bytes)
692693
}
693-
694+
695+
public static func checkSupportedFrontendFlags(flags: Set<String>, fs: FileSystem) -> Bool {
696+
do {
697+
let executor = try SPMSwiftDriverExecutor(resolver: ArgsResolver(fileSystem: fs), fileSystem: fs, env: [:])
698+
let driver = try Driver(args: ["swiftc"], executor: executor)
699+
return driver.supportedFrontendFlags.intersection(flags) == flags
700+
} catch {
701+
return false
702+
}
703+
}
704+
694705
/// The arguments needed to compile this target.
695706
public func compileArguments() -> [String] {
696707
var args = [String]()
@@ -733,8 +744,12 @@ public final class SwiftTargetBuildDescription {
733744
// can construct the linker flags. In the future we will use a generated
734745
// code stub for the cases in which the linker doesn't support it, so that
735746
// we can rename the symbol unconditionally.
736-
if buildParameters.linkerFlagsForRenamingMainFunction(of: target) != nil {
737-
args += ["-Xfrontend", "-entry-point-function-name", "-Xfrontend", "\(target.c99name)_main"]
747+
// No `-` for these flags because the set of Strings in driver.supportedFrontendFlags do
748+
// not have a leading `-`
749+
if SwiftTargetBuildDescription.checkSupportedFrontendFlags(flags: ["entry-point-function-name"], fs: self.fs) {
750+
if buildParameters.linkerFlagsForRenamingMainFunction(of: target) != nil {
751+
args += ["-Xfrontend", "-entry-point-function-name", "-Xfrontend", "\(target.c99name)_main"]
752+
}
738753
}
739754
}
740755

@@ -2080,7 +2095,7 @@ private func generateResourceInfoPlist(
20802095
return true
20812096
}
20822097

2083-
fileprivate extension Triple {
2098+
fileprivate extension TSCUtility.Triple {
20842099
var isSupportingStaticStdlib: Bool {
20852100
isLinux() || arch == .wasm32
20862101
}

Sources/SPMTestSupport/Resources.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import SPMBuildCore
1313
import Foundation
1414
import PackageLoading
1515
import Workspace
16+
import Build
1617

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

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

0 commit comments

Comments
 (0)