Figure out .product line based on .package line for Package.swift dependencies?

13 Views Asked by At

This is about using SPM without Xcode.

Here are simple directions for creating a Swift command line executable for Mac: https://www.swift.org/getting-started/cli-swiftpm/

They show Package.swift file as:

    let package = Package(
        name: "MyCLI",
        dependencies: [
          .package(url: "https://github.com/apple/example-package-figlet", branch: "main"),
        ],
        targets: [
            // Targets are the basic building blocks of a package, defining a module or a test suite.
            // Targets can depend on other targets in this package and products from dependencies.
            .executableTarget(
                name: "MyCLI",
                dependencies: [
                    .product(name: "Figlet", package: "example-package-figlet"),
                ],
                path: "Sources"),
        ]
    )

However, all libraries, in their README, only show the top .package(url: line.

For example from https://github.com/mkrd/Swift-BigInt:

.package(url: "https://github.com/mkrd/Swift-BigInt.git", from: "2.0.0")

But for swift build command executed from Terminal to work, the other .product(name:package:) line must exist.

However, how do I determine the proper values for the .product line myself? Apparently, if I were using Xcode, then Xcode would magically fill in the .product line for me.

Using the Figlet example for comparison, I did the following steps.

    products: [
            .library(
                name: "WhateverIsHere"
  • For the package field, I used the final path element of the URL to the package.

Continuing with my second example, I did this to get BigNumber to work:

.product(name: "BigNumber", package: "Swift-BigInt")

My problem is that those values could be a coincidence, and I got lucky. Another library with a different URL or different Package.swift file might not work like that. What is the official answer? Further, I would have guessed that both values would have come from the library's Package.swift file and neither field would be derived from a path element in the URL.

0

There are 0 best solutions below