Commit | Line | Data |
a0d0e21e |
1 | package AutoSplit; |
2 | |
3 | require 5.000; |
4 | require Exporter; |
5 | |
6 | use Config; |
7 | use Carp; |
8 | |
9 | @ISA = qw(Exporter); |
10 | @EXPORT = qw(&autosplit &autosplit_lib_modules); |
3edbfbe5 |
11 | @EXPORT_OK = qw($Verbose $Keep $Maxlen $CheckForAutoloader $CheckModTime); |
a0d0e21e |
12 | |
13 | # for portability warn about names longer than $maxlen |
14 | $Maxlen = 8; # 8 for dos, 11 (14-".al") for SYSVR3 |
15 | $Verbose = 1; # 0=none, 1=minimal, 2=list .al files |
16 | $Keep = 0; |
3edbfbe5 |
17 | $CheckForAutoloader = 1; |
18 | $CheckModTime = 1; |
a0d0e21e |
19 | |
3edbfbe5 |
20 | $IndexFile = "autosplit.ix"; # file also serves as timestamp |
a0d0e21e |
21 | $maxflen = 255; |
22 | $maxflen = 14 if $Config{'d_flexfnam'} ne 'define'; |
23 | $vms = ($Config{'osname'} eq 'VMS'); |
24 | |
3edbfbe5 |
25 | |
a0d0e21e |
26 | sub autosplit{ |
27 | my($file, $autodir) = @_; |
3edbfbe5 |
28 | autosplit_file($file, $autodir, $Keep, $CheckForAutoloader, $CheckModTime); |
a0d0e21e |
29 | } |
30 | |
31 | |
a0d0e21e |
32 | # This function is used during perl building/installation |
33 | # ./miniperl -e 'use AutoSplit; autosplit_modules(@ARGV)' ... |
34 | |
35 | sub autosplit_lib_modules{ |
36 | my(@modules) = @_; # list of Module names |
37 | |
38 | foreach(@modules){ |
39 | s#::#/#g; # incase specified as ABC::XYZ |
40 | s#^lib/##; # incase specified as lib/*.pm |
41 | if ($vms && /[:>\]]/) { # may need to convert VMS-style filespecs |
42 | my ($dir,$name) = (/(.*])(.*)/); |
43 | $dir =~ s/.*lib[\.\]]//; |
44 | $dir =~ s#[\.\]]#/#g; |
45 | $_ = $dir . $name; |
46 | } |
3edbfbe5 |
47 | autosplit_file("lib/$_", "lib/auto", $Keep, $CheckForAutoloader, $CheckModTime); |
a0d0e21e |
48 | } |
49 | 0; |
50 | } |
51 | |
52 | |
53 | # private functions |
54 | |
55 | sub autosplit_file{ |
56 | my($filename, $autodir, $keep, $check_for_autoloader, $check_mod_time) = @_; |
57 | my(@names); |
58 | |
59 | # where to write output files |
60 | $autodir = "lib/auto" unless $autodir; |
3edbfbe5 |
61 | unless (-d $autodir){ |
62 | local($", @p)="/"; |
63 | foreach(split(/\//,$autodir)){ |
64 | push(@p, $_); |
65 | next if -d "@p/"; |
66 | mkdir("@p",0755) or die "AutoSplit unable to mkdir @p: $!"; |
67 | } |
68 | # We should never need to create the auto dir here. installperl |
69 | # (or similar) should have done it. Expecting it to exist is a valuable |
70 | # sanity check against autosplitting into some random directory by mistake. |
71 | print "Warning: AutoSplit had to create top-level $autodir unexpectedly.\n"; |
72 | } |
a0d0e21e |
73 | |
74 | # allow just a package name to be used |
75 | $filename .= ".pm" unless ($filename =~ m/\.pm$/); |
76 | |
77 | open(IN, "<$filename") || die "AutoSplit: Can't open $filename: $!\n"; |
78 | my($pm_mod_time) = (stat($filename))[9]; |
79 | my($autoloader_seen) = 0; |
80 | while (<IN>) { |
81 | # record last package name seen |
82 | $package = $1 if (m/^\s*package\s+([\w:]+)\s*;/); |
3edbfbe5 |
83 | ++$autoloader_seen if m/^\s*(use|require)\s+AutoLoader\b/; |
a0d0e21e |
84 | ++$autoloader_seen if m/\bISA\s*=.*\bAutoLoader\b/; |
3edbfbe5 |
85 | ++$autoloader_seen if m/^\s*sub\s+AUTOLOAD\b/; |
a0d0e21e |
86 | last if /^__END__/; |
87 | } |
3edbfbe5 |
88 | if ($check_for_autoloader && !$autoloader_seen){ |
89 | print "AutoSplit skipped $filename: no AutoLoader used\n" if ($Verbose>=2); |
90 | return 0 |
91 | } |
a0d0e21e |
92 | $_ or die "Can't find __END__ in $filename\n"; |
93 | |
94 | $package or die "Can't find 'package Name;' in $filename\n"; |
95 | |
96 | my($modpname) = $package; $modpname =~ s#::#/#g; |
97 | my($al_idx_file) = "$autodir/$modpname/$IndexFile"; |
98 | |
99 | die "Package $package does not match filename $filename" |
100 | unless ($filename =~ m/$modpname.pm$/ or |
101 | $vms && $filename =~ m/$modpname.pm/i); |
102 | |
103 | if ($check_mod_time){ |
104 | my($al_ts_time) = (stat("$al_idx_file"))[9] || 1; |
105 | if ($al_ts_time >= $pm_mod_time){ |
106 | print "AutoSplit skipped ($al_idx_file newer that $filename)\n" |
107 | if ($Verbose >= 2); |
108 | return undef; # one undef, not a list |
109 | } |
110 | } |
111 | |
112 | my($from) = ($Verbose>=2) ? "$filename => " : ""; |
113 | print "AutoSplitting $package ($from$autodir/$modpname)\n" |
114 | if $Verbose; |
115 | |
116 | unless (-d "$autodir/$modpname"){ |
117 | local($", @p)="/"; |
118 | foreach(split(/\//,"$autodir/$modpname")){ |
119 | push(@p, $_); |
42793c05 |
120 | next if -d "@p/"; |
a0d0e21e |
121 | mkdir("@p",0777) or die "AutoSplit unable to mkdir @p: $!"; |
122 | } |
123 | } |
124 | |
125 | # We must try to deal with some SVR3 systems with a limit of 14 |
126 | # characters for file names. Sadly we *cannot* simply truncate all |
127 | # file names to 14 characters on these systems because we *must* |
128 | # create filenames which exactly match the names used by AutoLoader.pm. |
129 | # This is a problem because some systems silently truncate the file |
130 | # names while others treat long file names as an error. |
131 | |
132 | # We do not yet deal with multiple packages within one file. |
133 | # Ideally both of these styles should work. |
134 | # |
135 | # package NAME; |
136 | # __END__ |
137 | # sub AAA { ... } |
138 | # package NAME::option1; |
139 | # sub BBB { ... } |
140 | # package NAME::option2; |
141 | # sub BBB { ... } |
142 | # |
143 | # package NAME; |
144 | # __END__ |
145 | # sub AAA { ... } |
146 | # sub NAME::option1::BBB { ... } |
147 | # sub NAME::option2::BBB { ... } |
148 | # |
149 | # For now both of these produce warnings. |
150 | |
151 | open(OUT,">/dev/null") || open(OUT,">nla0:"); # avoid 'not opened' warning |
152 | my(@subnames); |
153 | while (<IN>) { |
154 | if (/^package ([\w:]+)\s*;/) { |
155 | warn "package $1; in AutoSplit section ignored. Not currently supported."; |
156 | } |
157 | if (/^sub ([\w:]+)/) { |
158 | print OUT "1;\n"; |
159 | my($subname) = $1; |
160 | if ($subname =~ m/::/){ |
161 | warn "subs with package names not currently supported in AutoSplit section"; |
162 | } |
163 | push(@subnames, $subname); |
164 | my($lname, $sname) = ($subname, substr($subname,0,$maxflen-3)); |
165 | my($lpath) = "$autodir/$modpname/$lname.al"; |
166 | my($spath) = "$autodir/$modpname/$sname.al"; |
167 | unless(open(OUT, ">$lpath")){ |
168 | open(OUT, ">$spath") or die "Can't create $spath: $!\n"; |
169 | push(@names, $sname); |
170 | print " writing $spath (with truncated name)\n" |
171 | if ($Verbose>=1); |
172 | }else{ |
173 | push(@names, $lname); |
174 | print " writing $lpath\n" if ($Verbose>=2); |
175 | } |
176 | print OUT "# NOTE: Derived from $filename. ", |
177 | "Changes made here will be lost.\n"; |
178 | print OUT "package $package;\n\n"; |
179 | } |
180 | print OUT $_; |
181 | } |
182 | print OUT "1;\n"; |
183 | close(OUT); |
184 | close(IN); |
185 | |
186 | if (!$keep){ # don't keep any obsolete *.al files in the directory |
187 | my(%names); |
188 | @names{@names} = @names; |
189 | opendir(OUTDIR,"$autodir/$modpname"); |
190 | foreach(sort readdir(OUTDIR)){ |
191 | next unless /\.al$/; |
192 | my($subname) = m/(.*)\.al$/; |
193 | next if $names{substr($subname,0,$maxflen-3)}; |
194 | my($file) = "$autodir/$modpname/$_"; |
195 | print " deleting $file\n" if ($Verbose>=2); |
196 | unlink $file or carp "Unable to delete $file: $!"; |
197 | } |
198 | closedir(OUTDIR); |
199 | } |
200 | |
201 | open(TS,">$al_idx_file") or |
202 | carp "AutoSplit: unable to create timestamp file ($al_idx_file): $!"; |
203 | print TS "# Index created by AutoSplit for $filename (file acts as timestamp)\n"; |
204 | print TS map("sub $_ ;\n", @subnames); |
205 | close(TS); |
206 | |
207 | check_unique($package, $Maxlen, 1, @names); |
208 | |
209 | @names; |
210 | } |
211 | |
212 | |
213 | sub check_unique{ |
214 | my($module, $maxlen, $warn, @names) = @_; |
215 | my(%notuniq) = (); |
216 | my(%shorts) = (); |
217 | my(@toolong) = grep(length > $maxlen, @names); |
218 | |
219 | foreach(@toolong){ |
220 | my($trunc) = substr($_,0,$maxlen); |
221 | $notuniq{$trunc}=1 if $shorts{$trunc}; |
222 | $shorts{$trunc} = ($shorts{$trunc}) ? "$shorts{$trunc}, $_" : $_; |
223 | } |
224 | if (%notuniq && $warn){ |
225 | print "$module: some names are not unique when truncated to $maxlen characters:\n"; |
226 | foreach(keys %notuniq){ |
227 | print " $shorts{$_} truncate to $_\n"; |
228 | } |
229 | } |
230 | %notuniq; |
231 | } |
232 | |
233 | 1; |
234 | __END__ |
235 | |
236 | # test functions so AutoSplit.pm can be applied to itself: |
237 | sub test1{ "test 1\n"; } |
238 | sub test2{ "test 2\n"; } |
239 | sub test3{ "test 3\n"; } |
240 | sub test4{ "test 4\n"; } |
241 | |
242 | |