-
Notifications
You must be signed in to change notification settings - Fork 129
/
install.sh
executable file
·437 lines (388 loc) · 13.9 KB
/
install.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env bash
set -e
boost_url="https://boostorg.jfrog.io/artifactory/main/release/1.80.0/source/boost_1_80_0.tar.gz"
boost_additional_versions="1.81.0;1.80.0;1.79.0;1.78.0;1.77.0;1.76.0;1.75.0;1.74.0;1.73.0;1.72.0;1.71.0;1.70.0"
project_dir=$(pwd)
print_help() {
cat << EOF
Usage: ./install.sh [OPTIONS] [LIBRARY]
LIBRARY: one of: cxx, nodejs, python, emscripten
OPTIONS:
--clean Clean the build folder before compiling a library
--build-type Choose the build type (one of: debug, release)
--disable-openmp Disable OpenMP in the build.
--install-system-deps Install dependencies for compiling libraries (only aware of apt, brew and choco at the moment)
--install Install the CMake install targets to the prefix (see: --install-prefix)
--sudo-install Install the CMake install targets to the prefix with root privileges (see: --install-prefix)
--install-prefix Set the install prefix location for CMake installs (only valid when using --install)
--boost-prefix Set a custom path where to look for Boost
--python-executable Set a custom path (or name of) the Python executable
--python-prefix Set the python prefix, this will be passed to CMake as Python3_ROOT_DIR, to make sure CMake is using the correct Python installation.
--platform Set the platform, for when auto-detection doesn't work (one of: windows, macos, linux)
--macos-architecture Set the macOS architecture to compile for (one of: arm64, x86_64), useful for cross compiling.
--docker-image Set the docker image to forward this install command to, useful for cross compiling
--docker-before-install Run given commands in the docker container before running ./install.sh, (only valid when using --docker-image)
--cmake-generator Set the CMake Generator option
--cmake-generator-platform Set the CMake Generator Platform option, useful for cross compiling on Windows with the Visual Studio generator.
--node-architecture Set the node.js architecture to compile for, useful for cross compiling.
--test Run a test script after compiling the library
--help Shows this help page
EOF
exit 1
}
primary='\033[1;34m'
secondary='\033[1;35m'
nc='\033[0m'
prettyprint() {
printf "${primary}${1}${nc}${secondary}${2}${nc}\n"
}
get_os() {
if [ "${OCL_PLATFORM}" ]; then
echo "${OCL_PLATFORM}"
else
if [[ "${OSTYPE}" =~ ^darwin.* ]]; then
echo "macos"
elif [[ "${OSTYPE}" =~ ^linux.* ]]; then
echo "linux"
else
echo "windows"
fi
fi
}
determined_os=$(get_os)
prettyprint "Determined OS: " "${determined_os}"
# defaults
OCL_CLEAN="1"
OCL_BUILD_TYPE="release"
OCL_PLATFORM="${determined_os}"
OCL_INSTALL="1"
original_args="$*"
positional_args=()
while [[ "$#" -gt 0 ]]; do
case $1 in
--clean) OCL_CLEAN="1"; ;;
--build-type) OCL_BUILD_TYPE="$2"; shift ;;
--platform) OCL_PLATFORM="$2"; shift ;;
--install-system-deps) OCL_INSTALL_SYSTEM_DEPS="1"; ;;
--disable-openmp) OCL_DISABLE_OPENMP="1"; ;;
--install) OCL_INSTALL="1"; ;;
--sudo-install) OCL_SUDO_INSTALL="1"; ;;
--install-prefix) OCL_INSTALL_PREFIX="$2"; shift ;;
--boost-prefix) OCL_BOOST_PREFIX="$2"; shift ;;
--macos-architecture) OCL_MACOS_ARCHITECTURE="$2"; shift ;;
--docker-image) OCL_DOCKER_IMAGE="$2"; shift ;;
--docker-before-install) OCL_DOCKER_IMAGE_BEFORE_INSTALL="$2"; shift ;;
--cmake-generator) OCL_GENERATOR="$2"; shift ;;
--cmake-generator-platform) OCL_GENERATOR_PLATFORM="$2"; shift ;;
--python-executable) OCL_PYTHON_EXECUTABLE="$2"; shift ;;
--node-architecture) OCL_NODE_ARCH="$2"; shift ;;
--test) OCL_TEST="1"; ;;
--help|--*)
echo $1
print_help ;;
*)
# Append unmatched arguments to the positional_args array
positional_args+=("$1")
esac
shift
done
if [[ ${#positional_args[@]} != 1 ]] && [[ -z "${OCL_BUILD_LIBRARY}" ]]; then
print_help
exit 0
fi
if [[ ${#positional_args[@]} == 1 ]]; then
OCL_BUILD_LIBRARY="${positional_args[0]}"
fi
if [ -n "${OCL_INSTALL_PREFIX}" ] && [ -z "${OCL_INSTALL}" ] && [ -z "${OCL_SUDO_INSTALL}" ]; then
echo "WARN: Settings --install-prefix without setting --install or --sudo-install. add --install or --sudo-install option or remove the --install-prefix option"
fi
if [ "${OCL_BUILD_TYPE}" = "debug" ]; then
build_type="Debug"
build_type_lower="debug"
else
build_type="RelWithDebInfo"
build_type_lower="release"
fi
build_dir="${project_dir}/build/${OCL_BUILD_LIBRARY}/${build_type_lower}"
prettyprint "./install.sh " "$original_args"
num_procs() {
if [ "${determined_os}" = "macos" ]; then
sysctl -n hw.logicalcpu
elif [ "${determined_os}" = "linux" ]; then
nproc
else
echo "${NUMBER_OF_PROCESSORS:-"2"}"
fi
}
num_procs=$(num_procs)
prettyprint "Processor Cores: " "${num_procs}"
if [ -n "${OCL_DOCKER_IMAGE_BEFORE_INSTALL}" ] && [ -z "${OCL_DOCKER_IMAGE}" ]; then
prettyprint "Found before install, running: " "${OCL_DOCKER_IMAGE_BEFORE_INSTALL}"
eval "${OCL_DOCKER_IMAGE_BEFORE_INSTALL}"
fi
install_system_dependencies() {
if [ "${determined_os}" = "linux" ]; then
sudo apt update
sudo apt install -y --no-install-recommends git cmake curl build-essential libboost-dev python3 libboost-python-dev nodejs npm
elif [ "${determined_os}" = "macos" ]; then
brew install libomp boost python boost-python3 node
else
# @todo installing git and cmake fails in github's CI
choco install curl --no-progress
choco install boost-msvc-14.3 --no-progress
choco install nodejs --no-progress
fi
}
command_exists() {
command -v "${1}" >/dev/null 2>&1;
}
if [ -n "${OCL_DOCKER_IMAGE}" ]; then
prettyprint "Running the docker image with the following environment variables"
# collect all the options to check which ones are set and should be forwarded to the container
ALL_OPTIONS=($(compgen -A variable | grep '^OCL_'))
# create temporary env file
TMPENV="/tmp/.env${RANDOM}"
# empty the env file
echo "" > "${TMPENV}"
# escape the pwd to be used in find and replace later
ESCAPED_PWD=$(pwd | sed 's/\//\\\//g')
# look for options that are set
for OPTION in "${ALL_OPTIONS[@]}"; do
# exclude some that cause recursion or do not work in the container
if [ -n "${!OPTION}" ] && [ "${OPTION}" != "OCL_DOCKER_IMAGE" ] && [ "${OPTION}" != "OCL_INSTALL_SYSTEM_DEPS" ]; then
# save to options to the env file, replacing PWD with the container's /work mount path
FIXED_OPTION="${!OPTION//${ESCAPED_PWD}//work}"
echo "${OPTION}=${FIXED_OPTION}" >> "${TMPENV}"
prettyprint "${OPTION}: " "${FIXED_OPTION}"
fi
done
docker run --env-file "${TMPENV}" --user root -w /work -v "$(pwd):/work:rw" --rm "${OCL_DOCKER_IMAGE}" ./install.sh
exit 0
fi
# when the --macos-architecture flag is set, export the CMAKE_OSX_ARCHITECTURES variable for cross compiling and download libomp.
if [ -n "${OCL_MACOS_ARCHITECTURE}" ]; then
export CMAKE_OSX_ARCHITECTURES="${OCL_MACOS_ARCHITECTURE}"
fi
dependencies_nodejslib () {
(cd "${project_dir}/src/nodejslib" && npm install --silent)
}
dependencies_emscriptenlib () {
cd "${project_dir}"
if [ ! -d ../emsdk ]; then
(
cd ..
git clone https://github.com/emscripten-core/emsdk.git
)
fi
cd ../emsdk
./emsdk install latest
./emsdk activate latest
cd "${project_dir}"
}
build_clean() {
rm -rf "${build_dir}" || true
}
cmake_build() {
if [ "${determined_os}" = "windows" ]; then
cmake \
--build . \
--config "${build_type}" \
-j "${num_procs}"
else
cmake \
--build . \
-j "${num_procs}"
fi
}
cmake_install() {
if [ -n "${OCL_INSTALL}" ] || [ -n "${OCL_SUDO_INSTALL}" ]; then
prettyprint "Installing"
if [ "${determined_os}" = "windows" ]; then
${OCL_SUDO_INSTALL:+"sudo"} cmake \
--install . \
--config "${build_type}"
else
${OCL_SUDO_INSTALL:+"sudo"} cmake \
--install .
fi
fi
}
get_cmake_args() {
echo "-S . \
-B "${build_dir}" \
${OCL_GENERATOR:+"-G ${OCL_GENERATOR}"} \
${OCL_GENERATOR_PLATFORM:+"-A ${OCL_GENERATOR_PLATFORM}"} \
-D CMAKE_BUILD_TYPE="${build_type}" \
-D USE_STATIC_BOOST="ON" \
-D Boost_ADDITIONAL_VERSIONS="${boost_additional_versions}" \
-D VERSION_STRING="$(cat VERSION)" \
${OCL_DISABLE_OPENMP:+"-D USE_OPENMP=OFF"} \
${OCL_INSTALL_PREFIX:+"-D CMAKE_INSTALL_PREFIX=${OCL_INSTALL_PREFIX}"} \
${OCL_BOOST_PREFIX:+"-D BOOST_ROOT=${OCL_BOOST_PREFIX}"}"
}
get_cmakejs_args() {
echo "$(get_cmake_args)" | sed -e 's/-D /--CD/g' -e 's/-S/--directory/g' -e 's/-B/--out/g'
}
build_cxxlib() {
set -x
cmake $(get_cmake_args) -D BUILD_CXX_LIB="ON"
set +x
(cd "${build_dir}" && cmake_build)
if [ -n "${OCL_INSTALL}" ] || [ -n "${OCL_SUDO_INSTALL}" ]; then
prettyprint "Installing"
(cd "${build_dir}" && cmake_install)
fi
}
test_cxxlib() {
cd "${project_dir}/examples/cpp/test"
if [ -n "${OCL_CLEAN}" ]; then
prettyprint "Cleaning build directory..."
rm -rf build || true
fi
set -x
cmake \
-S . \
-B build \
${OCL_GENERATOR:+"-G ${OCL_GENERATOR}"} \
${OCL_GENERATOR_PLATFORM:+"-A ${OCL_GENERATOR_PLATFORM}"} \
-D Boost_ADDITIONAL_VERSIONS="${boost_additional_versions}" \
${OCL_BOOST_PREFIX:+"-DBOOST_ROOT=${OCL_BOOST_PREFIX}"}
set +x
cd build
cmake_build
if [ "${determined_os}" = "windows" ]; then
./test_example.exe
else
./test_example
fi
}
build_nodejslib() {
set -x
determined_arch=$(node -p 'process.arch')
install_prefix_fallback="${project_dir}/src/npmpackage/build/Release/${determined_os}-nodejs-${determined_arch}"
OCL_INSTALL_PREFIX="${OCL_INSTALL_PREFIX:-"${install_prefix_fallback}"}"
src/nodejslib/node_modules/.bin/cmake-js \
build \
$(get_cmakejs_args) \
--parallel "${num_procs}" \
${OCL_NODE_ARCH:+"--arch=${OCL_NODE_ARCH}"} \
--CDBUILD_NODEJS_LIB="ON" \
--config "${build_type}"
set +x
if [ -n "${OCL_INSTALL}" ] || [ -n "${OCL_SUDO_INSTALL}" ]; then
(cd "${build_dir}" && cmake_install)
fi
}
test_nodejslib() {
cd "${project_dir}/src/npmpackage"
npm install
npm run build-node
cd ../../examples/nodejs
npm link ../../src/npmpackage
if [ "${build_type}" = "debug" ]; then
export DEBUG="1"
fi
node test.js
}
get_python_executable() {
if command_exists "python3"; then
python_executable_fallback="python3"
else
python_executable_fallback="python"
fi
echo "${OCL_PYTHON_EXECUTABLE:-"${python_executable_fallback}"}"
}
build_pythonlib() {
python_executable=$(get_python_executable)
export CMAKE_ARGS="${OCL_GENERATOR:+"-G ${OCL_GENERATOR} "}\
${OCL_GENERATOR_PLATFORM:+"-A ${OCL_GENERATOR_PLATFORM} "}\
-D CMAKE_BUILD_TYPE=${build_type} \
${OCL_BOOST_PREFIX:+"-D BOOST_ROOT=${OCL_BOOST_PREFIX} "}"
cd "${project_dir}"
${python_executable} -m pip install --verbose .
}
test_pythonlib() {
python_executable=$(get_python_executable)
cd "${project_dir}/examples/python"
${python_executable} test.py
}
build_emscriptenlib() {
source "${project_dir}/../emsdk/emsdk_env.sh"
set -x
install_prefix_fallback="${project_dir}/src/npmpackage/build"
OCL_INSTALL_PREFIX="${OCL_INSTALL_PREFIX:-"${install_prefix_fallback}"}"
OCL_DISABLE_OPENMP="1"
emcmake cmake $(get_cmake_args) \
-D BUILD_EMSCRIPTEN_LIB="ON" \
-D USE_STATIC_BOOST="ON"
cd "${build_dir}"
if [ "${determined_os}" = "windows" ]; then
emmake make \
--config "${build_type}" \
-j "${num_procs}"
else
emmake make \
-j "${num_procs}"
fi
if [ -n "${OCL_INSTALL}" ] || [ -n "${OCL_SUDO_INSTALL}" ]; then
prettyprint "Installing"
${OCL_SUDO_INSTALL:+"sudo"} emmake make install
fi
set +x
}
test_emscriptenlib() {
cd "${project_dir}/src/npmpackage"
npm install
npm run build-emscripten
cd ../../examples/emscripten
npm link ../../src/npmpackage
npm start &
server_pid=$!
sleep 3
node test.js
kill ${server_pid}
}
if [ -n "${OCL_INSTALL_SYSTEM_DEPS}" ]; then
prettyprint "Installing system dependencies..."
install_system_dependencies
fi
if [ -n "${OCL_CLEAN}" ]; then
prettyprint "Cleaning build directory..."
build_clean
fi
if [[ "${OCL_BUILD_LIBRARY}" == "cxx" ]]; then
prettyprint "Building C++ library"
build_cxxlib
if [ -n "${OCL_TEST}" ]; then
prettyprint "Testing C++ library"
test_cxxlib
fi
fi
if [[ "${OCL_BUILD_LIBRARY}" == "python" ]]; then
prettyprint "Building Python library " "${OCL_BOOST_PYTHON_VERSION}"
build_pythonlib
if [ -n "${OCL_TEST}" ]; then
prettyprint "Testing Python library"
test_pythonlib
fi
fi
if [[ "${OCL_BUILD_LIBRARY}" == "emscripten" ]]; then
prettyprint "Installing emscripten dependencies"
dependencies_emscriptenlib
prettyprint "Building emscripten library"
build_emscriptenlib
if [ -n "${OCL_TEST}" ]; then
prettyprint "Testing emscripten library"
test_emscriptenlib
fi
fi
if [[ "${OCL_BUILD_LIBRARY}" == "nodejs" ]]; then
prettyprint "Installing node.js dependencies"
dependencies_nodejslib
prettyprint "Building node.js library"
build_nodejslib
if [ -n "${OCL_TEST}" ]; then
prettyprint "Testing node.js library"
test_nodejslib
fi
fi