Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpinstall / packager / xptlink.pl
blobe064cd6f70f902fa157e90706d116b4ac47cd799
1 #!/usr/bin/perl -w
2 #
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
16 # The Original Code is Mozilla Communicator client code, released
17 # March 31, 1998.
19 # The Initial Developer of the Original Code is
20 # Netscape Communications Corporation.
21 # Portions created by the Initial Developer are Copyright (C) 1998-1999
22 # the Initial Developer. All Rights Reserved.
24 # Contributor(s):
25 # Jonathan Granrose (granrose@netscape.com)
26 # Jean-Jacques Enser (jj@netscape.com)
28 # Alternatively, the contents of this file may be used under the terms of
29 # either of the GNU General Public License Version 2 or later (the "GPL"),
30 # or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 # in which case the provisions of the GPL or the LGPL are applicable instead
32 # of those above. If you wish to allow use of your version of this file only
33 # under the terms of either the GPL or the LGPL, and not to allow others to
34 # use your version of this file under the terms of the MPL, indicate your
35 # decision by deleting the provisions above and replace them with the notice
36 # and other provisions required by the GPL or the LGPL. If you do not delete
37 # the provisions above, a recipient may use your version of this file under
38 # the terms of any one of the MPL, the GPL or the LGPL.
40 # ***** END LICENSE BLOCK *****
42 # xptlink.pl -
44 # traverse directories created by pkgcp.pl and merge multiple .xpt files into
45 # a single .xpt file to improve startup performance.
48 use Getopt::Long;
50 # initialize variables
51 $srcdir = ""; # root directory being copied from
52 $destdir = ""; # root directory being copied to
53 $finaldir = ""; # where to put the final linked XPT
54 $verbose = 0; # shorthand for --debug 1
55 $debug = 0; # controls amount of debug output
56 $help = 0; # flag: if set, print usage
57 $xptlink = ""; # path to the xpt_link binary
59 # get command line options
60 $return = GetOptions( "source|s=s", \$srcdir,
61 "destination|d=s", \$destdir,
62 "final|f=s", \$finaldir,
63 "help|h", \$help,
64 "debug=i", \$debug,
65 "verbose|v", \$verbose,
66 "xptlink|x=s", \$xptlink,
67 "<>", \&do_badargument
70 if ($finaldir ne "") {
71 $bindir = "";
73 else {
74 $bindir = "bin/";
77 # remove extra slashes from $destdir
78 $destdir =~ s:/+:/:g;
80 # set debug level
81 if ($verbose && !($debug)) {
82 $debug = 1;
83 } elsif ($debug != 0) {
84 $debug = abs ($debug);
85 ($debug >= 2) && print "debug level: $debug\n";
88 # check usage
89 if (! $return)
91 die "Error: couldn't parse command line options. See \'$0 --help' for options.\nExiting...\n";
92 } else {
93 check_arguments();
96 $xptdirs = (); # directories in the destination directory
98 ($debug >= 1) && print "\nLinking .xpt files...\n";
99 ($debug >= 2) && print "do_xptlink():\n";
101 # get list of directories on which to run xptlink
102 opendir (DESTDIR, "$destdir") ||
103 die "Error: could not open directory $destdir. Exiting...\n";
104 @xptdirs = sort ( grep (!/^\./, readdir (DESTDIR) ) );
105 ($debug >= 4) && print "xptdirs: @xptdirs\n";
106 closedir (DESTDIR);
108 foreach my $component (@xptdirs) {
109 ($debug >= 1) && print "[$component]\n";
111 print ("Checking for '$destdir/$component/$bindir"."components'\n") if $debug >= 3;
113 if (-d "$destdir/$component/$bindir"."components") {
114 warn "File '$destdir/$component/$bindir"."components/$component.xpt' already exists."
115 if -f "$destdir/$component/$bindir"."components/$component.xpt";
117 # create list of .xpt files in cwd
118 my @xptfiles;
120 ($debug >= 4) && print "opendir: $destdir/$component/$bindir"."components\n";
121 opendir (COMPDIR, "$destdir/$component/$bindir"."components") ||
122 die "Error: cannot open $destdir/$component/$bindir"."components. Exiting...\n";
123 ($debug >= 3) && print "Creating list of .xpt files...\n";
124 my @files = sort ( grep (!/^\./, readdir (COMPDIR)));
125 foreach my $file (@files) {
126 ($debug >= 6) && print "$file\n";
127 if ( $file =~ /\.xpt$/ ) {
128 push @xptfiles, "$destdir/$component/$bindir"."components/$file";
129 ($debug >= 8) && print "xptfiles:\t@xptfiles\n";
132 closedir (COMPDIR);
134 # merge .xpt files into one if we found any in the dir
135 if ( scalar(@xptfiles) ) {
136 my ($merged, $fmerged);
137 if ($finaldir ne "") {
138 $merged = "$finaldir/$component.xpt";
140 else {
141 $fmerged = "$destdir/$component/$bindir"."components/$component.xpt";
142 $merged = $fmerged.".new";
145 my @realxptfiles;
146 my $realmerged;
147 if ($^O eq "cygwin") {
148 @realxptfiles = map {my $file = `cygpath -t mixed $_`;
149 chomp $file;
150 $file} @xptfiles;
151 $realmerged = `cygpath -t mixed $merged`;
152 chomp $realmerged;
154 else {
155 @realxptfiles = @xptfiles;
156 $realmerged = $merged;
159 my $cmdline = "$xptlink $realmerged @realxptfiles";
160 ($debug >= 4) && print "$cmdline\n";
161 system($cmdline) == 0 || die ("'$cmdline' failed");
163 if ($finaldir eq "") {
164 # remove old .xpt files in the component directory.
165 ($debug >= 2) && print "Deleting individual xpt files.\n";
166 for my $file (@xptfiles) {
167 ($debug >= 4) && print "\t$file";
168 unlink ($file) ||
169 die "Couldn't unlink file $file.\n";
170 ($debug >= 4) && print "\t\tdeleted\n\n";
173 ($debug >= 2) && print "Renaming $merged as $fmerged\n";
174 rename ($merged, $fmerged) ||
175 die "Rename of '$merged' to '$fmerged' failed.\n";
180 ($debug >= 1) && print "Linking .xpt files completed.\n";
182 exit (0);
186 # Check that arguments to script are valid.
188 sub check_arguments
190 my ($exitval) = 0;
192 ($debug >= 2) && print "check_arguments():\n";
194 # if --help print usage
195 if ($help) {
196 print_usage();
197 exit (1);
200 # make sure required variables are set:
201 # check source directory
202 if ( $srcdir eq "" ) {
203 print "Error: source directory (--source) not specified.\n";
204 $exitval += 8;
205 } elsif ((! -d $srcdir) || (! -r $srcdir)) {
206 print "Error: source directory \"$srcdir\" is not a directory or is unreadable.\n";
207 $exitval = 1;
210 # check directory
211 if ( $destdir eq "" ) {
212 print "Error: destination directory (--destdir) not specified.\n";
213 $exitval += 8;
214 } elsif ((! -d $destdir) || (! -w $destdir)) {
215 print "Error: destination directory \"$destdir\" is not a directory or is not writeable.\n";
216 $exitval += 2;
219 if ($exitval) {
220 print "See \'$0 --help\' for more information.\n";
221 print "Exiting...\n";
222 exit ($exitval);
225 if ($xptlink eq "") {
226 $xptlink = "$srcdir/bin/xpt_link";
232 # This is called by GetOptions when there are extra command line arguments
233 # it doesn't understand.
235 sub do_badargument
237 warn "Warning: unknown command line option specified: @_.\n";
242 # display usage information
244 sub print_usage
246 ($debug >= 2) && print "print_usage():\n";
248 print <<EOC
251 Traverse component directory specified and merge multiple existing
252 .xpt files into single new .xpt files for improved startup time.
254 Options:
256 -s, --source <directory>
257 Specifies the directory from which the component files were
258 copied. Typically, this will be the same directory used by
259 pkgcp.pl.
260 Required.
262 -d, --destination <directory>
263 Specifies the directory in which the component directories are
264 located. Typically, this will be the same directory used by
265 pkgcp.pl.
266 Required.
268 -o, --os [dos|unix]
269 Specifies which type of system this is. Used for setting path
270 delimiters correctly.
271 Required.
273 -h, --help
274 Prints this information.
275 Optional.
277 --debug [1-10]
278 Controls verbosity of debugging output, 10 being most verbose.
279 1 : same as --verbose.
280 2 : includes function calls.
281 3 : includes source and destination for each copy.
282 Optional.
284 -v, --verbose
285 Print component names and files copied/deleted.
286 Optional.
289 e.g.
291 $0 --os unix -source /builds/mozilla/dist --destination /h/lithium/install --os unix --verbose
293 Note: options can be specified by either a leading '--' or '-'.
298 # EOF