1#!/bin/bash
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# TODO(mmoss) This currently only works with official builds, since non-official
8# builds don't add the "${BUILDDIR}/installer/" files needed for packaging.
9
10set -e
11set -o pipefail
12if [ "$VERBOSE" ]; then
13  set -x
14fi
15set -u
16
17# Create the Debian changelog file needed by dpkg-gencontrol. This just adds a
18# placeholder change, indicating it is the result of an automatic build.
19# TODO(mmoss) Release packages should create something meaningful for a
20# changelog, but simply grabbing the actual 'svn log' is way too verbose. Do we
21# have any type of "significant/visible changes" log that we could use for this?
22gen_changelog() {
23  rm -f "${DEB_CHANGELOG}"
24  process_template "${SCRIPTDIR}/changelog.template" "${DEB_CHANGELOG}"
25  debchange -a --nomultimaint -m --changelog "${DEB_CHANGELOG}" \
26    "Release Notes: ${RELEASENOTES}"
27  GZLOG="${STAGEDIR}/usr/share/doc/${PACKAGE}-${CHANNEL}/changelog.gz"
28  mkdir -p "$(dirname "${GZLOG}")"
29  gzip -9 -c "${DEB_CHANGELOG}" > "${GZLOG}"
30  chmod 644 "${GZLOG}"
31}
32
33# Create the Debian control file needed by dpkg-deb.
34gen_control() {
35  dpkg-gencontrol -v"${VERSIONFULL}" -c"${DEB_CONTROL}" -l"${DEB_CHANGELOG}" \
36  -f"${DEB_FILES}" -p"${PACKAGE}-${CHANNEL}" -P"${STAGEDIR}" \
37  -O > "${STAGEDIR}/DEBIAN/control"
38  rm -f "${DEB_CONTROL}"
39}
40
41# Setup the installation directory hierachy in the package staging area.
42prep_staging_debian() {
43  prep_staging_common
44  install -m 755 -d "${STAGEDIR}/DEBIAN" \
45    "${STAGEDIR}/etc/cron.daily" \
46    "${STAGEDIR}/usr/share/menu" \
47    "${STAGEDIR}/usr/share/doc/${PACKAGE}"
48}
49
50# Put the package contents in the staging area.
51stage_install_debian() {
52  # Always use a different name for /usr/bin symlink depending on channel.
53  # First, to avoid file collisions. Second, to make it possible to
54  # use update-alternatives for /usr/bin/google-chrome.
55  local USR_BIN_SYMLINK_NAME="${PACKAGE}-${CHANNEL}"
56
57  if [ "$CHANNEL" != "stable" ]; then
58    # This would ideally be compiled into the app, but that's a bit too
59    # intrusive of a change for these limited use channels, so we'll just hack
60    # it into the wrapper script. The user can still override since it seems to
61    # work to specify --user-data-dir multiple times on the command line, with
62    # the last occurrence winning.
63    local SXS_USER_DATA_DIR="\${XDG_CONFIG_HOME:-\${HOME}/.config}/${PACKAGE}-${CHANNEL}"
64    local DEFAULT_FLAGS="--user-data-dir=\"${SXS_USER_DATA_DIR}\""
65
66    # Avoid file collisions between channels.
67    local INSTALLDIR="${INSTALLDIR}-${CHANNEL}"
68
69    local PACKAGE="${PACKAGE}-${CHANNEL}"
70
71    # Make it possible to distinguish between menu entries
72    # for different channels.
73    local MENUNAME="${MENUNAME} (${CHANNEL})"
74  fi
75  prep_staging_debian
76  stage_install_common
77  echo "Staging Debian install files in '${STAGEDIR}'..."
78  install -m 755 -d "${STAGEDIR}/${INSTALLDIR}/cron"
79  process_template "${BUILDDIR}/installer/common/repo.cron" \
80      "${STAGEDIR}/${INSTALLDIR}/cron/${PACKAGE}"
81  chmod 755 "${STAGEDIR}/${INSTALLDIR}/cron/${PACKAGE}"
82  pushd "${STAGEDIR}/etc/cron.daily/"
83  ln -snf "${INSTALLDIR}/cron/${PACKAGE}" "${PACKAGE}"
84  popd
85  process_template "${BUILDDIR}/installer/debian/debian.menu" \
86    "${STAGEDIR}/usr/share/menu/${PACKAGE}.menu"
87  chmod 644 "${STAGEDIR}/usr/share/menu/${PACKAGE}.menu"
88  process_template "${BUILDDIR}/installer/debian/postinst" \
89    "${STAGEDIR}/DEBIAN/postinst"
90  chmod 755 "${STAGEDIR}/DEBIAN/postinst"
91  process_template "${BUILDDIR}/installer/debian/prerm" \
92    "${STAGEDIR}/DEBIAN/prerm"
93  chmod 755 "${STAGEDIR}/DEBIAN/prerm"
94  process_template "${BUILDDIR}/installer/debian/postrm" \
95    "${STAGEDIR}/DEBIAN/postrm"
96  chmod 755 "${STAGEDIR}/DEBIAN/postrm"
97}
98
99# Actually generate the package file.
100do_package() {
101  echo "Packaging ${ARCHITECTURE}..."
102  PREDEPENDS="$COMMON_PREDEPS"
103  DEPENDS="${COMMON_DEPS}"
104  REPLACES=""
105  CONFLICTS=""
106  PROVIDES="www-browser"
107  gen_changelog
108  process_template "${SCRIPTDIR}/control.template" "${DEB_CONTROL}"
109  export DEB_HOST_ARCH="${ARCHITECTURE}"
110  if [ -f "${DEB_CONTROL}" ]; then
111    gen_control
112  fi
113  fakeroot dpkg-deb -Zlzma -b "${STAGEDIR}" .
114}
115
116# Remove temporary files and unwanted packaging output.
117cleanup() {
118  echo "Cleaning..."
119  rm -rf "${STAGEDIR}"
120  rm -rf "${TMPFILEDIR}"
121}
122
123usage() {
124  echo "usage: $(basename $0) [-c channel] [-a target_arch] [-o 'dir'] "
125  echo "                      [-b 'dir']"
126  echo "-c channel the package channel (trunk, asan, unstable, beta, stable)"
127  echo "-a arch    package architecture (ia32 or x64)"
128  echo "-o dir     package output directory [${OUTPUTDIR}]"
129  echo "-b dir     build input directory    [${BUILDDIR}]"
130  echo "-h         this help message"
131}
132
133# Check that the channel name is one of the allowable ones.
134verify_channel() {
135  case $CHANNEL in
136    stable )
137      CHANNEL=stable
138      RELEASENOTES="http://googlechromereleases.blogspot.com/search/label/Stable%20updates"
139      ;;
140    unstable|dev|alpha )
141      CHANNEL=unstable
142      RELEASENOTES="http://googlechromereleases.blogspot.com/search/label/Dev%20updates"
143      ;;
144    testing|beta )
145      CHANNEL=beta
146      RELEASENOTES="http://googlechromereleases.blogspot.com/search/label/Beta%20updates"
147      ;;
148    trunk|asan )
149      # Setting this to empty will prevent it from updating any existing configs
150      # from release packages.
151      REPOCONFIG=""
152      RELEASENOTES="http://googlechromereleases.blogspot.com/"
153      ;;
154    * )
155      echo
156      echo "ERROR: '$CHANNEL' is not a valid channel type."
157      echo
158      exit 1
159      ;;
160  esac
161}
162
163process_opts() {
164  while getopts ":o:b:c:a:h" OPTNAME
165  do
166    case $OPTNAME in
167      o )
168        OUTPUTDIR=$(readlink -f "${OPTARG}")
169        mkdir -p "${OUTPUTDIR}"
170        ;;
171      b )
172        BUILDDIR=$(readlink -f "${OPTARG}")
173        ;;
174      c )
175        CHANNEL="$OPTARG"
176        ;;
177      a )
178        TARGETARCH="$OPTARG"
179        ;;
180      h )
181        usage
182        exit 0
183        ;;
184      \: )
185        echo "'-$OPTARG' needs an argument."
186        usage
187        exit 1
188        ;;
189      * )
190        echo "invalid command-line option: $OPTARG"
191        usage
192        exit 1
193        ;;
194    esac
195  done
196}
197
198#=========
199# MAIN
200#=========
201
202SCRIPTDIR=$(readlink -f "$(dirname "$0")")
203OUTPUTDIR="${PWD}"
204STAGEDIR=$(mktemp -d -t deb.build.XXXXXX) || exit 1
205TMPFILEDIR=$(mktemp -d -t deb.tmp.XXXXXX) || exit 1
206DEB_CHANGELOG="${TMPFILEDIR}/changelog"
207DEB_FILES="${TMPFILEDIR}/files"
208DEB_CONTROL="${TMPFILEDIR}/control"
209CHANNEL="trunk"
210# Default target architecture to same as build host.
211if [ "$(uname -m)" = "x86_64" ]; then
212  TARGETARCH="x64"
213else
214  TARGETARCH="ia32"
215fi
216
217# call cleanup() on exit
218trap cleanup 0
219process_opts "$@"
220BUILDDIR=${BUILDDIR:=$(readlink -f "${SCRIPTDIR}/../../../../out/Release")}
221
222source ${BUILDDIR}/installer/common/installer.include
223
224get_version_info
225VERSIONFULL="${VERSION}-${PACKAGE_RELEASE}"
226
227if [ "$CHROMIUM_BUILD" = "_google_chrome" ]; then
228  source "${BUILDDIR}/installer/common/google-chrome.info"
229else
230  source "${BUILDDIR}/installer/common/chromium-browser.info"
231fi
232eval $(sed -e "s/^\([^=]\+\)=\(.*\)$/export \1='\2'/" \
233  "${BUILDDIR}/installer/theme/BRANDING")
234
235REPOCONFIG="deb http://dl.google.com/linux/chrome/deb/ stable main"
236verify_channel
237
238# Some Debian packaging tools want these set.
239export DEBFULLNAME="${MAINTNAME}"
240export DEBEMAIL="${MAINTMAIL}"
241
242# We'd like to eliminate more of these deps by relying on the 'lsb' package, but
243# that brings in tons of unnecessary stuff, like an mta and rpm. Until that full
244# 'lsb' package is installed by default on DEB distros, we'll have to stick with
245# the LSB sub-packages, to avoid pulling in all that stuff that's not installed
246# by default.
247
248# Need a dummy debian/control file for dpkg-shlibdeps.
249DUMMY_STAGING_DIR="${TMPFILEDIR}/dummy_staging"
250mkdir "$DUMMY_STAGING_DIR"
251cd "$DUMMY_STAGING_DIR"
252mkdir debian
253touch debian/control
254
255# Generate the dependencies,
256# TODO(mmoss): This is a workaround for a problem where dpkg-shlibdeps was
257# resolving deps using some of our build output shlibs (i.e.
258# out/Release/lib.target/libfreetype.so.6), and was then failing with:
259#   dpkg-shlibdeps: error: no dependency information found for ...
260# It's not clear if we ever want to look in LD_LIBRARY_PATH to resolve deps,
261# but it seems that we don't currently, so this is the most expediant fix.
262SAVE_LDLP=${LD_LIBRARY_PATH:-}
263unset LD_LIBRARY_PATH
264DPKG_SHLIB_DEPS=$(dpkg-shlibdeps -O "$BUILDDIR/chrome" | \
265  sed 's/^shlibs:Depends=//')
266if [ -n "$SAVE_LDLP" ]; then
267  LD_LIBRARY_PATH=$SAVE_LDLP
268fi
269
270# Format it nicely and save it for comparison.
271# The grep -v is for a duplicate libc6 dep caused by Lucid glibc silliness.
272echo "$DPKG_SHLIB_DEPS" | sed 's/, /\n/g' | \
273  grep -v '^libc6 (>= 2.3.6-6~)$' > actual
274
275# Compare the expected dependency list to the generate list.
276BAD_DIFF=0
277diff "$SCRIPTDIR/expected_deps_$TARGETARCH" actual || BAD_DIFF=1
278if [ $BAD_DIFF -ne 0 ] && [ -z "${IGNORE_DEPS_CHANGES:-}" ]; then
279  echo
280  echo "ERROR: Shared library dependencies changed!"
281  echo "If this is intentional, please update:"
282  echo "chrome/installer/linux/debian/expected_deps_ia32"
283  echo "chrome/installer/linux/debian/expected_deps_x64"
284  echo
285  exit $BAD_DIFF
286fi
287rm -rf "$DUMMY_STAGING_DIR"
288
289# Additional dependencies not in the dpkg-shlibdeps output.
290# Pull a more recent version of NSS than required by runtime linking, for
291# security and stability updates in NSS.
292ADDITION_DEPS="ca-certificates, libappindicator1, libcurl3, \
293  libnss3 (>= 3.14.3), lsb-base (>=3.2), xdg-utils (>= 1.0.2), wget"
294
295# Fix-up libnspr dependency due to renaming in Ubuntu (the old package still
296# exists, but it was moved to "universe" repository, which isn't installed by
297# default).
298DPKG_SHLIB_DEPS=$(sed \
299    's/\(libnspr4-0d ([^)]*)\), /\1 | libnspr4 (>= 4.9.5-0ubuntu0), /g' \
300    <<< $DPKG_SHLIB_DEPS)
301
302# Fix-up libudev dependency because Ubuntu 13.04 has libudev1 instead of
303# libudev0.
304DPKG_SHLIB_DEPS=$(sed 's/\(libudev0 ([^)]*)\), /\1 | libudev1 (>= 198), /g' \
305                  <<< $DPKG_SHLIB_DEPS)
306
307COMMON_DEPS="${DPKG_SHLIB_DEPS}, ${ADDITION_DEPS}"
308COMMON_PREDEPS="dpkg (>= 1.14.0)"
309
310
311# Make everything happen in the OUTPUTDIR.
312cd "${OUTPUTDIR}"
313
314case "$TARGETARCH" in
315  ia32 )
316    export ARCHITECTURE="i386"
317    stage_install_debian
318    ;;
319  x64 )
320    export ARCHITECTURE="amd64"
321    stage_install_debian
322    ;;
323  * )
324    echo
325    echo "ERROR: Don't know how to build DEBs for '$TARGETARCH'."
326    echo
327    exit 1
328    ;;
329esac
330
331do_package
332