How to pass -fmodule-map-file flag to a target?

I'm trying to build GitHub - apple/swift-syntax: SwiftPM package for SwiftSyntax library. on Windows. It is possible with right linker and compiler flags. My problem is -fmodule-map-file flag is ignored. Package.swift:

import PackageDescription

let swiftToolchain = <path to xctoolchain>"

let package = Package(
    name: "SwiftSyntaxHost",
    dependencies: [
        .package(
            name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax.git", 
            .revision("swift-DEVELOPMENT-SNAPSHOT-2021-03-02-a"))
    ],
    targets: [
        .executableTarget(
            name: "SwiftSyntaxHost",
            dependencies: ["SwiftSyntax"],
            swiftSettings: [
                // ignored
                .unsafeFlags([
                    "-Xcc", 
                    "-fmodule-map-file=\(swiftToolchain)\\usr\\include\\_InternalSwiftSyntaxParser\\module.modulemap"
                ])
            ],
            // passed to the linker correctly
            linkerSettings: [.unsafeFlags(["-L\(swiftToolchain)\\usr\\lib\\"])]),
        .testTarget(
            name: "SwiftSyntaxHostTests",
            dependencies: ["SwiftSyntaxHost"]),
    ]
)

One possible workaround is to add a system library target to SwiftSyntax with .h and module.modulemap from the toolchain. However, I would like to use SwiftSyntax as is.

SwiftSyntax is the reason that I started packaging _InternalSwiftSyntaxParser into the toolchain in the first place. Using -I should be sufficient, is there a reason that you are trying to load the module map explicitly? You could just do:

cSettings: [
  .headerSearchPath("\(swiftToolchain)/usr/include/_InternalSwiftSyntaxParser"),
]

I didn't know. I'll try.

headerSearchPath expects path relative to the target's directory

Ah, fair enough. However, .unsafeFlags(["-I", ..."]) should still be able to handle that.

Unfortunately, It doesn't.

import PackageDescription

let swiftToolchain = "c:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain"

let package = Package(
    name: "SwiftSyntaxHost",
    dependencies: [
        .package(name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax.git", .revision("swift-DEVELOPMENT-SNAPSHOT-2021-03-02-a"))
    ],
    targets: [
        .executableTarget(
            name: "SwiftSyntaxHost",
            dependencies: ["SwiftSyntax"],
            cSettings: [                
                .unsafeFlags([
                    "-I\(swiftToolchain)/usr/include/_InternalSwiftSyntaxParser"
                ])
            ],
            linkerSettings: [.unsafeFlags(["-L\(swiftToolchain)\\usr\\lib\\"])]
        ),
        .testTarget(
            name: "SwiftSyntaxHostTests",
            dependencies: ["SwiftSyntaxHost"]),
    ]
)

Result: error: no such module '_InternalSwiftSyntaxParser'

However, it works when the flag passed directly: swift build -Xcc -IC:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\include\_InternalSwiftSyntaxParser

Interesting, what if you were to use swiftSettings instead of cSettings?

Result is the same.

Terms of Service

Privacy Policy

Cookie Policy