-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathbuild.sh
executable file
·237 lines (199 loc) · 6.61 KB
/
build.sh
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2020 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
#
# FTL Engine
# Build script for FTL
#
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.
# Abort script if one command returns a non-zero value
set -e
# Set builddir
builddir="cmake/"
# Parse arguments
# If the first argument starts in "-D", we pass it to CMake
if [[ "${1}" == "-D"* ]]; then
cmake_args="${1}"
shift
fi
# Parse the remaining arguments
for var in "$@"
do
case "${var}" in
"clean" ) clean=1;;
"nobuild" ) nobuild=1;;
"install" ) install=1;;
"restart" ) restart=1;;
"tail" ) tail=1;;
"debug" ) debug=1;;
"dev" ) dev=1;;
"test" ) test=1;;
"clean-logs" ) clean_logs=1;;
"clang" ) clang=1;;
"ci" ) builddir="cmake_ci/";;
"-h" | "help" ) help=0;;
* ) echo -e "Unknown option: ${var}\n"; help=1;;
esac
done
# Display help text if requested
if [[ -n "${help}" ]]; then
cat << EOF
Usage: $0 [options]
Helper script simplifying the build process of Pi-hole FTL.
Shortcuts:
dev Build, install, restart, and tail logs.
debug Build, install, restart, and attach debugger.
Other options:
clean Clean the build environment before building.
nobuild Do not trigger a build, e.g., after cleaning.
install Install the built binaries (requires sudo).
restart Restart the pihole-FTL service (requires sudo).
clean-logs Clean the FTL and dnsmasq log files.
tail Tail (follow) the FTL and dnsmasq log files.
-h, help Display this help text.
Special CI options:
ci Use the CI build directory (cmake_ci/).
clang Use clang as the compiler.
test Run tests after building.
If no options are provided, the script will build the sources.
If the -d option is provided, the script will build, install, restart,
and tail the two most important log file. The -d option is intended
for development purposes.
EOF
exit "${help}"
fi
# debug, tail and dev are mutually exclusive
if [[ $((debug + tail + dev)) -gt 1 ]]; then
echo "Error: debug, tail, and dev are mutually exclusive options."
exit 1
fi
# If we are in debug mode, we also want to build, install, and restart
if [[ -n "${debug}" ]]; then
install=1
restart=1
fi
# If we are in dev mode, we want to build, install, restart, and tail the logs
# by default
if [[ -n "${dev}" ]]; then
install=1
restart=1
tail=1
fi
# Check if we need sudo
SUDO=""
if [[ -n "${install}" || -n "${restart}" || -n "${clean_logs}" ]]; then
# Check if we are root, if not, we need sudo
if [[ $(id -u) -ne 0 ]]; then
SUDO="sudo"
fi
fi
# Prepare build environment
if [[ -n "${clean}" ]]; then
echo "Cleaning build environment"
# Remove build directory
rm -rf "${builddir}"
fi
# Remove possibly outdated api/docs elements
for filename in src/api/docs/hex/* src/api/docs/hex/**/*; do
# Skip if not a file
if [ ! -f "${filename}" ]; then
continue
fi
# Get the original filename
original_filename="${filename/"src/api/docs/hex/"/"src/api/docs/content/"}"
# Remove the file if it is outdated
if [ "${filename}" -ot "${original_filename}" ]; then
rm "${filename}"
fi
done
# Remove compiled LUA scripts if older than the plain ones
for scriptname in src/lua/scripts/*.lua; do
if [ -f "${scriptname}.hex" ] && [ "${scriptname}.hex" -ot "${scriptname}" ]; then
rm "${scriptname}.hex"
fi
done
# If we are asked to NOT build, we exit here
if [[ -n ${nobuild} ]]; then
exit 0
fi
# Set compiler to clang if requested
if [[ -n "${clang}" ]]; then
export CC=clang
export CXX=clang++
export STATIC="false"
fi
# Configure build, pass CMake CACHE entries if present
# Wrap multiple options in "" as first argument to ./build.sh:
# ./build.sh "-DA=1 -DB=2" install
mkdir -p "${builddir}"
cd "${builddir}"
if [[ -n ${cmake_args} ]]; then
cmake "${cmake_args}" ..
else
cmake ..
fi
# Build the sources with the number of available cores
cmake --build . -- -j "$(nproc)"
# Checksum verification
./pihole-FTL verify
# If we are asked to install, we do this here (requires root privileges)
# Otherwise, we simply copy the binary one level down
if [[ -n "${install}" ]]; then
echo "Installing pihole-FTL"
${SUDO} cmake --install .
else
echo "Copying compiled pihole-FTL binary to repository root"
cp pihole-FTL ../
fi
# If we are asked to run tests, we do this here
if [[ -n "${test}" ]]; then
cd ..
bash test/arch_test.sh
bash test/run.sh
fi
# If we are asked to restart, we do this here
if [[ -n "${restart}" ]]; then
echo "Restarting pihole-FTL"
# First, reset the failure-counter in case a previous error caused a
# restarting loop now preventing systemd from starting FTL
${SUDO} systemctl reset-failed pihole-FTL
# Restart FTL
${SUDO} systemctl restart pihole-FTL
fi
# If we are asked to clean the logs, we do this here
if [[ -n "${clean_logs}" ]]; then
echo "Cleaning log files"
for log_file in "$(pihole-FTL --config files.log.ftl)" "$(pihole-FTL --config files.log.dnsmasq)"; do
echo "Cleaning ${log_file}"
echo "" | ${SUDO} tee "$log_file"
done
fi
# If we want to attach the debugger, we do this here
if [[ -n "${debug}" ]]; then
echo "Waiting for pihole-FTL to start..."
pid_file=$(pihole-FTL --config files.pid)
# Loop until the pid file is created and non-empty
while [ ! -f "${pid_file}" ] || [ ! -s "${pid_file}" ]; do
sleep 0.1
done
# Get the pid from the pid file
pid=$(cat "${pid_file}")
# Attach gdb to the process
echo "Attaching debugger to pihole-FTL (PID: ${pid})..."
${SUDO} gdb -p "${pid}"
fi
# If we are asked to tail the log, we do this here
if [[ -n "${tail}" ]]; then
# Check if tmux is installed
if ! command -v tmux &> /dev/null; then
echo "Error: tmux is not installed. Please install tmux to use the tail option."
exit 1
fi
# Get the log file locations
ftl_log=$(pihole-FTL --config files.log.ftl)
dnsmasq_log=$(pihole-FTL --config files.log.dnsmasq)
# Create tmux sub-session with two panes next to each other each running a tail command
tmux new-session -d "tail -f ${ftl_log}" \; split-window -h "tail -f ${dnsmasq_log}" \; attach
fi