|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eu |
| 4 | +set -o pipefail |
| 5 | + |
| 6 | +export MASON_RELEASE="${MASON_RELEASE:-v0.14.1}" |
| 7 | +export MASON_LLVM_RELEASE="${MASON_LLVM_RELEASE:-4.0.1}" |
| 8 | + |
| 9 | +PLATFORM=$(uname | tr A-Z a-z) |
| 10 | +if [[ ${PLATFORM} == 'darwin' ]]; then |
| 11 | + PLATFORM="osx" |
| 12 | +fi |
| 13 | + |
| 14 | +MASON_URL="https://s3.amazonaws.com/mason-binaries/${PLATFORM}-$(uname -m)" |
| 15 | + |
| 16 | +llvm_toolchain_dir="$(pwd)/.toolchain" |
| 17 | + |
| 18 | +function run() { |
| 19 | + local config=${1} |
| 20 | + # unbreak bash shell due to rvm bug on osx: https://github.com/direnv/direnv/issues/210#issuecomment-203383459 |
| 21 | + # this impacts any usage of scripts that are source'd (like this one) |
| 22 | + if [[ "${TRAVIS_OS_NAME:-}" == "osx" ]]; then |
| 23 | + echo 'shell_session_update() { :; }' > ~/.direnvrc |
| 24 | + fi |
| 25 | + |
| 26 | + # |
| 27 | + # COMPILER TOOLCHAIN |
| 28 | + # |
| 29 | + |
| 30 | + # We install clang++ without the mason client for a couple reasons: |
| 31 | + # 1) decoupling makes it viable to use a custom branch of mason that might |
| 32 | + # modify the upstream s3 bucket in a such a way that does not give |
| 33 | + # it access to build tools like clang++ |
| 34 | + # 2) Allows us to short-circuit and use a global clang++ install if it |
| 35 | + # is available to save space for local builds. |
| 36 | + GLOBAL_CLANG="${HOME}/.mason/mason_packages/${PLATFORM}-$(uname -m)/clang++/${MASON_LLVM_RELEASE}" |
| 37 | + GLOBAL_LLVM="${HOME}/.mason/mason_packages/${PLATFORM}-$(uname -m)/llvm/${MASON_LLVM_RELEASE}" |
| 38 | + if [[ -d ${GLOBAL_LLVM} ]]; then |
| 39 | + echo "Detected '${GLOBAL_LLVM}/bin/clang++', using it" |
| 40 | + local llvm_toolchain_dir=${GLOBAL_LLVM} |
| 41 | + elif [[ -d ${GLOBAL_CLANG} ]]; then |
| 42 | + echo "Detected '${GLOBAL_CLANG}/bin/clang++', using it" |
| 43 | + local llvm_toolchain_dir=${GLOBAL_CLANG} |
| 44 | + elif [[ -d ${GLOBAL_CLANG} ]]; then |
| 45 | + echo "Detected '${GLOBAL_CLANG}/bin/clang++', using it" |
| 46 | + local llvm_toolchain_dir=${GLOBAL_CLANG} |
| 47 | + elif [[ ! -d ${llvm_toolchain_dir} ]]; then |
| 48 | + BINARY="${MASON_URL}/clang++/${MASON_LLVM_RELEASE}.tar.gz" |
| 49 | + echo "Downloading ${BINARY}" |
| 50 | + mkdir -p ${llvm_toolchain_dir} |
| 51 | + curl -sSfL ${BINARY} | tar --gunzip --extract --strip-components=1 --directory=${llvm_toolchain_dir} |
| 52 | + fi |
| 53 | + |
| 54 | + # |
| 55 | + # MASON |
| 56 | + # |
| 57 | + |
| 58 | + function setup_mason() { |
| 59 | + local install_dir=${1} |
| 60 | + local mason_release=${2} |
| 61 | + mkdir -p ${install_dir} |
| 62 | + curl -sSfL https://github.com/mapbox/mason/archive/${mason_release}.tar.gz | tar --gunzip --extract --strip-components=1 --directory=${install_dir} |
| 63 | + } |
| 64 | + |
| 65 | + setup_mason $(pwd)/.mason ${MASON_RELEASE} |
| 66 | + |
| 67 | + # |
| 68 | + # ENV SETTINGS |
| 69 | + # |
| 70 | + |
| 71 | + echo "export PATH=${llvm_toolchain_dir}/bin:$(pwd)/.mason:$(pwd)/mason_packages/.link/bin:"'${PATH}' > ${config} |
| 72 | + echo "export CXX=${llvm_toolchain_dir}/bin/clang++" >> ${config} |
| 73 | + echo "export MASON_RELEASE=${MASON_RELEASE}" >> ${config} |
| 74 | + echo "export MASON_LLVM_RELEASE=${MASON_LLVM_RELEASE}" >> ${config} |
| 75 | + # https://github.com/google/sanitizers/wiki/AddressSanitizerAsDso |
| 76 | + RT_BASE=${llvm_toolchain_dir}/lib/clang/${MASON_LLVM_RELEASE}/lib/$(uname | tr A-Z a-z)/libclang_rt |
| 77 | + if [[ $(uname -s) == 'Darwin' ]]; then |
| 78 | + RT_PRELOAD=${RT_BASE}.asan_osx_dynamic.dylib |
| 79 | + else |
| 80 | + RT_PRELOAD=${RT_BASE}.asan-x86_64.so |
| 81 | + fi |
| 82 | + echo "export MASON_LLVM_RT_PRELOAD=${RT_PRELOAD}" >> ${config} |
| 83 | + SUPPRESSION_FILE="/tmp/leak_suppressions.txt" |
| 84 | + echo "leak:__strdup" > ${SUPPRESSION_FILE} |
| 85 | + echo "leak:v8::internal" >> ${SUPPRESSION_FILE} |
| 86 | + echo "leak:node::CreateEnvironment" >> ${SUPPRESSION_FILE} |
| 87 | + echo "leak:node::Init" >> ${SUPPRESSION_FILE} |
| 88 | + echo "export ASAN_SYMBOLIZER_PATH=${llvm_toolchain_dir}/bin/llvm-symbolizer" >> ${config} |
| 89 | + echo "export MSAN_SYMBOLIZER_PATH=${llvm_toolchain_dir}/bin/llvm-symbolizer" >> ${config} |
| 90 | + echo "export UBSAN_OPTIONS=print_stacktrace=1" >> ${config} |
| 91 | + echo "export LSAN_OPTIONS=suppressions=${SUPPRESSION_FILE}" >> ${config} |
| 92 | + echo "export ASAN_OPTIONS=symbolize=1:abort_on_error=1:detect_container_overflow=1:check_initialization_order=1:detect_stack_use_after_return=1" >> ${config} |
| 93 | + echo 'export MASON_SANITIZE="-fsanitize=address,undefined -fno-sanitize=vptr,function"' >> ${config} |
| 94 | + echo 'export MASON_SANITIZE_CXXFLAGS="${MASON_SANITIZE} -fno-sanitize=vptr,function -fsanitize-address-use-after-scope -fno-omit-frame-pointer -fno-common"' >> ${config} |
| 95 | + echo 'export MASON_SANITIZE_LDFLAGS="${MASON_SANITIZE}"' >> ${config} |
| 96 | + |
| 97 | + exit 0 |
| 98 | +} |
| 99 | + |
| 100 | +function usage() { |
| 101 | + >&2 echo "Usage" |
| 102 | + >&2 echo "" |
| 103 | + >&2 echo "$ ./scripts/setup.sh --config local.env" |
| 104 | + >&2 echo "$ source local.env" |
| 105 | + >&2 echo "" |
| 106 | + exit 1 |
| 107 | +} |
| 108 | + |
| 109 | +if [[ ! ${1:-} ]]; then |
| 110 | + usage |
| 111 | +fi |
| 112 | + |
| 113 | +# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash |
| 114 | +for i in "$@" |
| 115 | +do |
| 116 | +case $i in |
| 117 | + --config) |
| 118 | + if [[ ! ${2:-} ]]; then |
| 119 | + usage |
| 120 | + fi |
| 121 | + shift |
| 122 | + run $@ |
| 123 | + ;; |
| 124 | + -h | --help) |
| 125 | + usage |
| 126 | + shift |
| 127 | + ;; |
| 128 | + *) |
| 129 | + usage |
| 130 | + ;; |
| 131 | +esac |
| 132 | +done |
0 commit comments