Excluding files in Swift Packages - swift

To exclude entire sections of a file, I can use macros to target platforms such as #if os(iOS) || os(watchOS).
Is there a way to do this in Package.swift, or another way to target a few files for specific platforms in Swift Package Manager?

Is there a way to do this in Package.swift ... ?
Swifty stuff also works in Package.swift since the package declaration file is itself a .swift file.
Here are some examples which use Swift 5.3 Package Manager Conditional Target Dependencies SE-0273 condition and when.
// swift-tools-version:5.3
import PackageDescription
// ...
targets: [
.target(
name: "BKSecurity",
dependencies: [
.product(name: "Crypto", condition: .when(platforms: [.linux])),
"BKFoundation"
]),
// swift-tools-version:5.3
import PackageDescription
// ...
targets: [
.target(
name: "CombineShim",
dependencies: [
.product(name: "OpenCombine",
package: "OpenCombine",
condition: .when(platforms: [.wasi, .linux])
)]
),
.target(
name: "TokamakShim",
dependencies: [
.target(name: "TokamakDOM", condition: .when(platforms: [.wasi])),
"SomeCommonDependency"
]
),
// swift-tools-version:5.3
import PackageDescription
let supportsCoreAudio: BuildSettingCondition =
.when(platforms: [.iOS, .macOS, .tvOS, .watchOS])
let supportsALSA: BuildSettingCondition =
.when(platforms: [.linux])
let package = Package(
name: "portaudio",
// ...
targets: [
.target(
name: "libportaudio",
dependencies: [],
cSettings: [
.define("PA_USE_COREAUDIO", supportsCoreAudio),
.define("PA_USE_ALSA", supportsALSA)
],
linkerSettings: [
.linkedLibrary("asound", supportsALSA),
.linkedFramework("CoreAudio", supportsCoreAudio),
.linkedFramework("CoreServices", supportsCoreAudio),
.linkedFramework("CoreFoundation", supportsCoreAudio),
.linkedFramework("AudioUnit", supportsCoreAudio),
.linkedFramework("AudioToolbox", supportsCoreAudio)
]),
]
//...
)
Note that #if os(…) can be used in Package.swift. However, Package.swift is evaluated, built and executed in the context of the build platform. So, #if os(…) is useful in the context when the target platform is the same as the build platform e.g. macOS, Linux or Windows.
Package.swift
import PackageDescription
let package = Package(
// ...
targets: {
var targets: [Target] = [
.testTarget(
name: "QuickTests",
dependencies: [ "Quick", "Nimble" ],
exclude: ["SomeFile.ext"]
),
]
#if os(macOS)
// macOS build platform
targets.append(contentsOf: [
.target(name: "QuickSpecBase", dependencies: []),
.target(name: "Quick", dependencies: [ "QuickSpecBase" ]),
])
#else
// not macOS build platform, e.g. linux
targets.append(contentsOf: [
.target(name: "Quick", dependencies: []),
])
#endif
return targets
}(),
)
See Also
The Swift Programming Language: Statements > Conditional Compilation Block
GitHub/Apple/swift: lib/Basic/LangOptions.cpp
Currently supported compilation OSs: macOS, tvOS, watchOS, iOS, Linux, FreeBSD,OpenBSD, Windows, Android, PS4, Cygwin, Haiku, WASI
GitHub/Apple/swift-evolution: SE-0273 Package Manager Conditional Target Dependencies

Related

xCode SPM multiple targets named

In my project I have two dependencies
.package(name: "Web3swift", url: "https://github.com/skywinder/web3swift.git", from: "3.0.6"),
.package(name: "Random", url: "https://github.com/vapor-community/random.git", .upToNextMinor(from: "1.2.0"))
the Random package also has Core dependencies (https://github.com/vapor/core.git), and the target package name Web3swift also has Core in it
let package = Package(
name: "Web3swift",
platforms: [
.macOS(.v10_15), .iOS(.v13)
],
products: [
.library(name: "web3swift", targets: ["web3swift"])
],
dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", .upToNextMinor(from: "5.3.0")),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.5.1"))
],
targets: [
.target(name: "secp256k1"),
.target(
name: "Core",
dependencies: ["BigInt", "secp256k1", "CryptoSwift"]
),
.target(
name: "web3swift",
dependencies: ["Core", "BigInt", "secp256k1"],
exclude: excludeFiles,
resources: [
.copy("./Browser/browser.js"),
.copy("./Browser/browser.min.js"),
.copy("./Browser/wk.bridge.min.js")
]
),
Now I have a problem:
multiple targets named 'Core' in: 'core', 'web3swift'; consider using the `moduleAliases` parameter in manifest to provide unique names
Can you please explain how to fix this?
When you look at the developer documentation, you'll note that this is only supported in Swift 5.7+. To fix the "...is unavailable" message, you'll need to update the header in Package.swift to: // swift-tools-version:5.7 (also requires XCode > 14).
For full details, you can refer to:
The Swift 5.7 release notes
The evolution proposal: SE-0339
AFAICT, your changes look correct.

Adding swift-docc-plugin to my project makes Swift 5.5 builds fail

I have a project that I recently added swift-docc-plugin to, so that I can export the DocC documentation as a static website.
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "Saga",
platforms: [
.macOS(.v12)
],
products: [
.library(name: "Saga", targets: ["Saga"]),
.executable(name: "watch", targets: ["SagaCLI"])
],
dependencies: [
.package(url: "https://github.com/kylef/PathKit", from: "1.0.1"),
.package(url: "https://github.com/JohnSundell/Codextended.git", from: "0.1.0"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", branch: "main"),
],
targets: [
.target(
name: "Saga",
dependencies: [
"PathKit",
"Codextended",
]
),
.executableTarget(
name: "SagaCLI",
dependencies: ["PathKit"]
),
.testTarget(
name: "SagaTests",
dependencies: ["Saga"]
),
]
)
The problem is that on Swift Package Index all builds on Swift 5.5 and below are failing:
error: package at 'https://github.com/apple/swift-docc-plugin.git' # 859caac534e94ace18b894ccd9ed301ae4aeda84 is using Swift tools version 5.6.0 but the installed version is 5.5.0 in https://github.com/apple/swift-docc-plugin.git
See https://swiftpackageindex.com/builds/1DF06709-E2CA-4F56-B793-9CC7C8FC0A9D for a full build log.
How do I solve this? I could remove swift-docc-plugin from the dependencies I guess, and add it every time I want to export the docs, but that seems like a ridiculously annoying workaround. I don't really want to increase the minimum version of my library just because of swift-docc-plugin either.
The solution was pretty easy in the end. Just add this to the end of Package.swift
#if swift(>=5.6)
package.dependencies += [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
]
#endif
Unfortunately, I don’t think there’s a way around that due to the way we’re testing a package by building it.
Here's some more detail: https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/1633#issuecomment-1075899345

Including resources in swift package manager manifest

I am porting an objective c cocoa pod over to swift package manager. It has a bundle of fonts in the package. In the original pod spec, there was a link of code that created the bundle.
s.resource_bundles = {
'mathFonts' => [ 'fonts/*.otf', 'fonts/*.plist' ]
}
In the swift package, I created a static bundle, called mathFonts.bundle and added it to the package as shown below.
in the manifest file, I tried to copy the resources as shown below:
import PackageDescription
let package = Package(
name: "iosMath",
platforms: [
.iOS(.v8)
],
products: [
.library(
name: "iosMath",
targets: ["iosMath"]),
],
dependencies: [
],
targets: [
.target(
name: "iosMath",
resources: [.process("mathFonts.bundle")]), // **<- this is where I am adding the resources**
.testTarget(
name: "iosMathTests",
dependencies: ["iosMath"],
cSettings: [
.headerSearchPath("iosMath")// 5
]),
]
)
I'm getting a runtime crash here:
return [NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:#"mathFonts" withExtension:#"bundle"]];
When I manually copy the bundle into the directory of the project that I am importing this package into, it works just fine. How do I include this bundle so that it is made accessible when the package is installed?

Swift Package Manager with resources compile errors

I am trying to use resources inside my Package.swift file:
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(
name: "MyPackage",
targets: ["MyPackage"])
],
targets: [
.target(
name: "MyPackage",
resources: [
.process("Resources/one.xml"),
.process("Resources/two.json"),
.process("Resources/three.json"),
]
)
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage"]
)
]
)
When I import and compile the package in another project, I am getting lots of errors, such as:
Cannot infer contextual base in reference to member 'target'
or:
Reference to member 'process' cannot be resolved without a contextual type
The files are located in my package project in Sources -> MyPackage -> Resources
I also tried .copy("Resources/one.xml"), etc
What am I missing?
You missed a , after the target close parentheses:
.target(
name: "BioSwift",
resources: [
.process("Resources/unimod.xml"),
.process("Resources/aminoacids.json"),
.process("Resources/elements.json"),
.process("Resources/enzymes.json"),
.process("Resources/functionalgroups.json"),
.process("Resources/hydropathy.json")
]
), // Here is the missed `,`
Also, you don't need to add files one by one! Instead, you can add a directory:
.process("Resources")

