Prepare new maemo release
[maemo-rb.git] / tools / buildzip.pl
blob56bbe6de1a56f80951c86bd08931c02f6977dcaf
1 #!/usr/bin/perl
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id$
11 use strict;
13 use File::Copy; # For move() and copy()
14 use File::Find; # For find()
15 use File::Path qw(mkpath rmtree); # For rmtree()
16 use Cwd;
17 use Cwd 'abs_path';
18 use Getopt::Long qw(:config pass_through); # pass_through so not confused by -DTYPE_STUFF
20 my $ROOT="..";
22 my $ziptool;
23 my $output;
24 my $verbose;
25 my $install;
26 my $exe;
27 my $target;
28 my $modelname;
29 my $incfonts;
30 my $target_id; # passed in, not currently used
31 my $rbdir; # can be non-.rockbox for special builds
32 my $app;
33 my $mklinks;
35 sub glob_mkdir {
36 my ($dir) = @_;
37 mkpath ($dir, $verbose, 0777);
38 return 1;
41 sub glob_install {
42 my ($_src, $dest, $opts) = @_;
44 unless ($opts) {
45 $opts = "-m 0664";
48 foreach my $src (glob($_src)) {
49 unless ( -d $src || !(-e $src)) {
50 system("install $opts \"$src\" \"$dest\"");
51 print "install $opts \"$src\" -> \"$dest\"\n" if $verbose;
54 return 1;
57 sub glob_copy {
58 my ($pattern, $destination) = @_;
59 print "glob_copy: $pattern -> $destination\n" if $verbose;
60 foreach my $path (glob($pattern)) {
61 copy($path, $destination);
65 sub glob_move {
66 my ($pattern, $destination) = @_;
67 print "glob_move: $pattern -> $destination\n" if $verbose;
68 foreach my $path (glob($pattern)) {
69 move($path, $destination);
73 sub glob_unlink {
74 my ($pattern) = @_;
75 print "glob_unlink: $pattern\n" if $verbose;
76 foreach my $path (glob($pattern)) {
77 unlink($path);
81 sub find_copyfile {
82 my ($pattern, $destination) = @_;
83 print "find_copyfile: $pattern -> $destination\n" if $verbose;
84 return sub {
85 my $path = $_;
86 my $source = getcwd();
87 if ($path =~ $pattern && filesize($path) > 0 && !($path =~ /$rbdir/)) {
88 if($mklinks) {
89 print "link $path $destination\n" if $verbose;
90 symlink($source.'/'.$path, $destination.'/'.$path);
91 } else {
92 print "cp $path $destination\n" if $verbose;
93 copy($path, $destination);
94 chmod(0755, $destination.'/'.$path);
100 sub find_installfile {
101 my ($pattern, $destination) = @_;
102 print "find_installfile: $pattern -> $destination\n" if $verbose;
103 return sub {
104 my $path = $_;
105 if ($path =~ $pattern) {
106 print "FIND_INSTALLFILE: $path\n";
107 glob_install($path, $destination);
113 sub make_install {
114 my ($src, $dest) = @_;
116 my $bindir = $dest;
117 my $libdir = $dest;
118 my $userdir = $dest;
120 my @plugins = ( "games", "apps", "demos", "viewers" );
121 my @userstuff = ( "backdrops", "codepages", "docs", "fonts", "langs", "themes", "wps", "eqs", "icons" );
122 my @files = ();
124 if ($app) {
125 $bindir .= "/bin";
126 $libdir .= "/lib/rockbox";
127 $userdir .= "/share/rockbox";
128 } else {
129 # for non-app builds we expect the prefix to be the dir above .rockbox
130 $bindir .= "/$rbdir";
131 $libdir = $userdir = $bindir;
133 if ($dest =~ /\/dev\/null/) {
134 die "ERROR: No PREFIX given\n"
137 if ((!$app) && -e $bindir && -e $src && (abs_path($bindir) eq abs_path($src))) {
138 return 1;
141 # binary
142 unless ($exe eq "") {
143 unless (glob_mkdir($bindir)) {
144 return 0;
146 glob_install($exe, $bindir, "-m 0775");
149 # codecs
150 unless (glob_mkdir("$libdir/codecs")) {
151 return 0;
153 # Android has codecs installed as native libraries so they are not needed
154 # in the zip.
155 if ($modelname !~ /android/) {
156 glob_install("$src/codecs/*", "$libdir/codecs", "-m 0755");
159 # plugins
160 unless (glob_mkdir("$libdir/rocks")) {
161 return 0;
163 foreach my $t (@plugins) {
164 unless (glob_mkdir("$libdir/rocks/$t")) {
165 return 0;
167 glob_install("$src/rocks/$t/*", "$libdir/rocks/$t", "-m 0755");
170 # rocks/viewers/lua
171 unless (glob_mkdir("$libdir/rocks/viewers/lua")) {
172 return 0;
174 glob_install("$src/rocks/viewers/lua/*", "$libdir/rocks/viewers/lua");
176 # all the rest directories
177 foreach my $t (@userstuff) {
178 unless (glob_mkdir("$userdir/$t")) {
179 return 0;
181 glob_install("$src/$t/*", "$userdir/$t");
184 # wps/ subfolders and bitmaps
185 opendir(DIR, $src . "/wps");
186 @files = readdir(DIR);
187 closedir(DIR);
189 foreach my $_dir (@files) {
190 my $dir = "wps/" . $_dir;
191 if ( -d "$src/$dir" && $_dir !~ /\.\.?/) {
192 unless (glob_mkdir("$userdir/$dir")) {
193 return 0;
195 glob_install("$src/$dir/*", "$userdir/$dir");
199 # rest of the files, excluding the binary
200 opendir(DIR,$src);
201 @files = readdir(DIR);
202 closedir(DIR);
204 foreach my $file (grep (/[a-zA-Z]+\.(txt|config|ignore|sh)/,@files)) {
205 glob_install("$src/$file", "$userdir/");
207 return 1;
210 # Get options
211 GetOptions ( 'r|root=s' => \$ROOT,
212 'z|ziptool:s' => \$ziptool,
213 'm|modelname=s' => \$modelname, # The model name as used in ARCHOS in the root makefile
214 'i|id=s' => \$target_id, # The target id name as used in TARGET_ID in the root makefile
215 'o|output:s' => \$output,
216 'f|fonts=s' => \$incfonts, # 0 - no fonts, 1 - fonts only 2 - fonts and package
217 'v|verbose' => \$verbose,
218 'install=s' => \$install, # install destination
219 'rbdir:s' => \$rbdir, # If we want to put in a different directory
220 'l|link' => \$mklinks, # If we want to create links instead of copying files
221 'a|app:s' => \$app, # Is this an Application build?
224 # GetOptions() doesn't remove the params from @ARGV if their value was ""
225 # Thus we use the ":" for those for which we use a default value in case of ""
226 # and assign the default value afterwards
227 if ($ziptool eq '') {
228 $ziptool = "zip -r9";
230 if ($output eq '') {
231 $output = "rockbox.zip"
233 if ($rbdir eq '') {
234 $rbdir = ".rockbox";
237 # Now @ARGV shuold be free of any left-overs GetOptions left
238 ($target, $exe) = @ARGV;
240 my $firmdir="$ROOT/firmware";
241 my $appsdir="$ROOT/apps";
242 my $viewer_bmpdir="$ROOT/apps/plugins/bitmaps/viewer_defaults";
244 my $cppdef = $target;
246 sub gettargetinfo {
247 open(GCC, ">gcctemp");
248 # Get the LCD screen depth and graphical status
249 print GCC <<STOP
250 \#include "config.h"
251 #ifdef HAVE_LCD_BITMAP
252 Bitmap: yes
253 Depth: LCD_DEPTH
254 LCD Width: LCD_WIDTH
255 LCD Height: LCD_HEIGHT
256 Icon Width: CONFIG_DEFAULT_ICON_WIDTH
257 Icon Height: CONFIG_DEFAULT_ICON_HEIGHT
258 #endif
259 Codec: CONFIG_CODEC
260 #ifdef HAVE_REMOTE_LCD
261 Remote Depth: LCD_REMOTE_DEPTH
262 Remote Icon Width: CONFIG_REMOTE_DEFAULT_ICON_WIDTH
263 Remote Icon Height: CONFIG_REMOTE_DEFAULT_ICON_HEIGHT
264 #else
265 Remote Depth: 0
266 #endif
267 #ifdef HAVE_RECORDING
268 Recording: yes
269 #endif
270 STOP
272 close(GCC);
274 my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";
276 # print STDERR "CMD $c\n";
278 open(TARGET, "$c|");
280 my ($bitmap, $width, $height, $depth, $swcodec, $icon_h, $icon_w);
281 my ($remote_depth, $remote_icon_h, $remote_icon_w);
282 my ($recording);
283 my $icon_count = 1;
284 while(<TARGET>) {
285 # print STDERR "DATA: $_";
286 if($_ =~ /^Bitmap: (.*)/) {
287 $bitmap = $1;
289 elsif($_ =~ /^Depth: (\d*)/) {
290 $depth = $1;
292 elsif($_ =~ /^LCD Width: (\d*)/) {
293 $width = $1;
295 elsif($_ =~ /^LCD Height: (\d*)/) {
296 $height = $1;
298 elsif($_ =~ /^Icon Width: (\d*)/) {
299 $icon_w = $1;
301 elsif($_ =~ /^Icon Height: (\d*)/) {
302 $icon_h = $1;
304 elsif($_ =~ /^Codec: (\d*)/) {
305 # SWCODEC is 1, the others are HWCODEC
306 $swcodec = ($1 == 1);
308 elsif($_ =~ /^Remote Depth: (\d*)/) {
309 $remote_depth = $1;
311 elsif($_ =~ /^Remote Icon Width: (\d*)/) {
312 $remote_icon_w = $1;
314 elsif($_ =~ /^Remote Icon Height: (\d*)/) {
315 $remote_icon_h = $1;
317 if($_ =~ /^Recording: (.*)/) {
318 $recording = $1;
321 close(TARGET);
322 unlink("gcctemp");
324 return ($bitmap, $depth, $width, $height, $icon_w, $icon_h, $recording,
325 $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h);
328 sub filesize {
329 my ($filename)=@_;
330 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
331 $atime,$mtime,$ctime,$blksize,$blocks)
332 = stat($filename);
333 return $size;
337 sub buildzip {
338 my ($image, $fonts)=@_;
339 my $libdir = $install;
340 my $temp_dir = ".rockbox";
342 print "buildzip: image=$image fonts=$fonts\n" if $verbose;
344 my ($bitmap, $depth, $width, $height, $icon_w, $icon_h, $recording,
345 $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h) =
346 &gettargetinfo();
348 # print "Bitmap: $bitmap\nDepth: $depth\nSwcodec: $swcodec\n";
350 # remove old traces
351 rmtree($temp_dir);
353 glob_mkdir($temp_dir);
355 if(!$bitmap) {
356 # always disable fonts on non-bitmap targets
357 $fonts = 0;
359 if($fonts) {
360 glob_mkdir("$temp_dir/fonts");
361 chdir "$temp_dir/fonts";
362 my $cmd = "$ROOT/tools/convbdf -f $ROOT/fonts/*bdf >/dev/null 2>&1";
363 print($cmd."\n") if $verbose;
364 system($cmd);
365 chdir("../../");
367 if($fonts < 2) {
368 # fonts-only package, return
369 return;
373 # create the file so the database does not try indexing a folder
374 open(IGNORE, ">$temp_dir/database.ignore") || die "can't open database.ignore";
375 close(IGNORE);
377 # the samsung ypr0 has a loader script that's needed in the zip
378 if ($modelname =~ /samsungypr0/) {
379 glob_copy("$ROOT/utils/ypr0tools/rockbox.sh", "$temp_dir/");
381 # add .nomedia on Android
382 # in the zip.
383 if ($modelname =~ /android/) {
384 open(NOMEDIA, ">$temp_dir/.nomedia") || die "can't open .nomedia";
385 close(NOMEDIA);
388 glob_mkdir("$temp_dir/langs");
389 glob_mkdir("$temp_dir/rocks");
390 glob_mkdir("$temp_dir/rocks/games");
391 glob_mkdir("$temp_dir/rocks/apps");
392 glob_mkdir("$temp_dir/rocks/demos");
393 glob_mkdir("$temp_dir/rocks/viewers");
395 if ($recording) {
396 glob_mkdir("$temp_dir/recpresets");
399 if($swcodec) {
400 glob_mkdir("$temp_dir/eqs");
402 glob_copy("$ROOT/lib/rbcodec/dsp/eqs/*.cfg", "$temp_dir/eqs/"); # equalizer presets
405 glob_mkdir("$temp_dir/wps");
406 glob_mkdir("$temp_dir/icons");
407 glob_mkdir("$temp_dir/themes");
408 glob_mkdir("$temp_dir/codepages");
410 if($bitmap) {
411 system("$ROOT/tools/codepages");
413 else {
414 system("$ROOT/tools/codepages -m");
417 glob_move('*.cp', "$temp_dir/codepages/");
419 if($bitmap && $depth > 1) {
420 glob_mkdir("$temp_dir/backdrops");
423 glob_mkdir("$temp_dir/codecs");
425 # Android has codecs installed as native libraries so they are not needed
426 # in the zip.
427 if ($modelname !~ /android/) {
428 find(find_copyfile(qr/.*\.codec/, abs_path("$temp_dir/codecs/")), 'lib/rbcodec/codecs');
431 # remove directory again if no codec was copied
432 rmdir("$temp_dir/codecs");
434 find(find_copyfile(qr/\.(rock|ovl|lua)/, abs_path("$temp_dir/rocks/")), 'apps/plugins');
436 # exclude entries for the image file types not supported by the imageviewer for the target.
437 my $viewers = "$ROOT/apps/plugins/viewers.config";
438 my $c="cat $viewers | gcc $cppdef -I. -I$firmdir/export -E -P -include config.h -";
440 open VIEWERS, "$c|" or die "can't open viewers.config";
441 my @viewers = <VIEWERS>;
442 close VIEWERS;
444 open VIEWERS, ">$temp_dir/viewers.config" or
445 die "can't create $temp_dir/viewers.config";
447 foreach my $line (@viewers) {
448 if ($line =~ /([^,]*),([^,]*),/) {
449 my ($ext, $plugin)=($1, $2);
450 my $r = "${plugin}.rock";
451 my $oname;
453 my $dir = $r;
454 my $name;
456 # strip off the last slash and file name part
457 $dir =~ s/(.*)\/(.*)/$1/;
458 # store the file name part
459 $name = $2;
461 # get .ovl name (file part only)
462 $oname = $name;
463 $oname =~ s/\.rock$/.ovl/;
465 # print STDERR "$ext $plugin $dir $name $r\n";
467 if(-e "$temp_dir/rocks/$name") {
468 if($dir ne "rocks") {
469 # target is not 'rocks' but the plugins are always in that
470 # dir at first!
471 move("$temp_dir/rocks/$name", "$temp_dir/rocks/$r");
473 print VIEWERS $line;
475 elsif(-e "$temp_dir/rocks/$r") {
476 # in case the same plugin works for multiple extensions, it
477 # was already moved to the viewers dir
478 print VIEWERS $line;
481 if(-e "$temp_dir/rocks/$oname") {
482 # if there's an "overlay" file for the .rock, move that as
483 # well
484 move("$temp_dir/rocks/$oname", "$temp_dir/rocks/$dir");
488 close VIEWERS;
490 open CATEGORIES, "$ROOT/apps/plugins/CATEGORIES" or
491 die "can't open CATEGORIES";
492 my @rock_targetdirs = <CATEGORIES>;
493 close CATEGORIES;
494 foreach my $line (@rock_targetdirs) {
495 if ($line =~ /([^,]*),(.*)/) {
496 my ($plugin, $dir)=($1, $2);
497 move("$temp_dir/rocks/${plugin}.rock", "$temp_dir/rocks/$dir/${plugin}.rock");
498 if(-e "$temp_dir/rocks/${plugin}.ovl") {
499 # if there's an "overlay" file for the .rock, move that as
500 # well
501 move("$temp_dir/rocks/${plugin}.ovl", "$temp_dir/rocks/$dir");
503 if(-e "$temp_dir/rocks/${plugin}.lua") {
504 # if this is a lua script, move it to the appropriate dir
505 move("$temp_dir/rocks/${plugin}.lua", "$temp_dir/rocks/$dir/");
510 glob_unlink("$temp_dir/rocks/*.lua"); # Clean up unwanted *.lua files (e.g. actions.lua, buttons.lua)
512 copy("$ROOT/apps/tagnavi.config", "$temp_dir/");
513 copy("$ROOT/apps/plugins/disktidy.config", "$temp_dir/rocks/apps/");
515 if($bitmap) {
516 copy("$ROOT/apps/plugins/sokoban.levels", "$temp_dir/rocks/games/sokoban.levels"); # sokoban levels
517 copy("$ROOT/apps/plugins/snake2.levels", "$temp_dir/rocks/games/snake2.levels"); # snake2 levels
518 copy("$ROOT/apps/plugins/rockbox-fonts.config", "$temp_dir/rocks/viewers/");
521 if(-e "$temp_dir/rocks/demos/pictureflow.rock") {
522 copy("$ROOT/apps/plugins/bitmaps/native/pictureflow_emptyslide.100x100x16.bmp",
523 "$temp_dir/rocks/demos/pictureflow_emptyslide.bmp");
524 my ($pf_logo);
525 if ($width < 200) {
526 $pf_logo = "pictureflow_logo.100x18x16.bmp";
527 } else {
528 $pf_logo = "pictureflow_logo.193x34x16.bmp";
530 copy("$ROOT/apps/plugins/bitmaps/native/$pf_logo",
531 "$temp_dir/rocks/demos/pictureflow_splash.bmp");
535 if($image) {
536 # image is blank when this is a simulator
537 if( filesize("rockbox.ucl") > 1000 ) {
538 copy("rockbox.ucl", "$temp_dir/rockbox.ucl"); # UCL for flashing
540 if( filesize("rombox.ucl") > 1000) {
541 copy("rombox.ucl", "$temp_dir/rombox.ucl"); # UCL for flashing
544 # Check for rombox.target
545 if ($image=~/(.*)\.(\w+)$/)
547 my $romfile = "rombox.$2";
548 if (filesize($romfile) > 1000)
550 copy($romfile, "$temp_dir/$romfile");
555 glob_mkdir("$temp_dir/docs");
556 for(("COPYING",
557 "LICENSES",
558 "KNOWN_ISSUES"
559 )) {
560 copy("$ROOT/docs/$_", "$temp_dir/docs/$_.txt");
562 if ($fonts) {
563 copy("$ROOT/docs/profontdoc.txt", "$temp_dir/docs/profontdoc.txt");
565 for(("sample.colours",
566 "sample.icons"
567 )) {
568 copy("$ROOT/docs/$_", "$temp_dir/docs/$_");
571 # Now do the WPS dance
572 if(-d "$ROOT/wps") {
573 my $wps_build_cmd="perl $ROOT/wps/wpsbuild.pl ";
574 $wps_build_cmd=$wps_build_cmd."-v " if $verbose;
575 $wps_build_cmd=$wps_build_cmd." --tempdir=$temp_dir --rbdir=$rbdir -r $ROOT -m $modelname $ROOT/wps/WPSLIST $target";
576 print "wpsbuild: $wps_build_cmd\n" if $verbose;
577 system("$wps_build_cmd");
578 print "wps_build_cmd: done\n" if $verbose;
580 else {
581 print STDERR "No wps module present, can't do the WPS magic!\n";
584 # until buildwps.pl is fixed, manually copy the classic_statusbar theme across
585 mkdir "$temp_dir/wps/classic_statusbar", 0777;
586 glob_copy("$ROOT/wps/classic_statusbar/*.bmp", "$temp_dir/wps/classic_statusbar");
587 if ($swcodec) {
588 if ($depth == 16) {
589 copy("$ROOT/wps/classic_statusbar.sbs", "$temp_dir/wps");
590 } elsif ($depth > 1) {
591 copy("$ROOT/wps/classic_statusbar.grey.sbs", "$temp_dir/wps/classic_statusbar.sbs");
592 } else {
593 copy("$ROOT/wps/classic_statusbar.mono.sbs", "$temp_dir/wps/classic_statusbar.sbs");
595 } else {
596 copy("$ROOT/wps/classic_statusbar.112x64x1.sbs", "$temp_dir/wps/classic_statusbar.sbs");
598 if ($remote_depth != $depth) {
599 copy("$ROOT/wps/classic_statusbar.mono.sbs", "$temp_dir/wps/classic_statusbar.rsbs");
600 } else {
601 copy("$temp_dir/wps/classic_statusbar.sbs", "$temp_dir/wps/classic_statusbar.rsbs");
603 copy("$temp_dir/wps/rockbox_none.sbs", "$temp_dir/wps/rockbox_none.rsbs");
605 # and the info file
606 copy("rockbox-info.txt", "$temp_dir/rockbox-info.txt");
608 # copy the already built lng files
609 glob_copy('apps/lang/*lng', "$temp_dir/langs/");
610 glob_copy('apps/lang/*.zip', "$temp_dir/langs/");
612 # copy the .lua files
613 glob_mkdir("$temp_dir/rocks/viewers/lua/");
614 glob_copy('apps/plugins/lua/*.lua', "$temp_dir/rocks/viewers/lua/");
617 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
618 localtime(time);
620 $mon+=1;
621 $year+=1900;
623 #$date=sprintf("%04d%02d%02d", $year,$mon, $mday);
624 #$shortdate=sprintf("%02d%02d%02d", $year%100,$mon, $mday);
626 # made once for all targets
627 sub runone {
628 my ($target, $fonts)=@_;
630 # Strip the leading / from $rbdir unless we are installing an application
631 # build - the layout is different (no .rockbox, but bin/lib/share)
632 unless ($app && $install) {
633 $rbdir = substr($rbdir, 1);
636 # build a full install .rockbox ($rbdir) directory
637 buildzip($target, $fonts);
639 unlink($output);
641 if($fonts == 1) {
642 # Don't include image file in fonts-only package
643 undef $target;
645 if($target && ($target !~ /(mod|ajz|wma)\z/i)) {
646 # On some targets, the image goes into .rockbox.
647 copy("$target", ".rockbox/$target");
648 undef $target;
651 if($install) {
652 if($mklinks) {
653 my $cwd = getcwd();
654 symlink("$cwd/.rockbox", "$install/.rockbox");
655 print "link .rockbox $install\n" if $verbose;
656 } else {
657 make_install(".rockbox", $install) or die "MKDIRFAILED\n";
658 rmtree(".rockbox");
659 print "rm .rockbox\n" if $verbose;
662 else {
663 unless (".rockbox" eq $rbdir) {
664 mkpath($rbdir);
665 rmtree($rbdir);
666 move(".rockbox", $rbdir);
667 print "mv .rockbox $rbdir\n" if $verbose;
669 system("$ziptool $output $rbdir $target >/dev/null");
670 print "$ziptool $output $rbdir $target >/dev/null\n" if $verbose;
671 rmtree("$rbdir");
672 print "rm $rbdir\n" if $verbose;
676 if(!$exe) {
677 # not specified, guess!
678 if($target =~ /(recorder|ondio)/i) {
679 $exe = "ajbrec.ajz";
681 elsif($target =~ /iriver/i) {
682 $exe = "rockbox.iriver";
684 else {
685 $exe = "archos.mod";
688 elsif(($exe =~ /rockboxui/)) {
689 # simulator, exclude the exe file
690 $exe = "";
692 elsif($exe eq "librockbox.so") {
693 # android, exclude the binary
694 $exe="";
697 runone($exe, $incfonts);