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