How to make Swift Macro Packages only for iOS platform?

I noticed when generating a new package, type macro, it adds the platforms: argument to the Package initializer in the Package.Swift file.

// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport

let package = Package(
    name: "MacroPackage",
>>  platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
    products: [
...

Is it possible to write macro packages just for the iOS platform? Or even better, for no specific platform?

Because, when removing the .macOS(.v10_15) from the platforms argument, I get an error saying that swift-syntax requires the macOS platform.

...
Creating working copy for https://github.com/apple/swift-syntax.git
Working copy of https://github.com/apple/swift-syntax.git resolved at 509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-06-05-a
**error:** the macro 'MacroPackageMacros' requires macos 10.13, but depends on the product 'SwiftSyntaxMacros' which requires macos 10.15; consider changing the macro 'MacroPackageMacros' to require macos 10.15 or later, or the product 'SwiftSyntaxMacros' to require macos 10.13 or earlier.
**error:** the macro 'MacroPackageMacros' requires macos 10.13, but depends on the product 'SwiftCompilerPlugin' which requires macos 10.15; consider changing the macro 'MacroPackageMacros' to require macos 10.15 or later, or the product 'SwiftCompilerPlugin' to require macos 10.13 or earlier.

I would assume that macros can be part of a library package. In fact, the swift package init --type macro command generates a library product, which can be imported into another package or an executable.

I also saw the discussion about Swift Macros support in Linux, so is it at this point even possible to make a platform agnostic macro?

1 Like

Macros are always platform-agnostic, the platforms API just defines the minimum deployment target of targets in a given package. Since macros require swift-syntax, the minimum deployment target for a macro can't be lower than the one of swift-syntax itself.

1 Like

Oh right. So basically omitting the macOS platform just assumes the earliest available version (which is 10.13), hence the error.

And the macOS platform in use in this scenario is the platform running the toolchain (where the macros get executed). Cross compilation is still happening on the macOS platform (even when the target is iOS arm64).

I know that macros are completely sandboxed and we shouldn’t do anything beyond basic swift, or anything dependent on the platform in our macro implementation. But just to confirm, the toolchain that builds and executes macros is running on the system platform (macOS in this case), right?