Created
April 12, 2019 14:16
-
-
Save fsaintjacques/b4302b2f8a218dadfad5a2b942cef2fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu | |
PWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
SRC_DIR=$(realpath "${PWD}/..") | |
CXX_SRC=${SRC_DIR}/cpp | |
# The following can be set | |
: "${CMAKE:=cmake}" | |
error() { | |
echo "$@" >&2 | |
exit 1 | |
} | |
check_installed() { | |
which "$1" > /dev/null | |
} | |
parse_argv() { | |
check_installed docopts || error "docopts is missing, install with 'pip install docopts'" | |
# Parse the argv into an associative array stored in a local variable named | |
# __args. We cannot write in the `args` implicit scoped variable due to | |
# redefinition and shadowing. | |
eval "$(docopts -A __args -V - -h - : "$@" <<EOF | |
Usage: $0 [options] <build_dir> | |
Prepare a cmake build directory for arrow. | |
Options: | |
--cc=<compiler> Set C compiler [default: cc]. | |
--cxx=<compiler> Set C++ compiler [default: c++]. | |
--cxx-flags=<extra> Set extra compiler flags. | |
--generator=<gen> CMake generator type [default: Ninja]. | |
--build-type=<build> CMake build type [default: DEBUG]. | |
--warn-level=<level> Warning level, possible options are EVERYTHING, | |
CHECKIN, PRODUCTION [default: CHECKIN]. | |
--cmake-extra=<args> CMake extra argument to pass. | |
--with-tests=<opt> Activate tests [default: ON]. | |
--with-benchmarks=<opt> Activate benchmarks [default: ON]. | |
--with-python=<opt> Activate python bindings [default: ON]. | |
--with-parquet=<opt> Activate parquet component [default: ON]. | |
--with-gandiva=<opt> Activate gandiva component [default: ON]. | |
--with-plasma=<opt> Activate plasma component [default: ON]. | |
--with-flight=<opt> Activate flight component [default: OFF]. | |
--with-asan=<opt> Activate ASAN sanitization [default: OFF]. | |
--force Delete exisiting build dir if found. | |
--trace Trace this script. | |
--help Show help options. | |
---- | |
EOF | |
)" | |
# copy __args into args | |
# disable shellcheck since __args is defined in the previous eval. | |
# shellcheck disable=SC2154 | |
for k in "${!__args[@]}"; do args+=(["$k"]=${__args["$k"]}); done | |
# enable `set -x` if tracing is requested | |
if [[ "${args[--trace]}" == "true" ]]; then set -x; fi | |
} | |
build_cmake_argv() { | |
# Define generator | |
cmake_argv+=("-G${args[--generator]}") | |
# toolchain | |
export "CC=${args[--cc]}" | |
export "CXX=${args[--cxx]}" | |
if [[ ! -z "${args[--cxx-flags]:-}" ]]; then | |
cmake_argv+=("-DARROW_CXXFLAGS=${args[--cxx-flags]}") | |
fi | |
# Build type | |
cmake_argv+=("-DCMAKE_BUILD_TYPE=${args[--build-type]}") | |
cmake_argv+=("-DARROW_BUILD_TESTS=${args[--with-tests]}") | |
cmake_argv+=("-DARROW_BUILD_BENCHMARKS=${args[--with-benchmarks]}") | |
cmake_argv+=("-DBUILD_WARNING_LEVEL=${args[--warn-level]}") | |
cmake_argv+=("-DARROW_USE_ASAN=${args[--with-asan]}") | |
# Components | |
cmake_argv+=("-DARROW_PYTHON=${args[--with-python]}") | |
cmake_argv+=("-DARROW_PARQUET=${args[--with-parquet]}") | |
cmake_argv+=("-DARROW_GANDIVA=${args[--with-gandiva]}") | |
cmake_argv+=("-DARROW_PLASMA=${args[--with-plasma]}") | |
cmake_argv+=("-DARROW_FLIGHT=${args[--with-flight]}") | |
# conda-ism | |
if [[ ! -z "${CONDA_PREFIX:-}" ]]; then | |
local arrow_home=${CONDA_PREFIX} | |
for v in ARROW_HOME ARROW_BUILD_TOOLCHAIN PARQUET_HOME BOOST_HOME; do | |
export "${v}=${arrow_home}" | |
done | |
cmake_argv+=("-DCMAKE_INSTALL_PREFIX=${arrow_home}") | |
cmake_argv+=("-DCMAKE_INSTALL_LIBDIR=lib") | |
fi | |
if [[ ! -z "${args[--cmake-extra]:-}" ]]; then | |
cmake_argv+=(${args[--cmake-extra]}) | |
fi | |
cmake_argv+=("$CXX_SRC") | |
} | |
invoke_cmake() { | |
local build_dir=${args[<build_dir>]} | |
local force=${args[--force]} | |
# Deal with existing build_dir | |
if [[ -e "$build_dir" ]]; then | |
if [[ "$force" != "true" ]]; then | |
error "$build_dir already exists, pass --force to override" | |
fi | |
rm -r "$build_dir" | |
fi | |
# CMake 3.13 supports the `-B<build_dir>` option, until then we resort to the | |
# old pushd/popd mechanism. | |
mkdir "$build_dir" | |
pushd "$build_dir" | |
"${CMAKE}" "${cmake_argv[@]}" | |
popd | |
} | |
arrow_create_cpp_build() { | |
# The following local variables are captured by lexical scope due to bash | |
# incapacity to accept/return array variables (without using nameref which | |
# are found only in bash 4.3 and later). | |
# Parse docopts arguments into `args` associative map. | |
declare -A args | |
# Define a local array to store cmake arguments. | |
declare -a cmake_argv | |
parse_argv "$@" | |
build_cmake_argv | |
invoke_cmake | |
} | |
main() { | |
arrow_create_cpp_build "$@" | |
} | |
# Invoke main only if the script is executed and not sourced. | |
[[ "${BASH_SOURCE[0]}" != "${0}" ]] || main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment