Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
14 KiB

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