PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/lib/AutoSplit.pm

#
Perl | 488 lines | 402 code | 61 blank | 25 comment | 39 complexity | 99986506396e30cb024b87d6e9b21b25 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. package AutoSplit;
  2. use 5.005_64;
  3. use Exporter ();
  4. use Config qw(%Config);
  5. use Carp qw(carp);
  6. use File::Basename ();
  7. use File::Path qw(mkpath);
  8. use File::Spec::Functions qw(curdir catfile);
  9. use strict;
  10. our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Verbose, $Keep, $Maxlen,
  11. $CheckForAutoloader, $CheckModTime);
  12. $VERSION = "1.0305";
  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 includes 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. # allow checking for valid ': attrlist' attachments
  114. my $nested;
  115. $nested = qr{ \( (?: (?> [^()]+ ) | (??{ $nested }) )* \) }x;
  116. my $one_attr = qr{ (?> (?! \d) \w+ (?:$nested)? ) (?:\s*\:\s*|\s+(?!\:)) }x;
  117. my $attr_list = qr{ \s* : \s* (?: $one_attr )* }x;
  118. sub autosplit{
  119. my($file, $autodir, $keep, $ckal, $ckmt) = @_;
  120. # $file - the perl source file to be split (after __END__)
  121. # $autodir - the ".../auto" dir below which to write split subs
  122. # Handle optional flags:
  123. $keep = $Keep unless defined $keep;
  124. $ckal = $CheckForAutoloader unless defined $ckal;
  125. $ckmt = $CheckModTime unless defined $ckmt;
  126. autosplit_file($file, $autodir, $keep, $ckal, $ckmt);
  127. }
  128. # This function is used during perl building/installation
  129. # ./miniperl -e 'use AutoSplit; autosplit_lib_modules(@ARGV)' ...
  130. sub autosplit_lib_modules{
  131. my(@modules) = @_; # list of Module names
  132. while(defined($_ = shift @modules)){
  133. while (m#(.*?[^:])::([^:].*)#) { # in case specified as ABC::XYZ
  134. $_ = catfile($1, $2);
  135. }
  136. s|\\|/|g; # bug in ksh OS/2
  137. s#^lib/##s; # incase specified as lib/*.pm
  138. my($lib) = catfile(curdir(), "lib");
  139. if ($Is_VMS) { # may need to convert VMS-style filespecs
  140. $lib =~ s#^\[\]#.\/#;
  141. }
  142. s#^$lib\W+##s; # incase specified as ./lib/*.pm
  143. if ($Is_VMS && /[:>\]]/) { # may need to convert VMS-style filespecs
  144. my ($dir,$name) = (/(.*])(.*)/s);
  145. $dir =~ s/.*lib[\.\]]//s;
  146. $dir =~ s#[\.\]]#/#g;
  147. $_ = $dir . $name;
  148. }
  149. autosplit_file(catfile($lib, $_), catfile($lib, "auto"),
  150. $Keep, $CheckForAutoloader, $CheckModTime);
  151. }
  152. 0;
  153. }
  154. # private functions
  155. sub autosplit_file {
  156. my($filename, $autodir, $keep, $check_for_autoloader, $check_mod_time)
  157. = @_;
  158. my(@outfiles);
  159. local($_);
  160. local($/) = "\n";
  161. # where to write output files
  162. $autodir ||= catfile(curdir(), "lib", "auto");
  163. if ($Is_VMS) {
  164. ($autodir = VMS::Filespec::unixpath($autodir)) =~ s|/\z||;
  165. $filename = VMS::Filespec::unixify($filename); # may have dirs
  166. }
  167. unless (-d $autodir){
  168. mkpath($autodir,0,0755);
  169. # We should never need to create the auto dir
  170. # here. installperl (or similar) should have done
  171. # it. Expecting it to exist is a valuable sanity check against
  172. # autosplitting into some random directory by mistake.
  173. print "Warning: AutoSplit had to create top-level " .
  174. "$autodir unexpectedly.\n";
  175. }
  176. # allow just a package name to be used
  177. $filename .= ".pm" unless ($filename =~ m/\.pm\z/);
  178. open(IN, "<$filename") or die "AutoSplit: Can't open $filename: $!\n";
  179. my($pm_mod_time) = (stat($filename))[9];
  180. my($autoloader_seen) = 0;
  181. my($in_pod) = 0;
  182. my($def_package,$last_package,$this_package,$fnr);
  183. while (<IN>) {
  184. # Skip pod text.
  185. $fnr++;
  186. $in_pod = 1 if /^=\w/;
  187. $in_pod = 0 if /^=cut/;
  188. next if ($in_pod || /^=cut/);
  189. # record last package name seen
  190. $def_package = $1 if (m/^\s*package\s+([\w:]+)\s*;/);
  191. ++$autoloader_seen if m/^\s*(use|require)\s+AutoLoader\b/;
  192. ++$autoloader_seen if m/\bISA\s*=.*\bAutoLoader\b/;
  193. last if /^__END__/;
  194. }
  195. if ($check_for_autoloader && !$autoloader_seen){
  196. print "AutoSplit skipped $filename: no AutoLoader used\n"
  197. if ($Verbose>=2);
  198. return 0;
  199. }
  200. $_ or die "Can't find __END__ in $filename\n";
  201. $def_package or die "Can't find 'package Name;' in $filename\n";
  202. my($modpname) = _modpname($def_package);
  203. if ($Is_VMS) {
  204. $modpname = VMS::Filespec::unixify($modpname); # may have dirs
  205. }
  206. # this _has_ to match so we have a reasonable timestamp file
  207. die "Package $def_package ($modpname.pm) does not ".
  208. "match filename $filename"
  209. unless ($filename =~ m/\Q$modpname.pm\E$/ or
  210. ($^O eq 'dos') or ($^O eq 'MSWin32') or
  211. $Is_VMS && $filename =~ m/$modpname.pm/i);
  212. my($al_idx_file) = catfile($autodir, $modpname, $IndexFile);
  213. if ($check_mod_time){
  214. my($al_ts_time) = (stat("$al_idx_file"))[9] || 1;
  215. if ($al_ts_time >= $pm_mod_time){
  216. print "AutoSplit skipped ($al_idx_file newer than $filename)\n"
  217. if ($Verbose >= 2);
  218. return undef; # one undef, not a list
  219. }
  220. }
  221. my($modnamedir) = catfile($autodir, $modpname);
  222. print "AutoSplitting $filename ($modnamedir)\n"
  223. if $Verbose;
  224. unless (-d $modnamedir){
  225. mkpath($modnamedir,0,0777);
  226. }
  227. # We must try to deal with some SVR3 systems with a limit of 14
  228. # characters for file names. Sadly we *cannot* simply truncate all
  229. # file names to 14 characters on these systems because we *must*
  230. # create filenames which exactly match the names used by AutoLoader.pm.
  231. # This is a problem because some systems silently truncate the file
  232. # names while others treat long file names as an error.
  233. my $Is83 = $maxflen==11; # plain, case INSENSITIVE dos filenames
  234. my(@subnames, $subname, %proto, %package);
  235. my @cache = ();
  236. my $caching = 1;
  237. $last_package = '';
  238. while (<IN>) {
  239. $fnr++;
  240. $in_pod = 1 if /^=\w/;
  241. $in_pod = 0 if /^=cut/;
  242. next if ($in_pod || /^=cut/);
  243. # the following (tempting) old coding gives big troubles if a
  244. # cut is forgotten at EOF:
  245. # next if /^=\w/ .. /^=cut/;
  246. if (/^package\s+([\w:]+)\s*;/) {
  247. $this_package = $def_package = $1;
  248. }
  249. if (/^sub\s+([\w:]+)(\s*(?:\(.*?\))?(?:$attr_list)?)/) {
  250. print OUT "# end of $last_package\::$subname\n1;\n"
  251. if $last_package;
  252. $subname = $1;
  253. my $proto = $2 || '';
  254. if ($subname =~ s/(.*):://){
  255. $this_package = $1;
  256. } else {
  257. $this_package = $def_package;
  258. }
  259. my $fq_subname = "$this_package\::$subname";
  260. $package{$fq_subname} = $this_package;
  261. $proto{$fq_subname} = $proto;
  262. push(@subnames, $fq_subname);
  263. my($lname, $sname) = ($subname, substr($subname,0,$maxflen-3));
  264. $modpname = _modpname($this_package);
  265. my($modnamedir) = catfile($autodir, $modpname);
  266. mkpath($modnamedir,0,0777);
  267. my($lpath) = catfile($modnamedir, "$lname.al");
  268. my($spath) = catfile($modnamedir, "$sname.al");
  269. my $path;
  270. if (!$Is83 and open(OUT, ">$lpath")){
  271. $path=$lpath;
  272. print " writing $lpath\n" if ($Verbose>=2);
  273. } else {
  274. open(OUT, ">$spath") or die "Can't create $spath: $!\n";
  275. $path=$spath;
  276. print " writing $spath (with truncated name)\n"
  277. if ($Verbose>=1);
  278. }
  279. push(@outfiles, $path);
  280. my $lineno = $fnr - @cache;
  281. print OUT <<EOT;
  282. # NOTE: Derived from $filename.
  283. # Changes made here will be lost when autosplit is run again.
  284. # See AutoSplit.pm.
  285. package $this_package;
  286. #line $lineno "$filename (autosplit into $path)"
  287. EOT
  288. print OUT @cache;
  289. @cache = ();
  290. $caching = 0;
  291. }
  292. if($caching) {
  293. push(@cache, $_) if @cache || /\S/;
  294. } else {
  295. print OUT $_;
  296. }
  297. if(/^\}/) {
  298. if($caching) {
  299. print OUT @cache;
  300. @cache = ();
  301. }
  302. print OUT "\n";
  303. $caching = 1;
  304. }
  305. $last_package = $this_package if defined $this_package;
  306. }
  307. if ($subname) {
  308. print OUT @cache,"1;\n# end of $last_package\::$subname\n";
  309. close(OUT);
  310. }
  311. close(IN);
  312. if (!$keep){ # don't keep any obsolete *.al files in the directory
  313. my(%outfiles);
  314. # @outfiles{@outfiles} = @outfiles;
  315. # perl downcases all filenames on VMS (which upcases all filenames) so
  316. # we'd better downcase the sub name list too, or subs with upper case
  317. # letters in them will get their .al files deleted right after they're
  318. # created. (The mixed case sub name won't match the all-lowercase
  319. # filename, and so be cleaned up as a scrap file)
  320. if ($Is_VMS or $Is83) {
  321. %outfiles = map {lc($_) => lc($_) } @outfiles;
  322. } else {
  323. @outfiles{@outfiles} = @outfiles;
  324. }
  325. my(%outdirs,@outdirs);
  326. for (@outfiles) {
  327. $outdirs{File::Basename::dirname($_)}||=1;
  328. }
  329. for my $dir (keys %outdirs) {
  330. opendir(OUTDIR,$dir);
  331. foreach (sort readdir(OUTDIR)){
  332. next unless /\.al\z/;
  333. my($file) = catfile($dir, $_);
  334. $file = lc $file if $Is83 or $Is_VMS;
  335. next if $outfiles{$file};
  336. print " deleting $file\n" if ($Verbose>=2);
  337. my($deleted,$thistime); # catch all versions on VMS
  338. do { $deleted += ($thistime = unlink $file) } while ($thistime);
  339. carp "Unable to delete $file: $!" unless $deleted;
  340. }
  341. closedir(OUTDIR);
  342. }
  343. }
  344. open(TS,">$al_idx_file") or
  345. carp "AutoSplit: unable to create timestamp file ($al_idx_file): $!";
  346. print TS "# Index created by AutoSplit for $filename\n";
  347. print TS "# (file acts as timestamp)\n";
  348. $last_package = '';
  349. for my $fqs (@subnames) {
  350. my($subname) = $fqs;
  351. $subname =~ s/.*:://;
  352. print TS "package $package{$fqs};\n"
  353. unless $last_package eq $package{$fqs};
  354. print TS "sub $subname $proto{$fqs};\n";
  355. $last_package = $package{$fqs};
  356. }
  357. print TS "1;\n";
  358. close(TS);
  359. _check_unique($filename, $Maxlen, 1, @outfiles);
  360. @outfiles;
  361. }
  362. sub _modpname ($) {
  363. my($package) = @_;
  364. my $modpname = $package;
  365. if ($^O eq 'MSWin32') {
  366. $modpname =~ s#::#\\#g;
  367. } else {
  368. while ($modpname =~ m#(.*?[^:])::([^:].*)#) {
  369. $modpname = catfile($1, $2);
  370. }
  371. }
  372. $modpname;
  373. }
  374. sub _check_unique {
  375. my($filename, $maxlen, $warn, @outfiles) = @_;
  376. my(%notuniq) = ();
  377. my(%shorts) = ();
  378. my(@toolong) = grep(
  379. length(File::Basename::basename($_))
  380. > $maxlen,
  381. @outfiles
  382. );
  383. foreach (@toolong){
  384. my($dir) = File::Basename::dirname($_);
  385. my($file) = File::Basename::basename($_);
  386. my($trunc) = substr($file,0,$maxlen);
  387. $notuniq{$dir}{$trunc} = 1 if $shorts{$dir}{$trunc};
  388. $shorts{$dir}{$trunc} = $shorts{$dir}{$trunc} ?
  389. "$shorts{$dir}{$trunc}, $file" : $file;
  390. }
  391. if (%notuniq && $warn){
  392. print "$filename: some names are not unique when " .
  393. "truncated to $maxlen characters:\n";
  394. foreach my $dir (sort keys %notuniq){
  395. print " directory $dir:\n";
  396. foreach my $trunc (sort keys %{$notuniq{$dir}}) {
  397. print " $shorts{$dir}{$trunc} truncate to $trunc\n";
  398. }
  399. }
  400. }
  401. }
  402. 1;
  403. __END__
  404. # test functions so AutoSplit.pm can be applied to itself:
  405. sub test1 ($) { "test 1\n"; }
  406. sub test2 ($$) { "test 2\n"; }
  407. sub test3 ($$$) { "test 3\n"; }
  408. sub testtesttesttest4_1 { "test 4\n"; }
  409. sub testtesttesttest4_2 { "duplicate test 4\n"; }
  410. sub Just::Another::test5 { "another test 5\n"; }
  411. sub test6 { return join ":", __FILE__,__LINE__; }
  412. package Yet::Another::AutoSplit;
  413. sub testtesttesttest4_1 ($) { "another test 4\n"; }
  414. sub testtesttesttest4_2 ($$) { "another duplicate test 4\n"; }
  415. package Yet::More::Attributes;
  416. sub test_a1 ($) : locked :locked { 1; }
  417. sub test_a2 : locked { 1; }