c12570828f73424049bc7ff43e68c989b1b50311
[p5sagit/p5-mst-13.2.git] / lib / lib_pm.PL
1 use Config;
2 use File::Basename qw(&basename &dirname);
3 use File::Spec;
4 use Cwd;
5
6 my $origdir = cwd;
7 chdir dirname($0);
8 my $file = basename($0, '.PL');
9 $file =~ s!_(pm)$!.$1!i;
10
11 my $useConfig;
12 my $Config_archname;
13 my $Config_version;
14 my $Config_inc_version_list;
15
16 # Expand the variables only if explicitly requested
17 # or if a previously installed lib.pm does this, too
18 # because otherwise relocating Perl becomes much harder.
19
20 my $expand_config_vars = 0;
21 if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
22   $expand_config_vars = 1;
23 }
24 elsif (exists $ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
25   $expand_config_vars = 0;
26 }
27 else {
28   eval <<'HERE';
29   require lib;
30   my $lib_file = $INC{"lib.pm"};
31   open my $fh, '<', $lib_file
32     or die "Could not open file '$lib_file' for reading: $!";
33   my $ConfigRegex = qr/(?:use|require)\s+Config(?:\s+|;)/;
34   while (defined($_ = <$fh>)) {
35     # crude heuristics to check that we were using Config
36     if (/^\s*$ConfigRegex/ || /^\s*eval.*$ConfigRegex/) {
37       $expand_config_vars = 0;
38       last;
39     }
40   }
41   $expand_config_vars = 1;
42 HERE
43   $expand_config_vars = 0 if $@;
44 }
45
46 if ($expand_config_vars) {
47     $useConfig = '';
48     $Config_archname = qq('$Config{archname}');
49     $Config_version  = qq('$Config{version}');
50     my @Config_inc_version_list =
51         reverse split / /, $Config{inc_version_list};
52     $Config_inc_version_list =
53         @Config_inc_version_list ?
54             qq(@Config_inc_version_list) : q(());
55 } else {
56     $useConfig = 'use Config;';
57     $Config_archname = q($Config{archname});
58     $Config_version  = q($Config{version});
59     $Config_inc_version_list =
60               q(reverse split / /, $Config{inc_version_list});
61 }
62  
63 open OUT,">$file" or die "Can't create $file: $!";
64  
65 print "Extracting $file (with variable substitutions)\n";
66  
67 # In this section, perl variables will be expanded during extraction.
68 # You can use $Config{...} to use Configure variables.
69  
70 print OUT <<"!GROK!THIS!";
71 package lib;
72
73 # THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL.
74 # ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD.
75
76 $useConfig
77
78 use strict;
79
80 my \$archname         = $Config_archname;
81 my \$version          = $Config_version;
82 my \@inc_version_list = $Config_inc_version_list;
83
84 !GROK!THIS!
85 print OUT <<'!NO!SUBS!';
86
87 our @ORIG_INC = @INC;   # take a handy copy of 'original' value
88 our $VERSION = '0.57';
89 my $Is_MacOS = $^O eq 'MacOS';
90 my $Mac_FS;
91 if ($Is_MacOS) {
92         require File::Spec;
93         $Mac_FS = eval { require Mac::FileSpec::Unixish };
94 }
95
96 sub import {
97     shift;
98
99     my %names;
100     foreach (reverse @_) {
101         my $path = $_;          # we'll be modifying it, so break the alias
102         if ($path eq '') {
103             require Carp;
104             Carp::carp("Empty compile time value given to use lib");
105         }
106
107         $path = _nativize($path);
108
109         if ($path !~ /\.par$/i && -e $path && ! -d _) {
110             require Carp;
111             Carp::carp("Parameter to use lib must be directory, not file");
112         }
113         unshift(@INC, $path);
114         # Add any previous version directories we found at configure time
115         foreach my $incver (@inc_version_list)
116         {
117             my $dir = $Is_MacOS
118                 ? File::Spec->catdir( $path, $incver )
119                 : "$path/$incver";
120             unshift(@INC, $dir) if -d $dir;
121         }
122         # Put a corresponding archlib directory in front of $path if it
123         # looks like $path has an archlib directory below it.
124         my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
125             = _get_dirs($path);
126         unshift(@INC, $arch_dir)         if -d $arch_auto_dir;
127         unshift(@INC, $version_dir)      if -d $version_dir;
128         unshift(@INC, $version_arch_dir) if -d $version_arch_dir;
129     }
130
131     # remove trailing duplicates
132     @INC = grep { ++$names{$_} == 1 } @INC;
133     return;
134 }
135
136
137 sub unimport {
138     shift;
139
140     my %names;
141     foreach (@_) {
142         my $path = _nativize($_);
143
144         my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
145             = _get_dirs($path);
146         ++$names{$path};
147         ++$names{$arch_dir}         if -d $arch_auto_dir;
148         ++$names{$version_dir}      if -d $version_dir;
149         ++$names{$version_arch_dir} if -d $version_arch_dir;
150     }
151
152     # Remove ALL instances of each named directory.
153     @INC = grep { !exists $names{$_} } @INC;
154     return;
155 }
156
157 sub _get_dirs {
158     my($dir) = @_;
159     my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
160
161     # we could use this for all platforms in the future, but leave it
162     # Mac-only for now, until there is more time for testing it.
163     if ($Is_MacOS) {
164         $arch_auto_dir    = File::Spec->catdir( $dir, $archname, 'auto' );
165         $arch_dir         = File::Spec->catdir( $dir, $archname, );
166         $version_dir      = File::Spec->catdir( $dir, $version );
167         $version_arch_dir = File::Spec->catdir( $dir, $version, $archname );
168     } else {
169         $arch_auto_dir    = "$dir/$archname/auto";
170         $arch_dir         = "$dir/$archname";
171         $version_dir      = "$dir/$version";
172         $version_arch_dir = "$dir/$version/$archname";
173     }
174     return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
175 }
176
177 sub _nativize {
178     my($dir) = @_;
179
180     if ($Is_MacOS && $Mac_FS && ! -d $dir) {
181         $dir = Mac::FileSpec::Unixish::nativize($dir);
182         $dir .= ":" unless $dir =~ /:$/;
183     }
184
185     return $dir;
186 }
187
188 1;
189 __END__
190
191 =head1 NAME
192
193 lib - manipulate @INC at compile time
194
195 =head1 SYNOPSIS
196
197     use lib LIST;
198
199     no lib LIST;
200
201 =head1 DESCRIPTION
202
203 This is a small simple module which simplifies the manipulation of @INC
204 at compile time.
205
206 It is typically used to add extra directories to perl's search path so
207 that later C<use> or C<require> statements will find modules which are
208 not located on perl's default search path.
209
210 =head2 Adding directories to @INC
211
212 The parameters to C<use lib> are added to the start of the perl search
213 path. Saying
214
215     use lib LIST;
216
217 is I<almost> the same as saying
218
219     BEGIN { unshift(@INC, LIST) }
220
221 For each directory in LIST (called $dir here) the lib module also
222 checks to see if a directory called $dir/$archname/auto exists.
223 If so the $dir/$archname directory is assumed to be a corresponding
224 architecture specific directory and is added to @INC in front of $dir.
225
226 The current value of C<$archname> can be found with this command:
227
228     perl -V:archname
229
230 To avoid memory leaks, all trailing duplicate entries in @INC are
231 removed.
232
233 =head2 Deleting directories from @INC
234
235 You should normally only add directories to @INC.  If you need to
236 delete directories from @INC take care to only delete those which you
237 added yourself or which you are certain are not needed by other modules
238 in your script.  Other modules may have added directories which they
239 need for correct operation.
240
241 The C<no lib> statement deletes all instances of each named directory
242 from @INC.
243
244 For each directory in LIST (called $dir here) the lib module also
245 checks to see if a directory called $dir/$archname/auto exists.
246 If so the $dir/$archname directory is assumed to be a corresponding
247 architecture specific directory and is also deleted from @INC.
248
249 =head2 Restoring original @INC
250
251 When the lib module is first loaded it records the current value of @INC
252 in an array C<@lib::ORIG_INC>. To restore @INC to that value you
253 can say
254
255     @INC = @lib::ORIG_INC;
256
257 =head1 CAVEATS
258
259 In order to keep lib.pm small and simple, it only works with Unix
260 filepaths.  This doesn't mean it only works on Unix, but non-Unix
261 users must first translate their file paths to Unix conventions.
262
263     # VMS users wanting to put [.stuff.moo] into 
264     # their @INC would write
265     use lib 'stuff/moo';
266
267 =head1 NOTES
268
269 In the future, this module will likely use File::Spec for determining
270 paths, as it does now for Mac OS (where Unix-style or Mac-style paths
271 work, and Unix-style paths are converted properly to Mac-style paths
272 before being added to @INC).
273
274 If you try to add a file to @INC as follows:
275
276   use lib 'this_is_a_file.txt';
277
278 C<lib> will warn about this. The sole exceptions are files with the
279 C<.par> extension which are intended to be used as libraries.
280
281 =head1 SEE ALSO
282
283 FindBin - optional module which deals with paths relative to the source file.
284
285 PAR - optional module which can treat C<.par> files as Perl libraries.
286
287 =head1 AUTHOR
288
289 Tim Bunce, 2nd June 1995.
290
291 C<lib> is maintained by the perl5-porters. Please direct
292 any questions to the canonical mailing list. Anything that
293 is applicable to the CPAN release can be sent to its maintainer,
294 though.
295
296 Maintainer: The Perl5-Porters <perl5-porters@perl.org>
297
298 Maintainer of the CPAN release: Steffen Mueller <smueller@cpan.org>
299
300 =head1 COPYRIGHT AND LICENSE
301
302 This package has been part of the perl core since perl 5.001.
303 It has been released separately to CPAN so older installations
304 can benefit from bug fixes.
305
306 This package has the same copyright and license as the perl core.
307
308 =cut
309 !NO!SUBS!
310
311 close OUT or die "Can't close $file: $!";
312 chdir $origdir;