Search code examples
iosswiftmacosswift-package-managerswift-package

swift build: warning: '--product' cannot be used with the automatic product 'ProductName'; building the default target instead


I have a Package.swift manifest as follows:

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

import PackageDescription

let package = Package(
    name: "long-package-name-ios",
    defaultLocalization: "en",
    platforms: [
        .iOS(.v14),
        .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "PackageName",
            targets: ["PackageName"]),
        .library(
            name: "PackageNameImproved",
            targets: ["PackageNameImproved"]),
        .library(
            name: "PackageNameUI",
            targets: ["PackageNameUI"]),
    ],
    dependencies: [
        .package(
            name: "PackageLib",
            url: "ssh://apple.com/packagelib.git",
            .upToNextMinor(from: "1.2.3")
        )
    ],
    targets: [
        .target(
            name: "PackageName",
            dependencies: [
                .product(name: "PackageLib", package: "PackageLib"),
            ],
            path: "PackageName"),
        .target(
            name: "PackageName",
            dependencies: [
                "PackageName",
                .product(name: "PackageLib", package: "PackageLib"),
            ],
            path: "PackageNameImproved"),
        .target(
            name: "PackageNameUI",
            dependencies: [
                "PackageNameImproved",
            ],
            path: "PackageNameUI")
    ]
)

Package Manifest summary:

  • 3 targets
  • 1 target depends on the other one
  • Only PackageName is compatible with macOS, the other ones are iOS-only

Build commands

Building long-package-name-ios-Package(builds all targets within the package) for iOS:

swift build -Xswiftc "-sdk" -Xswiftc "$(xcrun --sdk iphonesimulator --show-sdk-path)" -Xswiftc "-target" -Xswiftc "arm64-apple-ios15.0-simulator"

[137/137] Build complete!

Now trying to build just the PackageName for macOS:

swift build -Xswiftc "-sdk" -Xswiftc "$(xcrun --sdk macosx --show-sdk-path)" --product "PackageName"

[1/1] Planning buildwarning: '--product' cannot be used with the automatic product 'PackageName'; building the default target instead

And it proceeds with building the default target long-package-name-ios-Package for the macOS platform which obviously fails due to inability to import, e.g. UIKit or any other iOS-only framework.

Question

How can I specify to build only PackageName for the macOS platform? I'm open to creating an Xcode Package, Schemes and other different configurations, but the end goals should be to:

  1. Integrate long-package-name-ios-Package (all targets) for iOS only
  2. Integrate PackageName for macOS

Solution

  • Edit: While my original answer makes the error go away, it is not possible to actually import the library into the plugin. (It fails to compile with a "no such module" error.) This seems to be intended behavior.


    The "automatic product" in the error refers to the fact that the PackageName has no configured library type. If you configure its type (either dynamic or static), the error goes away and the target can be built:

    .library(
      name: "PackageName",
      type: .dynamic,
      targets: ["PackageName"]
    )