summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 38d34d5903e761d78237b04afe5090b0ef311a9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Maintainer: he3als <me at he3als dot xyz>

pkgname=soulver-cpp-git
pkgver=r12.de956c0
pkgrel=1
pkgdesc="Simple C++ bindings for the SoulverCore Swift library"
arch=('x86_64')
url="https://github.com/vicinaehq/soulver-cpp"
license=('unknown')
depends=(
    'nlohmann-json'
    'gcc-libs'
    'glibc'
)
makedepends=(
    'git'
    'cmake'
    'ninja'
)
optdepends=(
    'swift-bin-6.1: Swift 6.1 toolchain (required for building)'
)
provides=('soulver-cpp')
conflicts=('soulver-cpp')
source=("${pkgname}::git+https://github.com/vicinaehq/soulver-cpp.git")
sha256sums=('SKIP')

_swift_version="6.1"
_swift_binary="swift"
_swift_search_prefixes=(
    "/usr/lib/swift-${_swift_version}"
    "/opt/swift-${_swift_version}"
    "/usr/local/swift-${_swift_version}"
    "/usr/lib/swift"
    "/opt/swift"
    "/usr/local/swift"
)

pkgver() {
    cd "$pkgname"
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

_find_swift() {
    local bin_path

    # check configured search paths
    for prefix in "${_swift_search_prefixes[@]}"; do
        bin_path="${prefix}/bin/${_swift_binary}"
        if [[ -x "$bin_path" ]]; then
            local ver
            ver=$("$bin_path" --version 2>&1 | grep -oP 'Swift version \K[0-9]+\.[0-9]+')
            if [[ "$ver" == "$_swift_version" ]]; then
                echo "$bin_path"
                return 0
            fi
        fi
    done

    # check if swift in PATH is the required version
    if command -v "$_swift_binary" &>/dev/null; then
        local ver
        ver=$("$_swift_binary" --version 2>&1 | grep -oP 'Swift version \K[0-9]+\.[0-9]+')
        if [[ "$ver" == "$_swift_version" ]]; then
            command -v "$_swift_binary"
            return 0
        fi
    fi

    return 1
}

prepare() {
    local swift_path
    swift_path=$(_find_swift)

    if [[ -z "$swift_path" ]]; then
        error "Swift ${_swift_version}.x not found. Please install swift-bin-${_swift_version} or another Swift ${_swift_version} toolchain."
        return 1
    fi

    # verify Swift version
    local swift_version
    swift_version=$("$swift_path" --version 2>&1 | grep -oP 'Swift version \K[0-9]+\.[0-9]+')
    if [[ "$swift_version" != "$_swift_version" ]]; then
        error "Swift ${_swift_version} required, found version $swift_version"
        return 1
    fi

    msg2 "Using Swift $swift_version at $swift_path"

    # export for build()
    echo "$swift_path" > "$srcdir/.swift_path"
}

build() {
    cd "$pkgname"

    local swift_bin
    swift_bin=$(cat "$srcdir/.swift_path")
    local swift_dir
    swift_dir=$(dirname "$swift_bin")

    # add Swift to PATH
    export PATH="$swift_dir:$PATH"

    cmake -GNinja -B build \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr
    cmake --build build
}

package() {
    cd "$pkgname"
    DESTDIR="$pkgdir" cmake --install build

    # install license (if it exists in the future)
    if [[ -f LICENSE ]]; then
        install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
    fi

    # install docs
    install -Dm644 README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
}