7 # This script acts as a simple interface for building extensions.
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.
12 # On Unix, it primarily used by the perl Makefile one extention at a time:
14 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
15 # @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
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.
22 # make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1
26 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext
28 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean
30 # make_ext.pl MAKE=dmake --dir=..\ext
32 # make_ext.pl MAKE=dmake --dir=..\ext --target=clean
34 # Will skip building extensions which are marked with an '!' char.
35 # Mostly because they still not ported to specified platform.
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.
41 # It may be deleted in a later release of perl so try to
42 # avoid using it for other purposes.
44 my $is_Win32 = $^O eq 'MSWin32';
45 my $is_VMS = $^O eq 'VMS';
46 my $is_Unix = !$is_Win32 && !$is_VMS;
48 require FindExt if $is_Win32;
50 my (%excl, %incl, %opts, @extspec, @pass_through);
55 } elsif (/^\+(.*)$/) {
57 } elsif (/^--([\w\-]+)$/) {
59 } elsif (/^--([\w\-]+)=(.*)$/) {
62 push @pass_through, $_;
68 my $static = $opts{static} || $opts{all};
69 my $dynamic = $opts{dynamic} || $opts{all};
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)
76 # canonise into X/Y form (pname)
79 if (s{^lib/auto/}{}) {
80 # Remove lib/auto prefix and /*.* suffix
82 } elsif (s{^ext/}{}) {
83 # Remove ext/ prefix and /pm_to_blib suffix
85 } elsif (s{::}{\/}g) {
92 my $makecmd = shift @pass_through; # Should be something like MAKE=make
93 unshift @pass_through, 'PERL_CORE=1';
95 my $dir = $opts{dir} || 'ext';
96 my $target = $opts{target};
97 $target = 'all' unless defined $target;
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.
106 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
107 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
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};
114 # Using an array of 0 or 1 elements makes the subsequent code simpler.
115 my @run = $Config{run};
116 @run = () if not defined $run[0] or $run[0] eq '';
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";
126 if (!@extspec and !$static and !$dynamic) {
127 die "$0: no extension specified\n";
131 my %extra_passthrough;
134 (my $here = getcwd()) =~ s{/}{\\}g;
136 if ($perl =~ m#^\.\.#) {
137 $perl = "$here\\$perl";
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);
146 system(@args) unless defined $::Cross::platform;
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});
156 push @ext, FindExt::static_ext() if $static;
157 push @ext, FindExt::dynamic_ext(), FindExt::nonxs_ext() if $dynamic;
159 foreach (sort @ext) {
160 if (%incl and !exists $incl{$_}) {
161 #warn "Skipping extension $ext\\$_, not in inclusion list\n";
164 if (exists $excl{$_}) {
165 warn "Skipping extension $ext\\$_, not ported to current platform";
169 if(FindExt::is_static($_)) {
170 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
173 chdir '..'; # now in the Perl build directory
177 push @extspec, (split ' ', $Config{static_ext}) if $static;
178 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
181 foreach my $spec (@extspec) {
185 if (-d "ext/$spec") {
186 # Old style ext/Data/Dumper/
187 $ext_pathname = "ext/$spec";
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);
193 # New style ext/Data-Dumper/
196 $ext_pathname = "ext/$copy";
198 my $up = $ext_pathname;
201 if ($Config{osname} eq 'catamount') {
202 # Snowball's chance of building extensions.
203 die "This is $Config{osname}, not building $mname, sorry.\n";
206 print "\tMaking $mname ($target)\n";
208 build_extension('ext', $ext_pathname, $up, $perl || "$up/miniperl",
210 [@pass_through, @{$extra_passthrough{$spec} || []}]);
213 sub build_extension {
214 my ($ext, $ext_dir, $return_dir, $perl, $lib_dir, $pass_through) = @_;
215 unless (chdir "$ext_dir") {
216 warn "Cannot cd to $ext_dir: $!";
221 $makefile = 'descrip.mms';
222 if ($target =~ /clean$/
224 && -f "${makefile}_old") {
225 $makefile = "${makefile}_old";
228 $makefile = 'Makefile';
232 print "\nRunning Makefile.PL in $ext_dir\n";
234 # Presumably this can be simplified
236 if (defined $::Cross::platform) {
237 # Inherited from win32/buildext.pl
238 @cross = "-MCross=$::Cross::platform";
239 } elsif ($opts{cross}) {
240 # Inherited from make_ext.pl
244 my @args = ("-I$lib_dir", @cross, 'Makefile.PL');
246 my $libd = VMS::Filespec::vmspath($lib_dir);
247 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
249 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN3DIR=none';
251 push @args, @$pass_through;
252 _quote_args(\@args) if $is_VMS;
253 print join(' ', @run, $perl, @args), "\n";
254 my $code = system @run, $perl, @args;
255 warn "$code from $ext_dir\'s Makefile.PL" if $code;
257 # Right. The reason for this little hack is that we're sitting inside
258 # a program run by ./miniperl, but there are tasks we need to perform
259 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
260 # Unfortunately, they can be run *after* 'clean', which deletes
262 # So we do our best to leave a set of instructions identical to what
263 # we would do if we are run directly as 'realclean' etc
264 # Whilst we're perfect, unfortunately the targets we call are not, as
265 # some of them rely on a $(PERL) for their own distclean targets.
266 # But this always used to be a problem with the old /bin/sh version of
270 foreach my $clean_target ('realclean', 'veryclean') {
271 my $file = "$return_dir/$clean_target$suffix";
272 open my $fh, '>>', $file or die "open $file: $!";
273 # Quite possible that we're being run in parallel here.
274 # Can't use Fcntl this early to get the LOCK_EX
275 flock $fh, 2 or warn "flock $file: $!";
278 if test ! -f Makefile -a -f Makefile.old; then
279 echo "Note: Using Makefile.old"
280 make -f Makefile.old $clean_target MAKE='@make' @pass_through
282 if test ! -f Makefile ; then
283 echo "Warning: No Makefile!"
285 make $clean_target MAKE='@make' @pass_through
289 close $fh or die "close $file: $!";
294 if (not -f $makefile) {
295 print "Warning: No Makefile!\n";
299 _macroify_passthrough($pass_through);
300 unshift @$pass_through, "/DESCRIPTION=$makefile";
303 if (!$target or $target !~ /clean$/) {
304 # Give makefile an opportunity to rewrite itself.
305 # reassure users that life goes on...
306 my @args = ('config', @$pass_through);
307 _quote_args(\@args) if $is_VMS;
308 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
310 my @targ = ($target, @$pass_through);
311 _quote_args(\@targ) if $is_VMS;
312 print "Making $target in $ext_dir\n@run @make @targ\n";
313 my $code = system(@run, @make, @targ);
314 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
316 chdir $return_dir || die "Cannot cd to $return_dir: $!";
320 my $args = shift; # must be array reference
322 # Do not quote qualifiers that begin with '/'.
324 $_ =~ s/\"/""/g; # escape C<"> by doubling
331 sub _macroify_passthrough {
332 my $passthrough = shift;
333 _quote_args($passthrough);
334 my $macro = '/MACRO=(' . join(',',@$passthrough) . ')';
336 @$passthrough[0] = $macro;