How to support Swift Macro in a project integrated by CocoaPods

255 Views Asked by At

I am exploring the usage of Swift Macro in my CocoaPods-integrated project. I have successfully created a custom macro by following the instructions, and it functions properly in the host project. However, in the Pods project, I am unable to import CustomMacro for all the frameworks and development pods.

At this point, I am curious if there is a workaround that supports both CocoaPods and Swift Package Macro.

And also I found a Github thread discussing about a similiar topic: https://github.com/CocoaPods/CocoaPods/issues/11942 , but there's no further conclusion.

So please help to give some advises about how to use Swift Macro in CocoaPods project if you have any experience.

2

There are 2 best solutions below

2
On BEST ANSWER

Inspired by @Soumuya Mahunt and some other post and discussion, I successfully integrate the Swift Macro to a CocoaPods managed project and its all pods. All the details and steps can be found in this blog or the demo project.

The macro pod can be easily used by a project target or a development pod target.

import MyMacro

let desc = #stringify(1 + 1)

For a development pod target or remote pod target, there would be some tricks to make it possible, add the following post_install script in the Podfile will be fine.

installer_representation.pods_project.build_configurations.each do |config|
  config.build_settings['OTHER_SWIFT_FLAGS'] = "$(inherited) -load-plugin-executable #{macro_product_folder}/release/SwiftyArchitectureMacros#SwiftyArchitectureMacros"
end

Please see the blog for detail.

1
On

If you want to distribute your macro library with CocoaPods, you have to build the macro target as an executable and provide that to the app/user target as:

-load-plugin-executable # Path to an executable compiler plugins and providing module names such as macros

I have written a step-by-step detailed article on this here. You can also see this in action as part of my MetaCodable library.