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