11cb0ef41Sopenharmony_ci#!/usr/bin/env bash
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci# Copyright 2013 the V8 project authors. All rights reserved.
41cb0ef41Sopenharmony_ci# Redistribution and use in source and binary forms, with or without
51cb0ef41Sopenharmony_ci# modification, are permitted provided that the following conditions are
61cb0ef41Sopenharmony_ci# met:
71cb0ef41Sopenharmony_ci#
81cb0ef41Sopenharmony_ci#     * Redistributions of source code must retain the above copyright
91cb0ef41Sopenharmony_ci#       notice, this list of conditions and the following disclaimer.
101cb0ef41Sopenharmony_ci#     * Redistributions in binary form must reproduce the above
111cb0ef41Sopenharmony_ci#       copyright notice, this list of conditions and the following
121cb0ef41Sopenharmony_ci#       disclaimer in the documentation and/or other materials provided
131cb0ef41Sopenharmony_ci#       with the distribution.
141cb0ef41Sopenharmony_ci#     * Neither the name of Google Inc. nor the names of its
151cb0ef41Sopenharmony_ci#       contributors may be used to endorse or promote products derived
161cb0ef41Sopenharmony_ci#       from this software without specific prior written permission.
171cb0ef41Sopenharmony_ci#
181cb0ef41Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191cb0ef41Sopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201cb0ef41Sopenharmony_ci# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211cb0ef41Sopenharmony_ci# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221cb0ef41Sopenharmony_ci# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231cb0ef41Sopenharmony_ci# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241cb0ef41Sopenharmony_ci# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251cb0ef41Sopenharmony_ci# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261cb0ef41Sopenharmony_ci# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271cb0ef41Sopenharmony_ci# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281cb0ef41Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci# This script will build libgcmole.so as well as a corresponding recent
311cb0ef41Sopenharmony_ci# version of Clang and LLVM. The Clang will be built with the locally
321cb0ef41Sopenharmony_ci# installed compiler and statically link against the local libstdc++ so
331cb0ef41Sopenharmony_ci# that the resulting binary is easier transferable between different
341cb0ef41Sopenharmony_ci# environments.
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciLLVM_RELEASE=9.0.1
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciBUILD_TYPE="Release"
391cb0ef41Sopenharmony_ci# BUILD_TYPE="Debug"
401cb0ef41Sopenharmony_ciTHIS_DIR="$(readlink -f "$(dirname "${0}")")"
411cb0ef41Sopenharmony_ciLLVM_PROJECT_DIR="${THIS_DIR}/bootstrap/llvm"
421cb0ef41Sopenharmony_ciBUILD_DIR="${THIS_DIR}/bootstrap/build"
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci# Die if any command dies.
451cb0ef41Sopenharmony_ciset -e
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciOS="$(uname -s)"
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci# Xcode and clang don't get along when predictive compilation is enabled.
501cb0ef41Sopenharmony_ci# http://crbug.com/96315
511cb0ef41Sopenharmony_ciif [[ "${OS}" = "Darwin" ]] && xcodebuild -version | grep -q 'Xcode 3.2' ; then
521cb0ef41Sopenharmony_ci  XCONF=com.apple.Xcode
531cb0ef41Sopenharmony_ci  if [[ "${GYP_GENERATORS}" != "make" ]] && \
541cb0ef41Sopenharmony_ci     [ "$(defaults read "${XCONF}" EnablePredictiveCompilation)" != "0" ]; then
551cb0ef41Sopenharmony_ci    echo
561cb0ef41Sopenharmony_ci    echo "          HEARKEN!"
571cb0ef41Sopenharmony_ci    echo "You're using Xcode3 and you have 'Predictive Compilation' enabled."
581cb0ef41Sopenharmony_ci    echo "This does not work well with clang (http://crbug.com/96315)."
591cb0ef41Sopenharmony_ci    echo "Disable it in Preferences->Building (lower right), or run"
601cb0ef41Sopenharmony_ci    echo "    defaults write ${XCONF} EnablePredictiveCompilation -boolean NO"
611cb0ef41Sopenharmony_ci    echo "while Xcode is not running."
621cb0ef41Sopenharmony_ci    echo
631cb0ef41Sopenharmony_ci  fi
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  SUB_VERSION=$(xcodebuild -version | sed -Ene 's/Xcode 3\.2\.([0-9]+)/\1/p')
661cb0ef41Sopenharmony_ci  if [[ "${SUB_VERSION}" < 6 ]]; then
671cb0ef41Sopenharmony_ci    echo
681cb0ef41Sopenharmony_ci    echo "          YOUR LD IS BUGGY!"
691cb0ef41Sopenharmony_ci    echo "Please upgrade Xcode to at least 3.2.6."
701cb0ef41Sopenharmony_ci    echo
711cb0ef41Sopenharmony_ci  fi
721cb0ef41Sopenharmony_cifi
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciecho Getting LLVM release "${LLVM_RELEASE}" in "${LLVM_PROJECT_DIR}"
751cb0ef41Sopenharmony_ciif ! [ -d "${LLVM_PROJECT_DIR}" ] || ! git -C "${LLVM_PROJECT_DIR}" remote get-url origin | grep -q -F "https://github.com/llvm/llvm-project.git" ; then
761cb0ef41Sopenharmony_ci  rm -rf "${LLVM_PROJECT_DIR}"
771cb0ef41Sopenharmony_ci  git clone --depth=1 --branch "llvmorg-${LLVM_RELEASE}" "https://github.com/llvm/llvm-project.git" "${LLVM_PROJECT_DIR}"
781cb0ef41Sopenharmony_cielse
791cb0ef41Sopenharmony_ci  git -C "${LLVM_PROJECT_DIR}" fetch --depth=1 origin "llvmorg-${LLVM_RELEASE}"
801cb0ef41Sopenharmony_ci  git -C "${LLVM_PROJECT_DIR}" checkout FETCH_HEAD
811cb0ef41Sopenharmony_cifi
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci# Echo all commands
841cb0ef41Sopenharmony_ciset -x
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciNUM_JOBS=3
871cb0ef41Sopenharmony_ciif [[ "${OS}" = "Linux" ]]; then
881cb0ef41Sopenharmony_ci  if [[ -e "/proc/cpuinfo" ]]; then
891cb0ef41Sopenharmony_ci    NUM_JOBS="$(grep -c "^processor" /proc/cpuinfo)"
901cb0ef41Sopenharmony_ci  else
911cb0ef41Sopenharmony_ci    # Hack when running in chroot
921cb0ef41Sopenharmony_ci    NUM_JOBS="32"
931cb0ef41Sopenharmony_ci  fi
941cb0ef41Sopenharmony_cielif [ "${OS}" = "Darwin" ]; then
951cb0ef41Sopenharmony_ci  NUM_JOBS="$(sysctl -n hw.ncpu)"
961cb0ef41Sopenharmony_cifi
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci# Build clang.
991cb0ef41Sopenharmony_ciif [ ! -e "${BUILD_DIR}" ]; then
1001cb0ef41Sopenharmony_ci  mkdir "${BUILD_DIR}"
1011cb0ef41Sopenharmony_cifi
1021cb0ef41Sopenharmony_cicd "${BUILD_DIR}"
1031cb0ef41Sopenharmony_cicmake -GNinja -DCMAKE_CXX_FLAGS="-static-libstdc++" -DLLVM_ENABLE_TERMINFO=OFF \
1041cb0ef41Sopenharmony_ci    -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DLLVM_ENABLE_PROJECTS=clang \
1051cb0ef41Sopenharmony_ci    -DLLVM_ENABLE_Z3_SOLVER=OFF "${LLVM_PROJECT_DIR}/llvm"
1061cb0ef41Sopenharmony_ciMACOSX_DEPLOYMENT_TARGET=10.5 ninja -j"${NUM_JOBS}" clang
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ciif [[ "${BUILD_TYPE}" = "Release" ]]; then
1091cb0ef41Sopenharmony_ci  # Strip the clang binary.
1101cb0ef41Sopenharmony_ci  STRIP_FLAGS=
1111cb0ef41Sopenharmony_ci  if [ "${OS}" = "Darwin" ]; then
1121cb0ef41Sopenharmony_ci    # See http://crbug.com/256342
1131cb0ef41Sopenharmony_ci    STRIP_FLAGS=-x
1141cb0ef41Sopenharmony_ci  fi
1151cb0ef41Sopenharmony_ci  strip ${STRIP_FLAGS} bin/clang
1161cb0ef41Sopenharmony_cifi
1171cb0ef41Sopenharmony_cicd -
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci# Build libgcmole.so
1201cb0ef41Sopenharmony_cimake -C "${THIS_DIR}" clean
1211cb0ef41Sopenharmony_cimake -C "${THIS_DIR}" LLVM_SRC_ROOT="${LLVM_PROJECT_DIR}/llvm" \
1221cb0ef41Sopenharmony_ci    CLANG_SRC_ROOT="${LLVM_PROJECT_DIR}/clang" \
1231cb0ef41Sopenharmony_ci    BUILD_ROOT="${BUILD_DIR}" $BUILD_TYPE
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciset +x
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciecho '#########################################################################'
1281cb0ef41Sopenharmony_ciecho 'Congratulations you compiled clang and libgcmole.so'
1291cb0ef41Sopenharmony_ciecho 
1301cb0ef41Sopenharmony_ciecho '# You can now run gcmole:'
1311cb0ef41Sopenharmony_ciecho 'tools/gcmole/gcmole.py \'
1321cb0ef41Sopenharmony_ciecho '   --clang-bin-dir="tools/gcmole/bootstrap/build/bin" \'
1331cb0ef41Sopenharmony_ciecho '   --clang-plugins-dir="tools/gcmole" \'
1341cb0ef41Sopenharmony_ciecho '   --v8-target-cpu=$CPU'
1351cb0ef41Sopenharmony_ciecho
136