
An example how to integrate Macros using SwiftSyntax into Bazel projects
Explore the docs »
Report Bug
·
Request Feature
Table of Contents
An example how to integrate Macros using SwiftSyntax into Bazel projects.
SwiftSyntax serves as a versatile tool in the realm of Swift development, offering a comprehensive set of libraries for parsing, inspecting, generating, and modifying Swift source code. Initially developed by Apple, it has evolved into an open-source project with active contributions from a diverse community. The library's documentation is readily available on swiftpackageindex, complemented by numerous articles in the GitHub readme. Notably, SwiftSyntax forms the backbone for essential tools like the Swift parser and swift-format.
A key aspect of SwiftSyntax's functionality lies in its ability to create an Abstract Syntax Tree (AST) of Swift source code. This AST serves as a high-level, secure, and efficient API, enabling developers to interact with their code in a structured manner. Exploring the Swift AST becomes more accessible through tools such as the Swift AST Explorer.
The landscape of Swift development witnessed a significant enhancement with the introduction of Swift Macros in the WWDC 2023 release of Swift 5.9. Leveraging SwiftSyntax as its foundation library, Swift Macros offer a novel approach to extending Swift by introducing new expressions, facilitating the creation of expressive libraries, and streamlining the elimination of unnecessary boilerplate code.
An intriguing dimension brought forth by Swift Macros is their role in extending the compiler with custom compile-time checks. While some may argue that similar outcomes can be achieved through reusable methods, Swift Macros stand out due to their unique features, making them an exceptional tool in the developer's toolkit.
Swift Macros were introduced as a feature bundled within Swift Packages. This approach enhances shareability. However, it also tightens the reliance on seamless integration between Xcode and the Swift Package Manager (SPM), which, from my experience and that of others, can be less than ideal in large projects with numerous dependencies.
Given the suboptimal experience offered by Apple’s ecosystem, which precludes optimization opportunities, developers uses external depencencies managers, especially Cocoapods. Besides they uses build systems as Bazel to automate tasks, compile and execute tests in a deterministic environment.
For such reason the objective of this repository was to check the viability together pros/const of define and integrate Swift Macros into Bazel environment.
The repository defines the following parts:
-
MacrosPlugin:
- Depends of SwiftSyntax library
- SwiftCompilerPlugin
- SwiftSyntaxBuilder
- SwiftSyntaxMacros
- Contains macros implementation
- Define which macros are visible to the compiler
- Depends of SwiftSyntax library
-
Macros:
- Depends of MacrosPlugin
- Public macro declaration which indicates:
- Macro name
- Type which contains the implementation of the expansion that our macro actually performs
- Module where the Type is defined, in our case MacrosPlugin
-
iOS Example App:
- Depends of Macros
- Define examples using the macros
To get a local copy up and running follow these simple example steps.
- Install Bazel - Instalation Guide
- Xcode 15.0
- Clone the repo
git clone https://github.com/asam139/swift-syntax-bazel-integration-example.git
- Run example app
bazel run //MacrosApp:xcodeproj
Use this space to show useful examples of how a project can be used. Additional screenshots, code examples and demos work well in this space. You may also link to more resources.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Saúl Moreno Abril- @93sauu
Project Link: Swift Syntax Bazel Integration Example