Package
See
Using the Package Manager
https://www.swift.org/getting-started/#using-the-package-manager
Package Manager
How Swift imports C APIs
https://github.com/apple/swift/blob/main/docs/HowSwiftImportsCAPIs.md
hello
mkdir hello
cd hello
swift package init
.
├── Package.swift
├── README.md
├── Sources
│ └── hello
│ └── hello.swift
└── Tests
└── helloTests
└── helloTests.swift
4 directories, 4 files
./code/package/hello/Package.swift
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "hello",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "hello",
targets: ["hello"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "hello",
dependencies: []),
.testTarget(
name: "helloTests",
dependencies: ["hello"]),
]
)
./code/package/hello/README
# hello
A description of this package.
./code/package/hello/Sources/hello/hello.swift
public struct hello {
public private(set) var text = "Hello, World!"
public init() {
}
}
./code/package/hello/Tests/helloTests/helloTests.swift
import XCTest
@testable import hello
final class helloTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(hello().text, "Hello, World!")
}
}
hello2
mkdir hello2
cd hello2
swift package init --type executable
.
├── Package.swift
├── README.md
├── Sources
│ └── hello2
│ └── hello2.swift
└── Tests
└── hello2Tests
└── hello2Tests.swift
4 directories, 4 files
./code/package/hello2/Package.swift
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "hello2",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.executableTarget(
name: "hello2",
dependencies: []),
.testTarget(
name: "hello2Tests",
dependencies: ["hello2"]),
]
)
./code/package/hello2/README
# hello2
A description of this package.
./code/package/hello2/Sources/hello2/hello2.swift
@main
public struct hello2 {
public private(set) var text = "Hello, World!"
public static func main() {
print(hello2().text)
}
}
./code/package/hello2/Tests/hello2Tests/hello2Tests.swift
import XCTest
@testable import hello2
final class hello2Tests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(hello2().text, "Hello, World!")
}
}
swift run hello2
Building for debugging...
[3/3] Linking hello2
Build complete! (0.88s)
Hello, World!