Platform specific source files

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.

1 Like

The manifest API does not contain anything that was specifically designed for that yet.

  • The most semantically correct way is to frame the file’s own source with #if os(macOS), etc.
  • 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:
    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
    
1 Like

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?

1 Like

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
1 Like
Terms of Service

Privacy Policy

Cookie Policy