Initial drop
[meego-developer-tools:meego-sdk-update-target.git] / meego-sdk-update-target
1 #!/bin/bash
2 # Copyright 2010, Intel Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; version 2 of the License.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17 # Authors:
18 #       Jackie Wu   <jackie.wu@intel.com> 
19 #       Jun Tian    <jun.j.tian@intel.com>
20
21 INTERNAL_DEBUG=n
22 PROGNAME=`basename "$0"`
23 PROG_VERSION=1.0.0
24
25 OS=""
26 VERSION=""
27 DIST="Invalid"
28 PKG_EXT="rpm"
29 REMOTE=
30 USERNAME=
31 PASSWORD=
32
33 #default values
34 DEVICE_NAME="handset"
35 DEVICE_ARCH="ia32"
36 MADDE_DIR=
37 MADDE_CACHE_DIR=
38 HOST_ARCH=i686
39
40 # Version of SDK
41 LATEST=
42 SYS_VERSION="1.1.80.9.20101207.1"
43 IMAGE_VERSION="1.1.80.9.20101207.1"
44 MEEGO_VERSION="1.1"
45 TOOLCHAIN=
46 QT_TOOLS="qt-tools-4.7.0"
47
48
49 SYS_NAME="meego-${DEVICE_NAME}-${DEVICE_ARCH}-madde-sysroot-${SYS_VERSION}"
50 IMAGE_NAME="meego-${DEVICE_NAME}-${DEVICE_ARCH}-qemu-${IMAGE_VERSION}"
51 TARGET_NAME="meego-${DEVICE_NAME}-${DEVICE_ARCH}-${IMAGE_VERSION}"
52 SYS_REPO_URL="http://repo.meego.com/MeeGo/builds/1.1.80"
53 IMAGE_REPO_URL="http://repo.meego.com/MeeGo/builds/1.1.80"
54 SYS_URL=
55 IMAGE_URL=
56
57 #For netbook, we just use the sysroot from core
58 if [ $DEVICE_NAME == "netbook" ]; then
59     SYS_NAME="meego-core-${DEVICE_ARCH}-madde-sysroot-${SYS_VERSION}"
60 fi
61
62 QEMU_ARGS=
63 QEMU_ARGS_COMMON=
64
65 function exitApp()
66 {
67     if [ $1 ]; then
68         echo -e "\n Error: Setup not complete\n"; exit 1;
69     else 
70         echo -e "\n Setup not complete\n"; exit 1;
71     fi
72 }
73
74 function setFontColor()
75 {
76     if [ "$1" == "red" ]; then
77         echo -en "\e[1;31m";
78     elif [ "$1" == "blue" ]; then
79         echo -en "\e[1;34m";
80     elif [ "$1" == "green" ]; then
81         echo -en "\e[1;32m";
82     else
83         tput sgr0   
84     fi
85 }
86
87 function usage()
88 {
89     cat << EOF
90 Version $PROG_VERSION
91 Usage: $PROGNAME [OPTION]
92
93   -u      <build>               Update the SDK based on build version when sysroot and image with the same version.
94                                         If the -sysroot and -runtime are specified, -u is used to specify the name 
95                                         of the target.                          
96   -s      <sysroot version>     Update the SDK sysroot with sysroot version
97   -r      <runtime version>     Update the SDK runtime/image with image version
98   -d      <device>              The default is "handset". Can be "handset", "netbook".
99   -a      <arch>                The default is "ia32". Can be "ia32", "armv7l".
100   -url    <repo url>            The url of the shared repository of sysroot and runtime.
101                                         The default is "http://repo.meego.com/MeeGo/builds/1.1.80"
102   -surl   <sysroot repo url>    The url of the sysroot repository. The default is 
103                                         "http://repo.meego.com/MeeGo/builds/1.1.80"
104   -rurl   <runtime repo url>    The url of the runtime/image repository. The default is 
105                                         "http://repo.meego.com/MeeGo/builds/1.1.80"
106   -sysroot <sysroot url>        The url of the sysroot tarball file. The file name has to be in format of:
107                                         "meego-\${device}-\${arch}-madde-sysroot-\${version}-fs.tar.gz", example:
108                                         "meego-handset-ia32-madde-sysroot-1.1.80.11.20101220.1-fs.tar.gz"
109   -runtime <runtime url>        The url of the runtime/image tarball file.The file name has to be in format of:
110                                         "meego-\${device}-\${arch}-qemu-\${version}-raw.tar.bz2", example:
111                                         "meego-handset-ia32-qemu-testing-daily-1.1.80.11.20101220.1-raw.tar.bz2"
112   -l                            Update to the latest version. Not supported yet. 
113   -host-arch <host arch>        The architect of the host machine. Can be "i686", "x86_64"
114   -h                            This help message
115   -v                            Version of this script
116
117   Examples:
118         $PROGNAME -u 1.1.80.9.20101207.1
119         $PROGNAME -s 1.1.80.9.20101207.1 -r 1.1.80.9.20101209.1 -d netbook -a ia32
120         $PROGNAME -u trunk-daily-1.1.80.10.20101208.1 -url http://download.meego.com/trunk-daily/builds/1.1.80
121         $PROGNAME -u trunk-daily-1.1.80.10.20101208.1 -surl http://download.meego.com/trunk-daily/builds/1.1.80 -rurl http://download.meego.com/trunk-daily/builds/1.1.80
122         $PROGNAME -u testing-daily-1.1.80.11.20101220.1 -sysroot http://download.meego.com/testing-daily/builds/1.1.80/testing-daily-1.1.80.11.20101220.1/handset/images/meego-handset-ia32-madde-sysroot/meego-handset-ia32-madde-sysroot-testing-daily-1.1.80.11.20101220.1-fs.tar.gz -runtime http://download.meego.com/testing-daily/builds/1.1.80/testing-daily-1.1.80.11.20101220.1/handset/images/meego-handset-ia32-qemu/meego-handset-ia32-qemu-testing-daily-1.1.80.11.20101220.1-raw.tar.bz2
123
124   Trouble Shooting:
125   If you have problem on this tool usage, please try following things:
126         * Add -debug to get some information. 
127         * Run "mad-admin list" to check if the target and runtime are listed.
128         * Check the generated .conf file in the /usr/lib/madde/linux-\$ARCH/cache/madde.conf.d.
129 EOF
130
131 # Waiting list and public when the components are ready. 
132 #  -madde-dir  <madde dir>      The default is "/usr/lib/madde/linux-i686"
133 #  -meego-version <version>     The release version of MeeGo. Can be 1.1, 1.2 ...Only the major version are needed. 
134 }
135
136 function optreqarg ()
137 {
138         if [ x"${2:-}" == x ]; then
139                 echo "Option '$1' requires an argument"
140                 usage
141                 exit -1;
142         fi
143 }
144
145 function checkParameters()
146 {
147     #TBD: use getopt or getopts
148     #while getopts "ht:r:p:v" OPTION
149     #do
150     # case $OPTION in
151     #  l)
152     #   REMOTE=1
153     #   ;;
154     #...
155
156     if [ -z "$1" ]; then
157         usage;
158         exit -1
159     fi
160  
161     while case ${1:-} in
162         -u)     optreqarg $1 ${2:-}; SYS_VERSION=$2; IMAGE_VERSION=$2; shift;;
163         -s)     optreqarg $1 ${2:-}; SYS_VERSION=$2; shift;;
164         -r)     optreqarg $1 ${2:-}; IMAGE_VERSION=$2; shift;;
165         -d)     optreqarg $1 ${2:-}; DEVICE_NAME=$2; shift;;
166         -a)     optreqarg $1 ${2:-}; DEVICE_ARCH=$2; shift;;
167         -url)   optreqarg $1 ${2:-}; SYS_REPO_URL=$2; IMAGE_REPO_URL=$2; shift;;
168         -surl)  optreqarg $1 ${2:-}; SYS_REPO_URL=$2; shift;;
169         -rurl)  optreqarg $1 ${2:-}; IMAGE_REPO_URL=$2; shift;;
170         -sysroot)       optreqarg $1 ${2:-}; SYS_URL=$2; shift;;
171         -runtime)       optreqarg $1 ${2:-}; IMAGE_URL=$2; shift;;
172         -host-arch)     optreqarg $1 ${2:-}; HOST_ARCH=$2; shift;;
173         -meego-version) optreqarg $1 ${2:-}; MEEGO_VERSION=$2; shift;;
174         -debug) INTERNAL_DEBUG=y;;
175         -l)     LATEST=true ; echo "$PROGNAME has not implemented that feature;"; usage; exit -1;;
176         -h)     usage; exit 0;;
177         -v)     echo "$PROGNAME: Version $PROG_VERSION"; exit 0;;
178         '')     false ;;
179         *)      echo "$PROGNAME: '$1': unknown option."; usage ; exit -1 ;;
180      esac
181 do
182         shift
183 done
184
185     if [ x$MEEGO_VERSION == "x" ]; then
186             echo "The meego version has to be specified so the correct toolchain can be selected"
187             exit -1;
188     fi
189
190     if [ $MEEGO_VERSION == "1.1" ]; then
191         if [ ${DEVICE_ARCH} == "ia32" ]; then
192             TOOLCHAIN="meego-sdk-i586-toolchain-1.1"
193         else
194             TOOLCHAIN="arm-2009q1-203-arm-none-linux-gnueabi"
195         fi
196     else # other versions
197         if [ $MEEGO_VERSION == "1.2" ]; then
198                 TOOLCHAIN="meego-${MEEGO_VERSION}-sdk-${DEVICE_ARCH}-toolchain";
199         else
200             echo "Currently only MeeGo 1.1 and 1.2 are supported. Please specify -meego-version with them."
201             exit -1;
202         fi
203     fi
204
205
206     MADDE_DIR="/usr/lib/madde/linux-${HOST_ARCH}"
207     MADDE_CACHE_DIR="${MADDE_DIR}/cache"
208
209     SYS_NAME="meego-${DEVICE_NAME}-${DEVICE_ARCH}-madde-sysroot-${SYS_VERSION}"
210     IMAGE_NAME="meego-${DEVICE_NAME}-${DEVICE_ARCH}-qemu-${IMAGE_VERSION}"
211     TARGET_NAME="meego-${DEVICE_NAME}-${DEVICE_ARCH}-${IMAGE_VERSION}"
212
213     if [ $DEVICE_NAME == "netbook" ]; then
214         # use core as netbook sysroot
215         SYS_NAME="meego-core-${DEVICE_ARCH}-madde-sysroot-${SYS_VERSION}"
216         QEMU_ARGS=qemugl-args
217         QEMU_ARGS_COMMON="-name MeeGo -m 1024 -boot c -hda ${IMAGE_NAME}-sda.raw \
218 -enable-kvm -vga std -enable-gl -device virtio-gl-pci -soundhw ac97"
219     fi
220
221     if [ x$SYS_URL == "x" ]; then
222         SYS_URL="${SYS_REPO_URL}/${SYS_VERSION}/${DEVICE_NAME}/images/meego-${DEVICE_NAME}-${DEVICE_ARCH}-madde-sysroot/meego-${DEVICE_NAME}-${DEVICE_ARCH}-madde-sysroot-${SYS_VERSION}-fs.tar.gz"
223         if [ $DEVICE_NAME == "netbook" ]; then
224             SYS_URL="${SYS_REPO_URL}/${SYS_VERSION}/core/images/meego-core-${DEVICE_ARCH}-madde-sysroot/meego-core-${DEVICE_ARCH}-madde-sysroot-${SYS_VERSION}-fs.tar.gz"
225         fi
226     fi
227
228     if [ x$IMAGE_URL == "x" ]; then
229         IMAGE_URL="${IMAGE_REPO_URL}/${IMAGE_VERSION}/${DEVICE_NAME}/images/meego-${DEVICE_NAME}-${DEVICE_ARCH}-qemu/meego-${DEVICE_NAME}-${DEVICE_ARCH}-qemu-${IMAGE_VERSION}-raw.tar.bz2"
230     fi
231
232     SYS_FILENAME=`basename $SYS_URL`
233     IMAGE_FILENAME=`basename $IMAGE_URL`
234
235     if [ $DEVICE_NAME == "handset" ]; then
236         QEMU_ARGS=qemugl-args
237         QEMU_ARGS_COMMON="-name MeeGo -m 1024 -boot c -hda ${IMAGE_NAME}-sda.raw \
238 -enable-kvm -vga std -enable-gl -device virtio-gl-pci -soundhw ac97 \
239 -skin /opt/meego/qemu-gl/share/qemugl/meego/skin/handset/skin.xml -usbdevice tablet"
240     fi
241
242     if [ $INTERNAL_DEBUG == "y" ]; then
243         echo "debug: "
244         echo "toolchain: ${TOOLCHAIN}"
245         echo "sysroot url: ${SYS_URL}, file: ${SYS_FILENAME}"
246         echo "image url: ${IMAGE_URL}, file: ${IMAGE_FILENAME}"
247         exit
248     fi
249 }
250
251 function checkOSVersion()
252 {
253     DIST="Invalid"
254
255     if [ -f /etc/lsb-release ]; then
256         OS=$(awk '{ print $1 }' /etc/issue)
257         VERSION=$(awk '{ print $2 }' /etc/issue | \
258             grep -v ^$ | awk -F. '{print $1"."$2}')
259     fi
260
261     if [ -f /etc/redhat-release ]; then
262         OS=$(awk '{ print $1; getline }' /etc/issue)
263         VERSION=$(awk '{ print $3; getline }' /etc/issue)
264     fi
265
266     if [ "$OS" == "Ubuntu" ]; then 
267         if [ $(expr match "$VERSION" "9.10") -ge 4 ]; then
268             DIST="ubuntu/09.10"
269         elif [ $(expr match "$VERSION" "10.04") -ge 5 ]; then
270             DIST="ubuntu/10.04"
271         elif [ $(expr match "$VERSION" "10.10") -ge 5 ]; then
272             DIST="ubuntu/10.10"
273         fi
274         PKG_EXT="deb"
275     fi
276
277     if [ "$OS" == "Fedora" ]; then 
278         if [ "$VERSION" == "12" ]; then
279             DIST="fedora/12"
280         elif [ "$VERSION" == "13" ]; then
281             DIST="fedora/13"
282         fi
283         PKG_EXT="rpm"
284     fi
285
286     if [ "$DIST" == "Invalid" ]; then
287         echo " Your distribution version is not supported yet."
288         exitApp 1
289     fi
290 }
291
292 function checkWelcome()
293 {
294     echo -e "\n Welcome to the MeeGo SDK Target Update Tool\n"
295     echo " This script updates the targets with runtime image and sysroot built from MeeGo."
296
297     if ! test -w ${MADDE_DIR} ; then
298             setFontColor "blue"
299             echo -e "\n Error:  This install script must be run as root\n"
300             setFontColor "default"
301             exit 1
302     fi
303     
304     echo -e " During the update, the following files will be downloaded to ${MADDE_CACHE_DIR}: "
305     echo -e "    ${SYS_URL}"
306     echo -e "    ${IMAGE_URL}"
307     echo -e ""
308 }
309
310 function checkDiskSize()
311 {
312 # Calculate Disk size.  Prompt user to continue
313     DISKSPACE=$(echo $(df -h .)  | awk ' { print $11 }')
314     echo " Update a new MeeGo target takes about 5GB of disk space"
315     echo " You have $DISKSPACE of free disk space"
316
317     setFontColor "blue"
318     echo ""
319     read -p " Would you like to update the new target now (y/n)? "
320     setFontColor "default"
321
322     if [ "$REPLY" != "y" ] && [ "$REPLY" != "Y" ]; then
323         exitApp
324     fi
325 }
326
327
328 function checkPkgInstalled()
329 {
330     local PKG="$1"
331     if [ "$OS" == "Ubuntu" ]; then
332         VAL=$(apt-cache policy $PKG 2>&1 | grep "Install\|Unable" | awk '{ print $2 }' )
333         if [ "$VAL" != "Unable" ] && [ "$VAL" != "(none)" ]; then
334             PKGS="$PKGS $PKG"
335         fi
336     fi
337     
338     if [ "$OS" == "Fedora" ]; then
339         VAL=$(yum info $PKG 2>&1 | grep ": installed" | awk '{ print $1 }')
340         if [ "$VAL" != "" ]; then
341             PKGS="$PKGS $PKG"
342         fi
343     fi
344 }
345
346 function checkSDKInstalled()
347 {
348     # Banner 1/5
349     echo -e "\n----------------------------------------------------------\n"
350     echo -e "  Part 1 (of 4): Check for previous MeeGo SDK installation  "
351     echo -e "\n----------------------------------------------------------"
352
353     # Checking existence of MeeGo SDK packages (must uninstall)
354     echo -e "\n Checking for previous installation of MeeGo SDK..."
355
356     PKGS=""
357     checkPkgInstalled "qt-tools"
358     checkPkgInstalled "madde"
359     checkPkgInstalled "qemu-gl"
360     checkPkgInstalled "meego-sdk-qt-qmlviewer"
361     checkPkgInstalled "meego-sdk-qt4-qmlviewer"
362     checkPkgInstalled "meego-sdk-qtcreator"
363     checkPkgInstalled "meego-sdk-qt-creator"
364     checkPkgInstalled "meego-sdk-libqtcore4"
365     checkPkgInstalled "meego-sdk-qt-qmake"
366     checkPkgInstalled "meego-sdk-qt4-qmake"
367     MADDE_LIB=0
368     if [ -e "${MADDE_CACHE_DIR}/madde.conf.d" ]; then
369         MADDE_LIB=1
370         echo " Found madde components"
371     fi
372     if [ "$PKGS" == "" ] && [ $MADDE_LIB -eq 0 ]; then
373         echo " No previous MeeGo SDK installation detected, please install it first."
374         exitApp 1
375     fi
376 }
377
378 function installRuntime()
379 {
380     if [ ${DEVICE_ARCH} != "ia32" ]; then
381         echo -e "\n---------------------------------------------\n"
382         echo -e "  Part 3 (of 4): Download MeeGo QEMU Image Runtime    "
383         echo -e "                 (~690MB file) "
384         echo -e "  Skipped for ARM in this version "
385         echo -e "\n---------------------------------------------"
386         return
387     fi
388
389     echo -e "\n---------------------------------------------\n"
390     echo -e "  Part 3 (of 4): Download MeeGo QEMU Image Runtime    "
391     echo -e "                 (~690MB file) "
392     echo -e "  The Qemu image downloaded is at ${IMAGE_URL} "
393     echo -e "\n---------------------------------------------"
394     mad-admin create -f -e  ${IMAGE_NAME}-runtime
395     if [ "$?" -ne 2 ] && [ "$?" -ne 0 ]; then
396         exitApp 1
397     fi
398 }
399
400 function installTarget()
401 {
402     echo -e "\n-------------------------------------------\n"
403     echo -e "  Part 4 (of 4): Download MeeGo Toolchain      "
404     echo -e "                 (~280MB file) "
405     echo -e "  The Toolchain downloaded is at ${SYS_URL}  "
406     echo -e "\n-------------------------------------------"
407     mad-admin create -f ${TARGET_NAME}
408     if [ "$?" -ne 2 ] && [ "$?" -ne 0 ]; then
409 #   if [ "$?" -ne 0 ]; then
410         exitApp 1
411     fi
412 }
413
414 # Create sysroot configuration file
415 function createsysconf()
416 {
417     if [ ! -d ./madde.conf.d ]; then
418         mkdir ./madde.conf.d
419     fi
420
421     if [ -f ./madde.conf.d/${SYS_NAME}.conf} ]; then
422         rm -rf ./madde.conf.d/${SYS_NAME}.conf
423     fi
424
425     cat > ./madde.conf.d/${SYS_NAME}.conf << EOF
426
427 EOF
428 }
429
430 # Create target configuration file
431 function createIAtargetconf()
432 {
433     if [ ! -d ./madde.conf.d ]; then
434         mkdir ./madde.conf.d
435     fi
436
437     if [ -f ./madde.conf.d/${TARGET_NAME}.conf ]; then
438         rm -rf ./madde.conf.d/${TARGET_NAME}.conf
439     fi
440
441     cat > ./madde.conf.d/${TARGET_NAME}.conf << EOF
442 require meego-core-${DEVICE_ARCH}-${MEEGO_VERSION}
443 target ${TARGET_NAME}
444         sysroot ${SYS_FILENAME}
445         toolchain ${TOOLCHAIN}
446         runtime ${IMAGE_NAME}-runtime
447         ccxopts -m32 -march=core2 -mssse3 -mtune=atom -mfpmath=sse
448         qttools ${QT_TOOLS}
449         arch i586
450         os Linux
451 end
452
453 runtime ${IMAGE_NAME}-runtime
454         image ${IMAGE_FILENAME}
455         qemu qemu-meego
456         args ${QEMU_ARGS}
457         args-common ${QEMU_ARGS_COMMON}
458 end
459
460 file ${IMAGE_FILENAME}
461         url ${IMAGE_URL}
462 end
463
464 file ${SYS_FILENAME}
465     url ${SYS_URL}
466 end
467 EOF
468 }
469
470 function createARMtargetconf()
471 {
472     if [ ! -d ./madde.conf.d ]; then
473         mkdir ./madde.conf.d
474     fi
475
476     if [ -f ./madde.conf.d/${TARGET_NAME}.conf ]; then
477         rm -rf ./madde.conf.d/${TARGET_NAME}.conf
478     fi
479
480     cat > ./madde.conf.d/${TARGET_NAME}.conf << EOF
481 require meego-core-${DEVICE_ARCH}-${MEEGO_VERSION}
482 target ${TARGET_NAME}
483         sysroot ${SYS_FILENAME}
484         toolchain ${TOOLCHAIN}
485         ccxopts -march=armv7-a -mtune=cortex-a8 -mlittle-endian -mfpu=vfpv3 -mfloat-abi=softfp -D__SOFTFP__
486         qttools ${QT_TOOLS}
487         arch armv7l
488         os Linux
489 end
490
491 file ${SYS_FILENAME}
492     url ${SYS_URL}
493 end
494 EOF
495 }
496
497 function updateTargetAndRuntime()
498 {
499 #    createsysconf;
500     if [ ${DEVICE_ARCH} == "ia32" ]; then
501         createIAtargetconf;
502     else
503         createARMtargetconf;
504     fi
505
506        # Banner 1/3
507     echo -e "\n---------------------------------------\n"
508     echo -e "  Part 2 (of 4): Update Configuration files "
509     echo -e "\n---------------------------------------"
510     if [ -f ./madde.conf.d/${SYS_NAME}.conf ]; then
511         echo -e "Setting sysroot configuration file, version:" ${SYS_VERSION}
512         cp -rf ./madde.conf.d/${SYS_NAME}.conf \
513             ${MADDE_CACHE_DIR}/madde.conf.d \
514             || { exitApp 1; }
515     fi
516     if [ -f ./madde.conf.d/${TARGET_NAME}.conf ]; then
517         echo -e "Setting target configuration file, version:" \
518             ${IMAGE_VERSION}
519         cp -rf ./madde.conf.d/${TARGET_NAME}.conf \
520             ${MADDE_CACHE_DIR}/madde.conf.d \
521             || { exitApp 1; }
522     fi
523
524     installRuntime
525     installTarget
526
527     echo -e "\n Success:  MeeGo SDK has been updated with target ${TARGET_NAME}\n"
528 }
529
530 ##############################################
531 # Script starts execution here
532 # Each function exits on error
533 # Global variables carry data between functions (OS, VERSION, DIST)
534
535 checkParameters $@
536
537 checkOSVersion
538 checkWelcome
539 checkDiskSize
540 checkSDKInstalled
541
542 updateTargetAndRuntime