Swift Package Manager. Platform Conditional Build

I’m working on iOS+tvOS package which contains common targets and a number of platform-specific ones. Package has platform-specific libs as products (e.g. SomeLibProductMobile / SomeLibProductTV).

When I trying to build a package via Xcode package project for iOS simulator, the build fails due to it run build for all targets having compiled unsupported code for iOS (e.g. focus API and code under #if os(iOS).

Is there any way, to compile this thing, beside splitting code to 3 different packages (common, iOS and tvOS)?

I managed to find some old discussions but no clear understanding how it’s intended to resolve such a problems on SPM.

We use "Wrapper" targets to define a narrower set of platforms than the package set. See example at firebase-ios-sdk/Package.swift at master · firebase/firebase-ios-sdk · GitHub

Thank for the idia!
If I got it right, here is my Package.swift manifest:

  • I define platform specific targets
  • Set it as conditional dependencies for wrapper-target
  • And publish wrapper target as a product

What I missed? Because I still getting all the targets complied regardless of platform I selected for build =(

// swift-tools-version:5.5
import PackageDescription

let package = Package(
    name: "Dependencies",
    defaultLocalization: "us",
    platforms: [
        .iOS(.v14),
        .tvOS(.v14)
    ],
    
    // MARK: - Products

    products: [

        .library(
            name: "Product",
            targets: [
                "Product"
            ]
        )

    ],
    
    // MARK: - Dependencies

    dependencies: [],
    
    // MARK: - Targets

    targets: [

        .target(
            name: "Product",
            dependencies: [
                "CommonTarget",

                .target(name: "MobileTarget", condition: .when(platforms: [.iOS])),
                .target(name: "TVTarget", condition: .when(platforms: [.tvOS]))
            ],
            path: "SwiftPM-PlatformExclude/ProductWrapper"
        ),

        .target(
            name: "CommonTarget",
            path: "Product/CommonTarget"
        ),
        
        .target(
            name: "MobileTarget",
            dependencies: [
                "CommonTarget"
            ],
            path: "Product/MobileTarget"
        ),
        
        .target(
            name: "TVTarget",
            dependencies: [
                "CommonTarget"
            ],
            path: "Product/TVTarget"
        )

    ]
)

If include a package to a project it makes possible to build only the products (libs) being linked to a target being built.

So no additional work required for now: I just made an example-workspace project, add package as local and build platform I needed.