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