Skip to content
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

Allow passing extra arguments to plugin compilation #5966

Merged
merged 1 commit into from
Mar 20, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Note: This is in reverse chronological order, so newer entries are added to the
Swift Next
-----------

* [#5966]

Plugin compilation can be influenced by using `-Xbuild-tools-swiftc` arguments in the SwiftPM command line. This is similar to the existing mechanism for influencing the manifest compilation using `-Xmanifest` arguments. Manifest compilation will also be influenced by `-Xbuild-tools-swiftc`, but only if no other `-Xmanifest` arguments are provided. Using `-Xmanifest` will show a deprecation message. `-Xmanifest` will be removed in the future.

* [#5728]

In packages that specify resources using a future tools version, the generated resource bundle accessor will import `Foundation.Bundle` for its own implementation only. _Clients_ of such packages therefore no longer silently import `Foundation`, preventing inadvertent use of Foundation extensions to standard library APIs, which helps to avoid unexpected code size increases.
Expand Down Expand Up @@ -318,6 +322,7 @@ Swift 3.0
[#5874]: https://github.com/apple/swift-package-manager/pull/5874
[#5949]: https://github.com/apple/swift-package-manager/pull/5949
[#5892]: https://github.com/apple/swift-package-manager/pull/5892
[#5966]: https://github.com/apple/swift-package-manager/pull/5966
[#6060]: https://github.com/apple/swift-package-manager/pull/6060
[#6067]: https://github.com/apple/swift-package-manager/pull/6067
[#6114]: https://github.com/apple/swift-package-manager/pull/6114
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import PackagePlugin

@main
struct MyPlugin: BuildToolPlugin {

#if USE_CREATE
let verb = "Creating"
#else
let verb = "Generating"
#endif

func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
print("Hello from the Build Tool Plugin!")
guard let target = target as? SourceModuleTarget else { return [] }
Expand All @@ -12,7 +17,7 @@ struct MyPlugin: BuildToolPlugin {
let outputPath = context.pluginWorkDirectory.appending(outputName)
return .buildCommand(
displayName:
"Generating \(outputName) from \($0.lastComponent)",
"\(verb) \(outputName) from \($0.lastComponent)",
executable:
try context.tool(named: "MySourceGenBuildTool").path,
arguments: [
Expand Down
24 changes: 22 additions & 2 deletions Sources/CoreCommands/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,35 @@ public struct BuildOptions: ParsableArguments {
)
public var xcbuildFlags: [String] = []

@Option(
name: .customLong("Xbuild-tools-swiftc", withSingleDash: true),
parsing: .unconditionalSingleValue,
help: ArgumentHelp(
"Pass flag to Swift compiler invocations for build-time executables (manifest and plugins)",
visibility: .hidden
)
)
public var _buildToolsSwiftCFlags: [String] = []

@Option(
name: .customLong("Xmanifest", withSingleDash: true),
parsing: .unconditionalSingleValue,
help: ArgumentHelp(
"Pass flag to the manifest build invocation",
"Pass flag to the manifest build invocation. Deprecated: use '-Xbuild-tools-swiftc' instead",
visibility: .hidden
)
)
var manifestFlags: [String] = []
public var _deprecated_manifestFlags: [String] = []

var manifestFlags: [String] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

self._deprecated_manifestFlags.isEmpty ?
self._buildToolsSwiftCFlags :
self._deprecated_manifestFlags
}

var pluginSwiftCFlags: [String] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not blocking, but wasnt the idea to also expose this is a user facing flag? or is that for next PR so we can debbate the name? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean supporting both -Xbuild-tools-swiftc which applies to every build time tool and a -Xplugin-swiftc which only applies to plugins? I never got that impression from the feedback, but I might have missed that facet. I am not sure that I can see an scenario in which someone has different sets of flags for the same native machine depending if they are compiling a plugin or some other build time tool. The solution as implemented can grow in that direction, if needed.

Copy link
Contributor

@tomerd tomerd Mar 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have been my misunderstanding of he goal of this PR. in any case as you say this sets the ground work if needed, and this feedback is by no means to hold the PR back 👍

self._buildToolsSwiftCFlags
}

public var buildFlags: BuildFlags {
BuildFlags(
Expand Down
5 changes: 5 additions & 0 deletions Sources/CoreCommands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ public final class SwiftTool {
if let _ = options.security.netrcFilePath, options.security.netrc == false {
observabilityScope.emit(.mutuallyExclusiveArgumentsError(arguments: ["--disable-netrc", "--netrc-file"]))
}

if !options.build._deprecated_manifestFlags.isEmpty {
observabilityScope.emit(warning: "'-Xmanifest' option is deprecated; use '-Xbuild-tools-swiftc' instead")
}
}

func waitForObservabilityEvents(timeout: DispatchTime) {
Expand Down Expand Up @@ -620,6 +624,7 @@ public final class SwiftTool {
fileSystem: self.fileSystem,
cacheDir: cacheDir,
toolchain: self.getHostToolchain(),
extraPluginSwiftCFlags: self.options.build.pluginSwiftCFlags,
enableSandbox: !self.shouldDisableSandbox,
verboseOutput: self.logLevel <= .info
)
Expand Down
14 changes: 13 additions & 1 deletion Sources/Workspace/DefaultPluginScriptRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,25 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
private let fileSystem: FileSystem
private let cacheDir: AbsolutePath
private let toolchain: UserToolchain
private let extraPluginSwiftCFlags: [String]
private let enableSandbox: Bool
private let cancellator: Cancellator
private let verboseOutput: Bool

private let sdkRootCache = ThreadSafeBox<AbsolutePath>()

public init(fileSystem: FileSystem, cacheDir: AbsolutePath, toolchain: UserToolchain, enableSandbox: Bool = true, verboseOutput: Bool = false) {
public init(
fileSystem: FileSystem,
cacheDir: AbsolutePath,
toolchain: UserToolchain,
extraPluginSwiftCFlags: [String] = [],
enableSandbox: Bool = true,
verboseOutput: Bool = false
) {
self.fileSystem = fileSystem
self.cacheDir = cacheDir
self.toolchain = toolchain
self.extraPluginSwiftCFlags = extraPluginSwiftCFlags
self.enableSandbox = enableSandbox
self.cancellator = Cancellator(observabilityScope: .none)
self.verboseOutput = verboseOutput
Expand Down Expand Up @@ -209,6 +218,9 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
// Finally add the output path of the compiled executable.
commandLine += ["-o", execFilePath.pathString]

// Add any extra flags passed for the host in the command line
commandLine += self.extraPluginSwiftCFlags

if (verboseOutput) {
commandLine.append("-v")
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/FunctionalTests/PluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1009,4 +1009,25 @@ class PluginTests: XCTestCase {
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
}
}

func testPluginCanBeAffectedByXBuildToolsParameters() throws {
// Only run the test if the environment in which we're running actually supports Swift concurrency (which the
// plugin APIs require).
try XCTSkipIf(
!UserToolchain.default.supportsSwiftConcurrency(),
"skipping because test environment doesn't support concurrency"
)

try fixture(name: "Miscellaneous/Plugins") { fixturePath in
let (stdout, _) = try executeSwiftBuild(
fixturePath.appending(component: "MySourceGenPlugin"),
configuration: .Debug,
extraArgs: ["--product", "MyLocalTool", "-Xbuild-tools-swiftc", "-DUSE_CREATING"]
)
XCTAssert(stdout.contains("Linking MySourceGenBuildTool"), "stdout:\n\(stdout)")
XCTAssert(stdout.contains("Creating foo.swift from foo.dat"), "stdout:\n\(stdout)")
XCTAssert(stdout.contains("Linking MyLocalTool"), "stdout:\n\(stdout)")
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
}
}
}