-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support using a Swift file as a build tool plugin executable #8807
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
Support using a Swift file as a build tool plugin executable #8807
Conversation
A Swift file can be executed directly in the shell. Typically the file begins with a `#/usr/bin/swift` or an equivalent `xcrun` incantation, and then simply running `swift MyFile.swift` will automatically compile and execute it. Build tool plugins often leverage Swift, but to do so they add two targets, a `.plugin` and an `.executableTarget`. The executable target builds a binary that the plugin tool then calls during a build. Often this is more heavyweight than needed. By passing the path to a Swift file to the `executable` argument of the `.buildCommand` returned from your build tool plugin, you could bypass the need to create a separate executable target and just execute the file directly. However, because SwiftPM executes build tool plugins within a sandbox the sandbox script needs to be ammended with the clang module cache temporary directory so that the swift compiler can write the modules it needs to execute the file. Ammend the sandbox script to add write permissions to the root tmp directory that LLVM uses to write clang modules. With this addition you can now use a .swift file as a build tool plugin executable. rdar://152874736
@swift-ci please test |
@@ -0,0 +1,3 @@ | |||
#!/usr/bin/swift |
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.
does this support pointing to any executable or is it locked to swift? just checking ... one might prefer to use e.g. bash sometimes.
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.
You could use a bash script as a build tool plugin executable and I believe it would work today, without this patch, yes.
@swift-ci please test |
Fixtures/Miscellaneous/Plugins/SwiftFilePlugin/SimpleSwiftScript.swift
Outdated
Show resolved
Hide resolved
Fixtures/Miscellaneous/Plugins/SwiftFilePlugin/SimpleSwiftScript.swift
Outdated
Show resolved
Hide resolved
@swift-ci please test |
@swift-ci test windows |
1 similar comment
@swift-ci test windows |
A Swift file can be executed directly in the shell. Typically the file begins with a
#/usr/bin/swift
or an equivalentxcrun
incantation, and then simply runningswift MyFile.swift
will automatically compile and execute it.Build tool plugins often leverage Swift, but to do so they add two targets, a
.plugin
and an.executableTarget
. The executable target builds a binary that the plugin tool then calls during a build.Often this is more heavyweight than needed. By passing the path to a Swift file to the
executable
argument of the.buildCommand
returned from your build tool plugin, you could bypass the need to create a separate executable target and just execute the file directly.However, because SwiftPM executes build tool plugins within a sandbox the sandbox script needs to be ammended with the clang module cache temporary directory so that the swift compiler can write the modules it needs to execute the file.
Ammend the sandbox script to add write permissions to the root tmp directory that LLVM uses to write clang modules. With this addition you can now use a .swift file as a build tool plugin executable.
rdar://152874736