Strange Link Error With Command-Line Swift Test (May Be Swift Package Manager-Related)

I'm developing a set of interlinked SPM modules as a test/demo, and I've run into a strange linker error.
The problem occurs in this example, which has a Package.swift file that looks like this:
import PackageDescription
let package = Package(
name: "Package_C",
platforms: [
.iOS(.v11),
.tvOS(.v11),
.macOS(.v10_14),
.watchOS(.v5)
],
products: [
.library(
name: "Package-C",
type: .dynamic,
targets: ["Package_C"])
],
dependencies: [
.package(name: "Package_A", url: "git#github.com:LittleGreenViper/SPMArticle-Package_A.git", from: "1.0.0")
],
targets: [
.target(
name: "Package_C",
dependencies: [
.product(name: "Package-A", package: "Package_A")
],
path: "src"
),
.testTarget(
name: "Package_CTests",
dependencies: [
"Package_C"
],
path: "test"
)
]
)
The package builds fine with swift build, and everything works great, when run from Xcode.
The issue happens when I run swift test. I get the following error:
Undefined symbols for architecture x86_64:
"_$s9Package_CAAV6indentABSi_tcfC", referenced from:
_$s14Package_CTests4testC0C7QuicklyyyFSSyKXEfu_ in Package_CTests.swift.o
ld: symbol(s) not found for architecture x86_64
[4/5] Linking Package_CPackageTests
It looks like Package_C is not being linked in, but everything appears to be absolutely kosher (also, remember that everything else works fine. swift test is the only place this fails).
It should be noted that this project works fine, and does almost exactly the same thing.
The only difference that I can see, is the naming conventions of the directories. I'd really hate for that to be the issue, but I'll mess around with that, next.
Can anyone see what I can't see?
EDITED TO ADD: I should note that the static/dynamic thing doesn't seem to be the issue. I get the same error, even when I change Package_C to static.
Ugh. Looks like the issue is that the naming convention needs to be followed.
I switched from this:
To this:
and it started working. No file contents (other than removing the two path arguments from the Package.swift file) were changed.
Here is the new Package.swift file:
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "Package_C",
platforms: [
.iOS(.v11),
.tvOS(.v11),
.macOS(.v10_14),
.watchOS(.v5)
],
products: [
.library(
name: "Package-C",
type: .dynamic,
targets: ["Package_C"])
],
dependencies: [
.package(name: "Package_A", url: "git#github.com:LittleGreenViper/SPMArticle-Package_A.git", from: "1.0.0")
],
targets: [
.target(
name: "Package_C",
dependencies: [
.product(name: "Package-A", package: "Package_A")
]
),
.testTarget(
name: "Package_CTests",
dependencies: [
"Package_C"
]
)
]
)