Make some global function/variables local (by making them static)
[kugel-rb.git] / tools / buildzip.pl
blob0358459c9cb09808061b7b2d44153bfaa9b110f1
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 'abs_path';
17 use Getopt::Long qw(:config pass_through); # pass_through so not confused by -DTYPE_STUFF
19 my $ROOT="..";
21 my $ziptool="zip -r9";
22 my $output="rockbox.zip";
23 my $verbose;
24 my $install;
25 my $exe;
26 my $target;
27 my $modelname;
28 my $incfonts;
29 my $target_id; # passed in, not currently used
30 my $rbdir=".rockbox"; # can be changed for special builds
31 my $app;
34 sub glob_mkdir {
35 my ($dir) = @_;
36 mkpath ($dir, $verbose, 0777);
37 return 1;
40 sub glob_install {
41 my ($_src, $dest, $opts) = @_;
43 unless ($opts) {
44 $opts = "-m 0664";
47 foreach my $src (glob($_src)) {
48 unless ( -d $src || !(-e $src)) {
49 system("install $opts \"$src\" \"$dest\"");
50 print "install $opts \"$src\" -> \"$dest\"\n" if $verbose;
53 return 1;
56 sub glob_copy {
57 my ($pattern, $destination) = @_;
58 print "glob_copy: $pattern -> $destination\n" if $verbose;
59 foreach my $path (glob($pattern)) {
60 copy($path, $destination);
64 sub glob_move {
65 my ($pattern, $destination) = @_;
66 print "glob_move: $pattern -> $destination\n" if $verbose;
67 foreach my $path (glob($pattern)) {
68 move($path, $destination);
72 sub glob_unlink {
73 my ($pattern) = @_;
74 print "glob_unlink: $pattern\n" if $verbose;
75 foreach my $path (glob($pattern)) {
76 unlink($path);
80 sub find_copyfile {
81 my ($pattern, $destination) = @_;
82 print "find_copyfile: $pattern -> $destination\n" if $verbose;
83 return sub {
84 my $path = $_;
85 if ($path =~ $pattern && filesize($path) > 0 && !($path =~ /$rbdir/)) {
86 copy($path, $destination);
87 print "cp $path $destination\n" if $verbose;
88 chmod(0755, $destination.'/'.$path);
93 sub find_installfile {
94 my ($pattern, $destination) = @_;
95 print "find_installfile: $pattern -> $destination\n" if $verbose;
96 return sub {
97 my $path = $_;
98 if ($path =~ $pattern) {
99 print "FIND_INSTALLFILE: $path\n";
100 glob_install($path, $destination);
106 sub make_install {
107 my ($src, $dest) = @_;
109 my $bindir = $dest;
110 my $libdir = $dest;
111 my $userdir = $dest;
113 my @plugins = ( "games", "apps", "demos", "viewers" );
114 my @userstuff = ( "backdrops", "codepages", "docs", "fonts", "langs", "themes", "wps", "eqs", "icons" );
115 my @files = ();
117 if ($app) {
118 $bindir .= "/bin";
119 $libdir .= "/lib/rockbox";
120 $userdir .= "/share/rockbox";
121 } else {
122 # for non-app builds we expect the prefix to be the dir above .rockbox
123 $bindir .= "/.rockbox";
124 $libdir = $userdir = $bindir;
126 if ($dest =~ /\/dev\/null/) {
127 die "ERROR: No PREFIX given\n"
130 if ((!$app) && $src && (abs_path($dest) eq abs_path($src))) {
131 return 1;
134 # binary
135 unless ($exe eq "") {
136 unless (glob_mkdir($bindir)) {
137 return 0;
139 glob_install($exe, $bindir, "-m 0775");
142 # codecs
143 unless (glob_mkdir("$libdir/codecs")) {
144 return 0;
146 glob_install("$src/codecs/*", "$libdir/codecs");
148 # plugins
149 unless (glob_mkdir("$libdir/rocks")) {
150 return 0;
152 foreach my $t (@plugins) {
153 unless (glob_mkdir("$libdir/rocks/$t")) {
154 return 0;
156 glob_install("$src/rocks/$t/*", "$libdir/rocks/$t");
159 # rocks/viewers/lua
160 unless (glob_mkdir("$libdir/rocks/viewers/lua")) {
161 return 0;
163 glob_install("$src/rocks/viewers/lua/*", "$libdir/rocks/viewers/lua");
165 # all the rest directories
166 foreach my $t (@userstuff) {
167 unless (glob_mkdir("$userdir/$t")) {
168 return 0;
170 glob_install("$src/$t/*", "$userdir/$t");
173 # wps/ subfolders and bitmaps
174 opendir(DIR, $src . "/wps");
175 @files = readdir(DIR);
176 closedir(DIR);
178 foreach my $_dir (@files) {
179 my $dir = "wps/" . $_dir;
180 if ( -d "$src/$dir" && $_dir !~ /\.\.?/) {
181 unless (glob_mkdir("$userdir/$dir")) {
182 return 0;
184 glob_install("$src/$dir/*", "$userdir/$dir");
188 # rest of the files, excluding the binary
189 opendir(DIR,$src);
190 @files = readdir(DIR);
191 closedir(DIR);
193 foreach my $file (grep (/[a-zA-Z]+\.(txt|config|ignnore)/,@files)) {
194 glob_install("$src/$file", "$userdir/");
196 return 1;
199 # Get options
200 GetOptions ( 'r|root=s' => \$ROOT,
201 'z|ziptool=s' => \$ziptool,
202 'm|modelname=s' => \$modelname, # The model name as used in ARCHOS in the root makefile
203 'i|id=s' => \$target_id, # The target id name as used in TARGET_ID in the root makefile
204 'o|output=s' => \$output,
205 'f|fonts=s' => \$incfonts, # 0 - no fonts, 1 - fonts only 2 - fonts and package
206 'v|verbose' => \$verbose,
207 'install=s' => \$install, # install destination
208 'rbdir=s' => \$rbdir, # If we want to put in a different directory
211 ($target, $exe) = @ARGV;
213 my $firmdir="$ROOT/firmware";
214 my $appsdir="$ROOT/apps";
215 my $viewer_bmpdir="$ROOT/apps/plugins/bitmaps/viewer_defaults";
217 my $cppdef = $target;
219 sub gettargetinfo {
220 open(GCC, ">gcctemp");
221 # Get the LCD screen depth and graphical status
222 print GCC <<STOP
223 \#include "config.h"
224 #ifdef HAVE_LCD_BITMAP
225 Bitmap: yes
226 Depth: LCD_DEPTH
227 LCD Width: LCD_WIDTH
228 LCD Height: LCD_HEIGHT
229 Icon Width: CONFIG_DEFAULT_ICON_WIDTH
230 Icon Height: CONFIG_DEFAULT_ICON_HEIGHT
231 #endif
232 Codec: CONFIG_CODEC
233 #ifdef HAVE_REMOTE_LCD
234 Remote Depth: LCD_REMOTE_DEPTH
235 Remote Icon Width: CONFIG_REMOTE_DEFAULT_ICON_WIDTH
236 Remote Icon Height: CONFIG_REMOTE_DEFAULT_ICON_HEIGHT
237 #else
238 Remote Depth: 0
239 #endif
240 #ifdef HAVE_RECORDING
241 Recording: yes
242 #endif
243 STOP
245 close(GCC);
247 my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";
249 # print STDERR "CMD $c\n";
251 open(TARGET, "$c|");
253 my ($bitmap, $width, $height, $depth, $swcodec, $icon_h, $icon_w);
254 my ($remote_depth, $remote_icon_h, $remote_icon_w);
255 my ($recording);
256 my $icon_count = 1;
257 while(<TARGET>) {
258 # print STDERR "DATA: $_";
259 if($_ =~ /^Bitmap: (.*)/) {
260 $bitmap = $1;
262 elsif($_ =~ /^Depth: (\d*)/) {
263 $depth = $1;
265 elsif($_ =~ /^LCD Width: (\d*)/) {
266 $width = $1;
268 elsif($_ =~ /^LCD Height: (\d*)/) {
269 $height = $1;
271 elsif($_ =~ /^Icon Width: (\d*)/) {
272 $icon_w = $1;
274 elsif($_ =~ /^Icon Height: (\d*)/) {
275 $icon_h = $1;
277 elsif($_ =~ /^Codec: (\d*)/) {
278 # SWCODEC is 1, the others are HWCODEC
279 $swcodec = ($1 == 1);
281 elsif($_ =~ /^Remote Depth: (\d*)/) {
282 $remote_depth = $1;
284 elsif($_ =~ /^Remote Icon Width: (\d*)/) {
285 $remote_icon_w = $1;
287 elsif($_ =~ /^Remote Icon Height: (\d*)/) {
288 $remote_icon_h = $1;
290 if($_ =~ /^Recording: (.*)/) {
291 $recording = $1;
294 close(TARGET);
295 unlink("gcctemp");
297 return ($bitmap, $depth, $width, $height, $icon_w, $icon_h, $recording,
298 $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h);
301 sub filesize {
302 my ($filename)=@_;
303 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
304 $atime,$mtime,$ctime,$blksize,$blocks)
305 = stat($filename);
306 return $size;
309 sub buildzip {
310 my ($image, $fonts)=@_;
311 my $libdir = $install;
312 my $temp_dir = ".rockbox";
314 print "buildzip: image=$image fonts=$fonts\n" if $verbose;
316 my ($bitmap, $depth, $width, $height, $icon_w, $icon_h, $recording,
317 $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h) =
318 &gettargetinfo();
320 # print "Bitmap: $bitmap\nDepth: $depth\nSwcodec: $swcodec\n";
322 # remove old traces
323 rmtree($temp_dir);
325 glob_mkdir($temp_dir);
327 if(!$bitmap) {
328 # always disable fonts on non-bitmap targets
329 $fonts = 0;
331 if($fonts) {
332 glob_mkdir("$temp_dir/fonts");
333 chdir "$temp_dir/fonts";
334 my $cmd = "$ROOT/tools/convbdf -f $ROOT/fonts/*bdf >/dev/null 2>&1";
335 print($cmd."\n") if $verbose;
336 system($cmd);
337 chdir("../../");
339 if($fonts < 2) {
340 # fonts-only package, return
341 return;
345 # create the file so the database does not try indexing a folder
346 open(IGNORE, ">$temp_dir/database.ignore") || die "can't open database.ignore";
347 close(IGNORE);
349 glob_mkdir("$temp_dir/langs");
350 glob_mkdir("$temp_dir/rocks");
351 glob_mkdir("$temp_dir/rocks/games");
352 glob_mkdir("$temp_dir/rocks/apps");
353 glob_mkdir("$temp_dir/rocks/demos");
354 glob_mkdir("$temp_dir/rocks/viewers");
356 if ($recording) {
357 glob_mkdir("$temp_dir/recpresets");
360 if($swcodec) {
361 glob_mkdir("$temp_dir/eqs");
363 glob_copy("$ROOT/apps/eqs/*.cfg", "$temp_dir/eqs/"); # equalizer presets
366 glob_mkdir("$temp_dir/wps");
367 glob_mkdir("$temp_dir/themes");
368 if ($bitmap) {
369 open(THEME, ">$temp_dir/themes/rockbox_default_icons.cfg");
370 print THEME <<STOP
371 # this config file was auto-generated to make it
372 # easy to reset the icons back to default
373 iconset: -
374 # taken from apps/gui/icon.c
375 viewers iconset: /$rbdir/icons/viewers.bmp
376 remote iconset: -
377 # taken from apps/gui/icon.c
378 remote viewers iconset: /$rbdir/icons/remote_viewers.bmp
380 STOP
382 close(THEME);
385 glob_mkdir("$temp_dir/codepages");
387 if($bitmap) {
388 system("$ROOT/tools/codepages");
390 else {
391 system("$ROOT/tools/codepages -m");
394 glob_move('*.cp', "$temp_dir/codepages/");
396 if($bitmap && $depth > 1) {
397 glob_mkdir("$temp_dir/backdrops");
400 glob_mkdir("$temp_dir/codecs");
402 find(find_copyfile(qr/.*\.codec/, abs_path("$temp_dir/codecs/")), 'apps/codecs');
404 # remove directory again if no codec was copied
405 rmdir("$temp_dir/codecs");
407 find(find_copyfile(qr/\.(rock|ovl|lua)/, abs_path("$temp_dir/rocks/")), 'apps/plugins');
409 open VIEWERS, "$ROOT/apps/plugins/viewers.config" or
410 die "can't open viewers.config";
411 my @viewers = <VIEWERS>;
412 close VIEWERS;
414 open VIEWERS, ">$temp_dir/viewers.config" or
415 die "can't create $temp_dir/viewers.config";
417 foreach my $line (@viewers) {
418 if ($line =~ /([^,]*),([^,]*),/) {
419 my ($ext, $plugin)=($1, $2);
420 my $r = "${plugin}.rock";
421 my $oname;
423 my $dir = $r;
424 my $name;
426 # strip off the last slash and file name part
427 $dir =~ s/(.*)\/(.*)/$1/;
428 # store the file name part
429 $name = $2;
431 # get .ovl name (file part only)
432 $oname = $name;
433 $oname =~ s/\.rock$/.ovl/;
435 # print STDERR "$ext $plugin $dir $name $r\n";
437 if(-e "$temp_dir/rocks/$name") {
438 if($dir ne "rocks") {
439 # target is not 'rocks' but the plugins are always in that
440 # dir at first!
441 move("$temp_dir/rocks/$name", "$temp_dir/rocks/$r");
443 print VIEWERS $line;
445 elsif(-e "$temp_dir/rocks/$r") {
446 # in case the same plugin works for multiple extensions, it
447 # was already moved to the viewers dir
448 print VIEWERS $line;
451 if(-e "$temp_dir/rocks/$oname") {
452 # if there's an "overlay" file for the .rock, move that as
453 # well
454 move("$temp_dir/rocks/$oname", "$temp_dir/rocks/$dir");
458 close VIEWERS;
460 open CATEGORIES, "$ROOT/apps/plugins/CATEGORIES" or
461 die "can't open CATEGORIES";
462 my @rock_targetdirs = <CATEGORIES>;
463 close CATEGORIES;
464 foreach my $line (@rock_targetdirs) {
465 if ($line =~ /([^,]*),(.*)/) {
466 my ($plugin, $dir)=($1, $2);
467 move("$temp_dir/rocks/${plugin}.rock", "$temp_dir/rocks/$dir/${plugin}.rock");
468 if(-e "$temp_dir/rocks/${plugin}.ovl") {
469 # if there's an "overlay" file for the .rock, move that as
470 # well
471 move("$temp_dir/rocks/${plugin}.ovl", "$temp_dir/rocks/$dir");
473 if(-e "$temp_dir/rocks/${plugin}.lua") {
474 # if this is a lua script, move it to the appropriate dir
475 move("$temp_dir/rocks/${plugin}.lua", "$temp_dir/rocks/$dir/");
480 glob_unlink("$temp_dir/rocks/*.lua"); # Clean up unwanted *.lua files (e.g. actions.lua, buttons.lua)
482 if ($bitmap) {
483 glob_mkdir("$temp_dir/icons");
484 copy("$viewer_bmpdir/viewers.${icon_w}x${icon_h}x$depth.bmp", "$temp_dir/icons/viewers.bmp");
485 if ($remote_depth) {
486 copy("$viewer_bmpdir/remote_viewers.${remote_icon_w}x${remote_icon_h}x$remote_depth.bmp", "$temp_dir/icons/remote_viewers.bmp");
490 copy("$ROOT/apps/tagnavi.config", "$temp_dir/");
491 copy("$ROOT/apps/plugins/disktidy.config", "$temp_dir/rocks/apps/");
493 if($bitmap) {
494 copy("$ROOT/apps/plugins/sokoban.levels", "$temp_dir/rocks/games/sokoban.levels"); # sokoban levels
495 copy("$ROOT/apps/plugins/snake2.levels", "$temp_dir/rocks/games/snake2.levels"); # snake2 levels
496 copy("$ROOT/apps/plugins/rockbox-fonts.config", "$temp_dir/rocks/viewers/");
499 if(-e "$temp_dir/rocks/demos/pictureflow.rock") {
500 copy("$ROOT/apps/plugins/bitmaps/native/pictureflow_emptyslide.100x100x16.bmp",
501 "$temp_dir/rocks/demos/pictureflow_emptyslide.bmp");
502 my ($pf_logo);
503 if ($width < 200) {
504 $pf_logo = "pictureflow_logo.100x18x16.bmp";
505 } else {
506 $pf_logo = "pictureflow_logo.193x34x16.bmp";
508 copy("$ROOT/apps/plugins/bitmaps/native/$pf_logo",
509 "$temp_dir/rocks/demos/pictureflow_splash.bmp");
513 if($image) {
514 # image is blank when this is a simulator
515 if( filesize("rockbox.ucl") > 1000 ) {
516 copy("rockbox.ucl", "$temp_dir/rockbox.ucl"); # UCL for flashing
518 if( filesize("rombox.ucl") > 1000) {
519 copy("rombox.ucl", "$temp_dir/rombox.ucl"); # UCL for flashing
522 # Check for rombox.target
523 if ($image=~/(.*)\.(\w+)$/)
525 my $romfile = "rombox.$2";
526 if (filesize($romfile) > 1000)
528 copy($romfile, "$temp_dir/$romfile");
533 glob_mkdir("$temp_dir/docs");
534 for(("COPYING",
535 "LICENSES",
536 "KNOWN_ISSUES"
537 )) {
538 copy("$ROOT/docs/$_", "$temp_dir/docs/$_.txt");
540 if ($fonts) {
541 copy("$ROOT/docs/profontdoc.txt", "$temp_dir/docs/profontdoc.txt");
543 for(("sample.colours",
544 "sample.icons"
545 )) {
546 copy("$ROOT/docs/$_", "$temp_dir/docs/$_");
549 # Now do the WPS dance
550 if(-d "$ROOT/wps") {
551 my $wps_build_cmd="perl $ROOT/wps/wpsbuild.pl ";
552 $wps_build_cmd=$wps_build_cmd."-v " if $verbose;
553 $wps_build_cmd=$wps_build_cmd." --rbdir=$rbdir -r $ROOT -m $modelname $ROOT/wps/WPSLIST $target";
554 print "wpsbuild: $wps_build_cmd\n" if $verbose;
555 system("$wps_build_cmd");
556 print "wps_build_cmd: done\n" if $verbose;
558 else {
559 print STDERR "No wps module present, can't do the WPS magic!\n";
562 # until buildwps.pl is fixed, manually copy the classic_statusbar theme across
563 mkdir "$temp_dir/wps/classic_statusbar", 0777;
564 glob_copy("$ROOT/wps/classic_statusbar/*.bmp", "$temp_dir/wps/classic_statusbar");
565 if ($swcodec) {
566 if ($depth == 16) {
567 copy("$ROOT/wps/classic_statusbar.sbs", "$temp_dir/wps");
568 } elsif ($depth > 1) {
569 copy("$ROOT/wps/classic_statusbar.grey.sbs", "$temp_dir/wps/classic_statusbar.sbs");
570 } else {
571 copy("$ROOT/wps/classic_statusbar.mono.sbs", "$temp_dir/wps/classic_statusbar.sbs");
573 } else {
574 copy("$ROOT/wps/classic_statusbar.112x64x1.sbs", "$temp_dir/wps/classic_statusbar.sbs");
576 system("touch $temp_dir/wps/rockbox_none.sbs");
577 if ($remote_depth != $depth) {
578 copy("$ROOT/wps/classic_statusbar.mono.sbs", "$temp_dir/wps/classic_statusbar.rsbs");
579 } else {
580 copy("$temp_dir/wps/classic_statusbar.sbs", "$temp_dir/wps/classic_statusbar.rsbs");
582 copy("$temp_dir/wps/rockbox_none.sbs", "$temp_dir/wps/rockbox_none.rsbs");
584 # and the info file
585 copy("rockbox-info.txt", "$temp_dir/rockbox-info.txt");
587 # copy the already built lng files
588 glob_copy('apps/lang/*lng', "$temp_dir/langs/");
590 # copy the .lua files
591 glob_mkdir("$temp_dir/rocks/viewers/lua/");
592 glob_copy('apps/plugins/lua/*.lua', "$temp_dir/rocks/viewers/lua/");
595 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
596 localtime(time);
598 $mon+=1;
599 $year+=1900;
601 #$date=sprintf("%04d%02d%02d", $year,$mon, $mday);
602 #$shortdate=sprintf("%02d%02d%02d", $year%100,$mon, $mday);
604 # made once for all targets
605 sub runone {
606 my ($target, $fonts)=@_;
608 # in the app the the layout is different (no .rockbox, but bin/lib/share)
609 $app = ($modelname eq "application");
611 # build a full install .rockbox ($rbdir) directory
612 buildzip($target, $fonts);
614 unlink($output);
616 if($fonts == 1) {
617 # Don't include image file in fonts-only package
618 undef $target;
620 if($target && ($target !~ /(mod|ajz|wma)\z/i)) {
621 # On some targets, the image goes into .rockbox.
622 copy("$target", "$rbdir/$target");
623 undef $target;
626 if($install) {
627 make_install(".rockbox", $install) or die "MKDIRFAILED\n";
629 else {
630 unless (".rockbox" eq $rbdir) {
631 move(".rockbox", $rbdir);
632 print "mv .rockbox $rbdir\n" if $verbose;
634 system("$ziptool $output $rbdir $target >/dev/null");
635 print "$ziptool $output $rbdir $target >/dev/null\n" if $verbose;
637 rmtree(".rockbox");
638 print "rm .rockbox\n" if $verbose;
641 if(!$exe) {
642 # not specified, guess!
643 if($target =~ /(recorder|ondio)/i) {
644 $exe = "ajbrec.ajz";
646 elsif($target =~ /iriver/i) {
647 $exe = "rockbox.iriver";
649 else {
650 $exe = "archos.mod";
653 elsif(($exe =~ /rockboxui/)) {
654 # simulator, exclude the exe file
655 $exe = "";
658 runone($exe, $incfonts);