forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrees.pl
executable file
·444 lines (396 loc) · 10.3 KB
/
trees.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env perl
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is this file as it was released upon February 25, 1999.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1999 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Stephen Lamm <[email protected]>
#
# trees.pl - Navigate mozilla source trees.
# Depends on mozconfig.sh to find object directory.
# Use http://cvs-mirror.mozilla.org/webtools/build/config.cgi
# to create a mozconfig.sh file.
#
# Be sure to define a ~/.moztrees file (Run 'trees.pl -c' for an example).
#
require 'getopts.pl';
sub usage
{
my ($progname) = $0 =~ /([^\/]+)$/;
die "Usage: $progname [options] [<tree_name>] [<dir> <dir>...]
Getting Started:
1. trees.pl -e > ~/.moztrees
2. Edit ~/.moztrees for your setup.
3. trees.pl -t (tcsh users), or trees.pl -b (bash users)
4. Save output of step #3 into ~/.cshrc or ~/.bash_profile
Options:
-l List the available trees.
-o Move to the object directory.
-s Move to the source directory.
-h Print this usage message.
<tree_name> Directory name below mozilla (e.g. gecko/mozilla)
You only need to specify tree name when switching trees.
<dir> An alias or sub directory list.
Use '/' to move to the root of the tree.
Examples (with 'trees.pl' wrapped in 'moz' alias or function):
moz gecko cmd/xfe - cd to mozilla/cmd/xfe in gecko tree
moz editor public - cd to mozilla/editor/public
moz / - cd to mozilla/
moz gecko2 - cd to tree 'gecko2' (subdir is preserved)
";
}
&usage if !&Getopts('behlost');
&usage if $opt_h;
# Options for Getting Started
#
if ($opt_e)
{
print while <DATA>;
exit;
}
if ($opt_b)
{
# Print function for bash users
print q(function moz()
{
set -- `trees.pl $@`
if [ $# -eq 1 ]; then
cd $1
fi
}
);
exit;
}
if ($opt_t)
{
# Print alias for tcsh users
print q(alias moz 'set _moz=`trees.pl \!*`; if ("$_moz" != "") cd $_moz; endif; unset _moz'
);
exit;
}
######################################################################
# begin defines
$root_dir = "mozilla";
$test_file = "client.mk"; # Some file to verify the existence of a tree.
if (not -f "$ENV{HOME}/.moztrees")
{
die "No ~/.moztrees file. Run 'trees.pl -e' for example, or 'trees.pl -h' for help.\n";
}
push @INC, $ENV{HOME};
require '.moztrees';
# end defines
######################################################################
# begin main
# Option '-l': Print a list of trees.
#
if ($opt_l)
{
&print_tree_list(@tree_parent_dirs);
exit;
}
# Build regex pattern to match root directory.
$root_pat = "(?:\Q$root_dir\E)";
# Make 'moz /' goto '$tree_dir/mozilla'.
$dir_aliases{'/'} = '/';
# Get the current directory.
chop($pwd = `pwd`) unless defined($pwd = $ENV{PWD});
# Check if current directory to is a tree path.
#
($tree_dir, $tree_subdir, $topsrcdir, $objdir) = &parse_tree_path($pwd);
# Check for tree name as first argument.
#
if ($#ARGV >= 0)
{
$dir = &tree_lookup($ARGV[0]);
if ($dir ne '')
{
$tree_dir = $dir;
shift @ARGV;
} }
if ($tree_dir eq '')
{
warn "No tree found.\n";
&usage;
}
# Move between source and object directories
#
if ($opt_s)
{
$tree_dir = $topsrcdir;
}
if ($opt_o)
{
$objdir = find_objdir($tree_dir) if $objdir eq '';
$tree_dir = $objdir;
}
# Finally, check for directories
#
if ($#ARGV >= 0)
{
($tree_dir, $tree_subdir) = &dir_lookup($tree_dir, $tree_subdir, @ARGV);
}
$tree_dir =~ s|^(\S):|//$1|; # For bash on Windows, change "C:" to "//C"
print "$tree_dir/$tree_subdir";
# end main
######################################################################
sub print_tree_list
{
# Build a list of the trees
foreach $parent_dir (@tree_parent_dirs)
{
opendir(DIRHANDLE, $parent_dir) || next;
foreach $entry (readdir(DIRHANDLE))
{
next if $entry eq '.' || $entry eq '..' || $entry eq 'CVS';
if (-d "$parent_dir/$entry/$root_dir")
{
$trees{$entry} = "$parent_dir/$entry/$root_dir";
last; # Skip the inner loop
} } }
@treelist = ();
$maxlen = 0;
my ($tree_name, $tree_dir, $max_name_length);
while (($tree_name, $tree_dir) = each %trees)
{
if ($max_name_length < length($tree_name))
{
$max_name_length = length($tree_name);
}
push (@treelist, "$tree_dir=$tree_name");
}
foreach (sort @treelist)
{
($dir, $name) = split('=');
print stderr sprintf("%-${max_name_length}s %s\n", $name, $dir);
} }
# Check a path to see if it is in a source tree.
# Parses the path and returns the tree root and subdir.
#
sub parse_tree_path
{
my ($pwd) = @_;
my ($tree_dir) = '';
my ($tree_subdir) = '';
# Adjust cygwin path to one perl understands
$pwd =~ s@^//(\S)/@$1:/@;
if ($pwd =~ m@^(.*/$root_pat)(/.*)?$@ and -e "$1/$test_file")
{
$topsrcdir = $1;
$tree_dir = $topsrcdir;
$tree_subdir = $2;
$objdir = find_objdir($topsrcdir);
if ($pwd =~ m@^$objdir(/.*)?$@)
{
$tree_dir = $objdir;
$tree_subdir = $1;
}
} elsif (-d "$pwd/$root_dir" and -e "$pwd/$root_dir/$test_file")
{
$tree_dir = "$pwd/$root_dir";
$tree_subdir = "..";
} elsif ($pwd =~ m@^((.*)/obj-[^/]+)(/.*)?$@)
{
if (-d "$2/$root_dir")
{
$topsrcdir = $1;
$objdir = $2;
$tree_dir = $objdir;
$tree_subdir = $3;
} }
$tree_subdir = '/' if $tree_subdir eq '';
return ($tree_dir, $tree_subdir, $topsrcdir, $objdir);
}
# Check a name against the possible list of trees
#
sub tree_lookup
{
my $arg = $_[0];
my ($dir, $root);
return $trees{$arg} if defined($trees{$arg});
foreach $parent_dir (@tree_parent_dirs)
{
return "$parent_dir/$arg/$root_dir" if -d "$parent_dir/$arg/$root_dir";
}
return '';
}
sub dir_lookup
{
my ($tree_dir, $tree_subdir, $arg, @args) = @_;
return $tree_subdir if not $arg;
if (defined($dir_aliases{$arg}))
{
($dir1, $dir2) = split(/:/, $dir_aliases{$arg});
if ($dir1 eq 'objdir')
{
$objdir = find_objdir($tree_dir) if $objdir eq '';
$tree_dir = $objdir;
$tree_subdir = $dir2;
} elsif ($dir1 eq 'topsrcdir')
{
$tree_dir = $topsrcdir;
$tree_subdir = $dir2;
} else
{
$tree_subdir = $dir1;
}
}
else
{
# Check for subdirectory of 'search' directory
my ($found_dir) = 0;
foreach $dir ('',@search_dirs,$tree_subdir)
{
if (-d "$tree_dir/$dir/$arg")
{
$tree_subdir = "$dir/$arg";
$found_dir = 1;
last;
} }
die "Directory not found: $arg\n" if !$found_dir;
}
foreach $arg (@args)
{
# Subdirectory of current path
if (-d "$tree_dir/$tree_subdir/$arg")
{
$tree_subdir = "$tree_subdir/$arg";
}
else
{
die "Directory not found: $tree_dir/$tree_subdir/$arg\n";
} }
$tree_subdir =~ s@^/+(.+)@$1@;
return ($tree_dir, $tree_subdir);
}
# The only way to find the object directory is
# to find it in '.mozconfig'.
sub find_objdir {
my $topsrcdir = $_[0];
my $objdir = '';
my $home = $ENV{HOME};
my $mozconfig = '';
foreach $config ("$ENV{MOZCONFIG}","$ENV{MOZ_MYCONFIG}",
"$topsrcdir/.mozconfig",
"$topsrcdir/mozconfig", "topsrcdir/mozconfig.sh",
"$home/.mozconfig","$home/.mozconfig.sh") {
next if $config eq '';
if (-f $config) {
$mozconfig = $config;
last;
} }
$objdir_script = qq{
# sh
MOZCONFIG=$mozconfig
TOPSRCDIR=$topsrcdir
}.q{
ac_add_options() { :; }
mk_add_options() {
for _opt
do
case $_opt in
MOZ_OBJDIR=*) _objdir=`expr "$_opt" : "MOZ_OBJDIR=\(.*\)"` ;;
esac
done
}
. $MOZCONFIG
echo $_objdir
};
if ($mozconfig ne '') {
$objdir = `$objdir_script`;
chomp($objdir);
}
if ($objdir eq '')
{
$objdir = $topsrcdir;
} else
{
$objdir =~ s/\@TOPSRCDIR\@/$topsrcdir/;
if ($objdir =~ /\@CONFIG_GUESS\@/)
{
if (not defined($config_guess = $ENV{MOZ_CONFIG_GUESS})) {
$config_guess = `$topsrcdir/build/autoconf/config.guess`;
chomp($config_guess);
}
$objdir =~ s/\@CONFIG_GUESS\@/$config_guess/;
} }
return $objdir;
}
# end 'trees'
######################################################################
__END__
#! perl
# .moztrees - Defines the following perl variables for 'trees.pl' script,
#
# @tree_parent_dirs - Where to look for source trees.
# %dir_aliases - Aliases for common paths.
# @search_dirs - Additional directories to search for names.
#
# @tree_parent_dirs - Where to look for source trees.
#
# The script will look for
#
# @tree_parent_dirs/<tree_name>/mozilla
#
# where <tree_name> is any directory you put in <tree_parent_dirs>.
#
# For example, if you have the following source trees,
#
# /h/dance/export/slamm/raptor/mozilla
# /h/dance/export/slamm/raptor2/mozilla
#
# then you would add to the list,
#
# /h/dance/export/slamm (Note: two directories stripped).
#
# and you would use 'raptor' and 'raptor2' as the names of your trees,
#
# moz raptor2 webshell
#
@tree_parent_dirs =
(
'/export/slamm',
'/h/worms/export3/slamm'
);
# %dir_aliases - Directory aliases
#
# %dir_aliases = ('alias', 'path' [,'alias', 'path'...]);
#
# Possible 'path' settings:
# <path_relative_to_mozilla>
# topsrcdir:<path_relative_to_topsrcdir>
# objdir:<path_relative_to_objdir>
#
%dir_aliases =
(
'bin', 'objdir:dist/bin',
'viewer', 'webshell/tests/viewer'
);
# @search_dirs - 'moz' will search these directories if no directory
# aliases match.
#
# For example, if 'modules' is listed, then
#
# moz libpref
#
# would take you to mozilla/modules/libpref.
#
# (Give paths relative to mozilla.)
@search_dirs =
(
'lib',
'modules'
);
# Need to end with a true value
1;