This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add Module::Build 0.27_08
[perl5.git] / lib / Module / Build / Platform / Windows.pm
CommitLineData
bb4e9162
YST
1package Module::Build::Platform::Windows;
2
3use strict;
4
5use Config;
6use File::Basename;
7use File::Spec;
8use IO::File;
9
10use Module::Build::Base;
11
12use vars qw(@ISA);
13@ISA = qw(Module::Build::Base);
14
15
16sub manpage_separator {
17 return '.';
18}
19
20sub ACTION_realclean {
21 my ($self) = @_;
22
23 $self->SUPER::ACTION_realclean();
24
25 my $basename = basename($0);
26 $basename =~ s/(?:\.bat)?$//i;
27
28 if ( $basename eq $self->build_script ) {
29 if ( $self->build_bat ) {
30 my $full_progname = $0;
31 $full_progname =~ s/(?:\.bat)?$/.bat/i;
32
33 # Vodoo required to have a batch file delete itself without error;
34 # Syntax differs between 9x & NT: the later requires a null arg (???)
35 require Win32;
36 my $null_arg = (Win32::IsWinNT()) ? '""' : '';
37 my $cmd = qq(start $null_arg /min "\%comspec\%" /c del "$full_progname");
38
39 my $fh = IO::File->new(">> $basename.bat")
40 or die "Can't create $basename.bat: $!";
41 print $fh $cmd;
42 close $fh ;
43 } else {
44 $self->delete_filetree($self->build_script . '.bat');
45 }
46 }
47}
48
49sub make_executable {
50 my $self = shift;
51
52 $self->SUPER::make_executable(@_);
53
54 foreach my $script (@_) {
55 my %opts = ();
56 if ( $script eq $self->build_script ) {
57 $opts{ntargs} = q(-x -S %0 --build_bat %*);
58 $opts{otherargs} = q(-x -S "%0" --build_bat %1 %2 %3 %4 %5 %6 %7 %8 %9);
59 }
60
61 my $out = eval {$self->pl2bat(in => $script, update => 1, %opts)};
62 if ( $@ ) {
63 $self->log_warn("WARNING: Unable to convert file '$script' to an executable script:\n$@");
64 } else {
65 $self->SUPER::make_executable($out);
66 }
67 }
68}
69
70# This routine was copied almost verbatim from the 'pl2bat' utility
71# distributed with perl. It requires too much vodoo with shell quoting
72# differences and shortcomings between the various flavors of Windows
73# to reliably shell out
74sub pl2bat {
75 my $self = shift;
76 my %opts = @_;
77
78 # NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
79 $opts{ntargs} = '-x -S %0 %*' unless exists $opts{ntargs};
80 $opts{otherargs} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $opts{otherargs};
81
82 $opts{stripsuffix} = '/\\.plx?/' unless exists $opts{stripsuffix};
83 $opts{stripsuffix} = ($opts{stripsuffix} =~ m{^/([^/]*[^/\$]|)\$?/?$} ? $1 : "\Q$opts{stripsuffix}\E");
84
85 unless (exists $opts{out}) {
86 $opts{out} = $opts{in};
87 $opts{out} =~ s/$opts{stripsuffix}$//oi;
88 $opts{out} .= '.bat' unless $opts{in} =~ /\.bat$/i or $opts{in} =~ /^-$/;
89 }
90
91 my $head = <<EOT;
92 \@rem = '--*-Perl-*--
93 \@echo off
94 if "%OS%" == "Windows_NT" goto WinNT
95 perl $opts{otherargs}
96 goto endofperl
97 :WinNT
98 perl $opts{ntargs}
99 if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
100 if %errorlevel% == 9009 echo You do not have Perl in your PATH.
101 if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
102 goto endofperl
103 \@rem ';
104EOT
105
106 $head =~ s/^\s+//gm;
107 my $headlines = 2 + ($head =~ tr/\n/\n/);
108 my $tail = "\n__END__\n:endofperl\n";
109
110 my $linedone = 0;
111 my $taildone = 0;
112 my $linenum = 0;
113 my $skiplines = 0;
114
115 my $start = $Config{startperl};
116 $start = "#!perl" unless $start =~ /^#!.*perl/;
117
118 my $in = IO::File->new("< $opts{in}") or die "Can't open $opts{in}: $!";
119 my @file = <$in>;
120 $in->close;
121
122 foreach my $line ( @file ) {
123 $linenum++;
124 if ( $line =~ /^:endofperl\b/ ) {
125 if (!exists $opts{update}) {
126 warn "$opts{in} has already been converted to a batch file!\n";
127 return;
128 }
129 $taildone++;
130 }
131 if ( not $linedone and $line =~ /^#!.*perl/ ) {
132 if (exists $opts{update}) {
133 $skiplines = $linenum - 1;
134 $line .= "#line ".(1+$headlines)."\n";
135 } else {
136 $line .= "#line ".($linenum+$headlines)."\n";
137 }
138 $linedone++;
139 }
140 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
141 $line = "";
142 }
143 }
144
145 my $out = IO::File->new("> $opts{out}") or die "Can't open $opts{out}: $!";
146 print $out $head;
147 print $out $start, ( $opts{usewarnings} ? " -w" : "" ),
148 "\n#line ", ($headlines+1), "\n" unless $linedone;
149 print $out @file[$skiplines..$#file];
150 print $out $tail unless $taildone;
151 $out->close;
152
153 return $opts{out};
154}
155
156
157sub split_like_shell {
158 # As it turns out, Windows command-parsing is very different from
159 # Unix command-parsing. Double-quotes mean different things,
160 # backslashes don't necessarily mean escapes, and so on. So we
161 # can't use Text::ParseWords::shellwords() to break a command string
162 # into words. The algorithm below was bashed out by Randy and Ken
163 # (mostly Randy), and there are a lot of regression tests, so we
164 # should feel free to adjust if desired.
165
166 (my $self, local $_) = @_;
167
168 return @$_ if defined() && UNIVERSAL::isa($_, 'ARRAY');
169
170 my @argv;
171 return @argv unless defined() && length();
172
173 my $arg = '';
174 my( $i, $quote_mode ) = ( 0, 0 );
175
176 while ( $i < length() ) {
177
178 my $ch = substr( $_, $i , 1 );
179 my $next_ch = substr( $_, $i+1, 1 );
180
181 if ( $ch eq '\\' && $next_ch eq '"' ) {
182 $arg .= '"';
183 $i++;
184 } elsif ( $ch eq '\\' && $next_ch eq '\\' ) {
185 $arg .= '\\';
186 $i++;
187 } elsif ( $ch eq '"' && $next_ch eq '"' && $quote_mode ) {
188 $quote_mode = !$quote_mode;
189 $arg .= '"';
190 $i++;
191 } elsif ( $ch eq '"' && $next_ch eq '"' && !$quote_mode &&
192 ( $i + 2 == length() ||
193 substr( $_, $i + 2, 1 ) eq ' ' )
194 ) { # for cases like: a"" => [ 'a' ]
195 push( @argv, $arg );
196 $arg = '';
197 $i += 2;
198 } elsif ( $ch eq '"' ) {
199 $quote_mode = !$quote_mode;
200 } elsif ( $ch eq ' ' && !$quote_mode ) {
201 push( @argv, $arg ) if $arg;
202 $arg = '';
203 ++$i while substr( $_, $i + 1, 1 ) eq ' ';
204 } else {
205 $arg .= $ch;
206 }
207
208 $i++;
209 }
210
211 push( @argv, $arg ) if defined( $arg ) && length( $arg );
212 return @argv;
213}
214
2151;
216
217__END__
218
219=head1 NAME
220
221Module::Build::Platform::Windows - Builder class for Windows platforms
222
223=head1 DESCRIPTION
224
225The sole purpose of this module is to inherit from
226C<Module::Build::Base> and override a few methods. Please see
227L<Module::Build> for the docs.
228
229=head1 AUTHOR
230
231Ken Williams <ken@cpan.org>, Randy W. Sims <RandyS@ThePierianSpring.org>
232
233=head1 SEE ALSO
234
235perl(1), Module::Build(3)
236
237=cut