If there is one argument, and it looks like a commit, list data from the files
[p5sagit/p5-mst-13.2.git] / ext / util / make_ext.pl
CommitLineData
a2f19a19 1#!/bin/perl
2use strict;
3use warnings;
75f92628 4
a0d0e21e 5# This script acts as a simple interface for building extensions.
6# It primarily used by the perl Makefile:
7#
8# d_dummy $(dynamic_ext): miniperl preplibrary FORCE
a2f19a19 9# @$(RUN) ./miniperl ext/util/make_ext.pl dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
a0d0e21e 10#
11# It may be deleted in a later release of perl so try to
12# avoid using it for other purposes.
13
a2f19a19 14my $target = shift(@ARGV);
15my $extspec = shift(@ARGV);
16my $makecmd = shift(@ARGV); # Should be something like MAKE=make
17my $passthru = join(' ', @ARGV); # allow extra macro=value to be passed through
18print "\n";
a0d0e21e 19
fb73857a 20# Previously, $make was taken from config.sh. However, the user might
21# instead be running a possibly incompatible make. This might happen if
22# the user types "gmake" instead of a plain "make", for example. The
23# correct current value of MAKE will come through from the main perl
24# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
25# case third party users of this script (are there any?) don't have the
26# MAKE=$(MAKE) argument, which was added after 5.004_03.
a2f19a19 27my $make;
28if (defined($makecmd) and $makecmd =~ /^MAKE=(.*)$/) {
29 $make = $1;
30}
31else {
32 print "ext/util/make_ext: WARNING: Please include MAKE=\$(MAKE)\n";
33 print "\tin your call to make_ext. See ext/util/make_ext for details.\n";
34 exit(1);
35}
36
37# search config.sh for inclusion
38$ENV{CONFIG} = '' if not defined $ENV{CONFIG};
39if ($ENV{CONFIG} eq '') {
40 my $config;
41 foreach my $depth (0..4) {
42 my $file = ('../' x $depth) . 'config.sh';
43 $config = $file, last if -f $file;
44 }
45 print("Can't find config.sh generated by Configure"), exit(1)
46 unless defined $config;
47
48 load_config_sh($config);
49}
50
51# fallback to config.sh's MAKE
52$make ||= $ENV{make} || $ENV{MAKE};
53my $run = $ENV{run};
54$run = '' if not defined $run;
55$run .= ' ' if $run ne '';;
56
57if (!defined($extspec) or $extspec eq '') {
58 print "make_ext: no extension specified\n";
59 exit(1);
60}
a0d0e21e 61
75f92628 62# The Perl Makefile.SH will expand all extensions to
2698564b 63# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
75f92628 64# A user wishing to run make_ext might use
2698564b 65# X (or X/Y or X::Y if nested)
75f92628 66
67# canonise into X/Y form (pname)
a2f19a19 68
69my $pname = $extspec;
70if ($extspec =~ /^lib/) {
71 # Remove lib/auto prefix and /*.* suffix
72 $pname =~ s{^lib/auto/}{};
73 $pname =~ s{[^/]*\.[^/]*$}{};
74}
75elsif ($extspec =~ /^ext/) {
76 # Remove ext/ prefix and /pm_to_blib suffix
77 $pname =~ s{^ext/}{};
78 $pname =~ s{/pm_to_blib$}{};
79}
80elsif ($extspec =~ /::/) {
81 # Convert :: to /
82 $pname =~ s{::}{\/}g;
83}
84elsif ($extspec =~ /\..*o$/) {
85 $pname =~ s/\..*o//;
86}
87
88my $mname = $pname;
89$mname =~ s!/!::!g;
90my $depth = $pname;
91$depth =~ s![^/]+!..!g;
92my $makefile = "Makefile";
93my $makeargs = '';
94my $makeopts = '';
95
96if (not -d "ext/$pname") {
97 print "\tSkipping $extspec (directory does not exist)\n";
98 exit(0); # not an error ?
99}
100
101if ($ENV{osname} eq 'catamount') {
102 # Snowball's chance of building extensions.
103 print "This is $ENV{osname}, not building $mname, sorry.\n";
104 exit(0);
105}
106
107print "\tMaking $mname ($target)\n";
108
109chdir("ext/$pname");
a0d0e21e 110
94c41b70 111# check link type and do any preliminaries. Valid link types are
112# 'dynamic', 'static', and 'static_pic' (the last one respects
113# CCCDLFLAGS such as -fPIC -- see static_target in the main Makefile.SH)
a2f19a19 114if ($target eq 'dynamic') {
115 $makeargs = "LINKTYPE=dynamic";
116 $target = 'all';
117}
118elsif ($target eq 'static') {
119 $makeargs = "LINKTYPE=static CCCDLFLAGS=";
120 $target = 'all';
121}
122elsif ($target eq 'static_pic') {
123 $makeargs = "LINKTYPE=static";
124 $target = 'all';
125}
126elsif ($target eq 'nonxs') {
127 $makeargs = "";
128 $target = 'all';
129}
130elsif ($target =~ /clean$/) {
131 # If Makefile has been moved to Makefile.old by a make clean
132 # then use Makefile.old for realclean rather than rebuild it
133 if (! -f $makefile and -f "Makefile.old") {
134 $makefile = "Makefile.old";
135 $makeopts = "-f $makefile";
136 print "Note: Using Makefile.old\n";
137 }
138}
139elsif ($target eq '') {
140 print "make_ext: no make target specified (eg static or dynamic)\n";
141 exit(1);
142}
143else {
144 # for the time being we are strict about what make_ext is used for
145 print "make_ext: unknown make target '$target'\n";
146 exit(1);
147}
148
149
150if (not -f $makefile) {
151 if (-f "Makefile.PL") {
152 system("${run}../$depth/miniperl -I../$depth/lib Makefile.PL INSTALLDIRS=perl INSTALLMAN3DIR=none PERL_CORE=1 $passthru");
153 }
154}
155
156if (not -f $makefile) {
157 print "Warning: No Makefile!\n";
158}
159
160if ($target eq 'clean') {
161}
162elsif ($target eq 'realclean') {
163}
164else {
165 # Give makefile an opportunity to rewrite itself.
75f92628 166 # reassure users that life goes on...
a2f19a19 167 system( "$run$make config MAKE=$make $passthru" )
168 and print "$make config failed, continuing anyway...\n";
169}
170
171system(
172 "$run$make $target MAKE=$make $makeargs $passthru"
173) or exit();
174
175exit($?);
176
177# read config.sh and add its keys to our %ENV
178sub load_config_sh {
179 my $file = shift;
180 open my $fh, '<', $file or die "Could not open file '$file' as a 'config.sh': $!";
181 while (<$fh>) {
182 chomp;
183 next if /^\s*#/;
184 $ENV{$1} = $3 if /^(?!:)([^\s=]+)=('?)(.*?)\2$/;
185 }
186 close $fh;
187}