Integrate macperl patches #16926 and #16938;
[p5sagit/p5-mst-13.2.git] / lib / lib_pm.PL
CommitLineData
60ed1d8c 1use Config;
2use File::Basename qw(&basename &dirname);
3use File::Spec;
4use Cwd;
5
6my $origdir = cwd;
7chdir dirname($0);
8my $file = basename($0, '.PL');
4755096e 9$file =~ s!_(pm)$!.$1!i;
60ed1d8c 10
e9c6cca7 11my $useConfig;
12my $Config_archname;
13my $Config_version;
14my $Config_inc_version_list;
15
16# Expand the variables only if explicitly requested because
17# otherwise relocating Perl becomes much harder.
18
19if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
20 $useConfig = '';
21 $Config_archname = qq('$Config{archname}');
22 $Config_version = qq('$Config{version}');
23 my @Config_inc_version_list =
24 reverse split / /, $Config{inc_version_list};
25 $Config_inc_version_list =
26 @Config_inc_version_list ?
27 qq(@Config_inc_version_list) : q(());
28} else {
29 $useConfig = 'use Config;';
30 $Config_archname = q($Config{archname});
31 $Config_version = q($Config{version});
32 $Config_inc_version_list =
91a12f7d 33 q(reverse split / /, $Config{inc_version_list});
e9c6cca7 34}
60ed1d8c 35
36open OUT,">$file" or die "Can't create $file: $!";
37
38print "Extracting $file (with variable substitutions)\n";
39
40# In this section, perl variables will be expanded during extraction.
41# You can use $Config{...} to use Configure variables.
42
43print OUT <<"!GROK!THIS!";
e50aee73 44package lib;
45
427f4adb 46# THIS FILE IS AUTOMATICALLY GENERATED FROM lib_pm.PL.
47# ANY CHANGES TO THIS FILE WILL BE OVERWRITTEN BY THE NEXT PERL BUILD.
4633a7c4 48
e9c6cca7 49$useConfig
50
51my \$archname = $Config_archname;
52my \$version = $Config_version;
53my \@inc_version_list = $Config_inc_version_list;
60ed1d8c 54
55!GROK!THIS!
56print OUT <<'!NO!SUBS!';
4633a7c4 57
17f410f9 58our @ORIG_INC = @INC; # take a handy copy of 'original' value
59our $VERSION = '0.5564';
d5201bd2 60my $Is_MacOS = $^O eq 'MacOS';
61my $Mac_FS;
62if ($Is_MacOS) {
63 require File::Spec;
64 $Mac_FS = eval { require Mac::FileSpec::Unixish };
65}
e50aee73 66
67sub import {
68 shift;
aeb5d71d 69
70 my %names;
a5f75d66 71 foreach (reverse @_) {
016609bc 72 if ($_ eq '') {
af3dad46 73 require Carp;
774d564b 74 Carp::carp("Empty compile time value given to use lib");
af3dad46 75 }
d5201bd2 76
77 local $_ = _nativize($_);
78
20408e3c 79 if (-e && ! -d _) {
80 require Carp;
81 Carp::carp("Parameter to use lib must be directory, not file");
82 }
4633a7c4 83 unshift(@INC, $_);
d5201bd2 84 # Add any previous version directories we found at configure time
85 foreach my $invcer (@inc_version_list)
86 {
87 my $dir = $Is_MacOS
88 ? File::Spec->catdir( $_, $incver )
89 : "$_/$incver";
90 unshift(@INC, $dir) if -d $dir;
91 }
4633a7c4 92 # Put a corresponding archlib directory infront of $_ if it
93 # looks like $_ has an archlib directory below it.
d5201bd2 94 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
95 = _get_dirs($_);
96 unshift(@INC, $arch_dir) if -d $arch_auto_dir;
97 unshift(@INC, $version_dir) if -d $version_dir;
98 unshift(@INC, $version_arch_dir) if -d $version_arch_dir;
4633a7c4 99 }
abef537a 100
101 # remove trailing duplicates
102 @INC = grep { ++$names{$_} == 1 } @INC;
103 return;
e50aee73 104}
105
106
107sub unimport {
108 shift;
e50aee73 109
110 my %names;
aeb5d71d 111 foreach (@_) {
d5201bd2 112 local $_ = _nativize($_);
113
114 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
115 = _get_dirs($_);
4633a7c4 116 ++$names{$_};
d5201bd2 117 ++$names{$arch_dir} if -d $arch_auto_dir;
118 ++$names{$version_dir} if -d $version_dir;
119 ++$names{$version_arch_dir} if -d $version_arch_dir;
4633a7c4 120 }
e50aee73 121
aeb5d71d 122 # Remove ALL instances of each named directory.
123 @INC = grep { !exists $names{$_} } @INC;
abef537a 124 return;
e50aee73 125}
126
d5201bd2 127sub _get_dirs {
128 my($dir) = @_;
129 my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
130
131 # we could use this for all platforms in the future, but leave it
132 # Mac-only for now, until there is more time for testing it.
133 if ($Is_MacOS) {
134 $arch_auto_dir = File::Spec->catdir( $_, $archname, 'auto' );
135 $arch_dir = File::Spec->catdir( $_, $archname, );
136 $version_dir = File::Spec->catdir( $_, $version );
137 $version_arch_dir = File::Spec->catdir( $_, $version, $archname );
138 } else {
139 $arch_auto_dir = "$_/$archname/auto";
140 $arch_dir = "$_/$archname";
141 $version_dir = "$_/$version";
142 $version_arch_dir = "$_/$version/$archname";
143 }
144 return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
145}
146
147sub _nativize {
148 my($dir) = @_;
149
150 if ($Is_MacOS && $Mac_FS) {
151 $dir = Mac::FileSpec::Unixish::nativize($dir);
152 $dir .= ":" unless $dir =~ /:$/;
153 }
154
155 return $dir;
156}
157
4633a7c4 1581;
e50aee73 159__END__
160
161=head1 NAME
162
163lib - manipulate @INC at compile time
164
165=head1 SYNOPSIS
166
167 use lib LIST;
168
169 no lib LIST;
170
171=head1 DESCRIPTION
172
173This is a small simple module which simplifies the manipulation of @INC
174at compile time.
175
176It is typically used to add extra directories to perl's search path so
177that later C<use> or C<require> statements will find modules which are
178not located on perl's default search path.
179
aeb5d71d 180=head2 Adding directories to @INC
e50aee73 181
182The parameters to C<use lib> are added to the start of the perl search
183path. Saying
184
185 use lib LIST;
186
4633a7c4 187is I<almost> the same as saying
e50aee73 188
189 BEGIN { unshift(@INC, LIST) }
190
4633a7c4 191For each directory in LIST (called $dir here) the lib module also
192checks to see if a directory called $dir/$archname/auto exists.
193If so the $dir/$archname directory is assumed to be a corresponding
194architecture specific directory and is added to @INC in front of $dir.
195
aeb5d71d 196To avoid memory leaks, all trailing duplicate entries in @INC are
197removed.
4633a7c4 198
aeb5d71d 199=head2 Deleting directories from @INC
e50aee73 200
201You should normally only add directories to @INC. If you need to
202delete directories from @INC take care to only delete those which you
203added yourself or which you are certain are not needed by other modules
204in your script. Other modules may have added directories which they
205need for correct operation.
206
aeb5d71d 207The C<no lib> statement deletes all instances of each named directory
208from @INC.
e50aee73 209
4633a7c4 210For each directory in LIST (called $dir here) the lib module also
211checks to see if a directory called $dir/$archname/auto exists.
212If so the $dir/$archname directory is assumed to be a corresponding
213architecture specific directory and is also deleted from @INC.
214
aeb5d71d 215=head2 Restoring original @INC
e50aee73 216
217When the lib module is first loaded it records the current value of @INC
218in an array C<@lib::ORIG_INC>. To restore @INC to that value you
4633a7c4 219can say
e50aee73 220
221 @INC = @lib::ORIG_INC;
222
e7bf5e49 223=head1 CAVEATS
224
225In order to keep lib.pm small and simple, it only works with Unix
226filepaths. This doesn't mean it only works on Unix, but non-Unix
227users must first translate their file paths to Unix conventions.
228
229 # VMS users wanting to put [.stuff.moo] into
230 # their @INC would write
231 use lib 'stuff/moo';
e50aee73 232
d5201bd2 233=head1 NOTES
234
235In the future, this module will likely use File::Spec for determining
236paths, as it does now for Mac OS (where Unix-style or Mac-style paths
237work, and Unix-style paths are converted properly to Mac-style paths
238before being added to @INC).
239
e50aee73 240=head1 SEE ALSO
241
af3dad46 242FindBin - optional module which deals with paths relative to the source file.
e50aee73 243
244=head1 AUTHOR
245
246Tim Bunce, 2nd June 1995.
247
248=cut
60ed1d8c 249!NO!SUBS!
250
251close OUT or die "Can't close $file: $!";
252chdir $origdir;