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