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