168 lines
4.6 KiB
Bash
Executable File
168 lines
4.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cd "$ROOT_DIR"
|
|
|
|
WITH_WHISPER="${WITH_WHISPER:-1}"
|
|
WHISPER_CUDA="${WHISPER_CUDA:-0}"
|
|
BUILD_GUI="${BUILD_GUI:-1}"
|
|
JOBS="${JOBS:-$(nproc)}"
|
|
|
|
WHISPER_DIR="./vendor/whisper.cpp"
|
|
MODEL_PATH="./models/ggml-base.en.bin"
|
|
|
|
need_cmd() {
|
|
if ! command -v "$1" >/dev/null 2>&1; then
|
|
echo "[build] error: required command '$1' not found" >&2
|
|
exit 2
|
|
fi
|
|
}
|
|
|
|
have_cmd() {
|
|
command -v "$1" >/dev/null 2>&1
|
|
}
|
|
|
|
CFLAGS_COMMON=(
|
|
-std=c99 -Wall -Wextra -Wpedantic -O2 -g
|
|
-D_POSIX_C_SOURCE=200809L
|
|
)
|
|
|
|
LDFLAGS_COMMON=(
|
|
-lpulse-simple -lpulse -lX11 -lXtst -lpthread
|
|
)
|
|
|
|
echo "[build] root: $ROOT_DIR"
|
|
echo "[build] WITH_WHISPER=$WITH_WHISPER WHISPER_CUDA=$WHISPER_CUDA BUILD_GUI=$BUILD_GUI JOBS=$JOBS"
|
|
|
|
need_cmd gcc
|
|
|
|
if [[ "$WITH_WHISPER" == "1" ]]; then
|
|
if ! have_cmd cmake; then
|
|
echo "[build] warning: cmake not found; forcing WITH_WHISPER=0" >&2
|
|
WITH_WHISPER=0
|
|
fi
|
|
fi
|
|
|
|
if [[ "$WITH_WHISPER" == "1" ]]; then
|
|
if [[ ! -d "$WHISPER_DIR" ]] && ! have_cmd git; then
|
|
echo "[build] warning: git not found and whisper.cpp is missing; forcing WITH_WHISPER=0" >&2
|
|
WITH_WHISPER=0
|
|
fi
|
|
fi
|
|
|
|
if [[ "$WITH_WHISPER" == "1" ]]; then
|
|
if [[ ! -f "$MODEL_PATH" ]] && ! have_cmd wget; then
|
|
echo "[build] warning: wget not found and model is missing; forcing WITH_WHISPER=0" >&2
|
|
WITH_WHISPER=0
|
|
fi
|
|
fi
|
|
|
|
if [[ "$BUILD_GUI" == "1" ]]; then
|
|
if ! have_cmd pkg-config; then
|
|
echo "[build] warning: pkg-config not found; forcing BUILD_GUI=0" >&2
|
|
BUILD_GUI=0
|
|
elif ! pkg-config --exists gtk+-3.0; then
|
|
echo "[build] warning: gtk+-3.0 dev files not found; forcing BUILD_GUI=0" >&2
|
|
BUILD_GUI=0
|
|
else
|
|
GTK_CFLAGS_STR="$(pkg-config --cflags gtk+-3.0 2>/dev/null || true)"
|
|
if [[ -z "$GTK_CFLAGS_STR" ]]; then
|
|
echo "[build] warning: gtk+-3.0 cflags are unavailable; forcing BUILD_GUI=0" >&2
|
|
BUILD_GUI=0
|
|
else
|
|
if ! printf '#include <gtk/gtk.h>\n' | gcc -x c -fsyntax-only $GTK_CFLAGS_STR - >/dev/null 2>&1; then
|
|
echo "[build] warning: gtk headers are not usable (gtk/gtk.h check failed); forcing BUILD_GUI=0" >&2
|
|
BUILD_GUI=0
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
echo "[build] effective: WITH_WHISPER=$WITH_WHISPER BUILD_GUI=$BUILD_GUI"
|
|
|
|
if [[ "$WITH_WHISPER" == "1" ]]; then
|
|
if [[ ! -d "$WHISPER_DIR" ]]; then
|
|
echo "[build] cloning whisper.cpp..."
|
|
mkdir -p ./vendor
|
|
git clone https://github.com/ggerganov/whisper.cpp.git "$WHISPER_DIR"
|
|
fi
|
|
|
|
echo "[build] configuring whisper.cpp..."
|
|
if [[ "$WHISPER_CUDA" == "1" ]]; then
|
|
cmake -S "$WHISPER_DIR" -B "$WHISPER_DIR/build" -DWHISPER_CUDA=ON
|
|
else
|
|
cmake -S "$WHISPER_DIR" -B "$WHISPER_DIR/build"
|
|
fi
|
|
|
|
echo "[build] building whisper.cpp..."
|
|
cmake --build "$WHISPER_DIR/build" -j"$JOBS"
|
|
|
|
mkdir -p ./models
|
|
if [[ ! -f "$MODEL_PATH" ]]; then
|
|
echo "[build] downloading model $MODEL_PATH..."
|
|
wget -O "$MODEL_PATH" \
|
|
https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin
|
|
fi
|
|
fi
|
|
|
|
mkdir -p ./src
|
|
|
|
echo "[build] building CLI binary..."
|
|
CLI_CFLAGS=("${CFLAGS_COMMON[@]}")
|
|
CLI_LDFLAGS=("${LDFLAGS_COMMON[@]}")
|
|
|
|
if [[ "$WITH_WHISPER" == "1" ]]; then
|
|
CLI_CFLAGS+=(
|
|
-DWITH_WHISPER=1
|
|
-isystem "$WHISPER_DIR/include"
|
|
-isystem "$WHISPER_DIR/ggml/include"
|
|
)
|
|
CLI_LDFLAGS+=(
|
|
-L"$WHISPER_DIR/build/src"
|
|
-L"$WHISPER_DIR/build/ggml/src"
|
|
-Wl,-rpath,"$WHISPER_DIR/build/src"
|
|
-Wl,-rpath,"$WHISPER_DIR/build/ggml/src"
|
|
-lwhisper -lggml
|
|
)
|
|
fi
|
|
|
|
gcc "${CLI_CFLAGS[@]}" \
|
|
./src/main.c ./src/config.c ./src/hotkey.c ./src/audio.c ./src/transcribe.c ./src/typer.c \
|
|
-o ./voice_linux \
|
|
"${CLI_LDFLAGS[@]}"
|
|
|
|
if [[ "$BUILD_GUI" == "1" ]]; then
|
|
echo "[build] building GTK3 GUI binary..."
|
|
|
|
read -r -a GTK_CFLAGS <<< "$(pkg-config --cflags gtk+-3.0)"
|
|
read -r -a GTK_LIBS <<< "$(pkg-config --libs gtk+-3.0)"
|
|
|
|
GUI_CFLAGS=("${CFLAGS_COMMON[@]}" "${GTK_CFLAGS[@]}")
|
|
GUI_LDFLAGS=("${LDFLAGS_COMMON[@]}" "${GTK_LIBS[@]}")
|
|
|
|
if [[ "$WITH_WHISPER" == "1" ]]; then
|
|
GUI_CFLAGS+=(
|
|
-DWITH_WHISPER=1
|
|
-isystem "$WHISPER_DIR/include"
|
|
-isystem "$WHISPER_DIR/ggml/include"
|
|
)
|
|
GUI_LDFLAGS+=(
|
|
-L"$WHISPER_DIR/build/src"
|
|
-L"$WHISPER_DIR/build/ggml/src"
|
|
-Wl,-rpath,"$WHISPER_DIR/build/src"
|
|
-Wl,-rpath,"$WHISPER_DIR/build/ggml/src"
|
|
-lwhisper -lggml
|
|
)
|
|
fi
|
|
|
|
gcc "${GUI_CFLAGS[@]}" \
|
|
./src/gui_main.c ./src/config.c ./src/audio.c ./src/transcribe.c ./src/typer.c \
|
|
-o ./voice_linux_gui \
|
|
"${GUI_LDFLAGS[@]}"
|
|
fi
|
|
|
|
echo "[build] done: ./voice_linux (and ./voice_linux_gui if BUILD_GUI=1)"
|
|
echo "[build] run CLI: ./voice_linux"
|
|
echo "[build] run GUI: ./voice_linux_gui"
|