1#!/bin/bash -e
2
3# Copyright (c) 2012 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Script to install everything needed to build chromium (well, ideally, anyway)
8# See http://code.google.com/p/chromium/wiki/LinuxBuildInstructions
9# and http://code.google.com/p/chromium/wiki/LinuxBuild64Bit
10
11usage() {
12  echo "Usage: $0 [--options]"
13  echo "Options:"
14  echo "--[no-]syms: enable or disable installation of debugging symbols"
15  echo "--[no-]arm: enable or disable installation of arm cross toolchain"
16  echo "--[no-]chromeos-fonts: enable or disable installation of Chrome OS"\
17       "fonts"
18  echo "--[no-]nacl: enable or disable installation of prerequisites for"\
19       "building standalone NaCl and all its toolchains"
20  echo "--no-prompt: silently select standard options/defaults"
21  echo "--quick-check: quickly try to determine if dependencies are installed"
22  echo "               (this avoids interactive prompts and sudo commands,"
23  echo "               so might not be 100% accurate)"
24  echo "--unsupported: attempt installation even on unsupported systems"
25  echo "Script will prompt interactively if options not given."
26  exit 1
27}
28
29# Checks whether a particular package is available in the repos.
30# USAGE: $ package_exists <package name>
31package_exists() {
32  apt-cache pkgnames | grep -x "$1" > /dev/null 2>&1
33}
34
35# These default to on because (some) bots need them and it keeps things
36# simple for the bot setup if all bots just run the script in its default
37# mode.  Developers who don't want stuff they don't need installed on their
38# own workstations can pass --no-arm --no-nacl when running the script.
39do_inst_arm=1
40do_inst_nacl=1
41
42while test "$1" != ""
43do
44  case "$1" in
45  --syms)                   do_inst_syms=1;;
46  --no-syms)                do_inst_syms=0;;
47  # TODO(phajdan.jr): Remove the lib32 flags when nothing else refers to them.
48  --lib32)                  do_inst_lib32=1;;
49  --no-lib32)               do_inst_lib32=0;;
50  --arm)                    do_inst_arm=1;;
51  --no-arm)                 do_inst_arm=0;;
52  --chromeos-fonts)         do_inst_chromeos_fonts=1;;
53  --no-chromeos-fonts)      do_inst_chromeos_fonts=0;;
54  --nacl)                   do_inst_nacl=1;;
55  --no-nacl)                do_inst_nacl=0;;
56  --no-prompt)              do_default=1
57                            do_quietly="-qq --assume-yes"
58    ;;
59  --quick-check)            do_quick_check=1;;
60  --unsupported)            do_unsupported=1;;
61  *) usage;;
62  esac
63  shift
64done
65
66# Check for lsb_release command in $PATH
67if ! which lsb_release > /dev/null; then
68  echo "ERROR: lsb_release not found in \$PATH" >&2
69  exit 1;
70fi
71
72lsb_release=$(lsb_release --codename --short)
73ubuntu_codenames="(precise|quantal|raring|saucy|trusty)"
74if [ 0 -eq "${do_unsupported-0}" ] && [ 0 -eq "${do_quick_check-0}" ] ; then
75  if [[ ! $lsb_release =~ $ubuntu_codenames ]]; then
76    echo "ERROR: Only Ubuntu 12.04 (precise) through 14.04 (trusty) are"\
77        "currently supported" >&2
78    exit 1
79  fi
80
81  if ! uname -m | egrep -q "i686|x86_64"; then
82    echo "Only x86 architectures are currently supported" >&2
83    exit
84  fi
85fi
86
87if [ "x$(id -u)" != x0 ] && [ 0 -eq "${do_quick_check-0}" ]; then
88  echo "Running as non-root user."
89  echo "You might have to enter your password one or more times for 'sudo'."
90  echo
91fi
92
93# Packages needed for chromeos only
94chromeos_dev_list="libbluetooth-dev libxkbcommon-dev"
95
96# Packages needed for development
97dev_list="apache2.2-bin bison cdbs curl dpkg-dev elfutils devscripts fakeroot
98          flex fonts-thai-tlwg g++ git-core git-svn gperf language-pack-da
99          language-pack-fr language-pack-he language-pack-zh-hant
100          libapache2-mod-php5 libasound2-dev libbrlapi-dev libav-tools
101          libbz2-dev libcairo2-dev libcap-dev libcups2-dev libcurl4-gnutls-dev
102          libdrm-dev libelf-dev libexif-dev libgconf2-dev libgl1-mesa-dev
103          libglib2.0-dev libglu1-mesa-dev libgnome-keyring-dev libgtk2.0-dev
104          libkrb5-dev libnspr4-dev libnss3-dev libpam0g-dev libpci-dev
105          libpulse-dev libsctp-dev libspeechd-dev libsqlite3-dev libssl-dev
106          libudev-dev libwww-perl libxslt1-dev libxss-dev libxt-dev libxtst-dev
107          mesa-common-dev openbox patch perl php5-cgi pkg-config python
108          python-cherrypy3 python-crypto python-dev python-openssl
109          python-psutil rpm ruby subversion ttf-dejavu-core ttf-indic-fonts
110          ttf-kochi-gothic ttf-kochi-mincho wdiff xfonts-mathml zip
111          $chromeos_dev_list"
112
113# 64-bit systems need a minimum set of 32-bit compat packages for the pre-built
114# NaCl binaries.
115if file /sbin/init | grep -q 'ELF 64-bit'; then
116  dev_list="${dev_list} libc6-i386 lib32gcc1 lib32stdc++6"
117fi
118
119# Run-time libraries required by chromeos only
120chromeos_lib_list="libpulse0 libbz2-1.0"
121
122# Full list of required run-time libraries
123lib_list="libatk1.0-0 libc6 libasound2 libcairo2 libcap2 libcups2 libexpat1
124          libexif12 libfontconfig1 libfreetype6 libglib2.0-0 libgnome-keyring0
125          libgtk2.0-0 libpam0g libpango1.0-0 libpci3 libpcre3 libpixman-1-0
126          libpng12-0 libspeechd2 libstdc++6 libsqlite3-0 libx11-6
127          libxau6 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxdmcp6
128          libxext6 libxfixes3 libxi6 libxinerama1 libxrandr2 libxrender1
129          libxtst6 zlib1g $chromeos_lib_list"
130
131# Debugging symbols for all of the run-time libraries
132dbg_list="libatk1.0-dbg libc6-dbg libcairo2-dbg libfontconfig1-dbg
133          libglib2.0-0-dbg libgtk2.0-0-dbg libpango1.0-0-dbg libpcre3-dbg
134          libpixman-1-0-dbg libsqlite3-0-dbg libx11-6-dbg libxau6-dbg
135          libxcb1-dbg libxcomposite1-dbg libxcursor1-dbg libxdamage1-dbg
136          libxdmcp6-dbg libxext6-dbg libxfixes3-dbg libxi6-dbg libxinerama1-dbg
137          libxrandr2-dbg libxrender1-dbg libxtst6-dbg zlib1g-dbg
138          libstdc++6-4.6-dbg"
139
140# arm cross toolchain packages needed to build chrome on armhf
141arm_list="libc6-dev-armhf-cross
142          linux-libc-dev-armhf-cross
143          g++-arm-linux-gnueabihf
144          linux-libc-dev:i386"
145
146# Packages to build NaCl, its toolchains, and its ports.
147naclports_list="ant autoconf bison cmake gawk intltool xutils-dev xsltproc"
148nacl_list="g++-mingw-w64-i686 lib32z1-dev
149           libasound2:i386 libcap2:i386 libelf-dev:i386 libexif12:i386
150           libfontconfig1:i386 libgconf-2-4:i386 libglib2.0-0:i386 libgpm2:i386
151           libgtk2.0-0:i386 libncurses5:i386 lib32ncurses5-dev
152           libnss3:i386 libpango1.0-0:i386
153           libssl0.9.8:i386 libtinfo-dev libtinfo-dev:i386 libtool
154           libxcomposite1:i386 libxcursor1:i386 libxdamage1:i386 libxi6:i386
155           libxrandr2:i386 libxss1:i386 libxtst6:i386 texinfo xvfb
156           ${naclports_list}"
157
158# Find the proper version of libgbm-dev. We can't just install libgbm-dev as
159# it depends on mesa, and only one version of mesa can exists on the system.
160# Hence we must match the same version or this entire script will fail.
161mesa_variant=""
162for variant in "-lts-quantal" "-lts-raring" "-lts-saucy" "-lts-trusty"; do
163  if $(dpkg-query -Wf'${Status}' libgl1-mesa-glx${variant} | \
164       grep -q " ok installed"); then
165    mesa_variant="${variant}"
166  fi
167done
168dev_list="${dev_list} libgbm-dev${mesa_variant}
169          libgles2-mesa-dev${mesa_variant}"
170nacl_list="${nacl_list} libgl1-mesa-glx${mesa_variant}:i386"
171
172# Some package names have changed over time
173if package_exists ttf-mscorefonts-installer; then
174  dev_list="${dev_list} ttf-mscorefonts-installer"
175else
176  dev_list="${dev_list} msttcorefonts"
177fi
178if package_exists libnspr4-dbg; then
179  dbg_list="${dbg_list} libnspr4-dbg libnss3-dbg"
180  lib_list="${lib_list} libnspr4 libnss3"
181else
182  dbg_list="${dbg_list} libnspr4-0d-dbg libnss3-1d-dbg"
183  lib_list="${lib_list} libnspr4-0d libnss3-1d"
184fi
185if package_exists libjpeg-dev; then
186  dev_list="${dev_list} libjpeg-dev"
187else
188  dev_list="${dev_list} libjpeg62-dev"
189fi
190if package_exists libudev1; then
191  dev_list="${dev_list} libudev1"
192  nacl_list="${nacl_list} libudev1:i386"
193else
194  dev_list="${dev_list} libudev0"
195  nacl_list="${nacl_list} libudev0:i386"
196fi
197if package_exists libbrlapi0.6; then
198  dev_list="${dev_list} libbrlapi0.6"
199else
200  dev_list="${dev_list} libbrlapi0.5"
201fi
202
203
204# Some packages are only needed if the distribution actually supports
205# installing them.
206if package_exists appmenu-gtk; then
207  lib_list="$lib_list appmenu-gtk"
208fi
209
210# Waits for the user to press 'Y' or 'N'. Either uppercase of lowercase is
211# accepted. Returns 0 for 'Y' and 1 for 'N'. If an optional parameter has
212# been provided to yes_no(), the function also accepts RETURN as a user input.
213# The parameter specifies the exit code that should be returned in that case.
214# The function will echo the user's selection followed by a newline character.
215# Users can abort the function by pressing CTRL-C. This will call "exit 1".
216yes_no() {
217  if [ 0 -ne "${do_default-0}" ] ; then
218    [ $1 -eq 0 ] && echo "Y" || echo "N"
219    return $1
220  fi
221  local c
222  while :; do
223    c="$(trap 'stty echo -iuclc icanon 2>/dev/null' EXIT INT TERM QUIT
224         stty -echo iuclc -icanon 2>/dev/null
225         dd count=1 bs=1 2>/dev/null | od -An -tx1)"
226    case "$c" in
227      " 0a") if [ -n "$1" ]; then
228               [ $1 -eq 0 ] && echo "Y" || echo "N"
229               return $1
230             fi
231             ;;
232      " 79") echo "Y"
233             return 0
234             ;;
235      " 6e") echo "N"
236             return 1
237             ;;
238      "")    echo "Aborted" >&2
239             exit 1
240             ;;
241      *)     # The user pressed an unrecognized key. As we are not echoing
242             # any incorrect user input, alert the user by ringing the bell.
243             (tput bel) 2>/dev/null
244             ;;
245    esac
246  done
247}
248
249if test "$do_inst_syms" = "" && test 0 -eq ${do_quick_check-0}
250then
251  echo "This script installs all tools and libraries needed to build Chromium."
252  echo ""
253  echo "For most of the libraries, it can also install debugging symbols, which"
254  echo "will allow you to debug code in the system libraries. Most developers"
255  echo "won't need these symbols."
256  echo -n "Do you want me to install them for you (y/N) "
257  if yes_no 1; then
258    do_inst_syms=1
259  fi
260fi
261if test "$do_inst_syms" = "1"; then
262  echo "Including debugging symbols."
263else
264  echo "Skipping debugging symbols."
265  dbg_list=
266fi
267
268# When cross building for arm on 64-bit systems the host binaries
269# that are part of v8 need to be compiled with -m32 which means
270# that basic multilib support is needed.
271if file /sbin/init | grep -q 'ELF 64-bit'; then
272  if [ "$lsb_release" = "trusty" ]; then
273    # gcc-multilib conflicts with the arm cross compiler in trusty but
274    # g++-4.8-multilib gives us the 32-bit support that we need.
275    arm_list="$arm_list g++-4.8-multilib"
276  else
277    arm_list="$arm_list g++-multilib"
278  fi
279fi
280
281if test "$do_inst_arm" = "1" ; then
282  echo "Including ARM cross toolchain."
283else
284  echo "Skipping ARM cross toolchain."
285  arm_list=
286fi
287
288if test "$do_inst_nacl" = "1"; then
289  echo "Including NaCl, NaCl toolchain, NaCl ports dependencies."
290else
291  echo "Skipping NaCl, NaCl toolchain, NaCl ports dependencies."
292  nacl_list=
293fi
294
295packages="$(
296  echo "${dev_list} ${lib_list} ${dbg_list} ${arm_list} ${nacl_list}" |
297  tr " " "\n" | sort -u | tr "\n" " "
298)"
299
300if [ 1 -eq "${do_quick_check-0}" ] ; then
301  failed_check="$(dpkg-query -W -f '${PackageSpec}:${Status}\n' \
302    ${packages} 2>&1 | grep -v "ok installed" || :)"
303  if [ -n "${failed_check}" ]; then
304    echo
305    nomatch="$(echo "${failed_check}" | \
306      sed -e "s/^No packages found matching \(.*\).$/\1/;t;d")"
307    missing="$(echo "${failed_check}" | \
308      sed -e "/^No packages found matching/d;s/^\(.*\):.*$/\1/")"
309    if [ "$nomatch" ]; then
310      # Distinguish between packages that actually aren't available to the
311      # system (i.e. not in any repo) and packages that just aren't known to
312      # dpkg (i.e. managed by apt).
313      unknown=""
314      for p in ${nomatch}; do
315        if apt-cache show ${p} > /dev/null 2>&1; then
316          missing="${p}\n${missing}"
317        else
318          unknown="${p}\n${unknown}"
319        fi
320      done
321      if [ -n "${unknown}" ]; then
322        echo "WARNING: The following packages are unknown to your system"
323        echo "(maybe missing a repo or need to 'sudo apt-get update'):"
324        echo -e "${unknown}" | sed -e "s/^/  /"
325      fi
326    fi
327    if [ -n "${missing}" ]; then
328      echo "WARNING: The following packages are not installed:"
329      echo -e "${missing}" | sed -e "s/^/  /"
330    fi
331    exit 1
332  fi
333  exit 0
334fi
335
336sudo apt-get update
337
338# We initially run "apt-get" with the --reinstall option and parse its output.
339# This way, we can find all the packages that need to be newly installed
340# without accidentally promoting any packages from "auto" to "manual".
341# We then re-run "apt-get" with just the list of missing packages.
342echo "Finding missing packages..."
343# Intentionally leaving $packages unquoted so it's more readable.
344echo "Packages required: " $packages
345echo
346new_list_cmd="sudo apt-get install --reinstall $(echo $packages)"
347if new_list="$(yes n | LANGUAGE=en LANG=C $new_list_cmd)"; then
348  # We probably never hit this following line.
349  echo "No missing packages, and the packages are up-to-date."
350elif [ $? -eq 1 ]; then
351  # We expect apt-get to have exit status of 1.
352  # This indicates that we cancelled the install with "yes n|".
353  new_list=$(echo "$new_list" |
354    sed -e '1,/The following NEW packages will be installed:/d;s/^  //;t;d')
355  new_list=$(echo "$new_list" | sed 's/ *$//')
356  if [ -z "$new_list" ] ; then
357    echo "No missing packages, and the packages are up-to-date."
358  else
359    echo "Installing missing packages: $new_list."
360    sudo apt-get install ${do_quietly-} ${new_list}
361  fi
362  echo
363else
364  # An apt-get exit status of 100 indicates that a real error has occurred.
365
366  # I am intentionally leaving out the '"'s around new_list_cmd,
367  # as this makes it easier to cut and paste the output
368  echo "The following command failed: " ${new_list_cmd}
369  echo
370  echo "It produces the following output:"
371  yes n | $new_list_cmd || true
372  echo
373  echo "You will have to install the above packages yourself."
374  echo
375  exit 100
376fi
377
378# Install the Chrome OS default fonts. This must go after running
379# apt-get, since install-chromeos-fonts depends on curl.
380if test "$do_inst_chromeos_fonts" != "0"; then
381  echo
382  echo "Installing Chrome OS fonts."
383  dir=`echo $0 | sed -r -e 's/\/[^/]+$//'`
384  if ! sudo $dir/linux/install-chromeos-fonts.py; then
385    echo "ERROR: The installation of the Chrome OS default fonts failed."
386    if [ `stat -f -c %T $dir` == "nfs" ]; then
387      echo "The reason is that your repo is installed on a remote file system."
388    else
389      echo "This is expected if your repo is installed on a remote file system."
390    fi
391    echo "It is recommended to install your repo on a local file system."
392    echo "You can skip the installation of the Chrome OS default founts with"
393    echo "the command line option: --no-chromeos-fonts."
394    exit 1
395  fi
396else
397  echo "Skipping installation of Chrome OS fonts."
398fi
399
400if test "$do_inst_nacl" = "1"; then
401  echo "Installing symbolic links for NaCl."
402  if [ ! -r /usr/lib/i386-linux-gnu/libcrypto.so ]; then
403    sudo ln -fs libcrypto.so.0.9.8 /usr/lib/i386-linux-gnu/libcrypto.so
404  fi
405  if [ ! -r /usr/lib/i386-linux-gnu/libssl.so ]; then
406    sudo ln -fs libssl.so.0.9.8 /usr/lib/i386-linux-gnu/libssl.so
407  fi
408else
409  echo "Skipping symbolic links for NaCl."
410fi
411