Integrate from macperl:
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / MM_NW5.pm
1 package ExtUtils::MM_NW5;
2
3 =head1 NAME
4
5 ExtUtils::MM_NW5 - methods to override UN*X behaviour in ExtUtils::MakeMaker
6
7 =head1 SYNOPSIS
8
9  use ExtUtils::MM_NW5; # Done internally by ExtUtils::MakeMaker if needed
10
11 =head1 DESCRIPTION
12
13 See ExtUtils::MM_Unix for a documentation of the methods provided
14 there. This package overrides the implementation of these methods, not
15 the semantics.
16
17 =over
18
19 =cut 
20
21 use strict;
22 use Config;
23 use File::Basename;
24
25 use vars qw(@ISA $VERSION);
26 $VERSION = '2.05';
27
28 require ExtUtils::MM_Win32;
29 @ISA = qw(ExtUtils::MM_Win32);
30
31 use ExtUtils::MakeMaker qw( &neatvalue );
32
33 $ENV{EMXSHELL} = 'sh'; # to run `commands`
34
35 my $BORLAND  = 1 if $Config{'cc'} =~ /^bcc/i;
36 my $GCC      = 1 if $Config{'cc'} =~ /^gcc/i;
37 my $DMAKE    = 1 if $Config{'make'} =~ /^dmake/i;
38
39
40 sub init_others {
41     my ($self) = @_;
42     $self->SUPER::init_others(@_);
43
44     # incpath is copied to makefile var INCLUDE in constants sub, here just 
45     # make it empty
46     my $libpth = $Config{'libpth'};
47     $libpth =~ s( )(;);
48     $self->{'LIBPTH'} = $libpth;
49     $self->{'BASE_IMPORT'} = $Config{'base_import'};
50  
51     # Additional import file specified from Makefile.pl
52     if($self->{'base_import'}) {
53         $self->{'BASE_IMPORT'} .= ', ' . $self->{'base_import'};
54     }
55  
56     $self->{'NLM_VERSION'} = $Config{'nlm_version'};
57     $self->{'MPKTOOL'}  = $Config{'mpktool'};
58     $self->{'TOOLPATH'} = $Config{'toolpath'};
59 }
60
61
62 =item constants (o)
63
64 Initializes lots of constants and .SUFFIXES and .PHONY
65
66 =cut
67
68 sub const_cccmd {
69     my($self,$libperl)=@_;
70     return $self->{CONST_CCCMD} if $self->{CONST_CCCMD};
71     return '' unless $self->needs_linking();
72     return $self->{CONST_CCCMD} = <<'MAKE_FRAG';
73 CCCMD = $(CC) $(CCFLAGS) $(INC) $(OPTIMIZE) \
74         $(PERLTYPE) $(MPOLLUTE) -o $@ \
75         -DVERSION=\"$(VERSION)\" -DXS_VERSION=\"$(XS_VERSION)\"
76 MAKE_FRAG
77
78 }
79
80 sub constants {
81     my($self) = @_;
82     my(@m,$tmp);
83
84 # Added LIBPTH, BASE_IMPORT, ABSTRACT, NLM_VERSION BOOT_SYMBOL, NLM_SHORT_NAME
85 # for NETWARE
86
87     for $tmp (qw/
88
89               AR_STATIC_ARGS NAME DISTNAME NAME_SYM VERSION
90               VERSION_SYM XS_VERSION INST_BIN INST_LIB
91               INST_ARCHLIB INST_SCRIPT PREFIX  INSTALLDIRS
92               INSTALLPRIVLIB INSTALLARCHLIB INSTALLSITELIB
93               INSTALLSITEARCH INSTALLBIN INSTALLSCRIPT PERL_LIB
94               PERL_ARCHLIB SITELIBEXP SITEARCHEXP LIBPERL_A MYEXTLIB
95               FIRST_MAKEFILE MAKE_APERL_FILE PERLMAINCC PERL_SRC
96               PERL_INC PERL FULLPERL LIBPTH BASE_IMPORT PERLRUN
97               FULLPERLRUN PERLRUNINST FULLPERLRUNINST
98               FULL_AR PERL_CORE NLM_VERSION MPKTOOL TOOLPATH
99
100               / ) {
101         next unless defined $self->{$tmp};
102         push @m, "$tmp = $self->{$tmp}\n";
103     }
104
105     (my $boot = $self->{'NAME'}) =~ s/:/_/g;
106     $self->{'BOOT_SYMBOL'}=$boot;
107     push @m, "BOOT_SYMBOL = $self->{'BOOT_SYMBOL'}\n";
108
109     # If the final binary name is greater than 8 chars,
110     # truncate it here.
111     if(length($self->{'BASEEXT'}) > 8) {
112         $self->{'NLM_SHORT_NAME'} = substr($self->{'BASEEXT'},0,8);
113         push @m, "NLM_SHORT_NAME = $self->{'NLM_SHORT_NAME'}\n";
114     }
115
116     push @m, qq{
117 VERSION_MACRO = VERSION
118 DEFINE_VERSION = -D\$(VERSION_MACRO)=\\\"\$(VERSION)\\\"
119 XS_VERSION_MACRO = XS_VERSION
120 XS_DEFINE_VERSION = -D\$(XS_VERSION_MACRO)=\\\"\$(XS_VERSION)\\\"
121 };
122
123     # Get the include path and replace the spaces with ;
124     # Copy this to makefile as INCLUDE = d:\...;d:\;
125     (my $inc = $Config{'incpath'}) =~ s/([ ]*)-I/;/g;
126
127     # Get the additional include path and append to INCLUDE, keeping it
128     # in INC will give problems during compilation, hence reset it
129     # after getting the value
130     $self->{INC} = '';
131
132     push @m, qq{
133 INCLUDE = $inc;
134 };
135
136     # Set the path to CodeWarrior binaries which might not have been set in
137     # any other place
138     push @m, qq{
139 PATH = \$(PATH);\$(TOOLPATH)
140 };
141
142     push @m, qq{
143 MAKEMAKER = $INC{'ExtUtils/MakeMaker.pm'}
144 MM_VERSION = $ExtUtils::MakeMaker::VERSION
145 };
146
147     push @m, q{
148 # FULLEXT = Pathname for extension directory (eg Foo/Bar/Oracle).
149 # BASEEXT = Basename part of FULLEXT. May be just equal FULLEXT. (eg Oracle)
150 # PARENT_NAME = NAME without BASEEXT and no trailing :: (eg Foo::Bar)
151 # DLBASE  = Basename part of dynamic library. May be just equal BASEEXT.
152 };
153
154     for $tmp (qw/
155               FULLEXT BASEEXT PARENT_NAME DLBASE VERSION_FROM INC DEFINE OBJECT
156               LDFROM LINKTYPE
157               / ) {
158         next unless defined $self->{$tmp};
159         push @m, "$tmp = $self->{$tmp}\n";
160     }
161
162     push @m, "
163 # Handy lists of source code files:
164 XS_FILES= ".join(" \\\n\t", sort keys %{$self->{XS}})."
165 C_FILES = ".join(" \\\n\t", @{$self->{C}})."
166 O_FILES = ".join(" \\\n\t", @{$self->{O_FILES}})."
167 H_FILES = ".join(" \\\n\t", @{$self->{H}})."
168 MAN1PODS = ".join(" \\\n\t", sort keys %{$self->{MAN1PODS}})."
169 MAN3PODS = ".join(" \\\n\t", sort keys %{$self->{MAN3PODS}})."
170 ";
171
172     for $tmp (qw/
173               INST_MAN1DIR        INSTALLMAN1DIR MAN1EXT
174               INST_MAN3DIR        INSTALLMAN3DIR MAN3EXT
175               /) {
176         next unless defined $self->{$tmp};
177         push @m, "$tmp = $self->{$tmp}\n";
178     }
179
180     push @m, qq{
181 .USESHELL :
182 } if $DMAKE;
183
184     push @m, q{
185 .NO_CONFIG_REC: Makefile
186 } if $ENV{CLEARCASE_ROOT};
187
188     # why not q{} ? -- emacs
189     push @m, qq{
190 # work around a famous dec-osf make(1) feature(?):
191 makemakerdflt: all
192
193 .SUFFIXES: .xs .c .C .cpp .cxx .cc \$(OBJ_EXT)
194
195 .PHONY: all config static dynamic test linkext manifest
196
197 # Where is the Config information that we are using/depend on
198 CONFIGDEP = \$(PERL_ARCHLIB)\\Config.pm \$(PERL_INC)\\config.h
199 };
200
201     my @parentdir = split(/::/, $self->{PARENT_NAME});
202     push @m, q{
203 # Where to put things:
204 INST_LIBDIR      = }. File::Spec->catdir('$(INST_LIB)',@parentdir)        .q{
205 INST_ARCHLIBDIR  = }. File::Spec->catdir('$(INST_ARCHLIB)',@parentdir)    .q{
206
207 INST_AUTODIR     = }. File::Spec->catdir('$(INST_LIB)','auto','$(FULLEXT)')       .q{
208 INST_ARCHAUTODIR = }. File::Spec->catdir('$(INST_ARCHLIB)','auto','$(FULLEXT)')   .q{
209 };
210
211     if ($self->has_link_code()) {
212         push @m, '
213 INST_STATIC  = $(INST_ARCHAUTODIR)\$(BASEEXT)$(LIB_EXT)
214 INST_DYNAMIC = $(INST_ARCHAUTODIR)\$(DLBASE).$(DLEXT)
215 INST_BOOT    = $(INST_ARCHAUTODIR)\$(BASEEXT).bs
216 ';
217     } else {
218         push @m, '
219 INST_STATIC  =
220 INST_DYNAMIC =
221 INST_BOOT    =
222 ';
223     }
224
225     $tmp = $self->export_list;
226     push @m, "
227 EXPORT_LIST = $tmp
228 ";
229     $tmp = $self->perl_archive;
230     push @m, "
231 PERL_ARCHIVE = $tmp
232 ";
233
234     push @m, q{
235 TO_INST_PM = }.join(" \\\n\t", sort keys %{$self->{PM}}).q{
236
237 PM_TO_BLIB = }.join(" \\\n\t", %{$self->{PM}}).q{
238 };
239
240     join('',@m);
241 }
242
243
244 =item static_lib (o)
245
246 =cut
247
248 sub static_lib {
249     my($self) = @_;
250
251     return '' unless $self->has_link_code;
252
253     my $m = <<'END';
254 $(INST_STATIC): $(OBJECT) $(MYEXTLIB) $(INST_ARCHAUTODIR)\.exists
255         $(RM_RF) $@
256 END
257
258     # If this extension has it's own library (eg SDBM_File)
259     # then copy that to $(INST_STATIC) and add $(OBJECT) into it.
260     $m .= <<'END'  if $self->{MYEXTLIB};
261         $self->{CP} $(MYEXTLIB) $@
262 END
263
264     my $ar_arg;
265     if( $BORLAND ) {
266         $ar_arg = '$@ $(OBJECT:^"+")';
267     }
268     elsif( $GCC ) {
269         $ar_arg = '-ru $@ $(OBJECT)';
270     }
271     else {
272         $ar_arg = '-type library -o $@ $(OBJECT)';
273     }
274
275     $m .= sprintf <<'END', $ar_arg;
276         $(AR) %s
277         $(NOECHO)echo "$(EXTRALIBS)" > $(INST_ARCHAUTODIR)\extralibs.ld
278         $(CHMOD) 755 $@
279 END
280
281     $m .= <<'END' if $self->{PERL_SRC};
282         $(NOECHO)echo "$(EXTRALIBS)" >> $(PERL_SRC)\ext.libs
283     
284     
285 END
286     $m .= $self->dir_target('$(INST_ARCHAUTODIR)');
287     return $m;
288 }
289
290 =item dynamic_lib (o)
291
292 Defines how to produce the *.so (or equivalent) files.
293
294 =cut
295
296 sub dynamic_lib {
297     my($self, %attribs) = @_;
298     return '' unless $self->needs_linking(); #might be because of a subdir
299
300     return '' unless $self->has_link_code;
301
302     my($otherldflags) = $attribs{OTHERLDFLAGS} || ($BORLAND ? 'c0d32.obj': '');
303     my($inst_dynamic_dep) = $attribs{INST_DYNAMIC_DEP} || "";
304     my($ldfrom) = '$(LDFROM)';
305
306     (my $boot = $self->{NAME}) =~ s/:/_/g;
307
308     my $m = <<'MAKE_FRAG';
309 # This section creates the dynamically loadable $(INST_DYNAMIC)
310 # from $(OBJECT) and possibly $(MYEXTLIB).
311 OTHERLDFLAGS = '.$otherldflags.'
312 INST_DYNAMIC_DEP = '.$inst_dynamic_dep.'
313
314 # Create xdc data for an MT safe NLM in case of mpk build
315 $(INST_DYNAMIC): $(OBJECT) $(MYEXTLIB) $(BOOTSTRAP)
316         @echo Export boot_$(BOOT_SYMBOL) > $(BASEEXT).def
317         @echo $(BASE_IMPORT) >> $(BASEEXT).def
318         @echo Import @$(PERL_INC)\perl.imp >> $(BASEEXT).def
319 MAKE_FRAG
320
321
322     if ( $self->{CCFLAGS} =~ m/ -DMPK_ON /) {
323         $m .= <<'MAKE_FRAG';
324         $(MPKTOOL) $(XDCFLAGS) $(BASEEXT).xdc
325         @echo xdcdata $(BASEEXT).xdc >> $(BASEEXT).def
326 MAKE_FRAG
327     }
328
329     # Reconstruct the X.Y.Z version.
330     my $version = join '.', map { sprintf "%d", $_ }
331                               $] =~ /(\d)\.(\d{3})(\d{2})/;
332     $m .= sprintf '     $(LD) $(LDFLAGS) $(OBJECT:.obj=.obj) -desc "Perl %s Extension ($(BASEEXT))  XS_VERSION: $(XS_VERSION)" -nlmversion $(NLM_VERSION)', $version;
333
334     # Taking care of long names like FileHandle, ByteLoader, SDBM_File etc
335     if($self->{NLM_SHORT_NAME}) {
336         # In case of nlms with names exceeding 8 chars, build nlm in the 
337         # current dir, rename and move to auto\lib.
338         $m .= q{ -o $(NLM_SHORT_NAME).$(DLEXT)}
339     } else {
340         $m .= q{ -o $(INST_AUTODIR)\\$(BASEEXT).$(DLEXT)}
341     }
342
343     # Add additional lib files if any (SDBM_File)
344     $m .= q{ $(MYEXTLIB) } if $self->{MYEXTLIB};
345
346     $m .= q{ $(PERL_INC)\Main.lib -commandfile $(BASEEXT).def}."\n";
347
348     if($self->{NLM_SHORT_NAME}) {
349         $m .= <<'MAKE_FRAG';
350         if exist $(INST_AUTODIR)\$(NLM_SHORT_NAME).$(DLEXT) del $(INST_AUTODIR)\$(NLM_SHORT_NAME).$(DLEXT) 
351         move $(NLM_SHORT_NAME).$(DLEXT) $(INST_AUTODIR)
352 MAKE_FRAG
353     }
354
355     $m .= <<'MAKE_FRAG';
356
357         $(CHMOD) 755 $@
358 MAKE_FRAG
359
360     $m .= $self->dir_target('$(INST_ARCHAUTODIR)');
361
362     return $m;
363 }
364
365
366 1;
367 __END__
368
369 =back
370
371 =cut 
372
373