source: trunk/third/firefox/config/make-jars.pl @ 21695

Revision 21695, 15.8 KB checked in by rbasch, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21694, which included commits to RCS files with non-trunk default branches.
Line 
1#!/perl
2
3# make-jars [-f] [-v] [-l] [-x] [-d <chromeDir>] [-s <srcdir>] [-t <topsrcdir>] [-z zipprog] [-o operating-system] < <jar.mn>
4
5my $cygwin_mountprefix = "";
6if ($^O eq "cygwin") {
7    $cygwin_mountprefix = `mount -p | awk '{ if (/^\\//) { print \$1; exit } }'`;
8    chomp($cygwin_mountprefix);
9    # Remove extra ^M caused by using dos-mode line-endings
10    chop $cygwin_mountprefix if (substr($cygwin_mountprefix, -1, 1) eq "\r");
11} else {
12    # we'll be pulling in some stuff from the script directory
13    require FindBin;
14    import FindBin;
15    push @INC, $FindBin::Bin;
16}
17
18use strict;
19
20use Getopt::Std;
21use Cwd;
22use File::stat;
23use Time::localtime;
24use File::Copy;
25use File::Path;
26use File::Spec;
27use IO::File;
28use Config;
29require mozLock;
30import mozLock;
31
32my $objdir = getcwd;
33
34# if there's a "--", everything after it goes into $defines.  We don't do
35# this with the remaining args in @ARGV after the getopts call because
36# old versions of Getopt::Std don't understand "--".
37my $ddindex = 0;
38foreach my $arg (@ARGV) {
39  ++$ddindex;
40  last if ($arg eq "--");
41}
42my $defines = join(' ', @ARGV[ $ddindex .. $#ARGV ]);
43
44getopts("d:s:t:f:avlD:o:p:xz:");
45
46my $baseFilesDir = ".";
47if (defined($::opt_s)) {
48    $baseFilesDir = $::opt_s;
49}
50
51my $topSrcDir;
52if (defined($::opt_t)) {
53    $topSrcDir = $::opt_t;
54}
55
56my $maxCmdline = 4000;
57if ($Config{'archname'} =~ /VMS/) {
58    $maxCmdline = 200;
59}
60
61my $chromeDir = ".";
62if (defined($::opt_d)) {
63    $chromeDir = $::opt_d;
64}
65
66my $verbose = 0;
67if (defined($::opt_v)) {
68    $verbose = 1;
69}
70
71my $fileformat = "jar";
72if (defined($::opt_f)) {
73    ($fileformat = $::opt_f) =~ tr/A-Z/a-z/;
74}
75
76if ("$fileformat" ne "jar" &&
77    "$fileformat" ne "flat" &&
78    "$fileformat" ne "symlink" &&
79    "$fileformat" ne "both") {
80    print "File format specified by -f option must be one of: jar, flat, both, or symlink.\n";
81    exit(1);
82}
83
84my $zipmoveopt = "";
85if ("$fileformat" eq "jar") {
86    $zipmoveopt = "-m -0";
87}
88if ("$fileformat" eq "both") {
89    $zipmoveopt = "-0";
90}
91
92my $nofilelocks = 0;
93if (defined($::opt_l)) {
94    $nofilelocks = 1;
95}
96
97my $autoreg = 1;
98if (defined($::opt_a)) {
99    $autoreg = 0;
100}
101
102my $preprocessor = "";
103if (defined($::opt_p)) {
104    $preprocessor = $::opt_p;
105}
106
107my $force_x11 = 0;
108if (defined($::opt_x)) {
109    $force_x11 = 1;
110}
111
112my $zipprog = $ENV{ZIP};
113if (defined($::opt_z)) {
114    $zipprog = $::opt_z;
115}
116
117if ($zipprog eq "") {
118    print "A valid zip program must be given via the -z option or the ZIP environment variable. Exiting.\n";
119    exit(1);
120}
121
122my $force_os;
123if (defined($::opt_o)) {
124    $force_os = $::opt_o;
125}
126
127if ($verbose) {
128    print "make-jars "
129        . "-v -d $chromeDir "
130        . "-z $zipprog "
131        . ($fileformat ? "-f $fileformat " : "")
132        . ($nofilelocks ? "-l " : "")
133        . ($baseFilesDir ? "-s $baseFilesDir " : "")
134        . "\n";
135}
136
137my $win32 = ($^O =~ /((MS)?win32)|cygwin|os2/i) ? 1 : 0;
138my $macos = ($^O =~ /MacOS|darwin/i) ? 1 : 0;
139my $unix  = !($win32 || $macos) ? 1 : 0;
140my $vms   = ($^O =~ /VMS/i) ? 1 : 0;
141
142if ($force_x11) {
143    $win32 = 0;
144    $macos = 0;
145    $unix = 1;
146}
147
148if (defined($force_os)) {
149    $win32 = 0;
150    $macos = 0;
151    $unix = 0;
152    if ($force_os eq "WINNT") {
153    $win32 = 1;
154    } elsif ($force_os eq "OS2") {
155    $win32 = 1;
156    } elsif ($force_os eq "Darwin") {
157    $macos = 1;
158    } else {
159    $unix = 1;
160    }
161}
162
163sub foreignPlatformFile
164{
165   my ($jarfile) = @_;
166   
167   if (!$win32 && index($jarfile, "-win") != -1) {
168     return 1;
169   }
170   
171   if (!$unix && index($jarfile, "-unix") != -1) {
172     return 1;
173   }
174
175   if (!$macos && index($jarfile, "-mac") != -1) {
176     return 1;
177   }
178
179   return 0;
180}
181
182sub zipErrorCheck($$)
183{
184    my ($err,$lockfile) = @_;
185    return if ($err == 0 || $err == 12);
186    mozUnlock($lockfile) if (!$nofilelocks);
187    die ("Error invoking zip: $err");
188}
189
190sub JarIt
191{
192    my ($destPath, $jarfile, $args, $overrides) = @_;
193    my $oldDir = cwd();
194    chdir("$destPath/$jarfile");
195
196    if ("$fileformat" eq "flat" || "$fileformat" eq "symlink") {
197    unlink("../$jarfile.jar") if ( -e "../$jarfile.jar");
198    chdir($oldDir);
199        return 0;
200    }
201
202    #print "cd $destPath/$jarfile\n";
203
204    my $lockfile = "../$jarfile.lck";
205
206    mozLock($lockfile) if (!$nofilelocks);
207
208    if (!($args eq "")) {
209        my $cwd = getcwd;
210        my $err = 0;
211
212        #print "$zipprog $zipmoveopt -u ../$jarfile.jar $args\n";
213
214        # Handle posix cmdline limits
215        while (length($args) > $maxCmdline) {
216            #print "Exceeding POSIX cmdline limit: " . length($args) . "\n";
217            my $subargs = substr($args, 0, $maxCmdline-1);
218            my $pos = rindex($subargs, " ");
219            $subargs = substr($args, 0, $pos);
220            $args = substr($args, $pos);
221
222            #print "$zipprog $zipmoveopt -u ../$jarfile.jar $subargs\n";
223            #print "Length of subargs: " . length($subargs) . "\n";
224            system("$zipprog $zipmoveopt -u ../$jarfile.jar $subargs") == 0 or
225                $err = $? >> 8;
226            zipErrorCheck($err,$lockfile);
227        }
228        #print "Length of args: " . length($args) . "\n";
229        #print "$zipprog $zipmoveopt -u ../$jarfile.jar $args\n";
230        system("$zipprog $zipmoveopt -u ../$jarfile.jar $args") == 0 or
231            $err = $? >> 8;
232        zipErrorCheck($err,$lockfile);
233    }
234
235    if (!($overrides eq "")) {
236        my $err = 0;
237        print "+++ overriding $overrides\n";
238         
239        while (length($overrides) > $maxCmdline) {
240            #print "Exceeding POSIX cmdline limit: " . length($overrides) . "\n";
241            my $subargs = substr($overrides, 0, $maxCmdline-1);
242            my $pos = rindex($subargs, " ");
243            $subargs = substr($overrides, 0, $pos);
244            $overrides = substr($overrides, $pos);
245
246            #print "$zipprog $zipmoveopt ../$jarfile.jar $subargs\n";       
247            #print "Length of subargs: " . length($subargs) . "\n";
248            system("$zipprog $zipmoveopt ../$jarfile.jar $subargs") == 0 or
249                $err = $? >> 8;
250            zipErrorCheck($err,$lockfile);
251        }
252        #print "Length of args: " . length($overrides) . "\n";
253        #print "$zipprog $zipmoveopt ../$jarfile.jar $overrides\n";
254        system("$zipprog $zipmoveopt ../$jarfile.jar $overrides\n") == 0 or
255        $err = $? >> 8;
256        zipErrorCheck($err,$lockfile);
257    }
258    mozUnlock($lockfile) if (!$nofilelocks);
259    chdir($oldDir);
260    #print "cd $oldDir\n";
261}
262
263sub _moz_rel2abs
264{
265    my ($path, $keep_file) = @_;
266    $path = File::Spec->rel2abs($path, $objdir);
267    my ($volume, $dirs, $file) = File::Spec->splitpath($path);
268    my (@dirs) = reverse File::Spec->splitdir($dirs);
269    my ($up) = File::Spec->updir();
270    foreach (reverse 0 .. $#dirs) {
271      splice(@dirs, $_, 2) if ($dirs[$_] eq $up);
272    }
273    $dirs = File::Spec->catdir(reverse @dirs);
274    return File::Spec->catpath($volume, $dirs, $keep_file && $file);
275}
276
277sub _moz_abs2rel
278{
279    my ($target, $linkname) = @_;
280    $target = _moz_rel2abs($target, 1);
281    $linkname = _moz_rel2abs($linkname);
282    return File::Spec->abs2rel($target, $linkname);
283}
284
285sub RegIt
286{
287    my ($chromeDir, $jarFileName, $chromeType, $pkgName) = @_;\
288    chop($pkgName) if ($pkgName =~ m/\/$/);
289    #print "RegIt:  $chromeDir, $jarFileName, $chromeType, $pkgName\n";
290
291    my $line;
292    if ($fileformat eq "flat" || $fileformat eq "symlink") {
293        $line = "$chromeType,install,url,resource:/chrome/$jarFileName/$chromeType/$pkgName/";
294    } else {
295        $line = "$chromeType,install,url,jar:resource:/chrome/$jarFileName.jar!/$chromeType/$pkgName/";
296    }
297    my $installedChromeFile = "$chromeDir/installed-chrome.txt";
298    my $lockfile = "$installedChromeFile.lck";
299    mozLock($lockfile) if (!$nofilelocks);
300    my $err = 0;
301    if (open(FILE, "<$installedChromeFile")) {
302        while (<FILE>) {
303            chomp;
304            if ($_ =~ $line) {
305                # line already appears in installed-chrome.txt file
306                # just update the mod date
307                close(FILE) or $err = 1;
308                if ($err) {
309                    mozUnlock($lockfile) if (!$nofilelocks);
310                    die "error: can't close $installedChromeFile: $!";
311                }
312                my $now = time;
313                utime($now, $now, $installedChromeFile) or $err = 1;
314                mozUnlock($lockfile) if (!$nofilelocks);
315                if ($err) {
316                    die "couldn't touch $installedChromeFile";
317                }
318                print "+++ updating chrome $installedChromeFile\n+++\t$line\n";
319                return;
320            }
321        }
322        close(FILE) or $err = 1;
323        if ($err) {
324            mozUnlock($lockfile) if (!$nofilelocks);
325            die "error: can't close $installedChromeFile: $!";
326        }
327    }
328    mozUnlock($lockfile) if (!$nofilelocks);
329   
330    my $dir = $installedChromeFile;
331    if ("$dir" =~ /([\w\d.\-\\\/\+]+)[\\\/]([\w\d.\-]+)/) {
332        $dir = $1;
333    }
334    mkpath($dir, 0, 0755);
335   
336    mozLock($lockfile) if (!$nofilelocks);
337    $err = 0;
338    open(FILE, ">>$installedChromeFile") or $err = 1;
339    if ($err) {
340        mozUnlock($lockfile) if (!$nofilelocks);
341        die "can't open $installedChromeFile: $!";
342    }
343    print FILE "$line\n";
344    close(FILE) or $err = 1;
345    mozUnlock($lockfile) if (!$nofilelocks);
346    if ($err) {
347        die "error: can't close $installedChromeFile: $!";
348    }
349    print "+++ adding chrome $installedChromeFile\n+++\t$line\n";
350}
351
352sub EnsureFileInDir
353{
354    my ($destPath, $srcPath, $destFile, $srcFile, $override, $preproc) = @_;
355    my $objPath;
356
357    #print "EnsureFileInDir($destPath, $srcPath, $destFile, $srcFile, $override)\n";
358
359    my $src = $srcFile;
360    if (defined($src)) {
361        if ($src =~ m|^/|) {
362            # "absolute" patch from topSrcDir
363            $src = $topSrcDir.$srcFile;
364        } elsif (! -e $src ) {
365            $src = "$srcPath/$srcFile";
366        }
367    }
368    else {
369        $src = "$srcPath/$destFile";
370        # check for the complete jar path in the dest dir
371        if (!-e $src) {
372            #else check for just the file name in the dest dir
373            my $dir = "";
374            my $file;
375            if ($destFile =~ /([\w\d.\-\_\\\/\+]+)[\\\/]([\w\d.\-\_]+)/) {
376                $dir = $1;
377                $file = $2;
378            }
379            else {
380                die "file not found: $srcPath/$destFile";
381            }
382            $src = "$srcPath/$file";
383            if (!-e $src) {
384                die "file not found: $srcPath/$destFile";
385            }
386        }
387    }
388
389    $srcPath = $src;
390    $destPath = "$destPath/$destFile";
391
392    my $srcStat = stat($srcPath);
393    my $srcMtime = $srcStat ? $srcStat->mtime : 0;
394
395    my $destStat = stat($destPath);
396    my $destMtime = $destStat ? $destStat->mtime : 0;
397    #print "destMtime = $destMtime, srcMtime = $srcMtime\n";
398
399    if (!-e $destPath || $destMtime < $srcMtime || $override) {
400        #print "copying $destPath, from $srcPath\n";
401        my $dir = "";
402        my $file;
403        if ($destPath =~ /([\w\d.\-\_\\\/\+]+)[\\\/]([\w\d.\-\_]+)/) {
404            $dir = $1;
405            $file = $2;
406        }
407        else {
408            $file = $destPath;
409        }
410
411        if ($srcPath) {
412            $file = $srcPath;
413        }
414        $objPath = "$objdir/$destFile";
415
416        if (!-e $file) {
417            if (!-e $objPath) {
418                die "error: file '$file' doesn't exist";
419            } else {
420                $file = "$objPath";
421            }
422        }
423        if (!-e $dir) {
424            mkpath($dir, 0, 0775) || die "can't mkpath $dir: $!";
425        }
426        unlink $destPath;       # in case we had a symlink on unix
427        if ($preproc) {
428            my $preproc_file = $file;
429            if ($^O eq 'cygwin' && $file =~ /^[a-zA-Z]:/) {
430                # convert to a cygwin path
431                $preproc_file =~ s|^([a-zA-Z]):|$cygwin_mountprefix/\1|;
432            }
433            if ($vms) {
434                # use a temporary file otherwise cmd is too long for system()
435                my $tmpFile = "$destPath.tmp";
436                open(TMP, ">$tmpFile") || die("$tmpFile: $!");
437                print(TMP "$^X $preprocessor $defines $preproc_file > $destPath");
438                close(TMP);
439                print "+++ preprocessing $preproc_file > $destPath\n";
440                if (system("bash \"$tmpFile\"") != 0) {
441                    die "Preprocessing of $file failed (VMS): ".($? >> 8);
442                }
443                unlink("$tmpFile") || die("$tmpFile: $!");
444            } else {
445                if (system("$^X $preprocessor $defines $preproc_file > $destPath") != 0) {
446                    die "Preprocessing of $file failed: ".($? >> 8);
447                }
448            }
449        } elsif ("$fileformat" eq "symlink") {
450            $file = _moz_abs2rel($file, $destPath);
451            symlink($file, $destPath) || die "symlink($file, $destPath) failed: $!";
452            return 1;
453        } else {
454            copy($file, $destPath) || die "copy($file, $destPath) failed: $!";
455        }
456
457        # fix the mod date so we don't jar everything (is this faster than just jarring everything?)
458        my $mtime = stat($file)->mtime || die $!;
459        my $atime = stat($file)->atime;
460        $atime = $mtime if !defined($atime);
461        utime($atime, $mtime, $destPath);
462
463        return 1;
464    }
465    return 0;
466}
467
468my @gLines = <STDIN>;
469
470while (defined($_ = shift @gLines)) {
471    chomp;
472
473start:
474    if (/^([\w\d.\-\_\\\/]+).jar\:\s*$/) {
475        my $jarfile = $1;
476        my $args = "";
477        my $overrides = "";
478        my $cwd = cwd();
479        print "+++ making chrome $cwd  => $chromeDir/$jarfile.jar\n";
480        while (defined($_ = shift @gLines)) {
481            if (/^\s+([\w\d.\-\_\\\/\+]+)\s*(\([\w\d.\-\_\\\/]+\))?$\s*/) {
482                my $dest = $1;
483                my $srcPath = defined($2) ? substr($2, 1, -1) : $2;
484                EnsureFileInDir("$chromeDir/$jarfile", $baseFilesDir, $dest, $srcPath, 0, 0);
485                $args = "$args$dest ";
486                if (!foreignPlatformFile($jarfile)  && $autoreg &&
487                    $dest =~ /([\w\d.\-\_\+]+)\/([\w\d.\-\_\\\/]+)contents.rdf/)
488                {
489                    my $chrome_type = $1;
490                    my $pkg_name = $2;
491                    RegIt($chromeDir, $jarfile, $chrome_type, $pkg_name);
492                }
493            } elsif (/^\+\s+([\w\d.\-\_\\\/\+]+)\s*(\([\w\d.\-\_\\\/]+\))?$\s*/) {
494                my $dest = $1;
495                my $srcPath = defined($2) ? substr($2, 1, -1) : $2;
496                EnsureFileInDir("$chromeDir/$jarfile", $baseFilesDir, $dest, $srcPath, 1, 0);
497                $overrides = "$overrides$dest ";
498                if (!foreignPlatformFile($jarfile)  && $autoreg && $dest =~ /([\w\d.\-\_\+]+)\/([\w\d.\-\_\\\/]+)contents.rdf/)
499                {
500                    my $chrome_type = $1;
501                    my $pkg_name = $2;
502                    RegIt($chromeDir, $jarfile, $chrome_type, $pkg_name);
503                }
504            } elsif (/^\*\+?\s+([\w\d.\-\_\\\/\+]+)\s*(\([\w\d.\-\_\\\/]+\))?$\s*/) {
505                # preprocessed (always override)
506                my $dest = $1;
507                my $srcPath = defined($2) ? substr($2, 1, -1) : $2;
508                EnsureFileInDir("$chromeDir/$jarfile", $baseFilesDir, $dest, $srcPath, 1, 1);
509                $overrides = "$overrides$dest ";
510                if (!foreignPlatformFile($jarfile)  && $autoreg && $dest =~ /([\w\d.\-\_\+]+)\/([\w\d.\-\_\\\/]+)contents.rdf/)
511                {
512                    my $chrome_type = $1;
513                    my $pkg_name = $2;
514                    RegIt($chromeDir, $jarfile, $chrome_type, $pkg_name);
515                }
516            } elsif (/^\s*$/) {
517                # end with blank line
518                last;
519            } else {
520                JarIt($chromeDir, $jarfile, $args, $overrides);
521                goto start;
522            }
523        }
524        JarIt($chromeDir, $jarfile, $args, $overrides);
525
526    } elsif (/^\s*\#.*$/) {
527        # skip comments
528    } elsif (/^\s*$/) {
529        # skip blank lines
530    } else {
531        close;
532        die "bad jar rule head at: $_";
533    }
534}
Note: See TracBrowser for help on using the repository browser.