Swift SPM

SPM
SPM Usage

仿照教學,建立 system lib(libgit2),並且在另一個專案引用 libgit2

產生 swift proj
1
swift package init --type executable # [empty|library|executable|system-module]
產生 XCode 專案
1
2
3
4
5
swift package \
-Xcc -std=c89 \
-Xswiftc -I/usr/local/include/git2 \
-Xlinker -L/usr/local/lib \
generate-xcodeproj
tree
1
2
3
4
5
6
7
├── Package.swift
├── Sources
│ └── Hello.swift
└── Tests
├── HelloTests
│ └── HelloTests.swift
└── LinuxMain.swift

Require System Libraries (libgit2)

首先要先安裝 libgit2,可自行選擇手動安裝或利用 brew

手動安裝
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# https://libgit2.github.com/
# Adding set(CMAKE_MACOSX_RPATH 1) into CMakeLists.txt
mkdir build && cd build
# cmake .. -DCMAKE_INSTALL_PREFIX=/install/prefix
cmake .. \
-DCMAKE_OSX_ARCHITECTURES="i386;x86_64" \
-DLIB_INSTALL_DIR=/usr/local/lib \
-DINCLUDE_INSTALL_DIR=/usr/local/include
cmake --build . --target install

# clean
rm /usr/local/lib/libgit2.0.25.1.dylib
rm /usr/local/lib/libgit2.25.dylib
rm /usr/local/lib/libgit2.dylib
rm /usr/local/lib/pkgconfig/libgit2.pc
rm /usr/local/include/git2.h
rm -rf /usr/local/include/git2
套件軟體安裝
1
brew install libgit2

建立 System Lib (Clibgit)

swift package init --type system-module

Package.swift
1
2
3
4
5
6
import PackageDescription

let package = Package(
name: "Clibgit",
pkgConfig: "libgit2"
)
module.modulemap
1
2
3
4
5
module Clibgit [system] {
header "/usr/local/include/git2.h"
link "git2"
export *
}
tag 版本(Clibgit)
1
2
3
4
git init
git add .
git commit -m "Initial Commit"
git tag 1.0.0

開始引用 System Lib

swift package init --type executable

Package.swift
1
2
3
4
5
6
7
8
import PackageDescription

let package = Package(
name: "example",
dependencies: [
.Package(url: "../Clibgit", majorVersion: 1)
]
)
main.swift
1
2
3
4
import Clibgit

let options = git_repository_init_options()
print(options)

執行

swift build -Xlinker -L/usr/local/lib/ && .build/debug/example

產生 XCode 專案

swift package -Xcc -std=c89 -Xswiftc -I/usr/local/include/git2 -Xlinker -L/usr/local/lib generate-xcodeproj


REPL include Clibgit

swift -I Clibgit


PackageDescription

Package
1
2
3
4
5
6
7
8
Package(
name: String,
pkgConfig: String? = nil, // pkg-config (.pc), for system modules
providers: [SystemPackageProvider]? = nil,
targets: [Target] = [],
dependencies: [Package.Dependency] = [],
exclude: [String] = []
)
Target
1
Target(name: "Bar", dependencies: ["Foo"])
Package.Dependency
1
2
3
.Package(url: "ssh://git@example.com/Greeter.git", versions: Version(1,0,0)..<Version(2,0,0)),
.Package(url: "../StringExtensions", "1.0.0"),
.Package(url: "https://github.com/MyAwesomePackage", majorVersion: 1, minor: 4),
exclude
1
exclude: ["Sources/Fixtures", "Sources/readme.md", "Tests/FooTests/images"]
pkgConfig
1
pkgConfig: "gtk+-3.0" // gtk+-3.0.pc, 從環境變數 PKG_CONFIG_PATH 下搜尋
Provider
1
2
3
4
 providers: [
.Brew("gtk+3"),
.Apt("gtk3")
]

待 …

.Package
1
2
3
```

``` swift Version