Added function to change current scratchbox target
[maemo-efl] / trunk / build-all.sh
1 #!/bin/bash
2
3 source "helper-functions.sh"
4
5 ###########################################################
6 # Variable definitions
7 ###########################################################
8 __this_script=`basename $0`
9 __this_script_dir=`dirname $0`
10 __orig_path=$PATH
11
12 # Binaries
13 __date=`which date`
14 __git=`which git`
15 __head=`which head`
16 __mkdir=`which mkdir`
17 __sed=`which sed`
18 __uname=`which uname`
19 __chmod=`which chmod`
20
21 # git definitions
22 __git_clone="$__git clone -q"
23 __git_pull="$__git pull -q -n"
24 __git_clean="$__git clean -q -x -d"
25 __git_reset="$__git reset --hard HEAD"
26 __git_repo="git://staff.get-e.org/"
27
28 # e17 git repositories paths
29 __e17_libs="e17/libs"
30 __e17_proto="e17/proto"
31 __e17_python_efl="$__e17_proto/python-efl"
32 __e17_python_etk="users/cmarcelo"
33
34 # modules to build
35 #__lib_modules="edb eet evas ecore embryo edje efreet epeg epsilon emotion etk ewl e_dbus exml enhance"
36 __lib_modules="eet:evas:ecore:embryo:edje:epsilon:etk:e_dbus"
37 __python_modules="python-evas:python-ecore:python-edje:python-epsilon:python-e_dbus"
38 __python_etk_module="python-etk"
39
40 # dpkg definitions
41 __common_build_pkg_options="-I.svn -I.git -I.gitignore -I.cvsignore -rfakeroot -us -uc -D -tc"
42
43 # Packages dirs and log file
44 __today=`$__date +%Y%m%d`
45 __base_pkg_dir="$__this_script_dir/packages_$__today"
46 __output_file="$PWD/$__base_pkg_dir/packages_$__today.log"
47
48 # Scratchbox definitions
49 __sbox_path="/scratchbox"
50
51 # Other
52 __update_repositories=1
53
54 ###########################################################
55 # Function definitions
56 ###########################################################
57
58 function usage() {
59     cat << EOF
60
61 Usage: $__this_script [OPTIONS]
62
63 Build script for EFL debian packages for Maemo.
64
65 Options:
66     -c      Don't update source repositories.
67     -h      Show this usage guide.
68     -s PATH Specify alternate scratchbox path (default: $__sbox_path).
69
70 EOF
71 }
72
73 function create_log_file() {
74     if [ ! -r $__output_file ]; then
75         msg_begin "Creating log file"
76         touch $__output_file
77         msg_end $?
78     else
79         msg "Using existing log file: $__output_file"
80     fi
81 }
82
83 function log_to_file() {
84     local now
85     local msg
86     msg=$1
87
88     if [ ! -r $__output_file ]; then
89         error "log_to_file(): Log file does not exist"
90     fi
91
92     now=`$__date +%H:%M:%S`
93     echo "$now: $msg" >> $__output_file
94 }
95
96 function start_log() {
97     local now
98     now=`$__date -R`
99
100     echo "" >> $__output_file
101
102     if [ $? != 0 ] ; then
103         error "start_log(): Could not create $__output_file file"
104     fi
105
106     cat << EOF >> $__output_file
107 *********************************************************
108 * Started $__this_script: $now *
109 *********************************************************
110
111 EOF
112 }
113
114 function finish_log() {
115     local now
116     now=`$__date -R`
117
118     if [ ! -r $__output_file ]; then
119         error "finish_log(): Log file does not exist"
120     fi
121
122     cat << EOF >> $__output_file
123
124 * Finished $__this_script: $now
125 EOF
126 }
127
128 function clone_repository() {
129     local repo
130     repo=$1
131     `$__git_clone $repo.git >> $__output_file 2>&1 `
132     return $?
133 }
134
135 function update_repository() {
136     local repo
137     repo=$1
138
139     if [ ! -d $repo ]; then
140         error "  Trying to update invalid repository"
141     fi
142
143     cd $repo
144     `$__git_pull >> $__output_file 2>&1`
145     cd - > /dev/null 2>&1
146
147     return 0
148 }
149
150 function clean_repository() {
151     local repo
152     repo=$1
153
154     if [ ! -d $repo ]; then
155         error "  Trying to clean invalid repository"
156     fi
157
158     cd $repo
159     `$__git_reset >> $__output_file 2>&1`
160     `$__git_clean >> $__output_file 2>&1`
161     cd - > /dev/null 2>&1
162
163     return 0
164 }
165
166 function build_pkg() {
167     local dir
168     dir=$1
169
170     cat << EOF >> $dir/sbox_build_pkg
171 #!/bin/bash
172
173 cd $dir/`basename $dir`
174 NOCONFIGURE=1 ./autogen.sh >> $dir/sbox_build_pkg.log 2>&1
175 $__sbox_dpkg_buildpackage $__build_pkg_options >> $dir/sbox_build_pkg.log 2>&1
176 exit \$?
177 EOF
178
179     `$__chmod 755 $dir/sbox_build_pkg`
180     `$__sbox $dir/sbox_build_pkg`
181     return $?
182 }
183
184 function install_pkg() {
185     echo "install_pkg"
186     return $?
187 }
188
189 function uninstall_pkg() {
190     echo "uninstall_pkg"
191     return $?
192 }
193
194 function get_sources() {
195     local modules
196     local repo
197     modules=`echo $1|$__sed -e 's/:/ /g'`
198     repo=$2
199
200     for module in $modules; do
201
202         if [ ! -d $module ]; then
203             error "Directory $module does not exist."
204         fi
205
206         cd $module
207
208         if [ -d $module/.git ]; then
209             if [ ! $__update_repositories -eq 0 ]; then
210                 log_to_file "Updating existing repository at $PWD/$module"
211
212                 msg_begin "  Updating $module repository"
213                 update_repository $module
214                 msg_end $?
215             else
216                 log_to_file "Skipping update of $module repository"
217             fi
218         elif [ -d $module ]; then
219             log_to_file "Removing invalid repository at $PWD/$module"
220             rm -rf $module
221         fi
222
223         if [ ! -d $module ]; then
224             msg_begin "  Cloning $repo/$module.git"
225             clone_repository $repo/$module
226             msg_end $?
227         fi
228
229         cd ..
230     done
231 }
232
233 function setup_distro() {
234     local target
235     local sources_list_file
236     local target_conf_file
237     local ret
238     target=$1
239     sources_list_file=$__sbox_path/users/$USER/targets/$target/etc/apt/sources.list
240     target_conf_file=$__sbox_path/users/$USER/targets/$target.config
241     ret=1
242
243     log_to_file "setup_distro(): Target $target"
244     log_to_file "setup_distro(): sources.list file : $sources_list_file"
245     log_to_file "setup_distro(): config file: $target_conf_file"
246
247     if [ "x$target" != "x" ] && [ -r $sources_list_file ] && [ -r $target_conf_file ]; then
248         # XXX: FIXME Figure out a better way to get the distro string
249         __distro=`$__head -1 $sources_list_file|cut -d" " -f3`
250         __arch=`grep SBOX_CPU= $target_conf_file|cut -d= -f2`
251         __pkg_dir="$__base_pkg_dir/$__distro/$__arch"
252         __build_pkg_options="$__common_build_pkg_options -sa"
253         if [ "x$__arch" = "xarm" ] ; then
254             __build_pkg_options="$__common_build_pkg_options -B"
255         fi
256         ret=0
257     fi
258
259     if [ $ret -eq 0 ]; then
260         log_to_file "setup_distro(): Configuration:"
261         log_to_file "setup_distro(): distro...........: $__distro"
262         log_to_file "setup_distro(): arch.............: $__arch"
263         log_to_file "setup_distro(): build options....: $__build_pkg_options"
264         log_to_file "setup_distro(): pkg_dir..........: $__pkg_dir"
265     fi
266
267     log_to_file "setup_distro(): Returning $ret"
268
269     return $ret
270 }
271
272 function setup_sbox() {
273     if [ ! -d $__sbox_path ]; then
274         error "Invalid scratchbox path: $__sbox_path"
275     fi
276
277     __sbox="$__sbox_path/login"
278     __sbox_tools_bin_path="$__sbox_path/tools/bin"
279     __sbox_sb_conf="$__sbox_tools_bin_path/sb-conf"
280
281     __sbox_maemo3_debian_bin_path="/scratchbox/devkits/maemo3-debian/bin"
282     __sbox_dpkg_buildpackage="$__sbox_maemo3_debian_bin_path/dpkg-buildpackage"
283
284     __sbox_debian_sarge_bin_path="/scratchbox/devkits/debian-sarge/bin"
285     __sbox_dch="$__sbox_debian_sarge_bin_path/dch"
286 }
287
288 function change_sbox_target() {
289     local target
290     target=$1
291
292     `$__sbox_sb_conf select $target`
293     return $?
294 }
295
296 ###########################################################
297 # Begin script
298 ###########################################################
299
300 # Parse comand line options
301 while getopts "chs:" opt ; do
302     case "$opt" in
303         c)
304             __update_repositories=0
305             ;;
306         h)
307             usage
308             exit 0
309             ;;
310         s)
311             __sbox_path=$OPTARG
312             if [ "x$__sbox_path" = "x" ] || [ ! -d $__sbox_path ]; then
313                 error "Invalid scratchbox path: $__sbox_path"
314             fi
315             ;;
316         [?])
317             usage
318             exit 1
319             ;;
320     esac
321 done
322
323 # Initial checks
324
325 # Check if we're running inside scratchbox
326 if [ -r /targets/links/scratchbox.config ]; then
327     error "You should run this script ouside the scratchbox environment."
328 fi
329
330 # Create base packages dir
331 if [ ! -d $__base_pkg_dir ]; then
332     msg_begin "Creating base packages dir $__base_pkg_dir"
333     $__mkdir -p $__base_pkg_dir
334     msg_end $?
335 fi
336
337 # Create log file
338 create_log_file
339 start_log
340
341 # Download modules under e17/libs
342 msg "Downloading e17 modules"
343 get_sources $__lib_modules $__git_repo$__e17_libs
344
345 # Download python modules under proto/python/efl
346 msg "Downloading python modules"
347 get_sources $__python_modules $__git_repo$__e17_python_efl
348
349 # Download python-etk module
350 get_sources $__python_etk_module $__git_repo$__e17_python_etk
351
352 # Setup scratchbox variables
353 setup_sbox
354
355 # For each scratchbox target
356 for target in `$__sbox_sb_conf list --targets`; do
357     msg_begin "Checking distro and arch for target $target"
358     setup_distro $target
359     msg_end $?
360
361     msg "  Distribution..: $__distro"
362     msg "  Architecture..: $__arch"
363     msg "  Build Options.: $__build_pkg_options"
364
365     if [ ! -d $__pkg_dir ]; then
366         msg_begin "Creating dir for $__distro $__arch packages"
367         $__mkdir -p $__pkg_dir
368         msg_end $?
369     fi
370
371     # For each module
372     for module in `echo $__lib_modules $__python_modules $__python_etk_module|$__sed -e 's/:/ /g'`; do
373
374         msg_begin "Changing current sbox target to $target"
375         change_sbox_target $target
376         msg_end $?
377
378         msg "  Building $module packages for $__distro $__arch"
379
380         if [ -d "./$module/$module/debian" ]; then
381             msg_begin "  Removing existing debian directory"
382             rm -rf "./$module/$module/debian"
383             msg_end $?
384         fi
385
386         msg_begin "  Copying specific debian directory"
387         if [ -d "./$module/debian_$__distro" ]; then
388             cp -r "./$module/debian_$__distro" "./$module/$module/debian"
389         elif [ -d "./$module/debian" ]; then
390             cp -r "./$module/debian" "./$module/$module/"
391         else
392             error "  Missing debian directory for $module"
393         fi
394         msg_end $?
395
396         if [ -d "/tmp/$module" ]; then
397             msg_begin "  Removing temporary $module"
398             rm -rf "/tmp/$module"
399             msg_end $?
400         fi
401
402         msg_begin "  Copying $module dir to /tmp"
403         cp -r $module /tmp
404         msg_end $?
405
406         msg_begin "  Cleaning $module repository"
407         clean_repository $module/$module
408         msg_end $?
409
410         msg_begin "  Building $module packages"
411         build_pkg /tmp/$module
412         msg_end $?
413
414         msg_begin "  Copying $module packages"
415         if [ "x$__arch" != "xarm" ]; then
416             mv /tmp/$module/*.dsc /tmp/$module/*.tar.gz $__pkg_dir
417         fi
418         mv /tmp/$module/*.deb /tmp/$module/*.changes $__pkg_dir
419         msg_end $?
420
421         msg_begin "  Removing temporary $module"
422         rm -rf /tmp/$module
423         msg_end $?
424
425         # Install packages
426
427         # Clean tree
428         clean_repository $module
429     done
430
431     # Uninstall all packages
432 done
433
434 # END
435 finish_log