summaryrefslogtreecommitdiff
path: root/package.sh
blob: 5c0cab00b7291678e29a7c26637d86f533fc7a7d (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
125
126
127
128
129
130
131
132
133
#!/bin/sh
# Build a single-file Linux distribution of stemsep.
#
#   ./package.sh                 native build (runs on systems with a recent glibc)
#   ./package.sh --portable      build stemsep in the gcc:7 (glibc 2.28) container
#   ./package.sh --no-build      package the existing ./stemsep
#
# The output embeds stemsep, libonnxruntime, the ONNX model, and license
# notices.  It extracts itself into a cache directory on first run.
set -eu
cd -- "$(dirname -- "$0")"

case "$(uname -s):$(uname -m)" in
    Linux:x86_64) ;;
    *)
        echo "error: only Linux x86-64 bundles are supported" >&2
        exit 1
        ;;
esac

output=dist/stemsep-linux-x86_64
zstd_level=${ZSTD_LEVEL:-19}
portable=0
skip_build=0
check_model=1
image=${DOCKER_IMAGE:-gcc:7}
model=models/htdemucs_6s.onnx
model_sha256=48f8e84945579f8ab340e083339e9221e03785dbe733a52c388200b6d3ca779a

usage()
{
    cat <<'EOF'
Usage: ./package.sh [OPTIONS]

  --portable        build stemsep in a Docker container based on glibc 2.28
  --no-build        do not rebuild stemsep; package the existing binary
  --output PATH     output file (default: dist/stemsep-linux-x86_64)
  --zstd-level N    compression level, 0 disables compression (default: 19)
  --skip-model-check  do not verify the pinned model SHA-256
  -h, --help        show this help
EOF
}

while [ $# -gt 0 ]; do
    case "$1" in
        --portable) portable=1 ;;
        --no-build) skip_build=1 ;;
        --output)
            [ $# -ge 2 ] || { echo "error: --output requires a value" >&2; exit 2; }
            output=$2
            shift
            ;;
        --zstd-level)
            [ $# -ge 2 ] || { echo "error: --zstd-level requires a value" >&2; exit 2; }
            zstd_level=$2
            shift
            ;;
        --skip-model-check) check_model=0 ;;
        -h|--help) usage; exit 0 ;;
        *) echo "error: unknown option '$1'" >&2; usage >&2; exit 2 ;;
    esac
    shift
done

if [ "$skip_build" -eq 0 ]; then
    if [ "$portable" -eq 1 ]; then
        command -v docker >/dev/null 2>&1 || {
            echo "error: --portable requires docker" >&2
            exit 1
        }
        echo "Building stemsep in $image (x86-64, glibc 2.28 baseline)..."
        docker run --rm --platform linux/amd64 \
            -u "$(id -u):$(id -g)" \
            -v "$PWD:/work" -w /work "$image" ./build.sh
    else
        ./build.sh
    fi
fi

[ -x ./stemsep ] || {
    echo "error: ./stemsep is missing; run ./build.sh first" >&2
    exit 1
}
[ -f "$model" ] || {
    echo "error: $model is missing; see README.md" >&2
    exit 1
}

if [ "$check_model" -eq 1 ]; then
    actual=$(sha256sum "$model" | cut -d ' ' -f 1)
    if [ "$actual" != "$model_sha256" ]; then
        echo "error: $model has SHA-256 $actual" >&2
        echo "       expected $model_sha256 (use --skip-model-check to override)" >&2
        exit 1
    fi
fi

command -v python3 >/dev/null 2>&1 || {
    echo "error: python3 is required to package the bundle" >&2
    exit 1
}

mkdir -p build dist
cc=${CC:-gcc}
echo "Building static launcher..."
$cc -std=c11 -O2 -DNDEBUG -Wall -Wextra -Wpedantic -static \
    -o build/launcher packaging/launcher.c -lzstd -pthread

echo "Packing bundle (zstd level $zstd_level)..."
set -- \
    --launcher build/launcher \
    --output "$output" \
    --zstd-level "$zstd_level" \
    --add stemsep=./stemsep \
    --add third_party/onnxruntime/lib/libonnxruntime.so.1.21.0=third_party/onnxruntime/lib/libonnxruntime.so.1.21.0 \
    --symlink third_party/onnxruntime/lib/libonnxruntime.so.1=libonnxruntime.so.1.21.0 \
    --add "$model=$model" \
    --add licenses/ONNX-Runtime-LICENSE.txt=third_party/onnxruntime/LICENSE \
    --add licenses/ONNX-Runtime-ThirdPartyNotices.txt=third_party/onnxruntime/ThirdPartyNotices.txt \
    --add licenses/htdemucs-6s-model-README.md=models/README.md \
    --add licenses/Demucs-LICENSE.txt=packaging/licenses/DEMUCS-LICENSE.txt \
    --add licenses/htdemucs-6s-ONNX-LICENSE.txt=packaging/licenses/HTDEMUCS-6S-ONNX-LICENSE.txt \
    --add licenses/dr_mp3-LICENSE.txt=third_party/dr_mp3/LICENSE \
    --add licenses/dr_wav-LICENSE.txt=third_party/dr_wav/LICENSE \
    --add README.md=README.md
if [ "$check_model" -eq 1 ]; then
    set -- "$@" --checksum "$model=$model_sha256"
fi
python3 packaging/mkbundle.py "$@"

echo "Bundle: $output"
echo "Run './$output' on Linux x86-64. First run extracts to \$STEMSEP_CACHE,"
echo "or \$XDG_CACHE_HOME/stemsep, or ~/.cache/stemsep."