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