Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for macros with new swift_compiler_plugin rule #1061

Merged
merged 20 commits into from
Sep 6, 2023
Merged

Conversation

keith
Copy link
Member

@keith keith commented Jun 15, 2023

This adds support for macros with a new swift_compiler_plugin rule. Example usage:

swift_compiler_plugin(
    name = "Macros",
    srcs = glob(["Macros/*.swift"]),
    deps = [
        "@SwiftSyntax",
        "@SwiftSyntax//:SwiftCompilerPlugin",
        "@SwiftSyntax//:SwiftSyntaxMacros",
    ],
)

swift_test(
    name = "MacrosTests",
    srcs = glob(["MacrosTests/*.swift"]),
    deps = [
        ":Macros",
        "@SwiftSyntax//:SwiftSyntaxMacrosTestSupport",
    ],
)

swift_library(
    name = "MacroLibrary",
    srcs = glob(["MacroLibrary/*.swift"]),
    plugins = [":Macros"],
)

swift_library(
    name = "MacroConsumer",
    srcs = glob(["Sources/*.swift"]),
    deps = [":MacroLibrary"],
)

keith referenced this pull request Jun 15, 2023
PiperOrigin-RevId: 540589258
brentleyjones added a commit to MobileNativeFoundation/rules_xcodeproj that referenced this pull request Jun 20, 2023
bazelbuild/rules_swift#1061

Signed-off-by: Brentley Jones <github@brentleyjones.com>
@keith
Copy link
Member Author

keith commented Jul 31, 2023

might need 69349ee

…ring debug builds.

LLDB wants these files to be present on the file system to step into macro expansions, because their file paths are recorded in debug info. The compiler uses `TMPDIR` (if set) as the location for those files, so we can have the worker set `TMPDIR` to a target-specific location before spawning `swiftc`. If this location is workspace-relative, then those file paths are also remapped via `-debug-prefix-map`—an added bonus.

The `macro_expansions` group will contain a tree (directory) artifact that contains a `swift-generated-sources` subdirectory (the compiler forces that name), which contains one or more `.swift` files representing the expanded macro buffers.

Manual cherry pick of 69349ee
keith referenced this pull request Sep 5, 2023
…during debug builds.

LLDB wants these files to be present on the file system to step into macro expansions, because their file paths are recorded in debug info. The compiler uses `TMPDIR` (if set) as the location for those files, so we can have the worker set `TMPDIR` to a target-specific location before spawning `swiftc`. If this location is workspace-relative, then those file paths are also remapped via `-debug-prefix-map`—an added bonus.

The `macro_expansions` group will contain a tree (directory) artifact that contains a `swift-generated-sources` subdirectory (the compiler forces that name), which contains one or more `.swift` files representing the expanded macro buffers.

PiperOrigin-RevId: 549932429
@keith keith changed the title Add swift_compiler_plugin Add support for macros with new swift_compiler_plugin rule Sep 5, 2023
@keith keith marked this pull request as ready for review September 5, 2023 22:37
@brentleyjones
Copy link
Collaborator

I'll update my local usage of this tomorrow to test it out.

@brentleyjones
Copy link
Collaborator

brentleyjones commented Sep 6, 2023

When using this accidentally with an older swift version, the errors are super cryptic:

<unknown>:0: error: if any output files are specified, they all must be

I wonder if we can throw an error when using the plugins attribute with an invalid Xcode or swiftc version. Maybe we do something based on SWIFT_FEATURE__SUPPORTS_MACROS?

@brentleyjones
Copy link
Collaborator

Local testing worked.

@keith
Copy link
Member Author

keith commented Sep 6, 2023

added a feature check inside swift_compiler_plugin to fail early in that case. that should be enough since it's the only thing that creates the provider required for use in plugins = [ ... ]

@keith keith enabled auto-merge (squash) September 6, 2023 20:29
@keith keith requested a review from brentleyjones September 6, 2023 23:02
@keith keith merged commit c9d1f5f into master Sep 6, 2023
@keith keith deleted the google-macros branch September 6, 2023 23:11
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants