-
Notifications
You must be signed in to change notification settings - Fork 1.4k
SwiftPM does not build all dependency executables before invoking plugin build command #5636
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
abertelrud
merged 1 commit into
swiftlang:main
from
abertelrud:eng/95759599-plugin-executable-dependencies-not-being-built
Jul 5, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Fixtures/Miscellaneous/Plugins/MyBuildToolPluginDependencies/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// swift-tools-version: 5.7 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MyBuildToolPluginDependencies", | ||
targets: [ | ||
// A local tool that uses a build tool plugin. | ||
.executableTarget( | ||
name: "MyLocalTool", | ||
plugins: [ | ||
"MySourceGenBuildToolPlugin", | ||
] | ||
), | ||
// The plugin that generates build tool commands to invoke MySourceGenBuildTool. | ||
.plugin( | ||
name: "MySourceGenBuildToolPlugin", | ||
capability: .buildTool(), | ||
dependencies: [ | ||
"MySourceGenBuildTool", | ||
] | ||
), | ||
// A command line tool that generates source files. | ||
.executableTarget( | ||
name: "MySourceGenBuildTool", | ||
dependencies: [ | ||
"MySourceGenBuildToolLib", | ||
] | ||
), | ||
// A library used by MySourceGenBuildTool (not the client). | ||
.target( | ||
name: "MySourceGenBuildToolLib" | ||
), | ||
] | ||
) |
38 changes: 38 additions & 0 deletions
38
...ous/Plugins/MyBuildToolPluginDependencies/Plugins/MySourceGenBuildToolPlugin/plugin.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import PackagePlugin | ||
|
||
@main | ||
struct MyPlugin: BuildToolPlugin { | ||
|
||
// Create build commands that don't invoke the MySourceGenBuildTool source generator | ||
// tool directly, but instead invoke a system tool that invokes it indirectly. We | ||
// want to test that we still end up with a dependency on not only that tool but also | ||
// on the library it depends on, even without including an explicit dependency on it. | ||
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] { | ||
print("Hello from the Build Tool Plugin!") | ||
guard let target = target as? SourceModuleTarget else { return [] } | ||
let inputFiles = target.sourceFiles.filter({ $0.path.extension == "dat" }) | ||
return try inputFiles.map { | ||
let inputFile = $0 | ||
let inputPath = inputFile.path | ||
let outputName = inputPath.stem + ".swift" | ||
let outputPath = context.pluginWorkDirectory.appending(outputName) | ||
return .buildCommand( | ||
displayName: | ||
"Generating \(outputName) from \(inputPath.lastComponent)", | ||
executable: | ||
Path("/usr/bin/env"), | ||
arguments: [ | ||
try context.tool(named: "MySourceGenBuildTool").path, | ||
"\(inputPath)", | ||
"\(outputPath)" | ||
], | ||
inputFiles: [ | ||
inputPath, | ||
], | ||
outputFiles: [ | ||
outputPath | ||
] | ||
) | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
Fixtures/Miscellaneous/Plugins/MyBuildToolPluginDependencies/Sources/MyLocalTool/foo.dat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let foo = "I am Foo!" |
1 change: 1 addition & 0 deletions
1
Fixtures/Miscellaneous/Plugins/MyBuildToolPluginDependencies/Sources/MyLocalTool/main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("Generated string Foo: '\(foo)'") |
18 changes: 18 additions & 0 deletions
18
...scellaneous/Plugins/MyBuildToolPluginDependencies/Sources/MySourceGenBuildTool/main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
import MySourceGenBuildToolLib | ||
|
||
// Sample source generator tool that emits a Swift variable declaration of a string containing the hex representation of the contents of a file as a quoted string. The variable name is the base name of the input file. The input file is the first argument and the output file is the second. | ||
if ProcessInfo.processInfo.arguments.count != 3 { | ||
print("usage: MySourceGenBuildTool <input> <output>") | ||
exit(1) | ||
} | ||
let inputFile = ProcessInfo.processInfo.arguments[1] | ||
let outputFile = ProcessInfo.processInfo.arguments[2] | ||
|
||
let variableName = URL(fileURLWithPath: inputFile).deletingPathExtension().lastPathComponent | ||
|
||
let inputData = FileManager.default.contents(atPath: inputFile) ?? Data() | ||
let dataAsHex = inputData.map { String(format: "%02hhx", $0) }.joined() | ||
let outputString = "public var \(variableName) = \(dataAsHex.quotedForSourceCode)\n" | ||
let outputData = outputString.data(using: .utf8) | ||
FileManager.default.createFile(atPath: outputFile, contents: outputData) |
11 changes: 11 additions & 0 deletions
11
...neous/Plugins/MyBuildToolPluginDependencies/Sources/MySourceGenBuildToolLib/library.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
extension String { | ||
|
||
public var quotedForSourceCode: String { | ||
return "\"" + self | ||
.replacingOccurrences(of: "\\", with: "\\\\") | ||
.replacingOccurrences(of: "\"", with: "\\\"") | ||
+ "\"" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the key change. Nothing particularly risky or anything that requires extra scrutiny, I think, but it seemed worth calling out amid all the more mundane changes.