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