michael@0: #!/usr/bin/env bash michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: # This script will check out llvm and clang into third_party/llvm and build it. michael@0: michael@0: # Do NOT CHANGE this if you don't know what you're doing -- see michael@0: # https://code.google.com/p/chromium/wiki/UpdatingClang michael@0: # Reverting problematic clang rolls is safe, though. michael@0: CLANG_REVISION=163674 michael@0: michael@0: THIS_DIR="$(dirname "${0}")" michael@0: LLVM_DIR="${THIS_DIR}/../../../third_party/llvm" michael@0: LLVM_BUILD_DIR="${LLVM_DIR}/../llvm-build" michael@0: LLVM_BOOTSTRAP_DIR="${LLVM_DIR}/../llvm-bootstrap" michael@0: CLANG_DIR="${LLVM_DIR}/tools/clang" michael@0: COMPILER_RT_DIR="${LLVM_DIR}/projects/compiler-rt" michael@0: STAMP_FILE="${LLVM_BUILD_DIR}/cr_build_revision" michael@0: michael@0: # ${A:-a} returns $A if it's set, a else. michael@0: LLVM_REPO_URL=${LLVM_URL:-https://llvm.org/svn/llvm-project} michael@0: michael@0: # Die if any command dies. michael@0: set -e michael@0: michael@0: OS="$(uname -s)" michael@0: michael@0: # Parse command line options. michael@0: force_local_build= michael@0: mac_only= michael@0: run_tests= michael@0: bootstrap= michael@0: while [[ $# > 0 ]]; do michael@0: case $1 in michael@0: --bootstrap) michael@0: bootstrap=yes michael@0: ;; michael@0: --force-local-build) michael@0: force_local_build=yes michael@0: ;; michael@0: --mac-only) michael@0: mac_only=yes michael@0: ;; michael@0: --run-tests) michael@0: run_tests=yes michael@0: ;; michael@0: --help) michael@0: echo "usage: $0 [--force-local-build] [--mac-only] [--run-tests] " michael@0: echo "--bootstrap: First build clang with CC, then with itself." michael@0: echo "--force-local-build: Don't try to download prebuilt binaries." michael@0: echo "--mac-only: Do initial download only on Mac systems." michael@0: echo "--run-tests: Run tests after building. Only for local builds." michael@0: exit 1 michael@0: ;; michael@0: esac michael@0: shift michael@0: done michael@0: michael@0: # --mac-only prevents the initial download on non-mac systems, but if clang has michael@0: # already been downloaded in the past, this script keeps it up to date even if michael@0: # --mac-only is passed in and the system isn't a mac. People who don't like this michael@0: # can just delete their third_party/llvm-build directory. michael@0: if [[ -n "$mac_only" ]] && [[ "${OS}" != "Darwin" ]] && michael@0: [[ "$GYP_DEFINES" != *clang=1* ]] && ! [[ -d "${LLVM_BUILD_DIR}" ]]; then michael@0: exit 0 michael@0: fi michael@0: michael@0: # Xcode and clang don't get along when predictive compilation is enabled. michael@0: # http://crbug.com/96315 michael@0: if [[ "${OS}" = "Darwin" ]] && xcodebuild -version | grep -q 'Xcode 3.2' ; then michael@0: XCONF=com.apple.Xcode michael@0: if [[ "${GYP_GENERATORS}" != "make" ]] && \ michael@0: [ "$(defaults read "${XCONF}" EnablePredictiveCompilation)" != "0" ]; then michael@0: echo michael@0: echo " HEARKEN!" michael@0: echo "You're using Xcode3 and you have 'Predictive Compilation' enabled." michael@0: echo "This does not work well with clang (http://crbug.com/96315)." michael@0: echo "Disable it in Preferences->Building (lower right), or run" michael@0: echo " defaults write ${XCONF} EnablePredictiveCompilation -boolean NO" michael@0: echo "while Xcode is not running." michael@0: echo michael@0: fi michael@0: michael@0: SUB_VERSION=$(xcodebuild -version | sed -Ene 's/Xcode 3\.2\.([0-9]+)/\1/p') michael@0: if [[ "${SUB_VERSION}" < 6 ]]; then michael@0: echo michael@0: echo " YOUR LD IS BUGGY!" michael@0: echo "Please upgrade Xcode to at least 3.2.6." michael@0: echo michael@0: fi michael@0: fi michael@0: michael@0: michael@0: # Check if there's anything to be done, exit early if not. michael@0: if [[ -f "${STAMP_FILE}" ]]; then michael@0: PREVIOUSLY_BUILT_REVISON=$(cat "${STAMP_FILE}") michael@0: if [[ -z "$force_local_build" ]] && \ michael@0: [[ "${PREVIOUSLY_BUILT_REVISON}" = "${CLANG_REVISION}" ]]; then michael@0: echo "Clang already at ${CLANG_REVISION}" michael@0: exit 0 michael@0: fi michael@0: fi michael@0: # To always force a new build if someone interrupts their build half way. michael@0: rm -f "${STAMP_FILE}" michael@0: michael@0: # Clobber pch files, since they only work with the compiler version that michael@0: # created them. Also clobber .o files, to make sure everything will be built michael@0: # with the new compiler. michael@0: if [[ "${OS}" = "Darwin" ]]; then michael@0: XCODEBUILD_DIR="${THIS_DIR}/../../../xcodebuild" michael@0: michael@0: # Xcode groups .o files by project first, configuration second. michael@0: if [[ -d "${XCODEBUILD_DIR}" ]]; then michael@0: echo "Clobbering .o files for Xcode build" michael@0: find "${XCODEBUILD_DIR}" -name '*.o' -exec rm {} + michael@0: fi michael@0: fi michael@0: michael@0: if [ -f "${THIS_DIR}/../../../WebKit.gyp" ]; then michael@0: # We're inside a WebKit checkout. michael@0: # TODO(thakis): try to unify the directory layout of the xcode- and michael@0: # make-based builds. http://crbug.com/110455 michael@0: MAKE_DIR="${THIS_DIR}/../../../../../../out" michael@0: else michael@0: # We're inside a Chromium checkout. michael@0: MAKE_DIR="${THIS_DIR}/../../../out" michael@0: fi michael@0: michael@0: for CONFIG in Debug Release; do michael@0: if [[ -d "${MAKE_DIR}/${CONFIG}/obj.target" || michael@0: -d "${MAKE_DIR}/${CONFIG}/obj.host" ]]; then michael@0: echo "Clobbering ${CONFIG} PCH and .o files for make build" michael@0: if [[ -d "${MAKE_DIR}/${CONFIG}/obj.target" ]]; then michael@0: find "${MAKE_DIR}/${CONFIG}/obj.target" -name '*.gch' -exec rm {} + michael@0: find "${MAKE_DIR}/${CONFIG}/obj.target" -name '*.o' -exec rm {} + michael@0: fi michael@0: if [[ -d "${MAKE_DIR}/${CONFIG}/obj.host" ]]; then michael@0: find "${MAKE_DIR}/${CONFIG}/obj.host" -name '*.o' -exec rm {} + michael@0: fi michael@0: fi michael@0: michael@0: # ninja puts its output below ${MAKE_DIR} as well. michael@0: if [[ -d "${MAKE_DIR}/${CONFIG}/obj" ]]; then michael@0: echo "Clobbering ${CONFIG} PCH and .o files for ninja build" michael@0: find "${MAKE_DIR}/${CONFIG}/obj" -name '*.gch' -exec rm {} + michael@0: find "${MAKE_DIR}/${CONFIG}/obj" -name '*.o' -exec rm {} + michael@0: find "${MAKE_DIR}/${CONFIG}/obj" -name '*.o.d' -exec rm {} + michael@0: fi michael@0: michael@0: if [[ "${OS}" = "Darwin" ]]; then michael@0: if [[ -d "${XCODEBUILD_DIR}/${CONFIG}/SharedPrecompiledHeaders" ]]; then michael@0: echo "Clobbering ${CONFIG} PCH files for Xcode build" michael@0: rm -rf "${XCODEBUILD_DIR}/${CONFIG}/SharedPrecompiledHeaders" michael@0: fi michael@0: fi michael@0: done michael@0: michael@0: if [[ -z "$force_local_build" ]]; then michael@0: # Check if there's a prebuilt binary and if so just fetch that. That's faster, michael@0: # and goma relies on having matching binary hashes on client and server too. michael@0: CDS_URL=https://commondatastorage.googleapis.com/chromium-browser-clang michael@0: CDS_FILE="clang-${CLANG_REVISION}.tgz" michael@0: CDS_OUT_DIR=$(mktemp -d -t clang_download.XXXXXX) michael@0: CDS_OUTPUT="${CDS_OUT_DIR}/${CDS_FILE}" michael@0: if [ "${OS}" = "Linux" ]; then michael@0: CDS_FULL_URL="${CDS_URL}/Linux_x64/${CDS_FILE}" michael@0: elif [ "${OS}" = "Darwin" ]; then michael@0: CDS_FULL_URL="${CDS_URL}/Mac/${CDS_FILE}" michael@0: fi michael@0: echo Trying to download prebuilt clang michael@0: if which curl > /dev/null; then michael@0: curl -L --fail "${CDS_FULL_URL}" -o "${CDS_OUTPUT}" || \ michael@0: rm -rf "${CDS_OUT_DIR}" michael@0: elif which wget > /dev/null; then michael@0: wget "${CDS_FULL_URL}" -O "${CDS_OUTPUT}" || rm -rf "${CDS_OUT_DIR}" michael@0: else michael@0: echo "Neither curl nor wget found. Please install one of these." michael@0: exit 1 michael@0: fi michael@0: if [ -f "${CDS_OUTPUT}" ]; then michael@0: rm -rf "${LLVM_BUILD_DIR}/Release+Asserts" michael@0: mkdir -p "${LLVM_BUILD_DIR}/Release+Asserts" michael@0: tar -xzf "${CDS_OUTPUT}" -C "${LLVM_BUILD_DIR}/Release+Asserts" michael@0: echo clang "${CLANG_REVISION}" unpacked michael@0: echo "${CLANG_REVISION}" > "${STAMP_FILE}" michael@0: rm -rf "${CDS_OUT_DIR}" michael@0: exit 0 michael@0: else michael@0: echo Did not find prebuilt clang at r"${CLANG_REVISION}", building michael@0: fi michael@0: fi michael@0: michael@0: echo Getting LLVM r"${CLANG_REVISION}" in "${LLVM_DIR}" michael@0: if ! svn co --force "${LLVM_REPO_URL}/llvm/trunk@${CLANG_REVISION}" \ michael@0: "${LLVM_DIR}"; then michael@0: echo Checkout failed, retrying michael@0: rm -rf "${LLVM_DIR}" michael@0: svn co --force "${LLVM_REPO_URL}/llvm/trunk@${CLANG_REVISION}" "${LLVM_DIR}" michael@0: fi michael@0: michael@0: echo Getting clang r"${CLANG_REVISION}" in "${CLANG_DIR}" michael@0: svn co --force "${LLVM_REPO_URL}/cfe/trunk@${CLANG_REVISION}" "${CLANG_DIR}" michael@0: michael@0: echo Getting compiler-rt r"${CLANG_REVISION}" in "${COMPILER_RT_DIR}" michael@0: svn co --force "${LLVM_REPO_URL}/compiler-rt/trunk@${CLANG_REVISION}" \ michael@0: "${COMPILER_RT_DIR}" michael@0: michael@0: # Echo all commands. michael@0: set -x michael@0: michael@0: NUM_JOBS=3 michael@0: if [[ "${OS}" = "Linux" ]]; then michael@0: NUM_JOBS="$(grep -c "^processor" /proc/cpuinfo)" michael@0: elif [ "${OS}" = "Darwin" ]; then michael@0: NUM_JOBS="$(sysctl -n hw.ncpu)" michael@0: fi michael@0: michael@0: # Build bootstrap clang if requested. michael@0: if [[ -n "${bootstrap}" ]]; then michael@0: echo "Building bootstrap compiler" michael@0: mkdir -p "${LLVM_BOOTSTRAP_DIR}" michael@0: cd "${LLVM_BOOTSTRAP_DIR}" michael@0: if [[ ! -f ./config.status ]]; then michael@0: # The bootstrap compiler only needs to be able to build the real compiler, michael@0: # so it needs no cross-compiler output support. In general, the host michael@0: # compiler should be as similar to the final compiler as possible, so do michael@0: # keep --disable-threads & co. michael@0: ../llvm/configure \ michael@0: --enable-optimized \ michael@0: --enable-targets=host-only \ michael@0: --disable-threads \ michael@0: --disable-pthreads \ michael@0: --without-llvmgcc \ michael@0: --without-llvmgxx michael@0: MACOSX_DEPLOYMENT_TARGET=10.5 make -j"${NUM_JOBS}" michael@0: fi michael@0: if [[ -n "${run_tests}" ]]; then michael@0: make check-all michael@0: fi michael@0: cd - michael@0: export CC="${PWD}/${LLVM_BOOTSTRAP_DIR}/Release+Asserts/bin/clang" michael@0: export CXX="${PWD}/${LLVM_BOOTSTRAP_DIR}/Release+Asserts/bin/clang++" michael@0: echo "Building final compiler" michael@0: fi michael@0: michael@0: # Build clang (in a separate directory). michael@0: # The clang bots have this path hardcoded in built/scripts/slave/compile.py, michael@0: # so if you change it you also need to change these links. michael@0: mkdir -p "${LLVM_BUILD_DIR}" michael@0: cd "${LLVM_BUILD_DIR}" michael@0: if [[ ! -f ./config.status ]]; then michael@0: ../llvm/configure \ michael@0: --enable-optimized \ michael@0: --disable-threads \ michael@0: --disable-pthreads \ michael@0: --without-llvmgcc \ michael@0: --without-llvmgxx michael@0: fi michael@0: michael@0: MACOSX_DEPLOYMENT_TARGET=10.5 make -j"${NUM_JOBS}" michael@0: cd - michael@0: michael@0: # Build plugin. michael@0: # Copy it into the clang tree and use clang's build system to compile the michael@0: # plugin. michael@0: PLUGIN_SRC_DIR="${THIS_DIR}/../plugins" michael@0: PLUGIN_DST_DIR="${LLVM_DIR}/tools/clang/tools/chrome-plugin" michael@0: PLUGIN_BUILD_DIR="${LLVM_BUILD_DIR}/tools/clang/tools/chrome-plugin" michael@0: rm -rf "${PLUGIN_DST_DIR}" michael@0: cp -R "${PLUGIN_SRC_DIR}" "${PLUGIN_DST_DIR}" michael@0: rm -rf "${PLUGIN_BUILD_DIR}" michael@0: mkdir -p "${PLUGIN_BUILD_DIR}" michael@0: cp "${PLUGIN_SRC_DIR}/Makefile" "${PLUGIN_BUILD_DIR}" michael@0: MACOSX_DEPLOYMENT_TARGET=10.5 make -j"${NUM_JOBS}" -C "${PLUGIN_BUILD_DIR}" michael@0: michael@0: if [[ -n "$run_tests" ]]; then michael@0: # Run a few tests. michael@0: "${PLUGIN_SRC_DIR}/tests/test.sh" "${LLVM_BUILD_DIR}/Release+Asserts" michael@0: cd "${LLVM_BUILD_DIR}" michael@0: make check-all michael@0: cd - michael@0: fi michael@0: michael@0: # After everything is done, log success for this revision. michael@0: echo "${CLANG_REVISION}" > "${STAMP_FILE}"