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
|
#!/bin/bash
# author: nasr
# linux build (x86_64 and aarch64). macos uses build_macos.sh, windows build.bat.
#
# ./compile.sh debug build + hot reload (build/design_ui.so)
# DEBUG=0 ./compile.sh release: one portable binary (build/release/design-it)
#
# knobs: DEBUG (1), HOT_RELOAD (= DEBUG), STATIC (= !DEBUG: link freetype + harfbuzz statically,
# built from source by scripts/deps.sh), NATIVE (0: 1 = tune for this cpu, binary won't run on
# older ones), CXX (g++), BUILD_DIR (build, release goes to build/release)
set -e
cd "$(dirname "$0")"
date
echo "Compiling design-it"
echo
DEBUG=${DEBUG:-1}
if [ "$DEBUG" -eq 1 ]; then BUILD_DIR=${BUILD_DIR:-build}; else BUILD_DIR=${BUILD_DIR:-build/release}; fi
SOURCE_DIR=source
GEN_DIR=${BUILD_DIR}/generated
TARGET="design-it"
CXX=${CXX:-g++}
ARCH=$(uname -m)
# NOTE: hot reload: the ui is also built as build/design_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}
STATIC=${STATIC:-$((1 - DEBUG))}
NATIVE=${NATIVE:-0}
INCLUDES="-I${SOURCE_DIR} -I${SOURCE_DIR}/base -I${SOURCE_DIR}/core -I${SOURCE_DIR}/platform -I${SOURCE_DIR}/render -I${SOURCE_DIR}/diagram -I${SOURCE_DIR}/app -I${GEN_DIR} -I${SOURCE_DIR}/render/shaders/prebuilt -I${SOURCE_DIR}/third_party"
INCLUDES+=" $(pkg-config --cflags xkbcommon)"
CFLAGS="-std=c++20 -fno-rtti -fno-exceptions -Wall -Wundef -Werror -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-missing-field-initializers -Wno-format-truncation -fno-omit-frame-pointer"
CFLAGS+=" -DVULKAN=1"
# NOTE: vulkan is dlopen'd at runtime (render_vk_loader), never linked. wayland and x11 are the
# two window backends picked at runtime, both are on every linux desktop.
LIBS="-pthread -lm -ldl -lwayland-client -lX11 $(pkg-config --libs xkbcommon)"
if [ "$STATIC" -eq 1 ]; then
DEPS_DIR=${DEPS_DIR:-build/deps/linux-${ARCH}}
scripts/deps.sh "$DEPS_DIR"
INCLUDES+=" -I${DEPS_DIR}/include/freetype2 -I${DEPS_DIR}/include/harfbuzz"
LIBS+=" ${DEPS_DIR}/lib/libharfbuzz.a ${DEPS_DIR}/lib/libfreetype.a -static-libstdc++ -static-libgcc"
else
INCLUDES+=" $(pkg-config --cflags freetype2 harfbuzz)"
LIBS+=" $(pkg-config --libs freetype2 harfbuzz)"
fi
if [ "$DEBUG" -eq 1 ]; then
CFLAGS+=" -O0 -g3 -DDEBUG=1"
else
CFLAGS+=" -O2 -g -DDEBUG=0"
if [ "$NATIVE" -eq 1 ]; then
case "$ARCH" in
x86_64|i?86) CFLAGS+=" -march=native" ;;
*) CFLAGS+=" -mcpu=native" ;;
esac
fi
fi
LINKER=""
if command -v mold >/dev/null 2>&1; then LINKER="-fuse-ld=mold"; fi
mkdir -p "$BUILD_DIR" "$GEN_DIR"
#- wayland protocols
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
#- shaders
# NOTE: optional: without glslc the checked in source/render/shaders/prebuilt/*.spv.h are used.
# after editing a shader with glslc around, copy the new ${GEN_DIR}/*.spv.h over the prebuilt ones.
if command -v glslc >/dev/null 2>&1; then
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
fi
echo "Target: [${TARGET}] ${ARCH}"
echo "Debug mode $(if [ "${DEBUG:-0}" -eq 1 ]; then echo [TRUE]; else echo [FALSE]; fi), static deps $(if [ "$STATIC" -eq 1 ]; then echo [TRUE]; else echo [FALSE]; fi)"
echo "Building ${BUILD_DIR}/${TARGET}..."
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+=" -DDESIGN_HOT_RELOAD=1"
$CXX $INCLUDES $CFLAGS -fPIC -shared -DDESIGN_UI_DLL=1 "${SOURCE_DIR}/app/app.cpp" $PROTO_OBJS $LINKER -o "${BUILD_DIR}/design_ui.so.tmp" $LIBS &
SO_PID=$!
fi
# NOTE: the binary defines malloc & co (heap profiling), export them so every library binds to them
EXPORTS=""
case "$CXX" in
*zig*) EXPORTS="-rdynamic" ;; # zig's linker has no --export-dynamic-symbol, export everything
*) for symbol in malloc calloc realloc free memalign aligned_alloc posix_memalign; do EXPORTS+=" -Wl,--export-dynamic-symbol=${symbol}"; done ;;
esac
time $CXX $INCLUDES $CFLAGS -no-pie "${SOURCE_DIR}/app/app.cpp" $PROTO_OBJS $LINKER $EXPORTS -o "${BUILD_DIR}/${TARGET}" $LIBS
if [ "$HOT_RELOAD" -eq 1 ]; then
wait $SO_PID
mv "${BUILD_DIR}/design_ui.so.tmp" "${BUILD_DIR}/design_ui.so"
fi
if [ "$DEBUG" -eq 0 ]; then
echo "shared libraries the binary needs (all part of a normal linux desktop):"
ldd "${BUILD_DIR}/${TARGET}" | awk '{print " " $1}' | grep -v "linux-vdso\|ld-linux"
fi
echo "==== BUILD SUCCESS ===="
|