Skip to content

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

Conversation

plemarquand
Copy link
Contributor

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

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
@plemarquand
Copy link
Contributor Author

@swift-ci please test

@@ -0,0 +1,3 @@
#!/usr/bin/swift
Copy link

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.

Copy link
Contributor Author

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.

@plemarquand
Copy link
Contributor Author

@swift-ci please test

@plemarquand
Copy link
Contributor Author

@swift-ci please test

@plemarquand
Copy link
Contributor Author

@swift-ci test windows

1 similar comment
@plemarquand
Copy link
Contributor Author

@swift-ci test windows

@plemarquand plemarquand merged commit 1ec5919 into swiftlang:main Jun 16, 2025
6 checks passed
@plemarquand plemarquand deleted the swift-files-as-build-tool-plugin-executables branch June 16, 2025 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants