Is there a way to specify platform specific source files for a target?
I'm looking for the best way to port this podspec that has a different set of sources for the iOS, macOS and tvOS variations.
Is there a way to specify platform specific source files for a target?
I'm looking for the best way to port this podspec that has a different set of sources for the iOS, macOS and tvOS variations.
The manifest API does not contain anything that was specifically designed for that yet.
#if os(macOS)
, etc.#if
statements in the manifest itself:let package = Package(/* ... */)
// There are a million ways to arrange this,
// but here is the general idea:
#if os(Linux)
package.targets.first(where: { $0.name == "MyTarget" })!
.exclude += ["Platform Shims/macOS"]
#elseif os(macOS)
package.targets.first(where: { $0.name == "MyTarget" })!
.exclude += ["Platform Shims/Linux"]
#endif
Now that we have some support for platform conditions in BuildSettingCondition, it might be interesting for someone to explore a proposal to allow a target to explicitly declare additional sources...
This seems to an approach for a host, but not a target platform. Adding if os(macOS)
does exclude the files from the macOS target, but it also excludes them from the iOS target.
How can I define files to include in the iOS target, but exclude from the macOS target?
Those bullet points were intended as two separate strategies.
You are doing the second bullet, where there was a caveat about crossâcompilation:
- If you do not care about crossâcompiling, then a viable workaround that doesnât touch the source files is to use
#if
statements in the manifest itself
The bullet you quote...
- The most semantically correct way is to frame the fileâs own source with
#if os(macOS)
, etc.
...was referring to the source of each file, not the manifest. For example:
NSApplication.swift
#if os(macOS)
import AppKit
extension NSApplication {
// ...
}
#endif
UIApplication.swift:
#if canImport(UIKIt) && !os(watchOS)
import UIKit
extension UIApplication {
// ...
}
#endif