Differentiate between shipped and runtime generated files with NEED_WRITE flag.
[kugel-rb.git] / tools / buildzip.pl
blob2fddb209807d4367ddd66c4623dd57106f1e9499
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_copy {
41 my ($pattern, $destination) = @_;
42 print "glob_copy: $pattern -> $destination\n" if $verbose;
43 foreach my $path (glob($pattern)) {
44 copy($path, $destination);
48 sub glob_move {
49 my ($pattern, $destination) = @_;
50 print "glob_move: $pattern -> $destination\n" if $verbose;
51 foreach my $path (glob($pattern)) {
52 move($path, $destination);
56 sub glob_unlink {
57 my ($pattern) = @_;
58 print "glob_unlink: $pattern\n" if $verbose;
59 foreach my $path (glob($pattern)) {
60 unlink($path);
64 sub find_copyfile {
65 my ($pattern, $destination) = @_;
66 print "find_copyfile: $pattern -> $destination\n" if $verbose;
67 return sub {
68 my $path = $_;
69 if ($path =~ $pattern && filesize($path) > 0 && !($path =~ /$rbdir/)) {
70 copy($path, $destination);
71 print "cp $path $destination\n" if $verbose;
72 chmod(0755, $destination.'/'.$path);
77 sub make_install {
78 my ($dest) = @_;
80 if ($dest =~ /\/dev\/null/) {
81 die "ERROR: No PREFIX given\n"
84 my $samedir = abs_path("$dest/$rbdir") eq abs_path($rbdir);
86 if ($app) {
87 unless (glob_mkdir($dest . "/bin")) {
88 return 0;
91 system("cp $exe $dest/bin/");
92 chmod(0755, "$dest/bin/$exe");
93 print "cp $exe $dest/bin/\n" if $verbose;
94 unless (glob_mkdir($dest . "/lib/rockbox/codecs")) {
95 return 0;
98 find(find_copyfile(qr/.*\.codec/, abs_path("$dest/lib/rockbox/codecs/")), 'apps/codecs');
101 #~ if (!$samedir) {
102 #~ system("mkdir -p \"$install\" && cp -vr $rbdir \"$dest/\"");
103 #~ }
105 return 1;
108 # Get options
109 GetOptions ( 'r|root=s' => \$ROOT,
110 'z|ziptool=s' => \$ziptool,
111 'm|modelname=s' => \$modelname, # The model name as used in ARCHOS in the root makefile
112 'i|id=s' => \$target_id, # The target id name as used in TARGET_ID in the root makefile
113 'o|output=s' => \$output,
114 'f|fonts=s' => \$incfonts, # 0 - no fonts, 1 - fonts only 2 - fonts and package
115 'v|verbose' => \$verbose,
116 'install=s' => \$install, # install destination
117 'rbdir=s' => \$rbdir, # If we want to put in a different directory
120 ($target, $exe) = @ARGV;
122 my $firmdir="$ROOT/firmware";
123 my $appsdir="$ROOT/apps";
124 my $viewer_bmpdir="$ROOT/apps/plugins/bitmaps/viewer_defaults";
126 my $cppdef = $target;
128 sub gettargetinfo {
129 open(GCC, ">gcctemp");
130 # Get the LCD screen depth and graphical status
131 print GCC <<STOP
132 \#include "config.h"
133 #ifdef HAVE_LCD_BITMAP
134 Bitmap: yes
135 Depth: LCD_DEPTH
136 LCD Width: LCD_WIDTH
137 LCD Height: LCD_HEIGHT
138 Icon Width: CONFIG_DEFAULT_ICON_WIDTH
139 Icon Height: CONFIG_DEFAULT_ICON_HEIGHT
140 #endif
141 Codec: CONFIG_CODEC
142 #ifdef HAVE_REMOTE_LCD
143 Remote Depth: LCD_REMOTE_DEPTH
144 Remote Icon Width: CONFIG_REMOTE_DEFAULT_ICON_WIDTH
145 Remote Icon Height: CONFIG_REMOTE_DEFAULT_ICON_HEIGHT
146 #else
147 Remote Depth: 0
148 #endif
149 #ifdef HAVE_RECORDING
150 Recording: yes
151 #endif
152 STOP
154 close(GCC);
156 my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";
158 # print STDERR "CMD $c\n";
160 open(TARGET, "$c|");
162 my ($bitmap, $width, $height, $depth, $swcodec, $icon_h, $icon_w);
163 my ($remote_depth, $remote_icon_h, $remote_icon_w);
164 my ($recording);
165 my $icon_count = 1;
166 while(<TARGET>) {
167 # print STDERR "DATA: $_";
168 if($_ =~ /^Bitmap: (.*)/) {
169 $bitmap = $1;
171 elsif($_ =~ /^Depth: (\d*)/) {
172 $depth = $1;
174 elsif($_ =~ /^LCD Width: (\d*)/) {
175 $width = $1;
177 elsif($_ =~ /^LCD Height: (\d*)/) {
178 $height = $1;
180 elsif($_ =~ /^Icon Width: (\d*)/) {
181 $icon_w = $1;
183 elsif($_ =~ /^Icon Height: (\d*)/) {
184 $icon_h = $1;
186 elsif($_ =~ /^Codec: (\d*)/) {
187 # SWCODEC is 1, the others are HWCODEC
188 $swcodec = ($1 == 1);
190 elsif($_ =~ /^Remote Depth: (\d*)/) {
191 $remote_depth = $1;
193 elsif($_ =~ /^Remote Icon Width: (\d*)/) {
194 $remote_icon_w = $1;
196 elsif($_ =~ /^Remote Icon Height: (\d*)/) {
197 $remote_icon_h = $1;
199 if($_ =~ /^Recording: (.*)/) {
200 $recording = $1;
203 close(TARGET);
204 unlink("gcctemp");
206 return ($bitmap, $depth, $width, $height, $icon_w, $icon_h, $recording,
207 $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h);
210 sub filesize {
211 my ($filename)=@_;
212 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
213 $atime,$mtime,$ctime,$blksize,$blocks)
214 = stat($filename);
215 return $size;
218 sub buildzip {
219 my ($image, $fonts)=@_;
220 my $libdir = $install;
222 print "buildzip: image=$image fonts=$fonts\n" if $verbose;
224 my ($bitmap, $depth, $width, $height, $icon_w, $icon_h, $recording,
225 $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h) =
226 &gettargetinfo();
228 if ($app) {
229 $libdir += "/lib"
232 # print "Bitmap: $bitmap\nDepth: $depth\nSwcodec: $swcodec\n";
234 # remove old traces
235 rmtree($rbdir);
237 glob_mkdir($rbdir);
239 if(!$bitmap) {
240 # always disable fonts on non-bitmap targets
241 $fonts = 0;
243 if($fonts) {
244 glob_mkdir("$rbdir/fonts");
245 chdir "$rbdir/fonts";
246 my $cmd = "$ROOT/tools/convbdf -f $ROOT/fonts/*bdf >/dev/null 2>&1";
247 print($cmd."\n") if $verbose;
248 system($cmd);
249 chdir("../../");
251 if($fonts < 2) {
252 # fonts-only package, return
253 return;
257 # create the file so the database does not try indexing a folder
258 open(IGNORE, ">$rbdir/database.ignore") || die "can't open database.ignore";
259 close(IGNORE);
261 glob_mkdir("$rbdir/langs");
262 glob_mkdir("$rbdir/rocks");
263 glob_mkdir("$rbdir/rocks/games");
264 glob_mkdir("$rbdir/rocks/apps");
265 glob_mkdir("$rbdir/rocks/demos");
266 glob_mkdir("$rbdir/rocks/viewers");
268 if ($recording) {
269 glob_mkdir("$rbdir/recpresets");
272 if($swcodec) {
273 glob_mkdir("$rbdir/eqs");
275 glob_copy("$ROOT/apps/eqs/*.cfg", "$rbdir/eqs/"); # equalizer presets
278 glob_mkdir("$rbdir/wps");
279 glob_mkdir("$rbdir/themes");
280 if ($bitmap) {
281 open(THEME, ">$rbdir/themes/rockbox_default_icons.cfg");
282 print THEME <<STOP
283 # this config file was auto-generated to make it
284 # easy to reset the icons back to default
285 iconset: -
286 # taken from apps/gui/icon.c
287 viewers iconset: /$rbdir/icons/viewers.bmp
288 remote iconset: -
289 # taken from apps/gui/icon.c
290 remote viewers iconset: /$rbdir/icons/remote_viewers.bmp
292 STOP
294 close(THEME);
297 glob_mkdir("$rbdir/codepages");
299 if($bitmap) {
300 system("$ROOT/tools/codepages");
302 else {
303 system("$ROOT/tools/codepages -m");
306 glob_move('*.cp', "$rbdir/codepages/");
308 if($bitmap && $depth > 1) {
309 glob_mkdir("$rbdir/backdrops");
312 unless ($app) {
313 glob_mkdir("$rbdir/codecs");
315 find(find_copyfile(qr/.*\.codec/, abs_path("$rbdir/codecs/")), 'apps/codecs');
317 # remove directory again if no codec was copied
318 rmdir("$rbdir/codecs");
321 find(find_copyfile(qr/\.(rock|ovl|lua)/, abs_path("$rbdir/rocks/")), 'apps/plugins');
323 open VIEWERS, "$ROOT/apps/plugins/viewers.config" or
324 die "can't open viewers.config";
325 my @viewers = <VIEWERS>;
326 close VIEWERS;
328 open VIEWERS, ">$rbdir/viewers.config" or
329 die "can't create $rbdir/viewers.config";
331 foreach my $line (@viewers) {
332 if ($line =~ /([^,]*),([^,]*),/) {
333 my ($ext, $plugin)=($1, $2);
334 my $r = "${plugin}.rock";
335 my $oname;
337 my $dir = $r;
338 my $name;
340 # strip off the last slash and file name part
341 $dir =~ s/(.*)\/(.*)/$1/;
342 # store the file name part
343 $name = $2;
345 # get .ovl name (file part only)
346 $oname = $name;
347 $oname =~ s/\.rock$/.ovl/;
349 # print STDERR "$ext $plugin $dir $name $r\n";
351 if(-e "$rbdir/rocks/$name") {
352 if($dir ne "rocks") {
353 # target is not 'rocks' but the plugins are always in that
354 # dir at first!
355 move("$rbdir/rocks/$name", "$rbdir/rocks/$r");
357 print VIEWERS $line;
359 elsif(-e "$rbdir/rocks/$r") {
360 # in case the same plugin works for multiple extensions, it
361 # was already moved to the viewers dir
362 print VIEWERS $line;
365 if(-e "$rbdir/rocks/$oname") {
366 # if there's an "overlay" file for the .rock, move that as
367 # well
368 move("$rbdir/rocks/$oname", "$rbdir/rocks/$dir");
372 close VIEWERS;
374 open CATEGORIES, "$ROOT/apps/plugins/CATEGORIES" or
375 die "can't open CATEGORIES";
376 my @rock_targetdirs = <CATEGORIES>;
377 close CATEGORIES;
378 foreach my $line (@rock_targetdirs) {
379 if ($line =~ /([^,]*),(.*)/) {
380 my ($plugin, $dir)=($1, $2);
381 move("$rbdir/rocks/${plugin}.rock", "$rbdir/rocks/$dir/${plugin}.rock");
382 if(-e "$rbdir/rocks/${plugin}.ovl") {
383 # if there's an "overlay" file for the .rock, move that as
384 # well
385 move("$rbdir/rocks/${plugin}.ovl", "$rbdir/rocks/$dir");
387 if(-e "$rbdir/rocks/${plugin}.lua") {
388 # if this is a lua script, move it to the appropriate dir
389 move("$rbdir/rocks/${plugin}.lua", "$rbdir/rocks/$dir/");
394 glob_unlink("$rbdir/rocks/*.lua"); # Clean up unwanted *.lua files (e.g. actions.lua, buttons.lua)
396 if ($bitmap) {
397 glob_mkdir("$rbdir/icons");
398 copy("$viewer_bmpdir/viewers.${icon_w}x${icon_h}x$depth.bmp", "$rbdir/icons/viewers.bmp");
399 if ($remote_depth) {
400 copy("$viewer_bmpdir/remote_viewers.${remote_icon_w}x${remote_icon_h}x$remote_depth.bmp", "$rbdir/icons/remote_viewers.bmp");
404 copy("$ROOT/apps/tagnavi.config", "$rbdir/");
405 copy("$ROOT/apps/plugins/disktidy.config", "$rbdir/rocks/apps/");
407 if($bitmap) {
408 copy("$ROOT/apps/plugins/sokoban.levels", "$rbdir/rocks/games/sokoban.levels"); # sokoban levels
409 copy("$ROOT/apps/plugins/snake2.levels", "$rbdir/rocks/games/snake2.levels"); # snake2 levels
410 copy("$ROOT/apps/plugins/rockbox-fonts.config", "$rbdir/rocks/viewers/");
413 if(-e "$rbdir/rocks/demos/pictureflow.rock") {
414 copy("$ROOT/apps/plugins/bitmaps/native/pictureflow_emptyslide.100x100x16.bmp",
415 "$rbdir/rocks/demos/pictureflow_emptyslide.bmp");
416 my ($pf_logo);
417 if ($width < 200) {
418 $pf_logo = "pictureflow_logo.100x18x16.bmp";
419 } else {
420 $pf_logo = "pictureflow_logo.193x34x16.bmp";
422 copy("$ROOT/apps/plugins/bitmaps/native/$pf_logo",
423 "$rbdir/rocks/demos/pictureflow_splash.bmp");
427 if($image) {
428 # image is blank when this is a simulator
429 if( filesize("rockbox.ucl") > 1000 ) {
430 copy("rockbox.ucl", "$rbdir/rockbox.ucl"); # UCL for flashing
432 if( filesize("rombox.ucl") > 1000) {
433 copy("rombox.ucl", "$rbdir/rombox.ucl"); # UCL for flashing
436 # Check for rombox.target
437 if ($image=~/(.*)\.(\w+)$/)
439 my $romfile = "rombox.$2";
440 if (filesize($romfile) > 1000)
442 copy($romfile, "$rbdir/$romfile");
447 glob_mkdir("$rbdir/docs");
448 for(("COPYING",
449 "LICENSES",
450 "KNOWN_ISSUES"
451 )) {
452 copy("$ROOT/docs/$_", "$rbdir/docs/$_.txt");
454 if ($fonts) {
455 copy("$ROOT/docs/profontdoc.txt", "$rbdir/docs/profontdoc.txt");
457 for(("sample.colours",
458 "sample.icons"
459 )) {
460 copy("$ROOT/docs/$_", "$rbdir/docs/$_");
463 # Now do the WPS dance
464 if(-d "$ROOT/wps") {
465 my $wps_build_cmd="perl $ROOT/wps/wpsbuild.pl ";
466 $wps_build_cmd=$wps_build_cmd."-v " if $verbose;
467 $wps_build_cmd=$wps_build_cmd." --rbdir=$rbdir -r $ROOT -m $modelname $ROOT/wps/WPSLIST $target";
468 print "wpsbuild: $wps_build_cmd\n" if $verbose;
469 system("$wps_build_cmd");
470 print "wps_build_cmd: done\n" if $verbose;
472 else {
473 print STDERR "No wps module present, can't do the WPS magic!\n";
476 # until buildwps.pl is fixed, manually copy the classic_statusbar theme across
477 mkdir "$rbdir/wps/classic_statusbar", 0777;
478 glob_copy("$ROOT/wps/classic_statusbar/*.bmp", "$rbdir/wps/classic_statusbar");
479 if ($swcodec) {
480 if ($depth == 16) {
481 copy("$ROOT/wps/classic_statusbar.sbs", "$rbdir/wps");
482 } elsif ($depth > 1) {
483 copy("$ROOT/wps/classic_statusbar.grey.sbs", "$rbdir/wps/classic_statusbar.sbs");
484 } else {
485 copy("$ROOT/wps/classic_statusbar.mono.sbs", "$rbdir/wps/classic_statusbar.sbs");
487 } else {
488 copy("$ROOT/wps/classic_statusbar.112x64x1.sbs", "$rbdir/wps/classic_statusbar.sbs");
490 system("touch $rbdir/wps/rockbox_none.sbs");
491 if ($remote_depth != $depth) {
492 copy("$ROOT/wps/classic_statusbar.mono.sbs", "$rbdir/wps/classic_statusbar.rsbs");
493 } else {
494 copy("$rbdir/wps/classic_statusbar.sbs", "$rbdir/wps/classic_statusbar.rsbs");
496 copy("$rbdir/wps/rockbox_none.sbs", "$rbdir/wps/rockbox_none.rsbs");
498 # and the info file
499 copy("rockbox-info.txt", "$rbdir/rockbox-info.txt");
501 # copy the already built lng files
502 glob_copy('apps/lang/*lng', "$rbdir/langs/");
504 # copy the .lua files
505 glob_mkdir("$rbdir/rocks/viewers/lua/");
506 glob_copy('apps/plugins/lua/*.lua', "$rbdir/rocks/viewers/lua/");
509 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
510 localtime(time);
512 $mon+=1;
513 $year+=1900;
515 #$date=sprintf("%04d%02d%02d", $year,$mon, $mday);
516 #$shortdate=sprintf("%02d%02d%02d", $year%100,$mon, $mday);
518 # made once for all targets
519 sub runone {
520 my ($target, $fonts)=@_;
522 $app = ($modelname eq "application");
524 # build a full install .rockbox ($rbdir) directory
525 buildzip($target, $fonts);
527 unlink($output);
529 if($fonts == 1) {
530 # Don't include image file in fonts-only package
531 undef $target;
533 if($target && ($target !~ /(mod|ajz|wma)\z/i)) {
534 # On some targets, the image goes into .rockbox.
535 copy("$target", "$rbdir/$target");
536 undef $target;
539 if($verbose) {
540 print "$ziptool $output $rbdir $target >/dev/null\n";
543 my $samedir = 1; # is the destination dir equal to source dir ?
545 if($install) {
546 make_install($install) or die "MKDIRFAILED\n";
548 else {
549 system("$ziptool $output $rbdir $target >/dev/null");
552 # remove the $rbdir afterwards
553 if (!$samedir) {
554 rmtree($rbdir);
558 if(!$exe) {
559 # not specified, guess!
560 if($target =~ /(recorder|ondio)/i) {
561 $exe = "ajbrec.ajz";
563 elsif($target =~ /iriver/i) {
564 $exe = "rockbox.iriver";
566 else {
567 $exe = "archos.mod";
570 elsif(($exe =~ /rockboxui/)) {
571 # simulator, exclude the exe file
572 $exe = "";
575 runone($exe, $incfonts);