How to prevent Xcode compiling a package for a target which is not designated?

  Kiến thức lập trình

I am building a package that contains a lot of classes, extensions and modules I have been building since I started developing for iOS in 2008.

Because I also develop for macOS, watchOS, iPadOS, and tvOS, this package has code for every flavor or Apple device.

In this package in particular, I have stuff for iOS and tvOS.

The package is declared like:

import PackageDescription

let package = Package(
  name: "MyPackage",
  platforms: [.iOS(.v16), .tvOS(.v13)],
  products: [
    // Products define the executables and libraries a package produces, making them visible to other packages.
    .library(
      name: "MyPackage",
      targets: ["MyPackage"]),
  ],
  targets: [
    .target(name: "MyPackage"),
    .testTarget(name: "MyPackageTests", dependencies: ["MyPackage"])
  ]
)

I have imported this package into a project that has two targets, one for iOS and another for watchOS because I want to use the stuff it contains for iOS in the iOS target.

I am now debugging the iOS target. Even so, Xcode gives a lot of errors, because it is trying to compile the package against the watchOS target.

This forces me to add #if !os("watchOS) and #endif in every file the package has to isolate the code and prevent it from being compiled by Xcode for the watchOS target. This is pretty stupid because the package is declared like this:

platforms: [.iOS(.v16), .tvOS(.v13)],

And because the package was just added for the iOS target.

Is there any way to do Xcode work as expected and not compile a package for a target it is not declared for?

LEAVE A COMMENT