Commit | Line | Data |
61edc683 |
1 | #!./miniperl |
a2f19a19 |
2 | use strict; |
3 | use warnings; |
b8d39eba |
4 | use Config; |
c3ef65fb |
5 | BEGIN { |
9ddff148 |
6 | unshift @INC, $^O eq 'MSWin32' ? '../ext/Cwd' : 'ext/Cwd'; |
c3ef65fb |
7 | } |
286d62c2 |
8 | use Cwd; |
75f92628 |
9 | |
392ef80c |
10 | # To clarify, this isn't the entire suite of modules considered "toolchain" |
11 | # It's not even all modules needed to build ext/ |
12 | # It's just the source paths of the (minimum complete set of) modules in ext/ |
13 | # needed to build the nonxs modules |
14 | # After which, all nonxs modules are in lib, which was always sufficient to |
15 | # allow miniperl to build everything else. |
16 | |
403f501d |
17 | my @toolchain = qw(ext/constant/lib ext/Cwd ext/ExtUtils-Command/lib |
b78fd716 |
18 | ext/ExtUtils-Install/lib ext/ExtUtils-MakeMaker/lib |
1a7ec96d |
19 | ext/ExtUtils-Manifest/lib ext/Text-ParseWords/lib |
4677aef7 |
20 | ext/File-Path/lib ext/AutoLoader/lib); |
f345288b |
21 | |
a0d0e21e |
22 | # This script acts as a simple interface for building extensions. |
286d62c2 |
23 | |
24 | # It's actually a cut and shut of the Unix version ext/utils/makeext and the |
25 | # Windows version win32/build_ext.pl hence the two invocation styles. |
26 | |
27 | # On Unix, it primarily used by the perl Makefile one extention at a time: |
a0d0e21e |
28 | # |
29 | # d_dummy $(dynamic_ext): miniperl preplibrary FORCE |
e2fabae1 |
30 | # @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL) |
a0d0e21e |
31 | # |
902aaf3e |
32 | # On Windows or VMS, |
286d62c2 |
33 | # If '--static' is specified, static extensions will be built. |
a34ce875 |
34 | # If '--dynamic' is specified, dynamic extensions will be built. |
35 | # If '--nonxs' is specified, nonxs extensions will be built. |
281da5ea |
36 | # If '--dynaloader' is specificied, DynaLoader will be built. |
286d62c2 |
37 | # If '--all' is specified, all extensions will be built. |
38 | # |
39 | # make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1 |
40 | # |
41 | # E.g. |
42 | # |
43 | # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext |
44 | # |
45 | # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean |
46 | # |
47 | # make_ext.pl MAKE=dmake --dir=..\ext |
48 | # |
49 | # make_ext.pl MAKE=dmake --dir=..\ext --target=clean |
50 | # |
51 | # Will skip building extensions which are marked with an '!' char. |
52 | # Mostly because they still not ported to specified platform. |
53 | # |
54 | # If any extensions are listed with a '+' char then only those |
55 | # extensions will be built, but only if they arent countermanded |
56 | # by an '!ext' and are appropriate to the type of building being done. |
57 | |
a0d0e21e |
58 | # It may be deleted in a later release of perl so try to |
59 | # avoid using it for other purposes. |
60 | |
e3b84025 |
61 | my $is_Win32 = $^O eq 'MSWin32'; |
62 | my $is_VMS = $^O eq 'VMS'; |
63 | my $is_Unix = !$is_Win32 && !$is_VMS; |
64 | |
286d62c2 |
65 | require FindExt if $is_Win32; |
66 | |
fc678412 |
67 | my (%excl, %incl, %opts, @extspec, @pass_through); |
97a26ad9 |
68 | |
69 | foreach (@ARGV) { |
70 | if (/^!(.*)$/) { |
71 | $excl{$1} = 1; |
72 | } elsif (/^\+(.*)$/) { |
73 | $incl{$1} = 1; |
74 | } elsif (/^--([\w\-]+)$/) { |
75 | $opts{$1} = 1; |
e2fabae1 |
76 | } elsif (/^--([\w\-]+)=(.*)$/) { |
77 | $opts{$1} = $2; |
e2fabae1 |
78 | } elsif (/=/) { |
fc678412 |
79 | push @pass_through, $_; |
ca5de986 |
80 | } elsif (length) { |
e2fabae1 |
81 | push @extspec, $_; |
97a26ad9 |
82 | } |
83 | } |
84 | |
286d62c2 |
85 | my $static = $opts{static} || $opts{all}; |
86 | my $dynamic = $opts{dynamic} || $opts{all}; |
a34ce875 |
87 | my $nonxs = $opts{nonxs} || $opts{all}; |
281da5ea |
88 | my $dynaloader = $opts{dynaloader} || $opts{all}; |
286d62c2 |
89 | |
ca5de986 |
90 | # The Perl Makefile.SH will expand all extensions to |
91 | # lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested) |
92 | # A user wishing to run make_ext might use |
93 | # X (or X/Y or X::Y if nested) |
94 | |
95 | # canonise into X/Y form (pname) |
96 | |
97 | foreach (@extspec) { |
7ad017a8 |
98 | if (s{^lib/auto/}{}) { |
ca5de986 |
99 | # Remove lib/auto prefix and /*.* suffix |
c5aac6ab |
100 | s{/[^/]+\.[^/]+$}{}; |
7ad017a8 |
101 | } elsif (s{^ext/}{}) { |
ca5de986 |
102 | # Remove ext/ prefix and /pm_to_blib suffix |
ca5de986 |
103 | s{/pm_to_blib$}{}; |
57df1c46 |
104 | # Targets are given as files on disk, but the extension spec is still |
105 | # written using /s for each :: |
106 | tr!-!/!; |
7ad017a8 |
107 | } elsif (s{::}{\/}g) { |
ca5de986 |
108 | # Convert :: to / |
7ad017a8 |
109 | } else { |
ca5de986 |
110 | s/\..*o//; |
111 | } |
112 | } |
113 | |
fc678412 |
114 | my $makecmd = shift @pass_through; # Should be something like MAKE=make |
115 | unshift @pass_through, 'PERL_CORE=1'; |
116 | |
286d62c2 |
117 | my $dir = $opts{dir} || 'ext'; |
e2fabae1 |
118 | my $target = $opts{target}; |
07f3cc2a |
119 | $target = 'all' unless defined $target; |
a0d0e21e |
120 | |
fb73857a |
121 | # Previously, $make was taken from config.sh. However, the user might |
122 | # instead be running a possibly incompatible make. This might happen if |
123 | # the user types "gmake" instead of a plain "make", for example. The |
124 | # correct current value of MAKE will come through from the main perl |
125 | # makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in |
126 | # case third party users of this script (are there any?) don't have the |
127 | # MAKE=$(MAKE) argument, which was added after 5.004_03. |
ca5de986 |
128 | unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) { |
129 | die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n"; |
a2f19a19 |
130 | } |
131 | |
eb1f8df7 |
132 | # This isn't going to cope with anything fancy, such as spaces inside command |
133 | # names, but neither did what it replaced. Once there is a use case that needs |
134 | # it, please supply patches. Until then, I'm sticking to KISS |
135 | my @make = split ' ', $1 || $Config{make} || $ENV{MAKE}; |
ca5de986 |
136 | # Using an array of 0 or 1 elements makes the subsequent code simpler. |
fc678412 |
137 | my @run = $Config{run}; |
138 | @run = () if not defined $run[0] or $run[0] eq ''; |
a2f19a19 |
139 | |
a0d0e21e |
140 | |
07f3cc2a |
141 | if ($target eq '') { |
ca5de986 |
142 | die "make_ext: no make target specified (eg all or clean)\n"; |
143 | } elsif ($target !~ /(?:^all|clean)$/) { |
144 | # for the time being we are strict about what make_ext is used for |
145 | die "$0: unknown make target '$target'\n"; |
a2f19a19 |
146 | } |
147 | |
281da5ea |
148 | if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) { |
286d62c2 |
149 | die "$0: no extension specified\n"; |
150 | } |
151 | |
152 | my $perl; |
153 | my %extra_passthrough; |
154 | |
155 | if ($is_Win32) { |
156 | (my $here = getcwd()) =~ s{/}{\\}g; |
157 | $perl = $^X; |
158 | if ($perl =~ m#^\.\.#) { |
159 | $perl = "$here\\$perl"; |
160 | } |
161 | (my $topdir = $perl) =~ s/\\[^\\]+$//; |
162 | # miniperl needs to find perlglob and pl2bat |
163 | $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}"; |
164 | my $pl2bat = "$topdir\\win32\\bin\\pl2bat"; |
165 | unless (-f "$pl2bat.bat") { |
166 | my @args = ($perl, ("$pl2bat.pl") x 2); |
167 | print "@args\n"; |
168 | system(@args) unless defined $::Cross::platform; |
169 | } |
170 | |
171 | print "In ", getcwd(); |
172 | chdir($dir) || die "Cannot cd to $dir\n"; |
173 | (my $ext = getcwd()) =~ s{/}{\\}g; |
174 | FindExt::scan_ext($ext); |
175 | FindExt::set_static_extensions(split ' ', $Config{static_ext}); |
176 | |
177 | my @ext; |
178 | push @ext, FindExt::static_ext() if $static; |
a34ce875 |
179 | push @ext, FindExt::dynamic_ext() if $dynamic; |
180 | push @ext, FindExt::nonxs_ext() if $nonxs; |
281da5ea |
181 | push @ext, 'DynaLoader' if $dynaloader; |
286d62c2 |
182 | |
183 | foreach (sort @ext) { |
184 | if (%incl and !exists $incl{$_}) { |
185 | #warn "Skipping extension $ext\\$_, not in inclusion list\n"; |
186 | next; |
187 | } |
188 | if (exists $excl{$_}) { |
189 | warn "Skipping extension $ext\\$_, not ported to current platform"; |
190 | next; |
191 | } |
192 | push @extspec, $_; |
281da5ea |
193 | if($_ eq 'DynaLoader') { |
194 | # No, we don't know why nmake can't work out the dependency chain |
195 | push @{$extra_passthrough{$_}}, 'DynaLoader.c'; |
196 | } elsif(FindExt::is_static($_)) { |
286d62c2 |
197 | push @{$extra_passthrough{$_}}, 'LINKTYPE=static'; |
198 | } |
199 | } |
200 | chdir '..'; # now in the Perl build directory |
201 | } |
902aaf3e |
202 | elsif ($is_VMS) { |
203 | $perl = $^X; |
204 | push @extspec, (split ' ', $Config{static_ext}) if $static; |
205 | push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic; |
a34ce875 |
206 | push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs; |
281da5ea |
207 | push @extspec, 'DynaLoader' if $dynaloader; |
902aaf3e |
208 | } |
286d62c2 |
209 | |
dc0655f7 |
210 | { |
211 | # Cwd needs to be built before Encode recurses into subdirectories. |
212 | # This seems to be the simplest way to ensure this ordering: |
213 | my (@first, @other); |
214 | foreach (@extspec) { |
215 | if ($_ eq 'Cwd') { |
216 | push @first, $_; |
217 | } else { |
218 | push @other, $_; |
219 | } |
220 | } |
221 | @extspec = (@first, @other); |
222 | } |
223 | |
e08c66ce |
224 | foreach my $spec (@extspec) { |
225 | my $mname = $spec; |
ca5de986 |
226 | $mname =~ s!/!::!g; |
238a6851 |
227 | my $ext_pathname; |
228 | if (-d "ext/$spec") { |
229 | # Old style ext/Data/Dumper/ |
230 | $ext_pathname = "ext/$spec"; |
231 | } else { |
232 | # New style ext/Data-Dumper/ |
233 | my $copy = $spec; |
234 | $copy =~ tr!/!-!; |
235 | $ext_pathname = "ext/$copy"; |
236 | } |
a2f19a19 |
237 | |
ca5de986 |
238 | if ($Config{osname} eq 'catamount') { |
a2f19a19 |
239 | # Snowball's chance of building extensions. |
ca5de986 |
240 | die "This is $Config{osname}, not building $mname, sorry.\n"; |
241 | } |
a2f19a19 |
242 | |
ca5de986 |
243 | print "\tMaking $mname ($target)\n"; |
a2f19a19 |
244 | |
acb65a20 |
245 | build_extension($ext_pathname, $perl, $mname, |
e08c66ce |
246 | [@pass_through, @{$extra_passthrough{$spec} || []}]); |
ca5de986 |
247 | } |
a0d0e21e |
248 | |
fc678412 |
249 | sub build_extension { |
acb65a20 |
250 | my ($ext_dir, $perl, $mname, $pass_through) = @_; |
251 | |
252 | my $up = $ext_dir; |
253 | $up =~ s![^/]+!..!g; |
254 | |
255 | $perl ||= "$up/miniperl"; |
256 | my $return_dir = $up; |
257 | my $lib_dir = "$up/lib"; |
bc72ff49 |
258 | # $lib_dir must be last, as we're copying files into it, and in a parallel |
259 | # make there's a race condition if one process tries to open a module that |
260 | # another process has half-written. |
f345288b |
261 | $ENV{PERL5LIB} |
bc72ff49 |
262 | = join $Config{path_sep}, (map {"$up/$_"} @toolchain), $lib_dir; |
acb65a20 |
263 | |
fc678412 |
264 | unless (chdir "$ext_dir") { |
265 | warn "Cannot cd to $ext_dir: $!"; |
266 | return; |
267 | } |
902aaf3e |
268 | my $makefile; |
269 | if ($is_VMS) { |
270 | $makefile = 'descrip.mms'; |
271 | if ($target =~ /clean$/ |
272 | && !-f $makefile |
273 | && -f "${makefile}_old") { |
274 | $makefile = "${makefile}_old"; |
275 | } |
276 | } else { |
277 | $makefile = 'Makefile'; |
278 | } |
fc678412 |
279 | |
902aaf3e |
280 | if (!-f $makefile) { |
e74f76b2 |
281 | if (!-f 'Makefile.PL') { |
282 | print "\nCreating Makefile.PL in $ext_dir for $mname\n"; |
283 | # We need to cope well with various possible layouts |
284 | my @dirs = split /::/, $mname; |
285 | my $leaf = pop @dirs; |
286 | my $leafname = "$leaf.pm"; |
287 | my $pathname = join '/', @dirs, $leafname; |
288 | my @locations = ($leafname, $pathname, "lib/$pathname"); |
289 | my $fromname; |
290 | foreach (@locations) { |
291 | if (-f $_) { |
292 | $fromname = $_; |
293 | last; |
294 | } |
295 | } |
296 | |
297 | unless ($fromname) { |
298 | die "For $mname tried @locations in in $ext_dir but can't find source"; |
299 | } |
300 | open my $fh, '>', 'Makefile.PL' |
301 | or die "Can't open Makefile.PL for writing: $!"; |
302 | print $fh <<"EOM"; |
303 | #-*- buffer-read-only: t -*- |
304 | |
305 | # This Makefile.PL was written by $0. |
306 | # It will be deleted automatically by make realclean |
307 | |
308 | use strict; |
309 | use ExtUtils::MakeMaker; |
310 | |
311 | WriteMakefile( |
312 | NAME => '$mname', |
313 | VERSION_FROM => '$fromname', |
314 | ABSTRACT_FROM => '$fromname', |
315 | realclean => {FILES => 'Makefile.PL'}, |
316 | ); |
317 | |
318 | # ex: set ro: |
319 | EOM |
320 | close $fh or die "Can't close Makefile.PL: $!"; |
321 | } |
fc678412 |
322 | print "\nRunning Makefile.PL in $ext_dir\n"; |
323 | |
324 | # Presumably this can be simplified |
325 | my @cross; |
326 | if (defined $::Cross::platform) { |
327 | # Inherited from win32/buildext.pl |
328 | @cross = "-MCross=$::Cross::platform"; |
329 | } elsif ($opts{cross}) { |
330 | # Inherited from make_ext.pl |
331 | @cross = '-MCross'; |
a2f19a19 |
332 | } |
fc678412 |
333 | |
73402eba |
334 | my @args = (@cross, 'Makefile.PL'); |
902aaf3e |
335 | if ($is_VMS) { |
336 | my $libd = VMS::Filespec::vmspath($lib_dir); |
337 | push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd"; |
338 | } else { |
a2b175af |
339 | push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none', |
340 | 'INSTALLMAN3DIR=none'; |
902aaf3e |
341 | } |
342 | push @args, @$pass_through; |
343 | _quote_args(\@args) if $is_VMS; |
344 | print join(' ', @run, $perl, @args), "\n"; |
345 | my $code = system @run, $perl, @args; |
fc678412 |
346 | warn "$code from $ext_dir\'s Makefile.PL" if $code; |
347 | |
61edc683 |
348 | # Right. The reason for this little hack is that we're sitting inside |
349 | # a program run by ./miniperl, but there are tasks we need to perform |
350 | # when the 'realclean', 'distclean' or 'veryclean' targets are run. |
351 | # Unfortunately, they can be run *after* 'clean', which deletes |
352 | # ./miniperl |
353 | # So we do our best to leave a set of instructions identical to what |
354 | # we would do if we are run directly as 'realclean' etc |
355 | # Whilst we're perfect, unfortunately the targets we call are not, as |
356 | # some of them rely on a $(PERL) for their own distclean targets. |
357 | # But this always used to be a problem with the old /bin/sh version of |
358 | # this. |
e3b84025 |
359 | if ($is_Unix) { |
fc678412 |
360 | my $suffix = '.sh'; |
361 | foreach my $clean_target ('realclean', 'veryclean') { |
ca5de986 |
362 | my $file = "$return_dir/$clean_target$suffix"; |
fc678412 |
363 | open my $fh, '>>', $file or die "open $file: $!"; |
364 | # Quite possible that we're being run in parallel here. |
365 | # Can't use Fcntl this early to get the LOCK_EX |
366 | flock $fh, 2 or warn "flock $file: $!"; |
367 | print $fh <<"EOS"; |
368 | cd $ext_dir |
369 | if test ! -f Makefile -a -f Makefile.old; then |
61edc683 |
370 | echo "Note: Using Makefile.old" |
eb1f8df7 |
371 | make -f Makefile.old $clean_target MAKE='@make' @pass_through |
61edc683 |
372 | else |
fc678412 |
373 | if test ! -f Makefile ; then |
61edc683 |
374 | echo "Warning: No Makefile!" |
375 | fi |
eb1f8df7 |
376 | make $clean_target MAKE='@make' @pass_through |
61edc683 |
377 | fi |
fc678412 |
378 | cd $return_dir |
61edc683 |
379 | EOS |
fc678412 |
380 | close $fh or die "close $file: $!"; |
381 | } |
61edc683 |
382 | } |
fc678412 |
383 | } |
a2f19a19 |
384 | |
902aaf3e |
385 | if (not -f $makefile) { |
a2f19a19 |
386 | print "Warning: No Makefile!\n"; |
fc678412 |
387 | } |
a2f19a19 |
388 | |
902aaf3e |
389 | if ($is_VMS) { |
390 | _macroify_passthrough($pass_through); |
391 | unshift @$pass_through, "/DESCRIPTION=$makefile"; |
392 | } |
393 | |
fc678412 |
394 | if (!$target or $target !~ /clean$/) { |
a2f19a19 |
395 | # Give makefile an opportunity to rewrite itself. |
75f92628 |
396 | # reassure users that life goes on... |
902aaf3e |
397 | my @args = ('config', @$pass_through); |
398 | _quote_args(\@args) if $is_VMS; |
399 | system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n"; |
fc678412 |
400 | } |
902aaf3e |
401 | my @targ = ($target, @$pass_through); |
402 | _quote_args(\@targ) if $is_VMS; |
403 | print "Making $target in $ext_dir\n@run @make @targ\n"; |
404 | my $code = system(@run, @make, @targ); |
fc678412 |
405 | die "Unsuccessful make($ext_dir): code=$code" if $code != 0; |
a2f19a19 |
406 | |
fc678412 |
407 | chdir $return_dir || die "Cannot cd to $return_dir: $!"; |
408 | } |
902aaf3e |
409 | |
410 | sub _quote_args { |
411 | my $args = shift; # must be array reference |
412 | |
413 | # Do not quote qualifiers that begin with '/'. |
414 | map { if (!/^\//) { |
415 | $_ =~ s/\"/""/g; # escape C<"> by doubling |
416 | $_ = q(").$_.q("); |
417 | } |
418 | } @{$args} |
419 | ; |
420 | } |
421 | |
422 | sub _macroify_passthrough { |
423 | my $passthrough = shift; |
424 | _quote_args($passthrough); |
425 | my $macro = '/MACRO=(' . join(',',@$passthrough) . ')'; |
426 | @$passthrough = (); |
427 | @$passthrough[0] = $macro; |
428 | } |