Some refactoring in the loop that constructs parameters for build_extension().
[p5sagit/p5-mst-13.2.git] / make_ext.pl
1 #!./miniperl
2 use strict;
3 use warnings;
4 use Config;
5 use Cwd;
6
7 # This script acts as a simple interface for building extensions.
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:
13 #
14 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
15 #       @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
16 #
17 # On Windows,
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
41 # It may be deleted in a later release of perl so try to
42 # avoid using it for other purposes.
43
44 my $is_Win32 = $^O eq 'MSWin32';
45 my $is_VMS = $^O eq 'VMS';
46 my $is_Unix = !$is_Win32 && !$is_VMS;
47
48 require FindExt if $is_Win32;
49
50 my (%excl, %incl, %opts, @extspec, @pass_through);
51
52 foreach (@ARGV) {
53     if (/^!(.*)$/) {
54         $excl{$1} = 1;
55     } elsif (/^\+(.*)$/) {
56         $incl{$1} = 1;
57     } elsif (/^--([\w\-]+)$/) {
58         $opts{$1} = 1;
59     } elsif (/^--([\w\-]+)=(.*)$/) {
60         $opts{$1} = $2;
61     } elsif (/=/) {
62         push @pass_through, $_;
63     } elsif (length) {
64         push @extspec, $_;
65     }
66 }
67
68 my $static = $opts{static} || $opts{all};
69 my $dynamic = $opts{dynamic} || $opts{all};
70
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) {
79     if (/^lib/) {
80         # Remove lib/auto prefix and /*.* suffix
81         s{^lib/auto/}{};
82         s{[^/]*\.[^/]*$}{};
83     } elsif (/^ext/) {
84         # Remove ext/ prefix and /pm_to_blib suffix
85         s{^ext/}{};
86         s{/pm_to_blib$}{};
87     } elsif (/::/) {
88         # Convert :: to /
89         s{::}{\/}g;
90     } elsif (/\..*o$/) {
91         s/\..*o//;
92     }
93 }
94
95 my $makecmd  = shift @pass_through; # Should be something like MAKE=make
96 unshift @pass_through, 'PERL_CORE=1';
97
98 my $dir  = $opts{dir} || 'ext';
99 my $target   = $opts{target};
100 $target = 'all' unless defined $target;
101
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.
109 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
110     die "$0:  WARNING:  Please include MAKE=\$(MAKE) in \@ARGV\n";
111 }
112
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};
117 # Using an array of 0 or 1 elements makes the subsequent code simpler.
118 my @run = $Config{run};
119 @run = () if not defined $run[0] or $run[0] eq '';
120
121
122 if ($target eq '') {
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";
127 }
128
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 }
178
179 foreach my $spec (@extspec)  {
180     my $mname = $spec;
181     $mname =~ s!/!::!g;
182     my $ext_pathname = "ext/$spec";
183     my $up = $ext_pathname;
184     $up =~ s![^/]+!..!g;
185
186     if ($Config{osname} eq 'catamount') {
187         # Snowball's chance of building extensions.
188         die "This is $Config{osname}, not building $mname, sorry.\n";
189     }
190
191     print "\tMaking $mname ($target)\n";
192
193     build_extension('ext', $ext_pathname, $up, $perl || "$up/miniperl",
194                     "$up/lib",
195                     [@pass_through, @{$extra_passthrough{$spec} || []}]);
196 }
197
198 sub build_extension {
199     my ($ext, $ext_dir, $return_dir, $perl, $lib_dir, $pass_through) = @_;
200     unless (chdir "$ext_dir") {
201         warn "Cannot cd to $ext_dir: $!";
202         return;
203     }
204     
205     if (!-f 'Makefile') {
206         print "\nRunning Makefile.PL in $ext_dir\n";
207
208         # Presumably this can be simplified
209         my @cross;
210         if (defined $::Cross::platform) {
211             # Inherited from win32/buildext.pl
212             @cross = "-MCross=$::Cross::platform";
213         } elsif ($opts{cross}) {
214             # Inherited from make_ext.pl
215             @cross = '-MCross';
216         }
217             
218         my @perl = (@run, $perl, "-I$lib_dir", @cross, 'Makefile.PL',
219                     'INSTALLDIRS=perl', 'INSTALLMAN3DIR=none',
220                     @$pass_through);
221         print join(' ', @perl), "\n";
222         my $code = system @perl;
223         warn "$code from $ext_dir\'s Makefile.PL" if $code;
224
225         # Right. The reason for this little hack is that we're sitting inside
226         # a program run by ./miniperl, but there are tasks we need to perform
227         # when the 'realclean', 'distclean' or 'veryclean' targets are run.
228         # Unfortunately, they can be run *after* 'clean', which deletes
229         # ./miniperl
230         # So we do our best to leave a set of instructions identical to what
231         # we would do if we are run directly as 'realclean' etc
232         # Whilst we're perfect, unfortunately the targets we call are not, as
233         # some of them rely on a $(PERL) for their own distclean targets.
234         # But this always used to be a problem with the old /bin/sh version of
235         # this.
236         if ($is_Unix) {
237             my $suffix = '.sh';
238             foreach my $clean_target ('realclean', 'veryclean') {
239                 my $file = "$return_dir/$clean_target$suffix";
240                 open my $fh, '>>', $file or die "open $file: $!";
241                 # Quite possible that we're being run in parallel here.
242                 # Can't use Fcntl this early to get the LOCK_EX
243                 flock $fh, 2 or warn "flock $file: $!";
244                 print $fh <<"EOS";
245 cd $ext_dir
246 if test ! -f Makefile -a -f Makefile.old; then
247     echo "Note: Using Makefile.old"
248     make -f Makefile.old $clean_target MAKE='@make' @pass_through
249 else
250     if test ! -f Makefile ; then
251         echo "Warning: No Makefile!"
252     fi
253     make $clean_target MAKE='@make' @pass_through
254 fi
255 cd $return_dir
256 EOS
257                 close $fh or die "close $file: $!";
258             }
259         }
260     }
261
262     if (not -f 'Makefile') {
263         print "Warning: No Makefile!\n";
264     }
265
266     if (!$target or $target !~ /clean$/) {
267         # Give makefile an opportunity to rewrite itself.
268         # reassure users that life goes on...
269         my @config = (@run, @make, 'config', @$pass_through);
270         system @config and print "@config failed, continuing anyway...\n";
271     }
272     my @targ = (@run, @make, $target, @$pass_through);
273     print "Making $target in $ext_dir\n@targ\n";
274     my $code = system @targ;
275     die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
276
277     chdir $return_dir || die "Cannot cd to $return_dir: $!";
278 }