forked from d99kris/nchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.sh
executable file
·289 lines (250 loc) · 9.62 KB
/
make.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env bash
# make.sh
#
# Copyright (C) 2020-2024 Kristofer Berggren
# All rights reserved.
#
# See LICENSE for redistribution information.
# helper functions
exiterr()
{
>&2 echo "${1}"
exit 1
}
show_usage()
{
echo "usage: make.sh [OPTIONS] ACTION"
echo ""
echo "Options:"
echo " --no-telegram - build without telegram support"
echo " --no-whatsapp - build without whatsapp support"
echo " --yes,-y - non-interactive mode, assume yes"
echo ""
echo "Action:"
echo " deps - install project dependencies"
echo " build - perform build"
echo " debug - perform debug build"
echo " tests - perform build and run tests"
echo " doc - perform build and generate documentation"
echo " install - perform build and install"
echo " all - perform deps, build, tests, doc and install"
echo " src - perform source code reformatting"
echo ""
}
function version_ge() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" == "$2";
}
# process arguments
DEPS="0"
BUILD="0"
DEBUG="0"
TESTS="0"
DOC="0"
INSTALL="0"
SRC="0"
YES=""
CMAKEARGS=""
if [[ "${#}" == "0" ]]; then
show_usage
exit 1
fi
while [[ ${#} -gt 0 ]]; do
case "${1%/}" in
deps)
DEPS="1"
;;
build)
BUILD="1"
;;
debug)
DEBUG="1"
;;
test*)
BUILD="1"
TESTS="1"
;;
doc)
BUILD="1"
DOC="1"
;;
install)
BUILD="1"
INSTALL="1"
;;
src)
SRC="1"
;;
all)
DEPS="1"
BUILD="1"
TESTS="1"
DOC="1"
INSTALL="1"
;;
--no-telegram)
CMAKEARGS="${CMAKEARGS} -DHAS_TELEGRAM=OFF"
;;
--no-whatsapp)
CMAKEARGS="${CMAKEARGS} -DHAS_WHATSAPP=OFF"
;;
-y)
YES="-y"
;;
--yes)
YES="-y"
;;
*)
show_usage
exit 1
;;
esac
shift
done
# deps
if [[ "${DEPS}" == "1" ]]; then
OS="$(uname)"
if [ "${OS}" == "Linux" ]; then
unset NAME
eval $(grep "^NAME=" /etc/os-release 2> /dev/null)
if [[ "${NAME}" == "Ubuntu" ]]; then
sudo apt update && sudo apt ${YES} install ccache cmake build-essential gperf help2man libreadline-dev libssl-dev libncurses-dev libncursesw5-dev ncurses-doc zlib1g-dev libsqlite3-dev libmagic-dev || exiterr "deps failed (${NAME}), exiting."
unset VERSION_ID
eval $(grep "^VERSION_ID=" /etc/os-release 2> /dev/null)
if version_ge "${VERSION_ID}" "22.04"; then
sudo apt ${YES} install golang || exiterr "deps failed (${NAME} apt golang), exiting."
else
sudo snap install go --classic || exiterr "deps failed (${NAME} snap go), exiting."
fi
elif [[ "${NAME}" == "Raspbian GNU/Linux" ]] || [[ "${NAME}" == "Debian GNU/Linux" ]]; then
sudo apt update && sudo apt ${YES} install ccache cmake build-essential gperf help2man libreadline-dev libssl-dev libncurses-dev libncursesw5-dev ncurses-doc zlib1g-dev libsqlite3-dev libmagic-dev golang || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "Gentoo" ]]; then
sudo emerge -n dev-util/cmake dev-util/ccache dev-util/gperf sys-apps/help2man sys-libs/readline dev-libs/openssl sys-libs/ncurses sys-libs/zlib dev-db/sqlite sys-apps/file dev-lang/go || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "Fedora Linux" ]]; then
sudo dnf ${YES} install git cmake clang golang ccache file-devel file-libs gperf readline-devel openssl-devel ncurses-devel sqlite-devel zlib-devel || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "Arch Linux" ]] || [[ "${NAME}" == "EndeavourOS" ]]; then
sudo pacman -S ccache cmake file go gperf help2man ncurses openssl readline sqlite zlib base-devel || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "Void" ]]; then
sudo xbps-install ${YES} base-devel go ccache cmake gperf help2man libmagick-devel readline-devel sqlite-devel file-devel openssl-devel || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "Alpine Linux" ]]; then
sudo apk add git build-base cmake ncurses-dev openssl-dev sqlite-dev file-dev go linux-headers zlib-dev ccache gperf readline || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "openSUSE Tumbleweed" ]]; then
sudo zypper install ${YES} -t pattern devel_C_C++ && sudo zypper install ${YES} go ccache cmake libopenssl-devel sqlite3-devel file-devel readline-devel || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "Chimera" ]]; then
doas apk add git cmake clang go ccache gperf readline-devel openssl-devel ncurses-devel sqlite-devel zlib-devel file-devel || exiterr "deps failed (${NAME}), exiting."
elif [[ "${NAME}" == "Rocky Linux" ]]; then
sudo yum config-manager --set-enabled powertools && sudo yum ${YES} groupinstall "Development Tools" && sudo yum ${YES} install git go cmake gperf readline-devel openssl-devel ncurses-devel zlib-devel sqlite-devel file-devel || exiterr "deps failed (${NAME}), exiting."
else
exiterr "deps failed (unsupported linux distro ${NAME}), exiting."
fi
elif [ "${OS}" == "Darwin" ]; then
if command -v brew &> /dev/null; then
GOPKG="go"
if [[ "${GITHUB_ACTIONS}" == "true" ]]; then
GOPKG="" # skip go in github actions
fi
HOMEBREW_NO_AUTO_UPDATE=1 brew install ${GOPKG} gperf cmake openssl ncurses ccache readline sqlite libmagic || exiterr "deps failed (${OS} brew), exiting."
elif command -v port &> /dev/null; then
sudo port -N install go gperf cmake openssl ncurses ccache readline sqlite3 libmagic || exiterr "deps failed (${OS} port), exiting."
else
exiterr "deps failed (${OS} missing brew and port), exiting."
fi
else
exiterr "deps failed (unsupported os ${OS}), exiting."
fi
fi
# src
if [[ "${SRC}" == "1" ]]; then
go fmt lib/wmchat/go/*.go || \
exiterr "go fmt failed, exiting."
uncrustify --update-config-with-doc -c etc/uncrustify.cfg -o etc/uncrustify.cfg && \
uncrustify -c etc/uncrustify.cfg --replace --no-backup src/*.{cpp,h} lib/common/src/*.h lib/duchat/src/*.{cpp,h} lib/ncutil/src/*.{cpp,h} lib/tgchat/src/*.{cpp,h} lib/wmchat/src/*.{cpp,h} || \
exiterr "unrustify failed, exiting."
fi
# build
if [[ "${BUILD}" == "1" ]]; then
OS="$(uname)"
if [ "${OS}" == "Linux" ]; then
MEM="$(( $(($(getconf _PHYS_PAGES) * $(getconf PAGE_SIZE) / (1000 * 1000 * 1000))) * 1000 ))" # in MB
elif [ "${OS}" == "Darwin" ]; then
MEM="$(( $(($(sysctl -n hw.memsize) / (1000 * 1000 * 1000))) * 1000 ))" # in MB
fi
MEM_NEEDED_PER_CORE="3500" # tdlib under g++ needs 3.5 GB
if [[ "$(c++ -dM -E -x c++ - < /dev/null | grep CLANG_ATOMIC > /dev/null ; echo ${?})" == "0" ]]; then
MEM_NEEDED_PER_CORE="1500" # tdlib under clang++ needs 1.5 GB
fi
MEM_MAX_THREADS="$((${MEM} / ${MEM_NEEDED_PER_CORE}))"
if [[ "${MEM_MAX_THREADS}" == "0" ]]; then
MEM_MAX_THREADS="1" # minimum 1 core
fi
if [[ "${OS}" == "Darwin" ]]; then
CPU_MAX_THREADS="$(sysctl -n hw.ncpu)"
else
CPU_MAX_THREADS="$(nproc)"
fi
if [[ ${MEM_MAX_THREADS} -gt ${CPU_MAX_THREADS} ]]; then
MAX_THREADS=${CPU_MAX_THREADS}
else
MAX_THREADS=${MEM_MAX_THREADS}
fi
MAKEARGS="-j${MAX_THREADS}"
echo "-- Using ${MAKEARGS} (${CPU_MAX_THREADS} cores, ${MEM} MB phys mem, ${MEM_NEEDED_PER_CORE} MB mem per core needed)"
mkdir -p build && cd build && cmake ${CMAKEARGS} .. && make -s ${MAKEARGS} && cd .. || exiterr "build failed, exiting."
fi
# debug
if [[ "${DEBUG}" == "1" ]]; then
OS="$(uname)"
if [ "${OS}" == "Linux" ]; then
MEM="$(( $(($(getconf _PHYS_PAGES) * $(getconf PAGE_SIZE) / (1000 * 1000 * 1000))) * 1000 ))" # in MB
elif [ "${OS}" == "Darwin" ]; then
MEM="$(( $(($(sysctl -n hw.memsize) / (1000 * 1000 * 1000))) * 1000 ))" # in MB
fi
MEM_NEEDED_PER_CORE="3500" # tdlib under g++ needs 3.5 GB
if [[ "$(c++ -dM -E -x c++ - < /dev/null | grep CLANG_ATOMIC > /dev/null ; echo ${?})" == "0" ]]; then
MEM_NEEDED_PER_CORE="1500" # tdlib under clang++ needs 1.5 GB
fi
MEM_MAX_THREADS="$((${MEM} / ${MEM_NEEDED_PER_CORE}))"
if [[ "${MEM_MAX_THREADS}" == "0" ]]; then
MEM_MAX_THREADS="1" # minimum 1 core
fi
if [[ "${OS}" == "Darwin" ]]; then
CPU_MAX_THREADS="$(sysctl -n hw.ncpu)"
else
CPU_MAX_THREADS="$(nproc)"
fi
if [[ ${MEM_MAX_THREADS} -gt ${CPU_MAX_THREADS} ]]; then
MAX_THREADS=${CPU_MAX_THREADS}
else
MAX_THREADS=${MEM_MAX_THREADS}
fi
MAKEARGS="-j${MAX_THREADS}"
echo "-- Using ${MAKEARGS} (${CPU_MAX_THREADS} cores, ${MEM} MB phys mem, ${MEM_NEEDED_PER_CORE} MB mem per core needed)"
mkdir -p dbgbuild && cd dbgbuild && cmake -DCMAKE_BUILD_TYPE=Debug ${CMAKEARGS} .. && make -s ${MAKEARGS} && cd .. || exiterr "debug build failed, exiting."
fi
# tests
if [[ "${TESTS}" == "1" ]]; then
true || exiterr "tests failed, exiting."
fi
# doc
if [[ "${DOC}" == "1" ]]; then
if [[ -x "$(command -v help2man)" ]]; then
if [[ "$(uname)" == "Darwin" ]]; then
SED="gsed -i"
else
SED="sed -i"
fi
help2man -n "ncurses chat" -N -o src/nchat.1 ./build/bin/nchat && ${SED} "s/\.\\\\\" DO NOT MODIFY THIS FILE\! It was generated by help2man.*/\.\\\\\" DO NOT MODIFY THIS FILE\! It was generated by help2man./g" src/nchat.1 || exiterr "doc failed, exiting."
fi
fi
# install
if [[ "${INSTALL}" == "1" ]]; then
OS="$(uname)"
if [ "${OS}" == "Linux" ]; then
cd build && sudo make install && cd .. || exiterr "install failed (linux), exiting."
elif [ "${OS}" == "Darwin" ]; then
cd build && make install && cd .. || exiterr "install failed (mac), exiting."
else
exiterr "install failed (unsupported os ${OS}), exiting."
fi
fi
# exit
exit 0