0

How can I add a 3rd party package as a dependency to my package with which I distribute an xcframework?

So, I have my product:

products: [
     .library(
            name: "MY-FRAMEWORK",
            targets: ["MY-FRAMEWORK", "SOME-3RD-PARTY-XCFRAMEWORK"]
     )
 ]

my dependency, which I require:

dependencies: [
    .package(url: "URL-TO-3RD-PARTY-PACKAGE", revision: "VERSION")
]

and my targets:

targets: [
    .binaryTarget(
        name: "MY-FRAMEWORK",
        path: "PATH-TO-MY-FRAMEWORK"
    ),
    .binaryTarget(
        name: "SOME-3RD-PARTY-XCFRAMEWORK",
        path: "PATH-TO-SOME-3RD-PARTY-XCFRAMEWORK"
    )
]

While compiling, the Package expresses his concern of me not using the dependency in any of the declared targets, which is true. Also, when trying to integrate my package to an application, I get an error that it cannot be imported due to some errors.

1. No such module '3RD-PARTY-PACKAGE'
2. Failed to build module 'MY-FRAMEWORK' for importation due to the errors above; the textual interface may be broken by project issues or a compiler bug.

Now, How can I use a package dependency for my library with SPM?

Deryck Lucian
  • 477
  • 6
  • 18
  • Does this answer your question? [Add package dependency for a binary target with Swift Package Manager](https://stackoverflow.com/questions/65220359/add-package-dependency-for-a-binary-target-with-swift-package-manager) – Paul Beusterien Aug 03 '22 at 14:32

1 Answers1

2

Ok, so for the last 2 days I was searching and trying to find a solution to my problem and I stumbled over this post. Looks like SPM truly does not support dependencies on binary targets. So, in order to make it work I was forced to create a wrapper target within the Package.

  1. I've created a group in the Package named 'Wrapper' and added two files: one .h file into an include group and one .m file in the root, both of them empty (just a comment with why I did this).

  2. Added a new target, which is the 'Wrapper' in which I added the package dependency and any 3rd party binary targets I had.

  3. Updated my library to use the 'Wrapper' as a target.

And that was it. Bellow, you can see the Package.swift file:

let package = Package(
    name: "MY-FRAMEWORK",
    products: [
        .library(
            name: "MY-FRAMEWORK",
            targets: ["MY-FRAMEWORK", "Wrapper"]
        )
    ],
    dependencies: [
        .package(name: "3RD-PARTY-PACKAGE", url: "URL-TO-3RD-PARTY-PACKAGE", revision: "VERSION")
    ],
    targets: [
        // .binaryTarget doesn't support package dependencies so we use a wrapper to fix this
        .target(
            name: "Wrapper",
            dependencies: [
                "SOME-3RD-PARTY-XCFRAMEWORK", "3RD-PARTY-PACKAGE"
            ],
            path: "Wrapper"
        ),
        .binaryTarget(
            name: "MY-FRAMEWORK",
            path: "MY-FRAMEWORK.xcframework"
        )
        .binaryTarget(
            name: "SOME-3RD-PARTY-XCFRAMEWORK",
            path: "PATH-TO-SOME-3RD-PARTY-XCFRAMEWORK.xcframework"
        )
    ]
)

and also the project structure:

project_structure

Hope it will help somebody in the future. All credits must be given to the original post I followed. Without it, I would have still be stuck and searching the web. Cheers mate!

// edit: You can also have just one swift file into the 'Wrapper' group, not only the two .h and .m files.

Deryck Lucian
  • 477
  • 6
  • 18