Unable to import local XCFramework module

Our project is split up into multiple Pods with CocoaPods and I'm currently trying to convert all of our podspecs to SPM. One of our projects has XCFramework dependencies that I'm trying to include from a local source, but my project files are complaining that the modules couldn't be found when I try to import them. Is there something I'm missing here? My Package.swift file looks something like this:

Code Block
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
    name: "MyPackage",
    defaultLocalization: "en_us",
    platforms: [
        .iOS(.v12),
        .watchOS(.v6),
        .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "MyPackage",
            targets: [
                "MyPackage"
            ]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: [
                .byName(name: "LocalXCFramework")
            ],
            path: "Sources/"
        ),
        
        .binaryTarget(
            name: "LocalXCFramework",
            path: "Frameworks/LocalXCFramework.xcframework"
        )
        
    ]
)


In this example, if I try to import the modules from LocalXCFramework, I get an error saying it couldn't be found.
This looks like a bug to me, would you mind filing a feedback report?
Sure thing, just filed one! FB7790145
Thank you!
This is the same issue I’m having here:

https://developer.apple.com/forums/thread/651258

will watch this thread
Hi, is there any update for this issue? I'm not able to follow the feedback report since it's private. binaryTarget is still broken with local xcframeworks for me.

Any updates here? This still seems to be an issue

Is there any workaround for this issue?

There is a workaround by defining a dummy C target that includes the XCFramework. This is explained on the forum: https://forums.swift.org/t/swift-package-and-xcframework-target-for-c-library-where-to-include-the-header/51163/6

You will find a link to a sample repository that show how to architecture the project: https://github.com/withuno/UnoSwift

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

import PackageDescription

let package = Package(
    name: "IBANKit",
    products: [
        // Products define the executables and libraries a package produces, making them visible to other packages.
        .library(
            name: "IBANKit",
            targets: ["IBANKit"]),
    ],
    dependencies: [],
    targets: [
       // Adds the C module as a dependencies to your C sources
        .target(
            name: "IBANKit", 
            dependencies: ["C"],
            path: "Sources/IBANKit"
        ),
        // Create a dummy c package that uses the XCFramework
        .target(
            name: "C",
            dependencies: ["IBANChecker-Core"],
            path: "Sources/C"
        ),
        .binaryTarget(name: "IBANChecker-Core", path: "Libs/IBANChecker-Core.xcframework"),
    ]
)

Then create a dummy .c source:

/// Sources/C/bridge.c
#include "bridge.h"

void __dummy() {}

and a .h in:

// Sources/C/include/bridge.h
#define IBANKIT_VERSION 1

/* Here include the header of the XCFramework */
#include "IBANChecker-Core.h"

And finally in your swift file

import C

// Now uses symbols defined in the XCFramework

I was able to do this by adding a module.modulemap file to the Headers. This is all within my library Package project:

// swift-tools-version: 6.0

import PackageDescription

/**
	This package wraps FTDI’s D2XX library for macOS. In order to simp
	xcodebuild -create-xcframework -library /Users/rmann/Downloads/lib3mf_sdk_v2.3.1/Bin/lib3mf.dylib -headers /Users/rmann/Downloads/lib3mf_sdk_v2.3.1/Bindings/C -output lib3mf.xcframework
*/

let package = Package(
	name: "SwiftD2XX",
	platforms: [.macOS(.v13)],
	products:
	[
		.library(
			name: "SwiftD2XX",
			targets: ["SwiftD2XX"]
		),
	],
	dependencies:
	[
		.package(url: "https://github.com/apple/swift-testing.git", branch: "main"),
	],
	targets:
	[
		.target(
			name: "SwiftD2XX",
			dependencies:
			[
				"ftd2xx"
			]
		),
		.binaryTarget(
			name: "ftd2xx",
			path: "../ftd2xx.xcframework"
		),
		.testTarget(
			name: "SwiftD2XXTests",
			dependencies:
			[
				"SwiftD2XX",
				.product(name: "Testing", package: "swift-testing"),
			]
		),
	]
)

When I build my xcframework, I use this:

xcodebuild -create-xcframework -library ftdi-d2xx/build/libftd2xx.a -headers ./headers -output ftd2xx.xcframework

And the headers folder contains not only the headers for the xcframework, but a module.modulemap:

module ftd2xx {
    header "ftd2xx.h"
    export *
}

This ends up getting copied into the xcframework’s Headers directory, and that allows other modules to import it (in this case, via import ftd2xx).

Unable to import local XCFramework module
 
 
Q