A bit of code policing.
[kugel-rb.git] / tools / buildzip.pl
blob107c1b7ff1b6288ea801d8ce8a258cf391865da1
1 #!/usr/bin/perl
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id$
11 use File::Copy; # For move() and copy()
12 use File::Find; # For find()
13 use File::Path; # For rmtree()
14 use Cwd 'abs_path';
16 sub glob_copy {
17 my ($pattern, $destination) = @_;
18 foreach my $path (glob($pattern)) {
19 copy($path, $destination);
23 sub glob_move {
24 my ($pattern, $destination) = @_;
25 foreach my $path (glob($pattern)) {
26 move($path, $destination);
30 sub glob_unlink {
31 my ($pattern) = @_;
32 foreach my $path (glob($pattern)) {
33 unlink($path);
37 sub find_copyfile {
38 my ($pattern, $destination) = @_;
39 return sub {
40 $path = $_;
41 if ($path =~ $pattern && filesize($path) > 0 && !($path =~ /\.rockbox/)) {
42 copy($path, $destination);
43 chmod(0755, $destination.'/'.$path);
48 $ROOT="..";
50 my $ziptool="zip -r9";
51 my $output="rockbox.zip";
52 my $verbose;
53 my $sim;
54 my $exe;
55 my $target;
56 my $archos;
57 my $incfonts;
59 while(1) {
60 if($ARGV[0] eq "-r") {
61 $ROOT=$ARGV[1];
62 shift @ARGV;
63 shift @ARGV;
66 elsif($ARGV[0] eq "-z") {
67 $ziptool=$ARGV[1];
68 shift @ARGV;
69 shift @ARGV;
72 elsif($ARGV[0] eq "-t") {
73 # The target name as used in ARCHOS in the root makefile
74 $archos=$ARGV[1];
75 shift @ARGV;
76 shift @ARGV;
78 elsif($ARGV[0] eq "-i") {
79 # The target id name as used in TARGET_ID in the root makefile
80 $target_id=$ARGV[1];
81 shift @ARGV;
82 shift @ARGV;
84 elsif($ARGV[0] eq "-o") {
85 $output=$ARGV[1];
86 shift @ARGV;
87 shift @ARGV;
89 elsif($ARGV[0] eq "-f") {
90 $incfonts=$ARGV[1]; # 0 - no fonts, 1 - fonts only 2 - fonts and package
91 shift @ARGV;
92 shift @ARGV;
95 elsif($ARGV[0] eq "-v") {
96 $verbose =1;
97 shift @ARGV;
99 elsif($ARGV[0] eq "-s") {
100 $sim =1;
101 shift @ARGV;
103 else {
104 $target = $ARGV[0];
105 $exe = $ARGV[1];
106 last;
111 my $firmdir="$ROOT/firmware";
112 my $appsdir="$ROOT/apps";
113 my $viewer_bmpdir="$ROOT/apps/plugins/bitmaps/viewer_defaults";
115 my $cppdef = $target;
117 sub gettargetinfo {
118 open(GCC, ">gcctemp");
119 # Get the LCD screen depth and graphical status
120 print GCC <<STOP
121 \#include "config.h"
122 #ifdef HAVE_LCD_BITMAP
123 Bitmap: yes
124 Depth: LCD_DEPTH
125 Icon Width: CONFIG_DEFAULT_ICON_WIDTH
126 Icon Height: CONFIG_DEFAULT_ICON_HEIGHT
127 #endif
128 Codec: CONFIG_CODEC
129 #ifdef HAVE_REMOTE_LCD
130 Remote Depth: LCD_REMOTE_DEPTH
131 Remote Icon Width: CONFIG_REMOTE_DEFAULT_ICON_WIDTH
132 Remote Icon Height: CONFIG_REMOTE_DEFAULT_ICON_HEIGHT
133 #else
134 Remote Depth: 0
135 #endif
136 #ifdef HAVE_RECORDING
137 Recording: yes
138 #endif
139 STOP
141 close(GCC);
143 my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";
145 # print STDERR "CMD $c\n";
147 open(TARGET, "$c|");
149 my ($bitmap, $depth, $swcodec, $icon_h, $icon_w);
150 my ($remote_depth, $remote_icon_h, $remote_icon_w);
151 my ($recording);
152 $icon_count = 1;
153 while(<TARGET>) {
154 # print STDERR "DATA: $_";
155 if($_ =~ /^Bitmap: (.*)/) {
156 $bitmap = $1;
158 elsif($_ =~ /^Depth: (\d*)/) {
159 $depth = $1;
161 elsif($_ =~ /^Icon Width: (\d*)/) {
162 $icon_w = $1;
164 elsif($_ =~ /^Icon Height: (\d*)/) {
165 $icon_h = $1;
167 elsif($_ =~ /^Codec: (\d*)/) {
168 # SWCODEC is 1, the others are HWCODEC
169 $swcodec = ($1 == 1);
171 elsif($_ =~ /^Remote Depth: (\d*)/) {
172 $remote_depth = $1;
174 elsif($_ =~ /^Remote Icon Width: (\d*)/) {
175 $remote_icon_w = $1;
177 elsif($_ =~ /^Remote Icon Height: (\d*)/) {
178 $remote_icon_h = $1;
180 if($_ =~ /^Recording: (.*)/) {
181 $recording = $1;
184 close(TARGET);
185 unlink("gcctemp");
187 return ($bitmap, $depth, $icon_w, $icon_h, $recording,
188 $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h);
191 sub filesize {
192 my ($filename)=@_;
193 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
194 $atime,$mtime,$ctime,$blksize,$blocks)
195 = stat($filename);
196 return $size;
199 sub buildzip {
200 my ($image, $fonts)=@_;
202 my ($bitmap, $depth, $icon_w, $icon_h, $recording, $swcodec,
203 $remote_depth, $remote_icon_w, $remote_icon_h) = &gettargetinfo();
205 # print "Bitmap: $bitmap\nDepth: $depth\nSwcodec: $swcodec\n";
207 # remove old traces
208 rmtree('.rockbox');
210 mkdir ".rockbox", 0777;
212 if(!$bitmap) {
213 # always disable fonts on non-bitmap targets
214 $fonts = 0;
216 if($fonts) {
217 mkdir ".rockbox/fonts", 0777;
218 chdir(".rockbox/fonts");
219 $cmd = "$ROOT/tools/convbdf -f $ROOT/fonts/*bdf >/dev/null 2>&1";
220 print($cmd."\n") if $verbose;
221 system($cmd);
222 chdir("../../");
224 if($fonts < 2) {
225 # fonts-only package, return
226 return;
230 # create the file so the database does not try indexing a folder
231 open(IGNORE, ">.rockbox/database.ignore") || die "can't open database.ignore";
232 close(IGNORE);
234 mkdir ".rockbox/langs", 0777;
235 mkdir ".rockbox/rocks", 0777;
236 mkdir ".rockbox/rocks/games", 0777;
237 mkdir ".rockbox/rocks/apps", 0777;
238 mkdir ".rockbox/rocks/demos", 0777;
239 mkdir ".rockbox/rocks/viewers", 0777;
241 if ($recording) {
242 mkdir ".rockbox/recpresets", 0777;
245 if($swcodec) {
246 mkdir ".rockbox/eqs", 0777;
248 glob_copy("$ROOT/apps/eqs/*.cfg", '.rockbox/eqs/'); # equalizer presets
251 mkdir ".rockbox/wps", 0777;
252 mkdir ".rockbox/themes", 0777;
253 if ($bitmap) {
254 open(THEME, ">.rockbox/themes/rockbox_default_icons.cfg");
255 print THEME <<STOP
256 # this config file was auto-generated to make it
257 # easy to reset the icons back to default
258 iconset: -
259 # taken from apps/gui/icon.c
260 viewers iconset: /.rockbox/icons/viewers.bmp
261 remote iconset: -
262 # taken from apps/gui/icon.c
263 remote viewers iconset: /.rockbox/icons/remote_viewers.bmp
265 STOP
267 close(THEME);
270 mkdir ".rockbox/codepages", 0777;
272 if($bitmap) {
273 system("$ROOT/tools/codepages");
275 else {
276 system("$ROOT/tools/codepages -m");
279 glob_move('*.cp', '.rockbox/codepages/');
281 if($bitmap) {
282 mkdir ".rockbox/codecs", 0777;
283 if($depth > 1) {
284 mkdir ".rockbox/backdrops", 0777;
287 find(find_copyfile(qr/.*\.codec/, abs_path('.rockbox/codecs/')), 'apps/codecs');
289 # remove directory again if no codec was copied
290 rmdir(".rockbox/codecs");
293 find(find_copyfile(qr/\.(rock|ovl)/, abs_path('.rockbox/rocks/')), 'apps/plugins');
295 open VIEWERS, "$ROOT/apps/plugins/viewers.config" or
296 die "can't open viewers.config";
297 @viewers = <VIEWERS>;
298 close VIEWERS;
300 open VIEWERS, ">.rockbox/viewers.config" or
301 die "can't create .rockbox/viewers.config";
303 foreach my $line (@viewers) {
304 if ($line =~ /([^,]*),([^,]*),/) {
305 my ($ext, $plugin)=($1, $2);
306 my $r = "${plugin}.rock";
307 my $oname;
309 my $dir = $r;
310 my $name;
312 # strip off the last slash and file name part
313 $dir =~ s/(.*)\/(.*)/$1/;
314 # store the file name part
315 $name = $2;
317 # get .ovl name (file part only)
318 $oname = $name;
319 $oname =~ s/\.rock$/.ovl/;
321 # print STDERR "$ext $plugin $dir $name $r\n";
323 if(-e ".rockbox/rocks/$name") {
324 if($dir ne "rocks") {
325 # target is not 'rocks' but the plugins are always in that
326 # dir at first!
327 move(".rockbox/rocks/$name", ".rockbox/rocks/$r");
329 print VIEWERS $line;
331 elsif(-e ".rockbox/rocks/$r") {
332 # in case the same plugin works for multiple extensions, it
333 # was already moved to the viewers dir
334 print VIEWERS $line;
337 if(-e ".rockbox/rocks/$oname") {
338 # if there's an "overlay" file for the .rock, move that as
339 # well
340 move(".rockbox/rocks/$oname", ".rockbox/rocks/$dir");
344 close VIEWERS;
346 open CATEGORIES, "$ROOT/apps/plugins/CATEGORIES" or
347 die "can't open CATEGORIES";
348 @rock_targetdirs = <CATEGORIES>;
349 close CATEGORIES;
350 foreach my $line (@rock_targetdirs) {
351 if ($line =~ /([^,]*),(.*)/) {
352 my ($plugin, $dir)=($1, $2);
353 move(".rockbox/rocks/${plugin}.rock", ".rockbox/rocks/$dir/${plugin}.rock");
357 if ($bitmap) {
358 mkdir ".rockbox/icons", 0777;
359 copy("$viewer_bmpdir/viewers.${icon_w}x${icon_h}x$depth.bmp", ".rockbox/icons/viewers.bmp");
360 if ($remote_depth) {
361 copy("$viewer_bmpdir/remote_viewers.${remote_icon_w}x${remote_icon_h}x$remote_depth.bmp", ".rockbox/icons/remote_viewers.bmp");
365 copy("$ROOT/apps/tagnavi.config", ".rockbox/");
366 copy("$ROOT/apps/plugins/disktidy.config", ".rockbox/rocks/apps/");
368 if($bitmap) {
369 copy("$ROOT/apps/plugins/sokoban.levels", ".rockbox/rocks/games/sokoban.levels"); # sokoban levels
370 copy("$ROOT/apps/plugins/snake2.levels", ".rockbox/rocks/games/snake2.levels"); # snake2 levels
373 if($image) {
374 # image is blank when this is a simulator
375 if( filesize("rockbox.ucl") > 1000 ) {
376 copy("rockbox.ucl", ".rockbox/rockbox.ucl"); # UCL for flashing
378 if( filesize("rombox.ucl") > 1000) {
379 copy("rombox.ucl", ".rockbox/rombox.ucl"); # UCL for flashing
382 # Check for rombox.target
383 if ($image=~/(.*)\.(\w+)$/)
385 my $romfile = "rombox.$2";
386 if (filesize($romfile) > 1000)
388 copy($romfile, ".rockbox/$romfile");
393 mkdir ".rockbox/docs", 0777;
394 for(("COPYING",
395 "LICENSES",
396 "KNOWN_ISSUES"
397 )) {
398 copy("$ROOT/docs/$_", ".rockbox/docs/$_.txt");
400 if ($fonts) {
401 copy("$ROOT/docs/profontdoc.txt", ".rockbox/docs/profontdoc.txt");
403 for(("sample.colours",
404 "sample.icons"
405 )) {
406 copy("$ROOT/docs/$_", ".rockbox/docs/$_");
409 # Now do the WPS dance
410 if(-d "$ROOT/wps") {
411 system("perl $ROOT/wps/wpsbuild.pl -r $ROOT $ROOT/wps/WPSLIST $target");
413 else {
414 print STDERR "No wps module present, can't do the WPS magic!\n";
417 # and the info file
418 copy("rockbox-info.txt", ".rockbox/rockbox-info.txt");
420 # copy the already built lng files
421 glob_copy('apps/lang/*lng', '.rockbox/langs/');
425 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
426 localtime(time);
428 $mon+=1;
429 $year+=1900;
431 #$date=sprintf("%04d%02d%02d", $year,$mon, $mday);
432 #$shortdate=sprintf("%02d%02d%02d", $year%100,$mon, $mday);
434 # made once for all targets
435 sub runone {
436 my ($target, $fonts)=@_;
438 # build a full install .rockbox directory
439 buildzip($target, $fonts);
441 unlink($output);
443 if($fonts == 1) {
444 # Don't include image file in fonts-only package
445 undef $target;
447 if($target && ($target !~ /(mod|ajz|wma)\z/i)) {
448 # On some targets, the image goes into .rockbox.
449 copy("$target", ".rockbox/$target");
450 undef $target;
453 if($verbose) {
454 print "$ziptool $output .rockbox $target >/dev/null\n";
457 if($sim) {
458 system("cp -r .rockbox archos/ >/dev/null");
460 else {
461 system("$ziptool $output .rockbox $target >/dev/null");
464 # remove the .rockbox afterwards
465 rmtree('.rockbox');
468 if(!$exe) {
469 # not specified, guess!
470 if($target =~ /(recorder|ondio)/i) {
471 $exe = "ajbrec.ajz";
473 elsif($target =~ /iriver/i) {
474 $exe = "rockbox.iriver";
476 else {
477 $exe = "archos.mod";
480 elsif($exe =~ /rockboxui/) {
481 # simulator, exclude the exe file
482 $exe = "";
485 runone($exe, $incfonts);