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
|
#!/bin/bash
# author: nasr
set -e
date
echo "author: Abdellah El Morabit"
echo "Compiling mp3_player"
echo
BUILD_DIR=${BUILD_DIR:-build}
SOURCE_DIR=source
GEN_DIR=${BUILD_DIR}/generated
TARGET="mp3_player"
DEBUG=${DEBUG:-1}
# NOTE: hot reload: the ui is also built as build/player_ui.so, the running app swaps it in when it
# changes (./reload.sh rebuilds just that). on by default for debug builds, off for release.
HOT_RELOAD=${HOT_RELOAD:-$DEBUG}
# NOTE: release defaults to -O2 + native tuning, debug keeps -O0 but the audio hot paths
# are still vectorized by hand, so playback is fine in both.
INCLUDES="-I${SOURCE_DIR} -I${SOURCE_DIR}/base -I${SOURCE_DIR}/core -I${SOURCE_DIR}/platform -I${SOURCE_DIR}/render -I${SOURCE_DIR}/audio -I${SOURCE_DIR}/net -I${SOURCE_DIR}/ipc -I${SOURCE_DIR}/player -I${GEN_DIR}"
INCLUDES+=" $(pkg-config --cflags libpipewire-0.3 freetype2 harfbuzz xkbcommon libmpg123 libcurl)"
CFLAGS="-std=c++20 -fno-rtti -fno-exceptions -Wall -Wundef -Werror -no-pie -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-missing-field-initializers -fno-omit-frame-pointer"
CFLAGS+=" -DWAYLAND=1 -DVULKAN=1"
LIBS="-pthread -lm -ldl -lvulkan -lwayland-client"
LIBS+=" $(pkg-config --libs libpipewire-0.3 freetype2 harfbuzz xkbcommon libmpg123 libcurl)"
if [ "$DEBUG" -eq 1 ]; then
CFLAGS+=" -O0 -g3 -DDEBUG=1"
else
CFLAGS+=" -O2 -mcpu=native -g -DDEBUG=0"
fi
mkdir -p "$BUILD_DIR" "$GEN_DIR"
#- wayland protocols
# NOTE: generated once, only regenerated when the xml is newer than the output
PROTOCOL_DIR=${SOURCE_DIR}/platform/protocols
for xml in ${PROTOCOL_DIR}/*.xml; do
name=$(basename "$xml" .xml)
if [ ! -f "${GEN_DIR}/${name}-client.h" ] || [ "$xml" -nt "${GEN_DIR}/${name}-client.h" ]; then
wayland-scanner client-header "$xml" "${GEN_DIR}/${name}-client.h"
wayland-scanner private-code "$xml" "${GEN_DIR}/${name}-protocol.c"
fi
done
#- catalog seed
# NOTE: data/catalog.qcat baked into the binary, so a first start without network still works.
# aligned so it's read in place like a loaded file. if it's missing (bootstrapping) the seed is
# empty and the app needs the network once.
SEED_HEADER="${GEN_DIR}/catalog_seed.h"
if [ -f data/catalog.qcat ]; then
xxd -i -n catalog_seed data/catalog.qcat | sed 's/^unsigned char/alignas(16) static const unsigned char/; s/^unsigned int/static const unsigned int/' > "$SEED_HEADER"
else
printf 'alignas(16) static const unsigned char catalog_seed[8] = {0};\nstatic const unsigned int catalog_seed_len = 0;\n' > "$SEED_HEADER"
fi
#- shaders
# NOTE: glslc -mfmt=c spits out a C initializer list, we #include it straight into a u32 array
for shader in ${SOURCE_DIR}/render/shaders/*.{vert,frag}; do
name=$(basename "$shader" | tr '.' '_')
glslc -O -mfmt=c "$shader" -o "${GEN_DIR}/${name}.spv.h"
done
echo "Target: [${TARGET}]"
echo "Debug mode $(if [ "${DEBUG:-0}" -eq 1 ]; then echo [TRUE]; else echo [FALSE]; fi)"
echo "Building ${BUILD_DIR}/${TARGET}..."
# NOTE: the generated protocol glue is plain C, compile it separately and link it in
PROTO_OBJS=""
for c in ${GEN_DIR}/*-protocol.c; do
obj="${c%.c}.o"
if [ ! -f "$obj" ] || [ "$c" -nt "$obj" ]; then
gcc -c -O2 -fPIC "$c" -o "$obj"
fi
PROTO_OBJS+=" $obj"
done
if [ "$HOT_RELOAD" -eq 1 ]; then
CFLAGS+=" -DPLAYER_HOT_RELOAD=1"
# NOTE: built to a temp name and renamed, the app only ever sees a complete .so
g++ $INCLUDES $CFLAGS -fPIC -shared -DPLAYER_UI_DLL=1 "${SOURCE_DIR}/player/player.cpp" $PROTO_OBJS -fuse-ld=mold -o "${BUILD_DIR}/player_ui.so.tmp" $LIBS &
SO_PID=$!
fi
# NOTE: the binary defines malloc & co (heap profiling), export them so every library binds to them
EXPORTS=""
for symbol in malloc calloc realloc free memalign aligned_alloc posix_memalign; do EXPORTS+=" -Wl,--export-dynamic-symbol=${symbol}"; done
time g++ $INCLUDES $CFLAGS "${SOURCE_DIR}/player/player.cpp" $PROTO_OBJS -fuse-ld=mold $EXPORTS -o "${BUILD_DIR}/${TARGET}" $LIBS
if [ "$HOT_RELOAD" -eq 1 ]; then
wait $SO_PID
mv "${BUILD_DIR}/player_ui.so.tmp" "${BUILD_DIR}/player_ui.so"
fi
echo "==== BUILD SUCCESS ===="
|