This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Continue what #4494 started; introduce uid and gid formats.
[perl5.git] / lib / AutoSplit.pm
CommitLineData
a0d0e21e
LW
1package AutoSplit;
2
4e6ea2c3
GS
3use Exporter ();
4use Config qw(%Config);
5use Carp qw(carp);
6use File::Basename ();
68dc0745 7use File::Path qw(mkpath);
4e6ea2c3
GS
8use strict;
9use vars qw(
10 $VERSION @ISA @EXPORT @EXPORT_OK
11 $Verbose $Keep $Maxlen $CheckForAutoloader $CheckModTime
12 );
a0d0e21e 13
09bef843 14$VERSION = "1.0304";
a0d0e21e
LW
15@ISA = qw(Exporter);
16@EXPORT = qw(&autosplit &autosplit_lib_modules);
3edbfbe5 17@EXPORT_OK = qw($Verbose $Keep $Maxlen $CheckForAutoloader $CheckModTime);
a0d0e21e 18
f06db76b
AD
19=head1 NAME
20
21AutoSplit - split a package for autoloading
22
cb1a09d0
AD
23=head1 SYNOPSIS
24
4e6ea2c3 25 autosplit($file, $dir, $keep, $check, $modtime);
84dc3c4d 26
4e6ea2c3 27 autosplit_lib_modules(@modules);
cb1a09d0 28
f06db76b
AD
29=head1 DESCRIPTION
30
31This function will split up your program into files that the AutoLoader
21c92a1d 32module can handle. It is used by both the standard perl libraries and by
33the MakeMaker utility, to automatically configure libraries for autoloading.
34
35The C<autosplit> interface splits the specified file into a hierarchy
36rooted at the directory C<$dir>. It creates directories as needed to reflect
37class hierarchy, and creates the file F<autosplit.ix>. This file acts as
38both forward declaration of all package routines, and as timestamp for the
39last update of the hierarchy.
40
4e6ea2c3
GS
41The remaining three arguments to C<autosplit> govern other options to
42the autosplitter.
43
44=over 2
45
46=item $keep
47
48If the third argument, I<$keep>, is false, then any
49pre-existing C<*.al> files in the autoload directory are removed if
50they are no longer part of the module (obsoleted functions).
51$keep defaults to 0.
52
53=item $check
54
55The
56fourth argument, I<$check>, instructs C<autosplit> to check the module
57currently being split to ensure that it does include a C<use>
58specification for the AutoLoader module, and skips the module if
59AutoLoader is not detected.
60$check defaults to 1.
61
62=item $modtime
63
64Lastly, the I<$modtime> argument specifies
65that C<autosplit> is to check the modification time of the module
66against that of the C<autosplit.ix> file, and only split the module if
67it is newer.
68$modtime defaults to 1.
69
70=back
21c92a1d 71
72Typical use of AutoSplit in the perl MakeMaker utility is via the command-line
73with:
74
75 perl -e 'use AutoSplit; autosplit($ARGV[0], $ARGV[1], 0, 1, 1)'
76
77Defined as a Make macro, it is invoked with file and directory arguments;
78C<autosplit> will split the specified file into the specified directory and
79delete obsolete C<.al> files, after checking first that the module does use
80the AutoLoader, and ensuring that the module is not already currently split
81in its current form (the modtime test).
82
83The C<autosplit_lib_modules> form is used in the building of perl. It takes
84as input a list of files (modules) that are assumed to reside in a directory
85B<lib> relative to the current directory. Each file is sent to the
86autosplitter one at a time, to be split into the directory B<lib/auto>.
87
88In both usages of the autosplitter, only subroutines defined following the
4e6ea2c3 89perl I<__END__> token are split out into separate files. Some
21c92a1d 90routines may be placed prior to this marker to force their immediate loading
91and parsing.
92
4e6ea2c3
GS
93=head2 Multiple packages
94
95As of version 1.01 of the AutoSplit module it is possible to have
96multiple packages within a single file. Both of the following cases
97are supported:
98
99 package NAME;
100 __END__
101 sub AAA { ... }
102 package NAME::option1;
103 sub BBB { ... }
104 package NAME::option2;
105 sub BBB { ... }
21c92a1d 106
4e6ea2c3
GS
107 package NAME;
108 __END__
109 sub AAA { ... }
110 sub NAME::option1::BBB { ... }
111 sub NAME::option2::BBB { ... }
21c92a1d 112
113=head1 DIAGNOSTICS
114
4e6ea2c3
GS
115C<AutoSplit> will inform the user if it is necessary to create the
116top-level directory specified in the invocation. It is preferred that
117the script or installation process that invokes C<AutoSplit> have
118created the full directory path ahead of time. This warning may
119indicate that the module is being split into an incorrect path.
21c92a1d 120
4e6ea2c3
GS
121C<AutoSplit> will warn the user of all subroutines whose name causes
122potential file naming conflicts on machines with drastically limited
123(8 characters or less) file name length. Since the subroutine name is
124used as the file name, these warnings can aid in portability to such
125systems.
21c92a1d 126
4e6ea2c3
GS
127Warnings are issued and the file skipped if C<AutoSplit> cannot locate
128either the I<__END__> marker or a "package Name;"-style specification.
21c92a1d 129
4e6ea2c3
GS
130C<AutoSplit> will also emit general diagnostics for inability to
131create directories or files.
f06db76b
AD
132
133=cut
134
a0d0e21e
LW
135# for portability warn about names longer than $maxlen
136$Maxlen = 8; # 8 for dos, 11 (14-".al") for SYSVR3
137$Verbose = 1; # 0=none, 1=minimal, 2=list .al files
138$Keep = 0;
3edbfbe5
TB
139$CheckForAutoloader = 1;
140$CheckModTime = 1;
a0d0e21e 141
4e6ea2c3
GS
142my $IndexFile = "autosplit.ix"; # file also serves as timestamp
143my $maxflen = 255;
a0d0e21e 144$maxflen = 14 if $Config{'d_flexfnam'} ne 'define';
39e571d4
LM
145if (defined (&Dos::UseLFN)) {
146 $maxflen = Dos::UseLFN() ? 255 : 11;
147}
4e6ea2c3 148my $Is_VMS = ($^O eq 'VMS');
a0d0e21e 149
09bef843
SB
150# allow checking for valid ': attrlist' attachments
151my $nested;
152$nested = qr{ \( (?: (?> [^()]+ ) | (?p{ $nested }) )* \) }x;
153my $one_attr = qr{ (?> (?! \d) \w+ (?:$nested)? ) [\s,]* }x;
154my $attr_list = qr{ \s* : \s* (?: $one_attr )* }x;
155
156
3edbfbe5 157
a0d0e21e 158sub autosplit{
4e6ea2c3 159 my($file, $autodir, $keep, $ckal, $ckmt) = @_;
75f92628
AD
160 # $file - the perl source file to be split (after __END__)
161 # $autodir - the ".../auto" dir below which to write split subs
162 # Handle optional flags:
4e6ea2c3 163 $keep = $Keep unless defined $keep;
75f92628
AD
164 $ckal = $CheckForAutoloader unless defined $ckal;
165 $ckmt = $CheckModTime unless defined $ckmt;
166 autosplit_file($file, $autodir, $keep, $ckal, $ckmt);
a0d0e21e
LW
167}
168
169
a0d0e21e 170# This function is used during perl building/installation
21c92a1d 171# ./miniperl -e 'use AutoSplit; autosplit_lib_modules(@ARGV)' ...
a0d0e21e
LW
172
173sub autosplit_lib_modules{
174 my(@modules) = @_; # list of Module names
175
3e3baf6d 176 while(defined($_ = shift @modules)){
a0d0e21e 177 s#::#/#g; # incase specified as ABC::XYZ
4633a7c4 178 s|\\|/|g; # bug in ksh OS/2
a0d0e21e 179 s#^lib/##; # incase specified as lib/*.pm
c6538b72 180 if ($Is_VMS && /[:>\]]/) { # may need to convert VMS-style filespecs
a0d0e21e
LW
181 my ($dir,$name) = (/(.*])(.*)/);
182 $dir =~ s/.*lib[\.\]]//;
183 $dir =~ s#[\.\]]#/#g;
184 $_ = $dir . $name;
185 }
92c28edd 186 autosplit_file("lib/$_", "lib/auto",
4e6ea2c3 187 $Keep, $CheckForAutoloader, $CheckModTime);
a0d0e21e
LW
188 }
189 0;
190}
191
192
193# private functions
194
4e6ea2c3
GS
195sub autosplit_file {
196 my($filename, $autodir, $keep, $check_for_autoloader, $check_mod_time)
197 = @_;
198 my(@outfiles);
6e7678af 199 local($_);
4e6ea2c3 200 local($/) = "\n";
a0d0e21e
LW
201
202 # where to write output files
4e6ea2c3 203 $autodir ||= "lib/auto";
f86702cc 204 if ($Is_VMS) {
4e6ea2c3 205 ($autodir = VMS::Filespec::unixpath($autodir)) =~ s|/$||;
f86702cc 206 $filename = VMS::Filespec::unixify($filename); # may have dirs
207 }
3edbfbe5 208 unless (-d $autodir){
68dc0745 209 mkpath($autodir,0,0755);
4e6ea2c3
GS
210 # We should never need to create the auto dir
211 # here. installperl (or similar) should have done
212 # it. Expecting it to exist is a valuable sanity check against
213 # autosplitting into some random directory by mistake.
214 print "Warning: AutoSplit had to create top-level " .
215 "$autodir unexpectedly.\n";
3edbfbe5 216 }
a0d0e21e
LW
217
218 # allow just a package name to be used
219 $filename .= ".pm" unless ($filename =~ m/\.pm$/);
220
4e6ea2c3 221 open(IN, "<$filename") or die "AutoSplit: Can't open $filename: $!\n";
a0d0e21e
LW
222 my($pm_mod_time) = (stat($filename))[9];
223 my($autoloader_seen) = 0;
f06db76b 224 my($in_pod) = 0;
4e6ea2c3 225 my($def_package,$last_package,$this_package,$fnr);
a0d0e21e 226 while (<IN>) {
f06db76b 227 # Skip pod text.
4e6ea2c3 228 $fnr++;
697fd008 229 $in_pod = 1 if /^=\w/;
f06db76b
AD
230 $in_pod = 0 if /^=cut/;
231 next if ($in_pod || /^=cut/);
232
a0d0e21e 233 # record last package name seen
4e6ea2c3 234 $def_package = $1 if (m/^\s*package\s+([\w:]+)\s*;/);
3edbfbe5 235 ++$autoloader_seen if m/^\s*(use|require)\s+AutoLoader\b/;
a0d0e21e
LW
236 ++$autoloader_seen if m/\bISA\s*=.*\bAutoLoader\b/;
237 last if /^__END__/;
238 }
3edbfbe5 239 if ($check_for_autoloader && !$autoloader_seen){
4e6ea2c3
GS
240 print "AutoSplit skipped $filename: no AutoLoader used\n"
241 if ($Verbose>=2);
242 return 0;
3edbfbe5 243 }
a0d0e21e
LW
244 $_ or die "Can't find __END__ in $filename\n";
245
4e6ea2c3 246 $def_package or die "Can't find 'package Name;' in $filename\n";
a0d0e21e 247
4e6ea2c3 248 my($modpname) = _modpname($def_package);
a0d0e21e 249
4e6ea2c3
GS
250 # this _has_ to match so we have a reasonable timestamp file
251 die "Package $def_package ($modpname.pm) does not ".
252 "match filename $filename"
68dc0745 253 unless ($filename =~ m/\Q$modpname.pm\E$/ or
39e571d4 254 ($^O eq 'dos') or ($^O eq 'MSWin32') or
c6538b72 255 $Is_VMS && $filename =~ m/$modpname.pm/i);
a0d0e21e 256
68dc0745 257 my($al_idx_file) = "$autodir/$modpname/$IndexFile";
258
a0d0e21e
LW
259 if ($check_mod_time){
260 my($al_ts_time) = (stat("$al_idx_file"))[9] || 1;
261 if ($al_ts_time >= $pm_mod_time){
4e6ea2c3 262 print "AutoSplit skipped ($al_idx_file newer than $filename)\n"
a0d0e21e
LW
263 if ($Verbose >= 2);
264 return undef; # one undef, not a list
265 }
266 }
267
4e6ea2c3 268 print "AutoSplitting $filename ($autodir/$modpname)\n"
a0d0e21e
LW
269 if $Verbose;
270
271 unless (-d "$autodir/$modpname"){
68dc0745 272 mkpath("$autodir/$modpname",0,0777);
a0d0e21e
LW
273 }
274
275 # We must try to deal with some SVR3 systems with a limit of 14
276 # characters for file names. Sadly we *cannot* simply truncate all
277 # file names to 14 characters on these systems because we *must*
278 # create filenames which exactly match the names used by AutoLoader.pm.
279 # This is a problem because some systems silently truncate the file
280 # names while others treat long file names as an error.
281
39e571d4
LM
282 my $Is83 = $maxflen==11; # plain, case INSENSITIVE dos filenames
283
4e6ea2c3 284 my(@subnames, $subname, %proto, %package);
96bc026d
CS
285 my @cache = ();
286 my $caching = 1;
4e6ea2c3 287 $last_package = '';
a0d0e21e 288 while (<IN>) {
4e6ea2c3 289 $fnr++;
53667d02 290 $in_pod = 1 if /^=\w/;
4e6ea2c3
GS
291 $in_pod = 0 if /^=cut/;
292 next if ($in_pod || /^=cut/);
293 # the following (tempting) old coding gives big troubles if a
294 # cut is forgotten at EOF:
295 # next if /^=\w/ .. /^=cut/;
296 if (/^package\s+([\w:]+)\s*;/) {
297 $this_package = $def_package = $1;
a0d0e21e 298 }
09bef843 299 if (/^sub\s+([\w:]+)(\s*(?:\(.*?\))?(?:$attr_list)?)/) {
4e6ea2c3
GS
300 print OUT "# end of $last_package\::$subname\n1;\n"
301 if $last_package;
302 $subname = $1;
303 my $proto = $2 || '';
304 if ($subname =~ s/(.*):://){
305 $this_package = $1;
306 } else {
307 $this_package = $def_package;
a0d0e21e 308 }
4e6ea2c3
GS
309 my $fq_subname = "$this_package\::$subname";
310 $package{$fq_subname} = $this_package;
311 $proto{$fq_subname} = $proto;
312 push(@subnames, $fq_subname);
a0d0e21e 313 my($lname, $sname) = ($subname, substr($subname,0,$maxflen-3));
4e6ea2c3
GS
314 $modpname = _modpname($this_package);
315 mkpath("$autodir/$modpname",0,0777);
a0d0e21e
LW
316 my($lpath) = "$autodir/$modpname/$lname.al";
317 my($spath) = "$autodir/$modpname/$sname.al";
4e6ea2c3
GS
318 my $path;
319 if (!$Is83 and open(OUT, ">$lpath")){
320 $path=$lpath;
a0d0e21e 321 print " writing $lpath\n" if ($Verbose>=2);
4e6ea2c3
GS
322 } else {
323 open(OUT, ">$spath") or die "Can't create $spath: $!\n";
324 $path=$spath;
325 print " writing $spath (with truncated name)\n"
326 if ($Verbose>=1);
a0d0e21e 327 }
4e6ea2c3
GS
328 push(@outfiles, $path);
329 print OUT <<EOT;
330# NOTE: Derived from $filename.
331# Changes made here will be lost when autosplit again.
332# See AutoSplit.pm.
333package $this_package;
334
335#line $fnr "$filename (autosplit into $path)"
336EOT
96bc026d
CS
337 print OUT @cache;
338 @cache = ();
339 $caching = 0;
340 }
341 if($caching) {
342 push(@cache, $_) if @cache || /\S/;
4e6ea2c3 343 } else {
96bc026d
CS
344 print OUT $_;
345 }
4e6ea2c3 346 if(/^\}/) {
96bc026d
CS
347 if($caching) {
348 print OUT @cache;
349 @cache = ();
350 }
351 print OUT "\n";
352 $caching = 1;
a0d0e21e 353 }
4e6ea2c3 354 $last_package = $this_package if defined $this_package;
a0d0e21e 355 }
4e6ea2c3 356 print OUT @cache,"1;\n# end of $last_package\::$subname\n";
a0d0e21e
LW
357 close(OUT);
358 close(IN);
4e6ea2c3 359
a0d0e21e 360 if (!$keep){ # don't keep any obsolete *.al files in the directory
4e6ea2c3
GS
361 my(%outfiles);
362 # @outfiles{@outfiles} = @outfiles;
363 # perl downcases all filenames on VMS (which upcases all filenames) so
364 # we'd better downcase the sub name list too, or subs with upper case
365 # letters in them will get their .al files deleted right after they're
8f8c40b1 366 # created. (The mixed case sub name won't match the all-lowercase
4e6ea2c3
GS
367 # filename, and so be cleaned up as a scrap file)
368 if ($Is_VMS or $Is83) {
369 %outfiles = map {lc($_) => lc($_) } @outfiles;
370 } else {
371 @outfiles{@outfiles} = @outfiles;
372 }
373 my(%outdirs,@outdirs);
374 for (@outfiles) {
375 $outdirs{File::Basename::dirname($_)}||=1;
376 }
377 for my $dir (keys %outdirs) {
378 opendir(OUTDIR,$dir);
379 foreach (sort readdir(OUTDIR)){
380 next unless /\.al$/;
381 my($file) = "$dir/$_";
8f8c40b1 382 $file = lc $file if $Is83 or $Is_VMS;
4e6ea2c3
GS
383 next if $outfiles{$file};
384 print " deleting $file\n" if ($Verbose>=2);
385 my($deleted,$thistime); # catch all versions on VMS
386 do { $deleted += ($thistime = unlink $file) } while ($thistime);
387 carp "Unable to delete $file: $!" unless $deleted;
388 }
389 closedir(OUTDIR);
a0d0e21e 390 }
a0d0e21e
LW
391 }
392
393 open(TS,">$al_idx_file") or
394 carp "AutoSplit: unable to create timestamp file ($al_idx_file): $!";
4e6ea2c3
GS
395 print TS "# Index created by AutoSplit for $filename\n";
396 print TS "# (file acts as timestamp)\n";
397 $last_package = '';
398 for my $fqs (@subnames) {
399 my($subname) = $fqs;
400 $subname =~ s/.*:://;
401 print TS "package $package{$fqs};\n"
402 unless $last_package eq $package{$fqs};
403 print TS "sub $subname $proto{$fqs};\n";
404 $last_package = $package{$fqs};
405 }
f06db76b 406 print TS "1;\n";
a0d0e21e
LW
407 close(TS);
408
4e6ea2c3 409 _check_unique($filename, $Maxlen, 1, @outfiles);
a0d0e21e 410
4e6ea2c3 411 @outfiles;
a0d0e21e
LW
412}
413
4e6ea2c3
GS
414sub _modpname ($) {
415 my($package) = @_;
416 my $modpname = $package;
417 if ($^O eq 'MSWin32') {
418 $modpname =~ s#::#\\#g;
419 } else {
420 $modpname =~ s#::#/#g;
421 }
422 $modpname;
423}
a0d0e21e 424
4e6ea2c3
GS
425sub _check_unique {
426 my($filename, $maxlen, $warn, @outfiles) = @_;
a0d0e21e
LW
427 my(%notuniq) = ();
428 my(%shorts) = ();
4e6ea2c3
GS
429 my(@toolong) = grep(
430 length(File::Basename::basename($_))
431 > $maxlen,
432 @outfiles
433 );
434
435 foreach (@toolong){
436 my($dir) = File::Basename::dirname($_);
437 my($file) = File::Basename::basename($_);
438 my($trunc) = substr($file,0,$maxlen);
439 $notuniq{$dir}{$trunc} = 1 if $shorts{$dir}{$trunc};
440 $shorts{$dir}{$trunc} = $shorts{$dir}{$trunc} ?
441 "$shorts{$dir}{$trunc}, $file" : $file;
a0d0e21e
LW
442 }
443 if (%notuniq && $warn){
4e6ea2c3
GS
444 print "$filename: some names are not unique when " .
445 "truncated to $maxlen characters:\n";
446 foreach my $dir (sort keys %notuniq){
447 print " directory $dir:\n";
448 foreach my $trunc (sort keys %{$notuniq{$dir}}) {
449 print " $shorts{$dir}{$trunc} truncate to $trunc\n";
450 }
a0d0e21e
LW
451 }
452 }
a0d0e21e
LW
453}
454
4551;
456__END__
457
458# test functions so AutoSplit.pm can be applied to itself:
4e6ea2c3
GS
459sub test1 ($) { "test 1\n"; }
460sub test2 ($$) { "test 2\n"; }
461sub test3 ($$$) { "test 3\n"; }
462sub testtesttesttest4_1 { "test 4\n"; }
463sub testtesttesttest4_2 { "duplicate test 4\n"; }
464sub Just::Another::test5 { "another test 5\n"; }
465sub test6 { return join ":", __FILE__,__LINE__; }
466package Yet::Another::AutoSplit;
467sub testtesttesttest4_1 ($) { "another test 4\n"; }
468sub testtesttesttest4_2 ($$) { "another duplicate test 4\n"; }
09bef843
SB
469package Yet::More::Attributes;
470sub test_a1 ($) : locked { 1; }
471sub test_a2 : locked { 1; }