1#!/bin/sh
2# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Script to build binutils for both i386 and AMD64 Linux architectures.
7# Must be run on an AMD64 supporting machine which has debootstrap and sudo
8# installed.
9# Uses Ubuntu Lucid chroots as build environment.
10
11set -e
12
13if [ x"$(whoami)" = x"root" ]; then
14  echo "Script must not be run as root."
15  exit 1
16fi
17sudo -v
18
19OUTPUTDIR="${1:-$PWD/output-$(date +%Y%m%d-%H%M%S)}"
20if [ ! -d "$OUTPUTDIR" ]; then
21  mkdir -p "$OUTPUTDIR"
22fi
23
24# Download the source
25VERSION=2.24
26wget -c http://ftp.gnu.org/gnu/binutils/binutils-$VERSION.tar.bz2
27
28# Verify the signature
29wget -c -q http://ftp.gnu.org/gnu/binutils/binutils-$VERSION.tar.bz2.sig
30if ! gpg --verify binutils-$VERSION.tar.bz2.sig; then
31  echo "GPG Signature failed to verify."
32  echo ""
33  echo "You may need to import the vendor GPG key with:"
34  echo "# gpg --keyserver pgp.mit.edu --recv-key 4AE55E93"
35  exit 1
36fi
37
38
39if [ ! -d binutils-$VERSION ]; then
40  # Extract the source
41  tar jxf binutils-$VERSION.tar.bz2
42
43  # Patch the source
44  (
45    cd binutils-$VERSION
46    patch -p1 < ../ehframe-race.patch
47  )
48fi
49
50for ARCH in i386 amd64; do
51  if [ ! -d lucid-chroot-$ARCH ]; then
52    # Refresh sudo credentials
53    sudo -v
54
55    # Create the chroot
56    echo ""
57    echo "Building chroot for $ARCH"
58    echo "============================="
59    sudo debootstrap \
60        --arch=$ARCH \
61        --include=build-essential,flex,bison \
62        lucid lucid-chroot-$ARCH
63    echo "============================="
64  fi
65
66  BUILDDIR=lucid-chroot-$ARCH/build
67
68  # Clean up any previous failed build attempts inside chroot
69  if [ -d "$BUILDDIR" ]; then
70    sudo rm -rf "$BUILDDIR"
71  fi
72
73  # Copy data into the chroot
74  sudo mkdir -p "$BUILDDIR"
75  sudo cp -a binutils-$VERSION "$BUILDDIR"
76  sudo cp -a build-one.sh "$BUILDDIR"
77
78  # Do the build
79  PREFIX=
80  case $ARCH in
81   i386)
82     PREFIX="setarch linux32"
83     ARCHNAME=i686-pc-linux-gnu
84   ;;
85   amd64)
86     PREFIX="setarch linux64"
87     ARCHNAME=x86_64-unknown-linux-gnu
88   ;;
89  esac
90  echo ""
91  echo "Building binutils for $ARCH"
92  LOGFILE="$OUTPUTDIR/build-$ARCH.log"
93  if ! sudo $PREFIX chroot lucid-chroot-$ARCH /build/build-one.sh /build/binutils-$VERSION > $LOGFILE 2>&1; then
94    echo "Build failed! See $LOGFILE for details."
95    exit 1
96  fi
97
98  # Copy data out of the chroot
99  sudo chown -R $(whoami) "$BUILDDIR/output/"
100
101  # Strip the output binaries
102  for i in "$BUILDDIR/output/$ARCHNAME/bin/*"; do
103     strip $i
104  done
105
106  # Copy them out of the chroot
107  cp -a "$BUILDDIR/output/$ARCHNAME" "$OUTPUTDIR"
108
109  # Clean up chroot
110  sudo rm -rf "$BUILDDIR"
111done
112
113echo "Check you are happy with the binaries in"
114echo "  $OUTPUTDIR"
115echo "Then"
116echo " * upload to Google Storage using the upload.sh script"
117echo " * roll dependencies"
118