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