Platform-dependent files in Swift Package

Viewed 247

I'm developing a cross-platform C++ library which supports iOS and macOS. The library could be integrated using Swift Package Manager. In C++ it's easy to have a single header file and multiple different implementation files that will be compiled for each platform. E.g, the header has a print() method and each target implements it differently:

macOS: "print_macOS"
iOS: "print_iOS"
Windows: "print_Windows"

Is it possible to somehow to make Swift Package Manager conditionally include/exclude specific files from the target depending on the platform?

The closest functionality I found is Conditional Target Dependencies, but it is about including/excluding specific libraries and not just files. I need to have a bit more granular approach.

Currently I'm using compile-time directives to resolve this issue:

#include "Print.h"

std::string Print::PrintMethod()
{
#if TARGET_OS_IPHONE
    return "print_iOS";
#else
    return "print_macOS";
#endif
}

So, are there any ways to achieve the same result but with different files? Note, that apart from those two files the target is completely identical for both platforms (iOS and macOS).

Reference Package.swift file:

// 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: "ResultingTarget",
    platforms: [
        .iOS(.v13), .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "ResultingTarget",
            targets: ["ResultingTarget"]),
    ],
    targets: [
        .target(
            name: "ResultingTarget",
            sources:
                [
                    // "File_for_iOS.mm", <----- Need to include only for iOS
                    "File_for_macOS.mm", <----- Need to include only for macOS
                    "Other_file.mm",
                ],
            publicHeadersPath: "Headers",
            cxxSettings: [
                .headerSearchPath("headers"),
            ]),
    ],
    cxxLanguageStandard: .cxx14
)
0 Answers
Related