xref: /aosp_15_r20/external/llvm/utils/crosstool/ARM/build-install-linux.sh (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker#!/bin/bash
2*9880d681SAndroid Build Coastguard Worker#
3*9880d681SAndroid Build Coastguard Worker# Compiles and installs a Linux/x86_64 -> Linux/ARM crosstool based on LLVM and
4*9880d681SAndroid Build Coastguard Worker# LLVM-GCC-4.2 using SVN snapshots in provided tarballs.
5*9880d681SAndroid Build Coastguard Worker
6*9880d681SAndroid Build Coastguard Workerset -o nounset
7*9880d681SAndroid Build Coastguard Workerset -o errexit
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard Workerecho -n "Welcome to LLVM Linux/X86_64 -> Linux/ARM crosstool "
10*9880d681SAndroid Build Coastguard Workerecho "builder/installer; some steps will require sudo privileges."
11*9880d681SAndroid Build Coastguard Worker
12*9880d681SAndroid Build Coastguard Workerreadonly INSTALL_ROOT="${INSTALL_ROOT:-/usr/local/crosstool}"
13*9880d681SAndroid Build Coastguard Worker# Both $USER and root *must* have read/write access to this dir.
14*9880d681SAndroid Build Coastguard Workerreadonly SCRATCH_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/llvm-project.XXXXXX")
15*9880d681SAndroid Build Coastguard Workerreadonly SRC_ROOT="${SCRATCH_ROOT}/src"
16*9880d681SAndroid Build Coastguard Workerreadonly OBJ_ROOT="${SCRATCH_ROOT}/obj"
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard Workerreadonly CROSS_HOST="x86_64-unknown-linux-gnu"
19*9880d681SAndroid Build Coastguard Workerreadonly CROSS_TARGET="arm-none-linux-gnueabi"
20*9880d681SAndroid Build Coastguard Workerreadonly CROSS_MARCH="${CROSS_MARCH:-armv6}"
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Workerreadonly CODE_SOURCERY="${INSTALL_ROOT}/codesourcery"
23*9880d681SAndroid Build Coastguard Workerreadonly CODE_SOURCERY_PKG_PATH="${CODE_SOURCERY_PKG_PATH:-${HOME}/codesourcery}"
24*9880d681SAndroid Build Coastguard Workerreadonly CODE_SOURCERY_HTTP="http://www.codesourcery.com/sgpp/lite/arm/portal/package1787/public"
25*9880d681SAndroid Build Coastguard Workerreadonly CODE_SOURCERY_PKG="arm-2007q3-51-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2"
26*9880d681SAndroid Build Coastguard Workerreadonly CODE_SOURCERY_ROOT="${CODE_SOURCERY}/arm-2007q3"
27*9880d681SAndroid Build Coastguard Workerreadonly CODE_SOURCERY_BIN="${CODE_SOURCERY_ROOT}/bin"
28*9880d681SAndroid Build Coastguard Worker# Make sure ${CROSS_TARGET}-* binutils are in command path
29*9880d681SAndroid Build Coastguard Workerexport PATH="${CODE_SOURCERY_BIN}:${PATH}"
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Workerreadonly CROSS_TARGET_AS="${CODE_SOURCERY_BIN}/${CROSS_TARGET}-as"
32*9880d681SAndroid Build Coastguard Workerreadonly CROSS_TARGET_LD="${CODE_SOURCERY_BIN}/${CROSS_TARGET}-ld"
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Workerreadonly SYSROOT="${CODE_SOURCERY_ROOT}/${CROSS_TARGET}/libc"
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Workerreadonly LLVM_PKG_PATH="${LLVM_PKG_PATH:-${HOME}/llvm-project/snapshots}"
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker# Latest SVN revisions known to be working in this configuration.
39*9880d681SAndroid Build Coastguard Workerreadonly LLVM_DEFAULT_REV="74530"
40*9880d681SAndroid Build Coastguard Workerreadonly LLVMGCC_DEFAULT_REV="74535"
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Workerreadonly LLVM_PKG="llvm-${LLVM_SVN_REV:-${LLVM_DEFAULT_REV}}.tar.bz2"
43*9880d681SAndroid Build Coastguard Workerreadonly LLVM_SRC_DIR="${SRC_ROOT}/llvm"
44*9880d681SAndroid Build Coastguard Workerreadonly LLVM_OBJ_DIR="${OBJ_ROOT}/llvm"
45*9880d681SAndroid Build Coastguard Workerreadonly LLVM_INSTALL_DIR="${INSTALL_ROOT}/${CROSS_TARGET}/llvm"
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Workerreadonly LLVMGCC_PKG="llvm-gcc-4.2-${LLVMGCC_SVN_REV:-${LLVMGCC_DEFAULT_REV}}.tar.bz2"
48*9880d681SAndroid Build Coastguard Workerreadonly LLVMGCC_SRC_DIR="${SRC_ROOT}/llvm-gcc-4.2"
49*9880d681SAndroid Build Coastguard Workerreadonly LLVMGCC_OBJ_DIR="${OBJ_ROOT}/llvm-gcc-4.2"
50*9880d681SAndroid Build Coastguard Workerreadonly LLVMGCC_INSTALL_DIR="${INSTALL_ROOT}/${CROSS_TARGET}/llvm-gcc-4.2"
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Workerreadonly MAKE_OPTS="${MAKE_OPTS:--j2}"
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker# Params:
55*9880d681SAndroid Build Coastguard Worker#   $1: directory to be created
56*9880d681SAndroid Build Coastguard Worker#   $2: optional mkdir command prefix, e.g. "sudo"
57*9880d681SAndroid Build Coastguard WorkercreateDir() {
58*9880d681SAndroid Build Coastguard Worker  if [[ ! -e $1 ]]; then
59*9880d681SAndroid Build Coastguard Worker    ${2:-} mkdir -p $1
60*9880d681SAndroid Build Coastguard Worker  elif [[ -e $1 && ! -d $1 ]]; then
61*9880d681SAndroid Build Coastguard Worker    echo "$1 exists but is not a directory; exiting."
62*9880d681SAndroid Build Coastguard Worker    exit 3
63*9880d681SAndroid Build Coastguard Worker  fi
64*9880d681SAndroid Build Coastguard Worker}
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard WorkersudoCreateDir() {
67*9880d681SAndroid Build Coastguard Worker  createDir $1 sudo
68*9880d681SAndroid Build Coastguard Worker  sudo chown ${USER} $1
69*9880d681SAndroid Build Coastguard Worker}
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker# Prints out and runs the command, but without logging -- intended for use with
72*9880d681SAndroid Build Coastguard Worker# lightweight commands that don't have useful output to parse, e.g. mkdir, tar,
73*9880d681SAndroid Build Coastguard Worker# etc.
74*9880d681SAndroid Build Coastguard WorkerrunCommand() {
75*9880d681SAndroid Build Coastguard Worker  local message="$1"
76*9880d681SAndroid Build Coastguard Worker  shift
77*9880d681SAndroid Build Coastguard Worker  echo "=> $message"
78*9880d681SAndroid Build Coastguard Worker  echo "==> Running: $*"
79*9880d681SAndroid Build Coastguard Worker  $*
80*9880d681SAndroid Build Coastguard Worker}
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard WorkerrunAndLog() {
83*9880d681SAndroid Build Coastguard Worker  local message="$1"
84*9880d681SAndroid Build Coastguard Worker  local log_file="$2"
85*9880d681SAndroid Build Coastguard Worker  shift 2
86*9880d681SAndroid Build Coastguard Worker  echo "=> $message; log in $log_file"
87*9880d681SAndroid Build Coastguard Worker  echo "==> Running: $*"
88*9880d681SAndroid Build Coastguard Worker  # Pop-up a terminal with the output of the current command?
89*9880d681SAndroid Build Coastguard Worker  # e.g.: xterm -e /bin/bash -c "$* >| tee $log_file"
90*9880d681SAndroid Build Coastguard Worker  $* &> $log_file
91*9880d681SAndroid Build Coastguard Worker  if [[ $? != 0 ]]; then
92*9880d681SAndroid Build Coastguard Worker    echo "Error occurred: see most recent log file for details."
93*9880d681SAndroid Build Coastguard Worker    exit
94*9880d681SAndroid Build Coastguard Worker  fi
95*9880d681SAndroid Build Coastguard Worker}
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard WorkerinstallCodeSourcery() {
98*9880d681SAndroid Build Coastguard Worker  # Unpack the tarball, creating the CodeSourcery dir, if necessary.
99*9880d681SAndroid Build Coastguard Worker  if [[ ! -d ${CODE_SOURCERY_ROOT} ]]; then
100*9880d681SAndroid Build Coastguard Worker    sudoCreateDir ${CODE_SOURCERY}
101*9880d681SAndroid Build Coastguard Worker    cd ${CODE_SOURCERY}
102*9880d681SAndroid Build Coastguard Worker    if [[ -e ${CODE_SOURCERY_PKG_PATH}/${CODE_SOURCERY_PKG} ]]; then
103*9880d681SAndroid Build Coastguard Worker      runCommand "Unpacking CodeSourcery in ${CODE_SOURCERY}" \
104*9880d681SAndroid Build Coastguard Worker          tar jxf ${CODE_SOURCERY_PKG_PATH}/${CODE_SOURCERY_PKG}
105*9880d681SAndroid Build Coastguard Worker    else
106*9880d681SAndroid Build Coastguard Worker      echo -n "CodeSourcery tarball not found in "
107*9880d681SAndroid Build Coastguard Worker      echo "${CODE_SOURCERY_PKG_PATH}/${CODE_SOURCERY_PKG}"
108*9880d681SAndroid Build Coastguard Worker      echo -n "Fix the path or download it from "
109*9880d681SAndroid Build Coastguard Worker      echo "${CODE_SOURCERY_HTTP}/${CROSS_TARGET}/${CODE_SOURCERY_PKG}"
110*9880d681SAndroid Build Coastguard Worker      exit
111*9880d681SAndroid Build Coastguard Worker    fi
112*9880d681SAndroid Build Coastguard Worker  else
113*9880d681SAndroid Build Coastguard Worker    echo "CodeSourcery install dir already exists; skipping."
114*9880d681SAndroid Build Coastguard Worker  fi
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker  # Verify our CodeSourcery toolchain installation.
117*9880d681SAndroid Build Coastguard Worker  if [[ ! -d "${SYSROOT}" ]]; then
118*9880d681SAndroid Build Coastguard Worker    echo -n "Error: CodeSourcery does not contain libc for ${CROSS_TARGET}: "
119*9880d681SAndroid Build Coastguard Worker    echo "${SYSROOT} not found."
120*9880d681SAndroid Build Coastguard Worker    exit
121*9880d681SAndroid Build Coastguard Worker  fi
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker  for tool in ${CROSS_TARGET_AS} ${CROSS_TARGET_LD}; do
124*9880d681SAndroid Build Coastguard Worker    if [[ ! -e $tool ]]; then
125*9880d681SAndroid Build Coastguard Worker      echo "${tool} not found; exiting."
126*9880d681SAndroid Build Coastguard Worker      exit
127*9880d681SAndroid Build Coastguard Worker    fi
128*9880d681SAndroid Build Coastguard Worker  done
129*9880d681SAndroid Build Coastguard Worker}
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard WorkerinstallLLVM() {
132*9880d681SAndroid Build Coastguard Worker  if [[ -d ${LLVM_INSTALL_DIR} ]]; then
133*9880d681SAndroid Build Coastguard Worker    echo "LLVM install dir ${LLVM_INSTALL_DIR} exists; skipping."
134*9880d681SAndroid Build Coastguard Worker    return
135*9880d681SAndroid Build Coastguard Worker  fi
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker  sudoCreateDir ${LLVM_INSTALL_DIR}
138*9880d681SAndroid Build Coastguard Worker
139*9880d681SAndroid Build Coastguard Worker  # Unpack LLVM tarball; should create the directory "llvm".
140*9880d681SAndroid Build Coastguard Worker  cd ${SRC_ROOT}
141*9880d681SAndroid Build Coastguard Worker  runCommand "Unpacking LLVM" tar jxf ${LLVM_PKG_PATH}/${LLVM_PKG}
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker  # Configure, build, and install LLVM.
144*9880d681SAndroid Build Coastguard Worker  createDir ${LLVM_OBJ_DIR}
145*9880d681SAndroid Build Coastguard Worker  cd ${LLVM_OBJ_DIR}
146*9880d681SAndroid Build Coastguard Worker  runAndLog "Configuring LLVM" ${LLVM_OBJ_DIR}/llvm-configure.log \
147*9880d681SAndroid Build Coastguard Worker      ${LLVM_SRC_DIR}/configure \
148*9880d681SAndroid Build Coastguard Worker      --disable-jit \
149*9880d681SAndroid Build Coastguard Worker      --enable-optimized \
150*9880d681SAndroid Build Coastguard Worker      --prefix=${LLVM_INSTALL_DIR} \
151*9880d681SAndroid Build Coastguard Worker      --target=${CROSS_TARGET} \
152*9880d681SAndroid Build Coastguard Worker      --with-llvmgccdir=${LLVMGCC_INSTALL_DIR}
153*9880d681SAndroid Build Coastguard Worker  runAndLog "Building LLVM" ${LLVM_OBJ_DIR}/llvm-build.log \
154*9880d681SAndroid Build Coastguard Worker      make ${MAKE_OPTS}
155*9880d681SAndroid Build Coastguard Worker  runAndLog "Installing LLVM" ${LLVM_OBJ_DIR}/llvm-install.log \
156*9880d681SAndroid Build Coastguard Worker      make ${MAKE_OPTS} install
157*9880d681SAndroid Build Coastguard Worker}
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard WorkerinstallLLVMGCC() {
160*9880d681SAndroid Build Coastguard Worker  if [[ -d ${LLVMGCC_INSTALL_DIR} ]]; then
161*9880d681SAndroid Build Coastguard Worker    echo "LLVM-GCC install dir ${LLVMGCC_INSTALL_DIR} exists; skipping."
162*9880d681SAndroid Build Coastguard Worker    return
163*9880d681SAndroid Build Coastguard Worker  fi
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker  sudoCreateDir ${LLVMGCC_INSTALL_DIR}
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker  # Unpack LLVM-GCC tarball; should create the directory "llvm-gcc-4.2".
168*9880d681SAndroid Build Coastguard Worker  cd ${SRC_ROOT}
169*9880d681SAndroid Build Coastguard Worker  runCommand "Unpacking LLVM-GCC" tar jxf ${LLVM_PKG_PATH}/${LLVMGCC_PKG}
170*9880d681SAndroid Build Coastguard Worker
171*9880d681SAndroid Build Coastguard Worker  # Configure, build, and install LLVM-GCC.
172*9880d681SAndroid Build Coastguard Worker  createDir ${LLVMGCC_OBJ_DIR}
173*9880d681SAndroid Build Coastguard Worker  cd ${LLVMGCC_OBJ_DIR}
174*9880d681SAndroid Build Coastguard Worker  runAndLog "Configuring LLVM-GCC" ${LLVMGCC_OBJ_DIR}/llvmgcc-configure.log \
175*9880d681SAndroid Build Coastguard Worker      ${LLVMGCC_SRC_DIR}/configure \
176*9880d681SAndroid Build Coastguard Worker      --enable-languages=c,c++ \
177*9880d681SAndroid Build Coastguard Worker      --enable-llvm=${LLVM_INSTALL_DIR} \
178*9880d681SAndroid Build Coastguard Worker      --prefix=${LLVMGCC_INSTALL_DIR} \
179*9880d681SAndroid Build Coastguard Worker      --program-prefix=llvm- \
180*9880d681SAndroid Build Coastguard Worker      --target=${CROSS_TARGET} \
181*9880d681SAndroid Build Coastguard Worker      --with-arch=${CROSS_MARCH} \
182*9880d681SAndroid Build Coastguard Worker      --with-as=${CROSS_TARGET_AS} \
183*9880d681SAndroid Build Coastguard Worker      --with-ld=${CROSS_TARGET_LD} \
184*9880d681SAndroid Build Coastguard Worker      --with-sysroot=${SYSROOT}
185*9880d681SAndroid Build Coastguard Worker  runAndLog "Building LLVM-GCC" ${LLVMGCC_OBJ_DIR}/llvmgcc-build.log \
186*9880d681SAndroid Build Coastguard Worker      make
187*9880d681SAndroid Build Coastguard Worker  runAndLog "Installing LLVM-GCC" ${LLVMGCC_OBJ_DIR}/llvmgcc-install.log \
188*9880d681SAndroid Build Coastguard Worker      make install
189*9880d681SAndroid Build Coastguard Worker}
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Workerecho "Building in ${SCRATCH_ROOT}; installing in ${INSTALL_ROOT}"
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard WorkercreateDir ${SRC_ROOT}
194*9880d681SAndroid Build Coastguard WorkercreateDir ${OBJ_ROOT}
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard WorkerinstallCodeSourcery
197*9880d681SAndroid Build Coastguard WorkerinstallLLVM
198*9880d681SAndroid Build Coastguard WorkerinstallLLVMGCC
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Workerecho "Done."
201