Local xcFrameworks in Swift Packages

In the talk, it was explained how to use a binaryTarget to add a .xcframework from a url, but what about a local path?

Take the following Package and file structure, is this the correct way to structure and refer to the .xcFramework?

| SamplePackage
| - | Package.swift
| - | README.swift
| - | Sources
| - | - | Sample
| - | - | - | file.swift
| - | - | SampleFramework
| - | - | - | framework.xcframework
| - | - | Tests
| - | - | - | LinuxMain.swift
| - | - | - | SampleTexts
| - | - | - | - | sampleTests.swift

Code Block swift
// 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: "Sample",
    platforms: [
        .iOS(.v13),
        .macOS(.v10_12)
    ],
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "Sample",
            targets: ["Sample", "SampleFramework"]),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target( name: "Sample"),
        .testTarget(
            name: "SampleTests",
            dependencies: ["Sample"]),
        .binaryTarget(
            name: "SampleFramework",
            path: "framework.xcframework")
    ]
)


I am getting the following error(s):
  • invalid custom path 'framework.xcframework' for target 'SampleFramework'

  • target path '/framework.xcframework' is not supported; it should be relative to package root

Answered by versatile1 in 615966022
The issue was that the .xcFramework needed to be at the root level of the Package!

| - | Sources
| - | Tests
| - | framework.xcframework

Code Block swift
.
.
.target(
name: "Sample"),       
.testTarget(
name: "SampleTests",           
dependencies: ["Sample"]),
.binaryTarget(
name: "SampleFramework",
path: "framework.xcframework")   
]
.
.


Hi,

have you tried to use a relative path to your Package.swift file?

"Sources/SampleFramework/framework.xcframework"

Best,
Boris
iOS dev
Accepted Answer
The issue was that the .xcFramework needed to be at the root level of the Package!

| - | Sources
| - | Tests
| - | framework.xcframework

Code Block swift
.
.
.target(
name: "Sample"),       
.testTarget(
name: "SampleTests",           
dependencies: ["Sample"]),
.binaryTarget(
name: "SampleFramework",
path: "framework.xcframework")   
]
.
.


I've also been trying to make this work but I have been unable to figure it out. When importing the binary framework I simply get a module not found error. I've reduced it down to a small case https://github.com/k0nserv/rust-swift-test.

EDIT: For completness sake I tried removing Rust from the equation by writing the library directly in C and building it with clang but it still doesn't work.

I had similar problems to make this working. Finally, I found that it's not enough to add the swift package. Once added, I need to go in the app target => General => Frameworks and add it as any Apple SDK or local framework. In the framework picker, there is a section where my swift package appears. So, Xcode recognises the package as a framework but you still need to include it in the target. Once this worked, I successfully tried to use a relative path in the .binaryTarget() and put my framework in a subfolder. Just needed to quit Xcode and remove the DerivedData folder to clear all caches.

Local xcFrameworks in Swift Packages
 
 
Q