- add third_party src.
[platform/framework/web/crosswalk.git] / src / native_client / tools / trusted_cross_toolchains / trusted-toolchain-creator.armhf.precise.sh
1 #!/bin/bash
2 # Copyright (c) 2012 The Native Client 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 #@ This script builds the (trusted) cross toolchain for arm.
7 #@ It must be run from the native_client/ directory.
8 #@
9 #@ The toolchain consists primarily of a jail with arm header and libraries.
10 #@ It also provides additional tools such as QEMU.
11 #@ It does NOT provide the actual cross compiler anymore.
12 #@ The cross compiler is now comming straight from a debian package.
13 #@ So there is a one-time step required for all machines using this TC.
14 #@ Which is especially true for build-bots:
15 #@
16 #@  tools/trusted_cross_toolchains/trusted-toolchain-creator.armhf.precise.sh  InstallCrossArmBasePackages
17 #@
18 #@
19 #@  Generally this script is invoked as:
20 #@  tools/trusted_cross_toolchains/trusted-toolchain-creator.armhf.precise.sh <mode> <args>*
21 #@  Available modes are shown below.
22 #@
23 #@ This Toolchain was tested with Ubuntu Precise
24 #@
25 #@ Usage of this TC:
26 #@  compile: arm-linux-gnueabihf-gcc -march=armv7-a -isystem ${JAIL}/usr/include
27 #@  link:    arm-linux-gnueabihf-gcc -L${JAIL}/usr/lib -L${JAIL}/usr/lib/arm-linux-gnueabihf
28 #@                                 -L${JAIL}/lib -L${JAIL}/lib/arm-linux-gnueabihf
29 #@
30 #@ Usage of QEMU
31 #@  TBD
32 #@
33 #@ List of modes:
34
35 ######################################################################
36 # Config
37 ######################################################################
38
39 set -o nounset
40 set -o errexit
41
42 readonly SCRIPT_DIR=$(dirname $0)
43
44 # this where we create the ARMEL "jail"
45 readonly INSTALL_ROOT=$(pwd)/toolchain/linux_arm-trusted
46
47 readonly TMP=/tmp/armhf-crosstool-precise
48
49 readonly REQUIRED_TOOLS="wget"
50
51 readonly MAKE_OPTS="-j8"
52
53 ######################################################################
54 # Package Config
55 ######################################################################
56
57 # this where we get the cross toolchain from for the manual install:
58 readonly CROSS_ARM_TC_REPO=http://archive.ubuntu.com/ubuntu
59 # this is where we get all the armhf packages from
60 readonly ARMEL_REPO=http://ports.ubuntu.com/ubuntu-ports
61
62 readonly PACKAGE_LIST="${ARMEL_REPO}/dists/precise/main/binary-armhf/Packages.bz2"
63 readonly PACKAGE_LIST2="${ARMEL_REPO}/dists/precise-security/main/binary-armhf/Packages.bz2"
64
65 # Packages for the host system
66 # NOTE: at one point we should get rid of the 4.5 packages
67 readonly CROSS_ARM_TC_PACKAGES="\
68   libc6-armhf-cross \
69   libc6-dev-armhf-cross \
70   libgcc1-armhf-cross \
71   libgomp1-armhf-cross \
72   linux-libc-dev-armhf-cross \
73   libgcc1-dbg-armhf-cross \
74   libgomp1-dbg-armhf-cross \
75   binutils-arm-linux-gnueabihf \
76   cpp-arm-linux-gnueabihf \
77   gcc-arm-linux-gnueabihf \
78   g++-arm-linux-gnueabihf \
79   cpp-4.5-arm-linux-gnueabihf \
80   gcc-4.5-arm-linux-gnueabihf \
81   g++-4.5-arm-linux-gnueabihf \
82   libmudflap0-dbg-armhf-cross
83 "
84
85 # Jail packages: these are good enough for native client
86 # NOTE: the package listing here should be updated using the
87 # GeneratePackageListXXX() functions below
88 readonly ARMEL_BASE_PACKAGES="\
89   libssl-dev \
90   libssl1.0.0 \
91   libgcc1 \
92   libc6 \
93   libc6-dev \
94   libstdc++6 \
95   libx11-dev \
96   libx11-6 \
97   x11proto-core-dev \
98   libxt-dev \
99   libxt6 \
100   zlib1g \
101   zlib1g-dev"
102
103 # Additional jail packages needed to build chrome
104 # NOTE: the package listing here should be updated using the
105 # GeneratePackageListXXX() functions below
106 readonly ARMEL_BASE_DEP_LIST="${SCRIPT_DIR}/packagelist.precise.armhf.base"
107 readonly ARMEL_BASE_DEP_FILES="$(cat ${ARMEL_BASE_DEP_LIST})"
108
109 readonly ARMEL_EXTRA_PACKAGES="\
110   comerr-dev \
111   krb5-multidev \
112   libasound2 \
113   libasound2-dev \
114   libatk1.0-0 \
115   libatk1.0-dev \
116   libbz2-1.0 \
117   libbz2-dev \
118   libcairo2 \
119   libcairo2-dev \
120   libcairo-gobject2 \
121   libcairo-script-interpreter2 \
122   libcomerr2 \
123   libcups2 \
124   libcups2-dev \
125   libdbus-1-3 \
126   libdbus-1-dev \
127   libexpat1 \
128   libexpat1-dev \
129   libffi6 \
130   libfontconfig1 \
131   libfontconfig1-dev \
132   libfreetype6 \
133   libfreetype6-dev \
134   libgconf-2-4 \
135   libgconf2-4 \
136   libgconf2-dev \
137   libgpg-error0 \
138   libgpg-error-dev \
139   libgcrypt11 \
140   libgcrypt11-dev \
141   libgdk-pixbuf2.0-0 \
142   libgdk-pixbuf2.0-dev \
143   libgnutls26 \
144   libgnutlsxx27 \
145   libgnutls-dev \
146   libgnutls-openssl27 \
147   libgssapi-krb5-2 \
148   libgssrpc4 \
149   libgtk2.0-0 \
150   libgtk2.0-dev \
151   libglib2.0-0 \
152   libglib2.0-dev \
153   libgnome-keyring0 \
154   libgnome-keyring-dev \
155   libkadm5clnt-mit8 \
156   libkadm5srv-mit8 \
157   libkdb5-6 \
158   libkrb5-3 \
159   libkrb5-dev \
160   libkrb5support0 \
161   libk5crypto3 \
162   libnspr4 \
163   libnspr4-dev \
164   libnss3 \
165   libnss3-dev \
166   libnss-db \
167   liborbit2 \
168   libpam0g \
169   libpam0g-dev \
170   libpango1.0-0 \
171   libpango1.0-dev \
172   libpci3 \
173   libpci-dev \
174   libpcre3 \
175   libpcre3-dev \
176   libpcrecpp0 \
177   libpixman-1-0 \
178   libpixman-1-dev \
179   libpng12-0 \
180   libpng12-dev \
181   libpulse0 \
182   libpulse-dev \
183   libpulse-mainloop-glib0 \
184   libselinux1 \
185   libspeechd2 \
186   libspeechd-dev \
187   libudev0 \
188   libudev-dev \
189   libxext-dev \
190   libxext6 \
191   libxau-dev \
192   libxau6 \
193   libxcb1 \
194   libxcb1-dev \
195   libxcb-render0 \
196   libxcb-render0-dev \
197   libxcb-shm0 \
198   libxcb-shm0-dev \
199   libxcomposite1 \
200   libxcomposite-dev \
201   libxcursor1 \
202   libxcursor-dev \
203   libxdamage1 \
204   libxdamage-dev \
205   libxdmcp6 \
206   libxfixes3 \
207   libxfixes-dev \
208   libxi6 \
209   libxi-dev \
210   libxinerama1 \
211   libxinerama-dev \
212   libxrandr2 \
213   libxrandr-dev \
214   libxrender1 \
215   libxrender-dev \
216   libxss1 \
217   libxss-dev \
218   libxtst6 \
219   libxtst-dev \
220   speech-dispatcher \
221   x11proto-composite-dev \
222   x11proto-damage-dev \
223   x11proto-fixes-dev \
224   x11proto-input-dev \
225   x11proto-kb-dev \
226   x11proto-randr-dev \
227   x11proto-record-dev \
228   x11proto-render-dev \
229   x11proto-scrnsaver-dev \
230   x11proto-xext-dev"
231
232 # NOTE: the package listing here should be updated using the
233 # GeneratePackageListXXX() functions below
234 readonly ARMEL_EXTRA_DEP_LIST="${SCRIPT_DIR}/packagelist.precise.armhf.extra"
235 readonly ARMEL_EXTRA_DEP_FILES="$(cat ${ARMEL_EXTRA_DEP_LIST})"
236
237 ######################################################################
238 # Helper
239 ######################################################################
240
241 Banner() {
242   echo "######################################################################"
243   echo $*
244   echo "######################################################################"
245 }
246
247
248 SubBanner() {
249   echo "......................................................................"
250   echo $*
251   echo "......................................................................"
252 }
253
254
255 Usage() {
256   egrep "^#@" $0 | cut --bytes=3-
257 }
258
259
260 DownloadOrCopy() {
261   if [[ -f "$2" ]] ; then
262      echo "$2 already in place"
263   elif [[ $1 =~  'http://' ]] ; then
264     SubBanner "downloading from $1 -> $2"
265     wget $1 -O $2
266   else
267     SubBanner "copying from $1"
268     cp $1 $2
269   fi
270 }
271
272
273 # some sanity checks to make sure this script is run from the right place
274 # with the right tools
275 SanityCheck() {
276   Banner "Sanity Checks"
277
278   if [[ $(basename $(pwd)) != "native_client" ]] ; then
279     echo "ERROR: run this script from the native_client/ dir"
280     exit -1
281   fi
282
283   if ! mkdir -p "${INSTALL_ROOT}" ; then
284      echo "ERROR: ${INSTALL_ROOT} can't be created."
285     exit -1
286   fi
287
288   if ! mkdir -p "${TMP}" ; then
289      echo "ERROR: ${TMP} can't be created."
290     exit -1
291   fi
292
293   for tool in ${REQUIRED_TOOLS} ; do
294     if ! which ${tool} ; then
295       echo "Required binary $tool not found."
296       echo "Exiting."
297       exit 1
298     fi
299   done
300 }
301
302
303 ChangeDirectory() {
304   # Change direcotry to top 'native_client' directory.
305   cd $(dirname ${BASH_SOURCE})
306   cd ../..
307 }
308
309
310 # TODO(robertm): consider wiping all of ${BASE_DIR}
311 ClearInstallDir() {
312   Banner "clearing dirs in ${INSTALL_ROOT}"
313   rm -rf ${INSTALL_ROOT}/*
314 }
315
316
317 CreateTarBall() {
318   local tarball=$1
319   Banner "creating tar ball ${tarball}"
320   tar cfz ${tarball} -C ${INSTALL_ROOT} .
321 }
322
323 ######################################################################
324 # One of these has to be run ONCE per machine
325 ######################################################################
326
327 #@
328 #@ InstallCrossArmBasePackages
329 #@
330 #@      Install packages needed for arm cross compilation.
331 InstallCrossArmBasePackages() {
332   sudo apt-get install ${CROSS_ARM_TC_PACKAGES}
333 }
334
335 ######################################################################
336 #
337 ######################################################################
338
339 #@
340 #@ InstallTrustedLinkerScript
341 #@
342 #@     This forces the loading address of sel_ldr like programs
343 #@     to higher memory areas where they do not conflict with
344 #@     untrusted binaries.
345 #@     This likely no longer used because of "nacl_helper_bootstrap".
346 InstallTrustedLinkerScript() {
347   local trusted_ld_script=${INSTALL_ROOT}/ld_script_arm_trusted
348   # We are using the output of "ld --verbose" which contains
349   # the linker script delimited by "=========".
350   # We are changing the image start address to 70000000
351   # to move the sel_ldr and other images "out of the way"
352   Banner "installing trusted linker script to ${trusted_ld_script}"
353
354   arm-linux-gnueabihf-ld  --verbose |\
355       grep -A 10000 "=======" |\
356       grep -v "=======" |\
357       sed -e 's/00008000/70000000/g' > ${trusted_ld_script}
358 }
359
360 HacksAndPatches() {
361   rel_path=toolchain/linux_arm-trusted
362   Banner "Misc Hacks & Patches"
363   # these are linker scripts with absolute pathnames in them
364   # which we rewrite here
365   lscripts="${rel_path}/usr/lib/arm-linux-gnueabihf/libpthread.so \
366             ${rel_path}/usr/lib/arm-linux-gnueabihf/libc.so"
367
368   SubBanner "Rewriting Linker Scripts"
369   echo $PWD
370   echo $PWD
371   echo $PWD
372   sed -i -e 's|/usr/lib/arm-linux-gnueabihf/||g' ${lscripts}
373   sed -i -e 's|/lib/arm-linux-gnueabihf/||g' ${lscripts}
374
375   # This is for chrome's ./build/linux/pkg-config-wrapper
376   # which overwrites PKG_CONFIG_PATH internally
377   SubBanner "Package Configs Symlink"
378   mkdir -p ${rel_path}/usr/share
379   ln -s ../lib/arm-linux-gnueabihf/pkgconfig ${rel_path}/usr/share/pkgconfig
380 }
381
382
383 InstallMissingArmLibrariesAndHeadersIntoJail() {
384   Banner "Install Libs And Headers Into Jail"
385
386   mkdir -p ${TMP}/armhf-packages
387   mkdir -p ${INSTALL_ROOT}
388   for file in $@ ; do
389     local package="${TMP}/armhf-packages/${file##*/}"
390     Banner "installing ${file}"
391     DownloadOrCopy ${ARMEL_REPO}/pool/${file} ${package}
392     SubBanner "extracting to ${INSTALL_ROOT}"
393     if [[ ! -s ${package} ]] ; then
394       echo
395       echo "ERROR: bad package ${package}"
396       exit -1
397     fi
398     dpkg --fsys-tarfile ${package}\
399       | tar -xvf - --exclude=./usr/share -C ${INSTALL_ROOT}
400   done
401 }
402
403
404 CleanupJailSymlinks() {
405   Banner "jail symlink cleanup"
406
407   pushd ${INSTALL_ROOT}
408   find usr/lib -type l -printf '%p %l\n' | while read link target; do
409     # skip links with non-absolute paths
410     if [[ ${target} != /* ]] ; then
411       continue
412     fi
413     echo "${link}: ${target}"
414     case "${link}" in
415       usr/lib/arm-linux-gnueabihf/*)
416         # Relativize the symlink.
417         ln -snfv "../../..${target}" "${link}"
418         ;;
419       usr/lib/*)
420         # Relativize the symlink.
421         ln -snfv "../..${target}" "${link}"
422         ;;
423     esac
424   done
425
426   find usr/lib -type l -printf '%p %l\n' | while read link target; do
427     # Make sure we catch new bad links.
428     # libnss_db.so is an exception this since is actually a broken link
429     # in Ubuntu. See /usr/lib/x86_64-linux-gnu/libnss_db.so on a
430     # precise desktop.
431     # TODO(sbc): remove this exception if/when Ubuntu fixes this link.
432     if [ "${link}" == "usr/lib/arm-linux-gnueabihf/libnss_db.so" ] ; then
433       echo "ignoring known bad link: ${link}"
434     elif [ ! -r "${link}" ]; then
435       echo "ERROR: FOUND BAD LINK ${link}"
436       exit -1
437     fi
438   done
439   popd
440 }
441
442 #@
443 #@ BuildAndInstallQemu
444 #@
445 #@     Build ARM emulator including some patches for better tracing
446 #
447 # Historic Notes:
448 # Traditionally we were builidng static 32 bit images of qemu on a
449 # 64bit system which would run then on both x86-32 and x86-64 systems.
450 # The latest version of qemu contains new dependencies which
451 # currently make it impossible to build such images on 64bit systems
452 # We can build a static 64bit qemu but it does not work with
453 # the sandboxed translators for unknown reason.
454 # So instead we chose to build 32bit shared images.
455 #
456
457 readonly QEMU_TARBALL=$(readlink -f ../third_party/qemu/qemu-1.0.1.tar.gz)
458 readonly QEMU_PATCH=$(readlink -f ../third_party/qemu/qemu-1.0.1.patch_arm)
459 readonly QEMU_DIR=qemu-1.0.1
460
461 BuildAndInstallQemu() {
462   local saved_dir=$(pwd)
463   local tmpdir="${TMP}/qemu.nacl"
464
465   Banner "Building qemu in ${tmpdir}"
466
467   if [[ -z "$QEMU_TARBALL" ]] ; then
468     echo "ERROR: missing qemu tarball: ../third_party/qemu/qemu-1.0.1.tar.gz"
469     exit 1
470   fi
471
472   rm -rf ${tmpdir}
473   mkdir ${tmpdir}
474   cd ${tmpdir}
475   SubBanner "Untaring ${QEMU_TARBALL}"
476   tar zxf ${QEMU_TARBALL}
477   cd ${QEMU_DIR}
478
479   SubBanner "Patching ${QEMU_PATCH}"
480   patch -p1 < ${QEMU_PATCH}
481
482   SubBanner "Configuring"
483   env -i PATH=/usr/bin/:/bin \
484     ./configure \
485     --extra-cflags="-m32" \
486     --extra-ldflags="-Wl,-rpath=/lib32" \
487     --disable-system \
488     --enable-linux-user \
489     --disable-darwin-user \
490     --disable-bsd-user \
491     --target-list=arm-linux-user \
492     --disable-smartcard-nss \
493     --disable-sdl
494
495 # see above for why we can no longer use -static
496 #    --static
497
498   SubBanner "Make"
499   env -i PATH=/usr/bin/:/bin \
500       V=99 make MAKE_OPTS=${MAKE_OPTS}
501
502   SubBanner "Install ${INSTALL_ROOT}"
503   cp arm-linux-user/qemu-arm ${INSTALL_ROOT}
504   cd ${saved_dir}
505   cp tools/trusted_cross_toolchains/qemu_tool_arm.sh ${INSTALL_ROOT}
506   ln -sf qemu_tool_arm.sh ${INSTALL_ROOT}/run_under_qemu_arm
507 }
508
509 #@
510 #@ BuildJail <tarball-name>
511 #@
512 #@    Build everything and package it
513 BuildJail() {
514   ClearInstallDir
515   InstallMissingArmLibrariesAndHeadersIntoJail \
516     ${ARMEL_BASE_DEP_FILES} \
517     ${ARMEL_EXTRA_DEP_FILES}
518   CleanupJailSymlinks
519   InstallTrustedLinkerScript
520   HacksAndPatches
521   #BuildAndInstallQemu
522   CreateTarBall $1
523 }
524
525 #
526 # GeneratePackageList
527 #
528 #     Looks up package names in ${TMP}/Packages and write list of URLs
529 #     to output file.
530 #
531 GeneratePackageList() {
532   local output_file=$1
533   echo "Updating: ${output_file}"
534   /bin/rm -f ${output_file}
535   shift
536   for pkg in $@ ; do
537     local pkg_full=$(grep -A 1 "${pkg}\$" ${TMP}/Packages | tail -1 | egrep -o "pool/.*")
538     if [[ -z ${pkg_full} ]]; then
539         echo "ERROR: missing package: $pkg"
540         exit 1
541     fi
542     echo $pkg_full | sed "s/^pool\///" >> $output_file
543   done
544   # sort -o does an in-place sort of this file
545   sort $output_file -o $output_file
546 }
547
548 #@
549 #@ UpdatePackageLists
550 #@
551 #@     Regenerate the armhf package lists such that they contain an up-to-date
552 #@     list of URLs within the ubuntu archive.
553 #@
554 UpdatePackageLists() {
555   local package_list="${TMP}/Packages.precise.bz2"
556   local package_list2="${TMP}/Packages.precise-security.bz2"
557   DownloadOrCopy ${PACKAGE_LIST} ${package_list}
558   DownloadOrCopy ${PACKAGE_LIST2} ${package_list2}
559   bzcat ${package_list} ${package_list2} | egrep '^(Package:|Filename:)' > ${TMP}/Packages
560
561   GeneratePackageList ${ARMEL_BASE_DEP_LIST} "${ARMEL_BASE_PACKAGES}"
562   GeneratePackageList ${ARMEL_EXTRA_DEP_LIST} "${ARMEL_EXTRA_PACKAGES}"
563 }
564
565 if [[ $# -eq 0 ]] ; then
566   echo "ERROR: you must specify a mode on the commandline"
567   echo
568   Usage
569   exit -1
570 elif [[ "$(type -t $1)" != "function" ]]; then
571   echo "ERROR: unknown function '$1'." >&2
572   echo "For help, try:"
573   echo "    $0 help"
574   exit 1
575 else
576   ChangeDirectory
577   SanityCheck
578   "$@"
579 fi