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