Upgrade to ExtUtils::MakeMaker 6.49_01
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / MM_Win32.pm
CommitLineData
68dc0745 1package ExtUtils::MM_Win32;
2
479d2113 3use strict;
4
b75c8c73 5
68dc0745 6=head1 NAME
7
8ExtUtils::MM_Win32 - methods to override UN*X behaviour in ExtUtils::MakeMaker
9
10=head1 SYNOPSIS
11
12 use ExtUtils::MM_Win32; # Done internally by ExtUtils::MakeMaker if needed
13
14=head1 DESCRIPTION
15
16See ExtUtils::MM_Unix for a documentation of the methods provided
17there. This package overrides the implementation of these methods, not
18the semantics.
19
68dc0745 20=cut
21
7292dc67 22use ExtUtils::MakeMaker::Config;
68dc0745 23use File::Basename;
ecf68df6 24use File::Spec;
f6d6199c 25use ExtUtils::MakeMaker qw( neatvalue );
68dc0745 26
f6d6199c 27require ExtUtils::MM_Any;
28require ExtUtils::MM_Unix;
a592ba15 29our @ISA = qw( ExtUtils::MM_Any ExtUtils::MM_Unix );
1487aac6 30our $VERSION = '6.49_01';
68dc0745 31
32$ENV{EMXSHELL} = 'sh'; # to run `commands`
68dc0745 33
a592ba15 34my $BORLAND = $Config{'cc'} =~ /^bcc/i ? 1 : 0;
35my $GCC = $Config{'cc'} =~ /^gcc/i ? 1 : 0;
479d2113 36
37
38=head2 Overridden methods
39
40=over 4
41
42=item B<dlsyms>
43
44=cut
3e3baf6d 45
68dc0745 46sub dlsyms {
47 my($self,%attribs) = @_;
48
49 my($funcs) = $attribs{DL_FUNCS} || $self->{DL_FUNCS} || {};
50 my($vars) = $attribs{DL_VARS} || $self->{DL_VARS} || [];
762efda7 51 my($funclist) = $attribs{FUNCLIST} || $self->{FUNCLIST} || [];
68dc0745 52 my($imports) = $attribs{IMPORTS} || $self->{IMPORTS} || {};
53 my(@m);
68dc0745 54
55 if (not $self->{SKIPHASH}{'dynamic'}) {
56 push(@m,"
57$self->{BASEEXT}.def: Makefile.PL
58",
f6d6199c 59 q! $(PERLRUN) -MExtUtils::Mksymlists \\
5e687e55 60 -e "Mksymlists('NAME'=>\"!, $self->{NAME},
61 q!\", 'DLBASE' => '!,$self->{DLBASE},
62 # The above two lines quoted differently to work around
63 # a bug in the 4DOS/4NT command line interpreter. The visible
64 # result of the bug was files named q('extension_name',) *with the
65 # single quotes and the comma* in the extension build directories.
68dc0745 66 q!', 'DL_FUNCS' => !,neatvalue($funcs),
762efda7 67 q!, 'FUNCLIST' => !,neatvalue($funclist),
68dc0745 68 q!, 'IMPORTS' => !,neatvalue($imports),
69 q!, 'DL_VARS' => !, neatvalue($vars), q!);"
70!);
71 }
72 join('',@m);
73}
74
479d2113 75=item replace_manpage_separator
76
77Changes the path separator with .
78
79=cut
80
68dc0745 81sub replace_manpage_separator {
82 my($self,$man) = @_;
83 $man =~ s,/+,.,g;
84 $man;
85}
86
479d2113 87
88=item B<maybe_command>
89
90Since Windows has nothing as simple as an executable bit, we check the
91file extension.
92
93The PATHEXT env variable will be used to get a list of extensions that
94might indicate a command, otherwise .com, .exe, .bat and .cmd will be
95used by default.
96
97=cut
98
68dc0745 99sub maybe_command {
100 my($self,$file) = @_;
846f184a 101 my @e = exists($ENV{'PATHEXT'})
102 ? split(/;/, $ENV{PATHEXT})
103 : qw(.com .exe .bat .cmd);
104 my $e = '';
105 for (@e) { $e .= "\Q$_\E|" }
106 chop $e;
107 # see if file ends in one of the known extensions
2b2708c8 108 if ($file =~ /($e)$/i) {
846f184a 109 return $file if -e $file;
110 }
111 else {
112 for (@e) {
113 return "$file$_" if -e "$file$_";
114 }
115 }
68dc0745 116 return;
117}
118
68dc0745 119
479d2113 120=item B<init_DIRFILESEP>
121
122Using \ for Windows.
123
124=cut
125
126sub init_DIRFILESEP {
127 my($self) = shift;
128
dedf98bc 129 # The ^ makes sure its not interpreted as an escape in nmake
2e65e370 130 $self->{DIRFILESEP} = $self->is_make_type('nmake') ? '^\\' :
131 $self->is_make_type('dmake') ? '\\\\'
132 : '\\';
68dc0745 133}
134
479d2113 135=item B<init_others>
136
137Override some of the Unix specific commands with portable
138ExtUtils::Command ones.
139
60537fc0 140Also provide defaults for LD and AR in case the %Config values aren't
141set.
3e3baf6d 142
479d2113 143LDLOADLIBS's default is changed to $Config{libs}.
3e3baf6d 144
479d2113 145Adjustments are made for Borland's quirks needing -L to come first.
3e3baf6d 146
147=cut
148
479d2113 149sub init_others {
150 my ($self) = @_;
151
152 # Used in favor of echo because echo won't strip quotes. :(
dedf98bc 153 $self->{ECHO} ||= $self->oneliner('print qq{@ARGV}', ['-l']);
e3aa3ecb 154 $self->{ECHO_N} ||= $self->oneliner('print qq{@ARGV}');
dedf98bc 155
5dca256e 156 $self->{TOUCH} ||= '$(ABSPERLRUN) -MExtUtils::Command -e touch';
157 $self->{CHMOD} ||= '$(ABSPERLRUN) -MExtUtils::Command -e chmod';
158 $self->{CP} ||= '$(ABSPERLRUN) -MExtUtils::Command -e cp';
159 $self->{RM_F} ||= '$(ABSPERLRUN) -MExtUtils::Command -e rm_f';
160 $self->{RM_RF} ||= '$(ABSPERLRUN) -MExtUtils::Command -e rm_rf';
161 $self->{MV} ||= '$(ABSPERLRUN) -MExtUtils::Command -e mv';
479d2113 162 $self->{NOOP} ||= 'rem';
5dca256e 163 $self->{TEST_F} ||= '$(ABSPERLRUN) -MExtUtils::Command -e test_f';
479d2113 164 $self->{DEV_NULL} ||= '> NUL';
165
7292dc67 166 $self->{FIXIN} ||= $self->{PERL_CORE} ?
6383bd23 167 "\$(PERLRUN) $self->{PERL_SRC}/win32/bin/pl2bat.pl" :
7292dc67 168 'pl2bat.bat';
169
60537fc0 170 $self->{LD} ||= $Config{ld} || 'link';
479d2113 171 $self->{AR} ||= $Config{ar} || 'lib';
172
173 $self->SUPER::init_others;
174
dedf98bc 175 # Setting SHELL from $Config{sh} can break dmake. Its ok without it.
176 delete $self->{SHELL};
177
479d2113 178 $self->{LDLOADLIBS} ||= $Config{libs};
179 # -Lfoo must come first for Borland, so we put it in LDDLFLAGS
180 if ($BORLAND) {
181 my $libs = $self->{LDLOADLIBS};
182 my $libpath = '';
183 while ($libs =~ s/(?:^|\s)(("?)-L.+?\2)(?:\s|$)/ /) {
184 $libpath .= ' ' if length $libpath;
185 $libpath .= $1;
186 }
187 $self->{LDLOADLIBS} = $libs;
188 $self->{LDDLFLAGS} ||= $Config{lddlflags};
189 $self->{LDDLFLAGS} .= " $libpath";
3e3baf6d 190 }
191
479d2113 192 return 1;
193}
3e3baf6d 194
3e3baf6d 195
7292dc67 196=item init_platform
3e3baf6d 197
479d2113 198Add MM_Win32_VERSION.
3e3baf6d 199
7292dc67 200=item platform_constants
3e3baf6d 201
479d2113 202=cut
3e3baf6d 203
479d2113 204sub init_platform {
205 my($self) = shift;
3e3baf6d 206
479d2113 207 $self->{MM_Win32_VERSION} = $VERSION;
208}
3e3baf6d 209
479d2113 210sub platform_constants {
211 my($self) = shift;
212 my $make_frag = '';
3e3baf6d 213
479d2113 214 foreach my $macro (qw(MM_Win32_VERSION))
215 {
216 next unless defined $self->{$macro};
217 $make_frag .= "$macro = $self->{$macro}\n";
218 }
3e3baf6d 219
479d2113 220 return $make_frag;
221}
3e3baf6d 222
3e3baf6d 223
7292dc67 224=item special_targets
3e3baf6d 225
479d2113 226Add .USESHELL target for dmake.
3e3baf6d 227
479d2113 228=cut
3e3baf6d 229
479d2113 230sub special_targets {
231 my($self) = @_;
3e3baf6d 232
479d2113 233 my $make_frag = $self->SUPER::special_targets;
3e3baf6d 234
2e65e370 235 $make_frag .= <<'MAKE_FRAG' if $self->is_make_type('dmake');
479d2113 236.USESHELL :
237MAKE_FRAG
3e3baf6d 238
479d2113 239 return $make_frag;
3e3baf6d 240}
241
242
7292dc67 243=item static_lib
68dc0745 244
479d2113 245Changes how to run the linker.
246
247The rest is duplicate code from MM_Unix. Should move the linker code
248to its own method.
68dc0745 249
250=cut
251
252sub static_lib {
253 my($self) = @_;
68dc0745 254 return '' unless $self->has_link_code;
255
256 my(@m);
257 push(@m, <<'END');
7292dc67 258$(INST_STATIC): $(OBJECT) $(MYEXTLIB) $(INST_ARCHAUTODIR)$(DFSEP).exists
68dc0745 259 $(RM_RF) $@
260END
479d2113 261
022735b4 262 # If this extension has its own library (eg SDBM_File)
68dc0745 263 # then copy that to $(INST_STATIC) and add $(OBJECT) into it.
479d2113 264 push @m, <<'MAKE_FRAG' if $self->{MYEXTLIB};
265 $(CP) $(MYEXTLIB) $@
266MAKE_FRAG
68dc0745 267
268 push @m,
910dfcc8 269q{ $(AR) }.($BORLAND ? '$@ $(OBJECT:^"+")'
270 : ($GCC ? '-ru $@ $(OBJECT)'
271 : '-out:$@ $(OBJECT)')).q{
479d2113 272 $(CHMOD) $(PERM_RWX) $@
273 $(NOECHO) $(ECHO) "$(EXTRALIBS)" > $(INST_ARCHAUTODIR)\extralibs.ld
68dc0745 274};
275
479d2113 276 # Old mechanism - still available:
277 push @m, <<'MAKE_FRAG' if $self->{PERL_SRC} && $self->{EXTRALIBS};
278 $(NOECHO) $(ECHO) "$(EXTRALIBS)" >> $(PERL_SRC)\ext.libs
279MAKE_FRAG
68dc0745 280
479d2113 281 join('', @m);
68dc0745 282}
283
68dc0745 284
7292dc67 285=item dynamic_lib
68dc0745 286
479d2113 287Complicated stuff for Win32 that I don't understand. :(
68dc0745 288
289=cut
290
291sub dynamic_lib {
292 my($self, %attribs) = @_;
293 return '' unless $self->needs_linking(); #might be because of a subdir
294
295 return '' unless $self->has_link_code;
296
3e3baf6d 297 my($otherldflags) = $attribs{OTHERLDFLAGS} || ($BORLAND ? 'c0d32.obj': '');
68dc0745 298 my($inst_dynamic_dep) = $attribs{INST_DYNAMIC_DEP} || "";
299 my($ldfrom) = '$(LDFROM)';
300 my(@m);
7a958ec3 301
5db10396 302# one thing for GCC/Mingw32:
303# we try to overcome non-relocateable-DLL problems by generating
7a958ec3 304# a (hopefully unique) image-base from the dll's name
305# -- BKS, 10-19-1999
306 if ($GCC) {
7a958ec3 307 my $dllname = $self->{BASEEXT} . "." . $self->{DLEXT};
308 $dllname =~ /(....)(.{0,4})/;
309 my $baseaddr = unpack("n", $1 ^ $2);
310 $otherldflags .= sprintf("-Wl,--image-base,0x%x0000 ", $baseaddr);
311 }
312
68dc0745 313 push(@m,'
314# This section creates the dynamically loadable $(INST_DYNAMIC)
315# from $(OBJECT) and possibly $(MYEXTLIB).
316OTHERLDFLAGS = '.$otherldflags.'
317INST_DYNAMIC_DEP = '.$inst_dynamic_dep.'
318
7292dc67 319$(INST_DYNAMIC): $(OBJECT) $(MYEXTLIB) $(BOOTSTRAP) $(INST_ARCHAUTODIR)$(DFSEP).exists $(EXPORT_LIST) $(PERL_ARCHIVE) $(INST_DYNAMIC_DEP)
68dc0745 320');
5b0d9cbe 321 if ($GCC) {
322 push(@m,
910dfcc8 323 q{ dlltool --def $(EXPORT_LIST) --output-exp dll.exp
324 $(LD) -o $@ -Wl,--base-file -Wl,dll.base $(LDDLFLAGS) }.$ldfrom.q{ $(OTHERLDFLAGS) $(MYEXTLIB) $(PERL_ARCHIVE) $(LDLOADLIBS) dll.exp
5b0d9cbe 325 dlltool --def $(EXPORT_LIST) --base-file dll.base --output-exp dll.exp
326 $(LD) -o $@ $(LDDLFLAGS) }.$ldfrom.q{ $(OTHERLDFLAGS) $(MYEXTLIB) $(PERL_ARCHIVE) $(LDLOADLIBS) dll.exp });
dc0d354b 327 } elsif ($BORLAND) {
328 push(@m,
329 q{ $(LD) $(LDDLFLAGS) $(OTHERLDFLAGS) }.$ldfrom.q{,$@,,}
2e65e370 330 .($self->is_make_type('dmake')
2977d345 331 ? q{$(PERL_ARCHIVE:s,/,\,) $(LDLOADLIBS:s,/,\,) }
dc0d354b 332 .q{$(MYEXTLIB:s,/,\,),$(EXPORT_LIST:s,/,\,)}
333 : q{$(subst /,\,$(PERL_ARCHIVE)) $(subst /,\,$(LDLOADLIBS)) }
334 .q{$(subst /,\,$(MYEXTLIB)),$(subst /,\,$(EXPORT_LIST))})
335 .q{,$(RESFILES)});
336 } else { # VC
337 push(@m,
338 q{ $(LD) -out:$@ $(LDDLFLAGS) }.$ldfrom.q{ $(OTHERLDFLAGS) }
339 .q{$(MYEXTLIB) $(PERL_ARCHIVE) $(LDLOADLIBS) -def:$(EXPORT_LIST)});
277189c8 340
1487aac6 341 # VS2005 (aka VC 8) or higher, but not for 64-bit compiler from Platform SDK
342 if ($Config{ivsize} == 4 && $Config{cc} eq 'cl' and $Config{ccversion} =~ /^(\d+)/ and $1 >= 14)
343 {
344 push(@m,
345 q{
346 mt -nologo -manifest $@.manifest -outputresource:$@;2 && del $@.manifest});
347 }
5b0d9cbe 348 }
68dc0745 349 push @m, '
479d2113 350 $(CHMOD) $(PERM_RWX) $@
68dc0745 351';
352
68dc0745 353 join('',@m);
354}
355
7292dc67 356=item extra_clean_files
479d2113 357
358Clean out some extra dll.{base,exp} files which might be generated by
359gcc. Otherwise, take out all *.pdb files.
360
361=cut
362
7292dc67 363sub extra_clean_files {
364 my $self = shift;
562c1c19 365
7292dc67 366 return $GCC ? (qw(dll.base dll.exp)) : ('*.pdb');
562c1c19 367}
368
479d2113 369=item init_linker
562c1c19 370
479d2113 371=cut
562c1c19 372
479d2113 373sub init_linker {
374 my $self = shift;
68dc0745 375
479d2113 376 $self->{PERL_ARCHIVE} = "\$(PERL_INC)\\$Config{libperl}";
377 $self->{PERL_ARCHIVE_AFTER} = '';
378 $self->{EXPORT_LIST} = '$(BASEEXT).def';
68dc0745 379}
380
45bc4d3a 381
68dc0745 382=item perl_script
383
479d2113 384Checks for the perl program under several common perl extensions.
68dc0745 385
386=cut
387
388sub perl_script {
389 my($self,$file) = @_;
cae6c631 390 return $file if -r $file && -f _;
479d2113 391 return "$file.pl" if -r "$file.pl" && -f _;
392 return "$file.plx" if -r "$file.plx" && -f _;
cae6c631 393 return "$file.bat" if -r "$file.bat" && -f _;
68dc0745 394 return;
395}
396
3e3baf6d 397
7292dc67 398=item xs_o
68dc0745 399
479d2113 400This target is stubbed out. Not sure why.
68dc0745 401
402=cut
403
479d2113 404sub xs_o {
405 return ''
68dc0745 406}
407
68dc0745 408
7292dc67 409=item pasthru
68dc0745 410
479d2113 411All we send is -nologo to nmake to prevent it from printing its damned
412banner.
68dc0745 413
414=cut
415
479d2113 416sub pasthru {
68dc0745 417 my($self) = shift;
2e65e370 418 return "PASTHRU = " . ($self->is_make_type('nmake') ? "-nologo" : "");
071e6b84 419}
68dc0745 420
3e3baf6d 421
7292dc67 422=item oneliner
3e3baf6d 423
479d2113 424These are based on what command.com does on Win98. They may be wrong
425for other Windows shells, I don't know.
3e3baf6d 426
427=cut
428
479d2113 429sub oneliner {
430 my($self, $cmd, $switches) = @_;
431 $switches = [] unless defined $switches;
3e3baf6d 432
479d2113 433 # Strip leading and trailing newlines
434 $cmd =~ s{^\n+}{};
435 $cmd =~ s{\n+$}{};
3e3baf6d 436
479d2113 437 $cmd = $self->quote_literal($cmd);
438 $cmd = $self->escape_newlines($cmd);
3e3baf6d 439
479d2113 440 $switches = join ' ', @$switches;
3e3baf6d 441
2977d345 442 return qq{\$(ABSPERLRUN) $switches -e $cmd --};
3e3baf6d 443}
444
68dc0745 445
479d2113 446sub quote_literal {
447 my($self, $text) = @_;
68dc0745 448
479d2113 449 # I don't know if this is correct, but it seems to work on
450 # Win98's command.com
451 $text =~ s{"}{\\"}g;
68dc0745 452
dedf98bc 453 # dmake eats '{' inside double quotes and leaves alone { outside double
454 # quotes; however it transforms {{ into { either inside and outside double
455 # quotes. It also translates }} into }. The escaping below is not
456 # 100% correct.
2e65e370 457 if( $self->is_make_type('dmake') ) {
dedf98bc 458 $text =~ s/{/{{/g;
459 $text =~ s/}}/}}}/g;
460 }
461
479d2113 462 return qq{"$text"};
68dc0745 463}
464
68dc0745 465
479d2113 466sub escape_newlines {
467 my($self, $text) = @_;
68dc0745 468
479d2113 469 # Escape newlines
470 $text =~ s{\n}{\\\n}g;
68dc0745 471
479d2113 472 return $text;
68dc0745 473}
474
68dc0745 475
7292dc67 476=item cd
477
478dmake can handle Unix style cd'ing but nmake (at least 1.5) cannot. It
479wants:
480
481 cd dir
482 command
483 another_command
484 cd ..
485
277189c8 486NOTE: This only works with simple relative directories. Throw it an absolute dir or something with .. in it and things will go wrong.
7292dc67 487
488=cut
489
490sub cd {
491 my($self, $dir, @cmds) = @_;
492
2e65e370 493 return $self->SUPER::cd($dir, @cmds) unless $self->is_make_type('nmake');
7292dc67 494
495 my $cmd = join "\n\t", map "$_", @cmds;
496
277189c8 497 my $updirs = $self->catdir(map { $self->updir } $self->splitdir($dir));
498
7292dc67 499 # No leading tab and no trailing newline makes for easier embedding.
277189c8 500 my $make_frag = sprintf <<'MAKE_FRAG', $dir, $cmd, $updirs;
7292dc67 501cd %s
502 %s
277189c8 503 cd %s
7292dc67 504MAKE_FRAG
505
506 chomp $make_frag;
507
508 return $make_frag;
509}
510
511
479d2113 512=item max_exec_len
68dc0745 513
2c91f887 514nmake 1.50 limits command length to 2048 characters.
68dc0745 515
516=cut
517
479d2113 518sub max_exec_len {
519 my $self = shift;
520
2c91f887 521 return $self->{_MAX_EXEC_LEN} ||= 2 * 1024;
68dc0745 522}
523
524
dedf98bc 525=item os_flavor
526
527Windows is Win32.
528
529=cut
530
531sub os_flavor {
532 return('Win32');
533}
534
535
8b503b1a 536=item cflags
537
538Defines the PERLDLL symbol if we are configured for static building since all
539code destined for the perl5xx.dll must be compiled with the PERLDLL symbol
540defined.
541
542=cut
543
544sub cflags {
545 my($self,$libperl)=@_;
546 return $self->{CFLAGS} if $self->{CFLAGS};
547 return '' unless $self->needs_linking();
548
549 my $base = $self->SUPER::cflags($libperl);
550 foreach (split /\n/, $base) {
551 /^(\S*)\s*=\s*(\S*)$/ and $self->{$1} = $2;
552 };
553 $self->{CCFLAGS} .= " -DPERLDLL" if ($self->{LINKTYPE} eq 'static');
554
555 return $self->{CFLAGS} = qq{
556CCFLAGS = $self->{CCFLAGS}
557OPTIMIZE = $self->{OPTIMIZE}
558PERLTYPE = $self->{PERLTYPE}
559};
560
561}
562
2e65e370 563sub is_make_type {
564 my($self, $type) = @_;
565 return !! ($self->make =~ /\b$type(?:\.exe)?$/);
566}
567
68dc0745 5681;
569__END__
570
571=back
572
573=cut
574
5b0d9cbe 575