-
Notifications
You must be signed in to change notification settings - Fork 245
/
build_helper.sh
executable file
·384 lines (344 loc) · 11.8 KB
/
build_helper.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash -eu
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# This is a helpful script to build MVFST in the supplied dir
# It pulls in dependencies such as folly and fizz in the _build/deps dir.
# Obtain the mvfst repository root folder at the very start
MVFST_ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Useful constants
COLOR_RED="\033[0;31m"
COLOR_GREEN="\033[0;32m"
COLOR_OFF="\033[0m"
usage() {
cat 1>&2 <<EOF
Usage ${0##*/} [-h|?] [-p PATH] [-i INSTALL_PREFIX]
-p BUILD_DIR (optional): Path of the base dir for mvfst
-i INSTALL_PREFIX (optional): install prefix path
-m (optional): Build folly without jemalloc
-s (optional): Skip installing system package dependencies
-c (optional): Use ccache
-f (optional): Skip fetching dependencies (to test local changes)
-h|? Show this help message
EOF
}
FETCH_DEPENDENCIES=true
while getopts ":hp:i:mscf" arg; do
case $arg in
p)
BUILD_DIR="${OPTARG}"
;;
i)
INSTALL_PREFIX="${OPTARG}"
;;
m)
MVFST_FOLLY_USE_JEMALLOC="n"
;;
s)
MVFST_SKIP_SYSTEM_DEPENDENCIES=true
;;
c)
MVFST_USE_CCACHE=true
;;
f)
FETCH_DEPENDENCIES=false
;;
h | *) # Display help.
usage
exit 0
;;
esac
done
# Validate required parameters
if [ -z "${BUILD_DIR-}" ] ; then
echo -e "${COLOR_RED}[ INFO ] Build dir is not set. So going to build into _build ${COLOR_OFF}"
BUILD_DIR=_build
mkdir -p $BUILD_DIR
fi
if [[ -n "${MVFST_FOLLY_USE_JEMALLOC-}" ]]; then
if [[ "$MVFST_FOLLY_USE_JEMALLOC" != "n" ]]; then
unset $MVFST_FOLLY_USE_JEMALLOC
fi
fi
### Configure necessary build and install directories
cd $BUILD_DIR || exit
BWD=$(pwd)
DEPS_DIR=$BWD/deps
mkdir -p "$DEPS_DIR"
MVFST_BUILD_DIR=$BWD/build
mkdir -p "$MVFST_BUILD_DIR"
if [ -z "${INSTALL_PREFIX-}" ]; then
FOLLY_INSTALL_DIR=$DEPS_DIR
FIZZ_INSTALL_DIR=$DEPS_DIR
MVFST_INSTALL_DIR=$BWD
else
FOLLY_INSTALL_DIR=$INSTALL_PREFIX
FIZZ_INSTALL_DIR=$INSTALL_PREFIX
MVFST_INSTALL_DIR=$INSTALL_PREFIX
fi
CMAKE_EXTRA_ARGS=(${CMAKE_EXTRA_ARGS-})
if [[ ! -z "${MVFST_USE_CCACHE-}" ]]; then
CCACHE=$(which ccache)
CMAKE_EXTRA_ARGS+=(-DCMAKE_C_COMPILER_LAUNCHER="${CCACHE}")
CMAKE_EXTRA_ARGS+=(-DCMAKE_CXX_COMPILER_LAUNCHER="${CCACHE}")
fi
if [[ ! -z "${MVFST_FOLLY_USE_JEMALLOC-}" ]]; then
CMAKE_EXTRA_ARGS+=(-DFOLLY_USE_JEMALLOC=0)
fi
# Default to parallel build width of 4.
# If we have "nproc", use that to get a better value.
# If not, then intentionally go a bit conservative and
# just use the default of 4 (e.g., some desktop/laptop OSs
# have a tendency to freeze if we actually use all cores).
set +x
nproc=4
if [ -z "$(hash nproc 2>&1)" ]; then
nproc=$(nproc)
fi
set -x
function install_dependencies_linux() {
sudo apt-get install \
g++ \
cmake \
m4 \
libboost-all-dev \
libevent-dev \
libdouble-conversion-dev \
libgoogle-glog-dev \
libgflags-dev \
libiberty-dev \
liblz4-dev \
liblzma-dev \
libsnappy-dev \
make \
zlib1g-dev \
binutils-dev \
libjemalloc-dev \
libssl-dev \
pkg-config \
libsodium-dev
}
function install_dependencies_mac() {
# install the default dependencies from homebrew
brew install \
cmake \
m4 \
boost \
double-conversion \
gflags \
glog \
libevent \
lz4 \
snappy \
xz \
openssl \
libsodium
brew link \
boost \
double-conversion \
gflags \
glog \
libevent \
lz4 \
snappy \
xz \
libsodium
}
function setup_fmt() {
FMT_DIR=$DEPS_DIR/fmt
FMT_BUILD_DIR=$DEPS_DIR/fmt/build/
FMT_TAG=$(grep "subdir = " ../build/fbcode_builder/manifests/fmt | cut -d "-" -f 2)
if [ ! -d "$FMT_DIR" ] ; then
echo -e "${COLOR_GREEN}[ INFO ] Cloning fmt repo ${COLOR_OFF}"
git clone https://github.com/fmtlib/fmt.git "$FMT_DIR"
fi
cd "$FMT_DIR"
git fetch --tags
git checkout "${FMT_TAG}"
echo -e "${COLOR_GREEN}Building fmt ${COLOR_OFF}"
mkdir -p "$FMT_BUILD_DIR"
cd "$FMT_BUILD_DIR" || exit
cmake \
-DCMAKE_PREFIX_PATH="$DEPS_DIR" \
-DCMAKE_INSTALL_PREFIX="$DEPS_DIR" \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DFMT_DOC=OFF \
-DFMT_TEST=OFF \
${CMAKE_EXTRA_ARGS[@]+"${CMAKE_EXTRA_ARGS[@]}"} \
..
make -j "$nproc"
make install
echo -e "${COLOR_GREEN}fmt is installed ${COLOR_OFF}"
cd "$BWD" || exit
}
function setup_googletest() {
GTEST_DIR=$DEPS_DIR/googletest
GTEST_BUILD_DIR=$DEPS_DIR/googletest/build/
GTEST_TAG=$(grep "subdir = " ../build/fbcode_builder/manifests/googletest | cut -d "-" -f 2,3)
if [ ! -d "$GTEST_DIR" ] ; then
echo -e "${COLOR_GREEN}[ INFO ] Cloning googletest repo ${COLOR_OFF}"
git clone https://github.com/google/googletest.git "$GTEST_DIR"
fi
cd "$GTEST_DIR"
git fetch --tags
git checkout "${GTEST_TAG}"
echo -e "${COLOR_GREEN}Building googletest ${COLOR_OFF}"
mkdir -p "$GTEST_BUILD_DIR"
cd "$GTEST_BUILD_DIR" || exit
cmake \
-DCMAKE_PREFIX_PATH="$DEPS_DIR" \
-DCMAKE_INSTALL_PREFIX="$DEPS_DIR" \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
..
make -j "$nproc"
make install
echo -e "${COLOR_GREEN}googletest is installed ${COLOR_OFF}"
cd "$BWD" || exit
}
function synch_dependency_to_commit() {
# Utility function to synch a dependency to a specific commit. Takes two arguments:
# - $1: folder of the dependency's git repository
# - $2: path to the text file containing the desired commit hash
if [ "$FETCH_DEPENDENCIES" = false ] ; then
return
fi
DEP_REV=$(sed 's/Subproject commit //' "$2")
pushd "$1"
git fetch
# Disable git warning about detached head when checking out a specific commit.
git -c advice.detachedHead=false checkout "$DEP_REV"
popd
}
function setup_zstd() {
ZSTD_DIR=$DEPS_DIR/zstd
ZSTD_BUILD_DIR=$DEPS_DIR/zstd/build/cmake/builddir
ZSTD_INSTALL_DIR=$DEPS_DIR
ZSTD_TAG=$(grep "subdir = " ../build/fbcode_builder/manifests/zstd | cut -d "-" -f 2 | cut -d "/" -f 1)
if [ ! -d "$ZSTD_DIR" ] ; then
echo -e "${COLOR_GREEN}[ INFO ] Cloning zstd repo ${COLOR_OFF}"
git clone https://github.com/facebook/zstd.git "$ZSTD_DIR"
fi
cd "$ZSTD_DIR"
git fetch --tags
git checkout "v${ZSTD_TAG}"
echo -e "${COLOR_GREEN}Building Zstd ${COLOR_OFF}"
mkdir -p "$ZSTD_BUILD_DIR"
cd "$ZSTD_BUILD_DIR" || exit
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_TESTS=OFF \
-DCMAKE_PREFIX_PATH="$ZSTD_INSTALL_DIR" \
-DCMAKE_INSTALL_PREFIX="$ZSTD_INSTALL_DIR" \
${CMAKE_EXTRA_ARGS[@]+"${CMAKE_EXTRA_ARGS[@]}"} \
..
make -j "$nproc"
make install
echo -e "${COLOR_GREEN}Zstd is installed ${COLOR_OFF}"
cd "$BWD" || exit
}
function setup_folly() {
FOLLY_DIR=$DEPS_DIR/folly
FOLLY_BUILD_DIR=$DEPS_DIR/folly/build/
if [ ! -d "$FOLLY_DIR" ] ; then
echo -e "${COLOR_GREEN}[ INFO ] Cloning folly repo ${COLOR_OFF}"
git clone https://github.com/facebook/folly.git "$FOLLY_DIR"
if [[ -z "${MVFST_SKIP_SYSTEM_DEPENDENCIES-}" ]]; then
echo -e "${COLOR_GREEN}[ INFO ] install dependencies ${COLOR_OFF}"
if [ "$Platform" = "Linux" ]; then
install_dependencies_linux
elif [ "$Platform" = "Mac" ]; then
install_dependencies_mac
else
echo -e "${COLOR_RED}[ ERROR ] Unknown platform: $Platform ${COLOR_OFF}"
exit 1
fi
else
echo -e "${COLOR_GREEN}[ INFO ] Skipping installing dependencies ${COLOR_OFF}"
fi
fi
synch_dependency_to_commit "$FOLLY_DIR" "$MVFST_ROOT_DIR/build/deps/github_hashes/facebook/folly-rev.txt"
if [ "$Platform" = "Mac" ]; then
# Homebrew installs OpenSSL in a non-default location on MacOS >= Mojave
# 10.14 because MacOS has its own SSL implementation. If we find the
# typical Homebrew OpenSSL dir, load OPENSSL_ROOT_DIR so that cmake
# will find the Homebrew version.
dir=/usr/local/opt/openssl
if [ -d $dir ]; then
export OPENSSL_ROOT_DIR=$dir
fi
fi
echo -e "${COLOR_GREEN}Building Folly ${COLOR_OFF}"
mkdir -p "$FOLLY_BUILD_DIR"
cd "$FOLLY_BUILD_DIR" || exit
# check for environment variable. If
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DCMAKE_PREFIX_PATH="$FOLLY_INSTALL_DIR" \
-DCMAKE_INSTALL_PREFIX="$FOLLY_INSTALL_DIR" \
${CMAKE_EXTRA_ARGS[@]+"${CMAKE_EXTRA_ARGS[@]}"} \
..
make -j "$nproc"
make install
echo -e "${COLOR_GREEN}Folly is installed ${COLOR_OFF}"
cd "$BWD" || exit
}
function setup_fizz() {
FIZZ_DIR=$DEPS_DIR/fizz
FIZZ_BUILD_DIR=$DEPS_DIR/fizz/build/
if [ ! -d "$FIZZ_DIR" ] ; then
echo -e "${COLOR_GREEN}[ INFO ] Cloning fizz repo ${COLOR_OFF}"
git clone https://github.com/facebookincubator/fizz "$FIZZ_DIR"
fi
synch_dependency_to_commit "$FIZZ_DIR" "$MVFST_ROOT_DIR/build/deps/github_hashes/facebookincubator/fizz-rev.txt"
echo -e "${COLOR_GREEN}Building Fizz ${COLOR_OFF}"
mkdir -p "$FIZZ_BUILD_DIR"
cd "$FIZZ_BUILD_DIR" || exit
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_TESTS=OFF \
-DCMAKE_PREFIX_PATH="$FIZZ_INSTALL_DIR" \
-DCMAKE_INSTALL_PREFIX="$FIZZ_INSTALL_DIR" \
${CMAKE_EXTRA_ARGS[@]+"${CMAKE_EXTRA_ARGS[@]}"} \
"$FIZZ_DIR/fizz"
make -j "$nproc"
make install
echo -e "${COLOR_GREEN}Fizz is installed ${COLOR_OFF}"
cd "$BWD" || exit
}
function detect_platform() {
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) Platform=Linux;;
Darwin*) Platform=Mac;;
*) Platform="UNKNOWN:${unameOut}"
esac
echo -e "${COLOR_GREEN}Detected platform: $Platform ${COLOR_OFF}"
}
function setup_rust() {
if ! [ -x "$(command -v rustc)" ] || ! [ -x "$(command -v cargo)" ]; then
echo -e "${COLOR_RED}[ ERROR ] Rust not found (required for CCP support).${COLOR_OFF}\n"
echo -e " To install rust, run the following command, then rerun build_helper.sh:\n"
echo -e " curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y\n"
echo -e " You may also need to run \`source $HOME/.cargo/env\` after installing to add rust to your PATH.\n\n"
exit
else
echo -e "${COLOR_GREEN}[ INFO ] Found rust (required for CCP support).${COLOR_OFF}"
fi
}
detect_platform
setup_fmt
setup_googletest
setup_zstd
setup_folly
setup_fizz
# build mvfst:
cd "$MVFST_BUILD_DIR" || exit
mvfst_cmake_build_args=(
-DCMAKE_PREFIX_PATH="$FOLLY_INSTALL_DIR" \
-DCMAKE_INSTALL_PREFIX="$MVFST_INSTALL_DIR" \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_TESTS=On \
${CMAKE_EXTRA_ARGS[@]+"${CMAKE_EXTRA_ARGS[@]}"} \
)
cmake "${mvfst_cmake_build_args[@]}" ../..
make -j "$nproc"
echo -e "${COLOR_GREEN}MVFST build is complete. To run unit test: \
cd _build/build && make test ${COLOR_OFF}"