# Using build tool plugins with .xcproj

**URL:** <https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064>\
**Category:** Using Swift\
**Tags:** xcode, packagemanager, xcodebuild, build-tools\
**Created:** [March 19, 2022, 10:05pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064 "2022-03-19T22:05:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [March 19, 2022, 10:05pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/1 "2022-03-19T22:05:21Z")

</div>

Hi, not sure if this is Xcode specific or not. But I’m using Xcode version 13.3 (13E113), and have successfully created a little build tool plugin in one of my Swift packages, which generates Swift code for files ending in a specific extension.

I have an iOS app that I want to use to run the code generation with, but it’s using an .xcodeproj, and so xcodebuild as the build system. Does anyone know if it can take advantage of these build tool plugins? The release notes read:

> Swift Packages now support build tool plugins, as defined in [SE-0303](https://github.com/apple/swift-evolution/blob/main/proposals/0303-swiftpm-extensible-build-tools.md) and [SE-0325](https://github.com/apple/swift-evolution/blob/main/proposals/0325-swiftpm-additional-plugin-apis.md). This allows packages to define plugins that can specify tools that should run during a build operation, for example to generate source code. This is supported in both `swift` `package` and in Xcode’s support for packages. (79876749)

---

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [March 19, 2022, 10:13pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/2 "2022-03-19T22:13:04Z")

</div>

I can see the plugin in the sidebar:

 ![image](https://global.discourse-cdn.com/swift/original/3X/1/8/189cf1b592b2acdecc87257969f099425aa9acd5.png)

Clicking on the arrow reveals the plugin file in finder. I can’t see Xcode actually attempting to run the build tool plugin.

---

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [March 21, 2022, 2:33pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/3 "2022-03-21T14:33:49Z")

</div>

From what I can tell, only **build tool plugins** show up in that package plugins section, i.e. command plugins don’t appear.

And if you do add a build tool plugin via a `Package.swift` file, then Xcode will translate it to a “run custom script” build step in xcodebuild. But once again, this is only if your project uses `Package.swift` and not a typical `.xcodeproj`, which seems to be a blocker for using these build tools with apps.

 ![image](https://global.discourse-cdn.com/swift/original/3X/8/3/837f0380d330c66f87aae08119648c87b47346d4.png)

---

<div class="post-metadata">

**Author:** ![abertelrud](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@abertelrud](https://forums.swift.org/u/abertelrud)\
**Post date:** [March 21, 2022, 5:04pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/4 "2022-03-21T17:04:03Z")

</div>

Currently, package plugins are a function only of Swift Packages, and are not related to Xcode projects. It should be possible to have an Xcode project that uses a local package that uses a build tool plugin, though.

And as you noted, command plugins are currently a feature only of the SwiftPM CLI.

---

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [June 6, 2022, 9:56pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/5 "2022-06-06T21:56:04Z")

</div>

From the [Xcode 14 beta release notes](https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes):

> - Xcode now provides Swift Package plugins with an XcodeProjectPlugin API that extends the Swift Package Manager’s PackagePlugin API. Using this API, the plugin can get a simplified description of the Xcode project’s structure. This lets adapted plugins running in Xcode take advantage of using this API on Xcode projects. You can still use packages that haven’t been adapted to support Xcode projects with Swift Packages in Xcode. (88196725)

This looks exactly like what I’m looking for. Looking forward to trying it out!

---

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [June 7, 2022, 7:28am UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/6 "2022-06-07T07:28:18Z")

</div>

Turns out this only seems to support command plugins for now. Hopefully build plugins will be supported at a later date!

---

<div class="post-metadata">

**Author:** ![NeoNacho](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/neonacho/32/1368_2.png) [@NeoNacho](https://forums.swift.org/u/NeoNacho)\
**Post date:** [June 7, 2022, 6:07pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/7 "2022-06-07T18:07:58Z")

</div>

Yep, see known issues in the release notes:

> Build tool plugins that are adapted to support Xcode projects can’t yet be used with Xcode targets to generate source code and resources. This functionality is expected to be available in a future Xcode 14 beta. (92415898)

---

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [July 11, 2022, 10:19am UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/8 "2022-07-11T10:19:36Z")

</div>

In Xcode 14 beta 3, I converted my plugin to an `XcodeBuildToolPlugin`, but I conversely now get an error in the prepare packages build step saying:

> Malformed input JSON: Plugin declared with `buildTool` capability but doesn't conform to `BuildToolPlugin` protocol

Is there another capability I need to define in `Package.swift` for Xcode build tools?

---

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [July 11, 2022, 10:59am UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/9 "2022-07-11T10:59:28Z")

</div>

Rewatching the WWDC session on package manager plugins, I realised that my plugin needs to conform to both `XcodeBuildToolPlugin` and `BuildToolPlugin`, not just the former.  
Would it be perhaps worthwhile inheriting `BuildToolPlugin` from the `XcodeBuildToolPlugin` protocol to enforce this?

Works a charm now anyway, this is a huge improvement to my workflow

---

<div class="post-metadata">

**Author:** ![doozMen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/doozmen/32/7803_2.png) [@doozMen](https://forums.swift.org/u/doozMen)\
**Post date:** [July 19, 2022, 10:08am UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/10 "2022-07-19T10:08:24Z")

</div>

I try to run a very simple plugin but get the following error

```swift
error: plugin compilation failed: error: link command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64

```

if somebody else has this this is because the plugin was missing `@main` In the WWDC22 talk of @NeoNacho this was missing.

 ![FX-3kEjWQAAs01T](https://global.discourse-cdn.com/swift/original/3X/b/b/bbc1bff1a0d54eeb2109f74e53885e352ba806ec.jpeg)

---

<div class="post-metadata">

**Author:** ![doozMen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/doozmen/32/7803_2.png) [@doozMen](https://forums.swift.org/u/doozMen)\
**Post date:** [July 25, 2022, 11:54am UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/11 "2022-07-25T11:54:00Z")

</div>

Did you actually get it to run? I cannot seam to get it to run no matter what I do.

---

<div class="post-metadata">

**Author:** ![Luke\_Lau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luke_lau/32/648_2.png) [@Luke\_Lau](https://forums.swift.org/u/Luke_Lau)\
**Post date:** [July 25, 2022, 1:13pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/12 "2022-07-25T13:13:39Z")

</div>

Yeah, I had to add it to the list of “Run build tool plug-ins" for my Xcode target in the build phases section

---

<div class="post-metadata">

**Author:** ![mickeyl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mickeyl/32/12258_2.png) [@mickeyl](https://forums.swift.org/u/mickeyl)\
**Post date:** [September 24, 2022, 9:50am UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/13 "2022-09-24T09:50:04Z")

</div>

Is there a more efficient way than debugging via file output or throwing errors? Like… could we attach a debugger somehow?

---

<div class="post-metadata">

**Author:** ![nonameplum](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nonameplum/32/6450_2.png) [@nonameplum](https://forums.swift.org/u/nonameplum)\
**Post date:** [March 29, 2023, 11:04am UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/14 "2023-03-29T11:04:58Z")

</div>

Not the greatest idea but I see that `NSLog` works :)

---

<div class="post-metadata">

**Author:** ![NeoNacho](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/neonacho/32/1368_2.png) [@NeoNacho](https://forums.swift.org/u/NeoNacho)\
**Post date:** [March 29, 2023, 5:05pm UTC](https://forums.swift.org/t/using-build-tool-plugins-with-xcproj/56064/15 "2023-03-29T17:05:37Z")

</div>

Had missed this thread, debugging plugins should generally be possible. Since the name is user-defined you can just use Xcode's attach to process by name feature.

The only caveat is that most plugins or executables they call will terminate very quickly, so it might be necessary to add a short sleep somewhere so that there's time to attach to the process.
