1 package Module::Build::Platform::Windows;
6 $VERSION = eval $VERSION;
13 use Module::Build::Base;
16 @ISA = qw(Module::Build::Base);
19 sub manpage_separator {
23 sub have_forkpipe { 0 }
26 my ($self, $value) = @_;
27 $value =~ s,^~(?= [/\\] | $ ),$ENV{HOME},x
32 sub ACTION_realclean {
35 $self->SUPER::ACTION_realclean();
37 my $basename = basename($0);
38 $basename =~ s/(?:\.bat)?$//i;
40 if ( lc $basename eq lc $self->build_script ) {
41 if ( $self->build_bat ) {
42 $self->log_info("Deleting $basename.bat\n");
43 my $full_progname = $0;
44 $full_progname =~ s/(?:\.bat)?$/.bat/i;
46 # Voodoo required to have a batch file delete itself without error;
47 # Syntax differs between 9x & NT: the later requires a null arg (???)
49 my $null_arg = (Win32::IsWinNT()) ? '""' : '';
50 my $cmd = qq(start $null_arg /min "\%comspec\%" /c del "$full_progname");
52 my $fh = IO::File->new(">> $basename.bat")
53 or die "Can't create $basename.bat: $!";
57 $self->delete_filetree($self->build_script . '.bat');
65 $self->SUPER::make_executable(@_);
67 foreach my $script (@_) {
70 if ( $script =~ /\.(bat|cmd)$/ ) {
71 $self->SUPER::make_executable($script);
74 # Perl script that needs to be wrapped in a batch script
77 if ( $script eq $self->build_script ) {
78 $opts{ntargs} = q(-x -S %0 --build_bat %*);
79 $opts{otherargs} = q(-x -S "%0" --build_bat %1 %2 %3 %4 %5 %6 %7 %8 %9);
82 my $out = eval {$self->pl2bat(in => $script, update => 1, %opts)};
84 $self->log_warn("WARNING: Unable to convert file '$script' to an executable script:\n$@");
86 $self->SUPER::make_executable($out);
92 # This routine was copied almost verbatim from the 'pl2bat' utility
93 # distributed with perl. It requires too much voodoo with shell quoting
94 # differences and shortcomings between the various flavors of Windows
95 # to reliably shell out
100 # NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
101 $opts{ntargs} = '-x -S %0 %*' unless exists $opts{ntargs};
102 $opts{otherargs} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $opts{otherargs};
104 $opts{stripsuffix} = '/\\.plx?/' unless exists $opts{stripsuffix};
105 $opts{stripsuffix} = ($opts{stripsuffix} =~ m{^/([^/]*[^/\$]|)\$?/?$} ? $1 : "\Q$opts{stripsuffix}\E");
107 unless (exists $opts{out}) {
108 $opts{out} = $opts{in};
109 $opts{out} =~ s/$opts{stripsuffix}$//oi;
110 $opts{out} .= '.bat' unless $opts{in} =~ /\.bat$/i or $opts{in} =~ /^-$/;
114 \@rem = '--*-Perl-*--
116 if "%OS%" == "Windows_NT" goto WinNT
117 perl $opts{otherargs}
121 if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
122 if %errorlevel% == 9009 echo You do not have Perl in your PATH.
123 if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
129 my $headlines = 2 + ($head =~ tr/\n/\n/);
130 my $tail = "\n__END__\n:endofperl\n";
137 my $start = $Config{startperl};
138 $start = "#!perl" unless $start =~ /^#!.*perl/;
140 my $in = IO::File->new("< $opts{in}") or die "Can't open $opts{in}: $!";
144 foreach my $line ( @file ) {
146 if ( $line =~ /^:endofperl\b/ ) {
147 if (!exists $opts{update}) {
148 warn "$opts{in} has already been converted to a batch file!\n";
153 if ( not $linedone and $line =~ /^#!.*perl/ ) {
154 if (exists $opts{update}) {
155 $skiplines = $linenum - 1;
156 $line .= "#line ".(1+$headlines)."\n";
158 $line .= "#line ".($linenum+$headlines)."\n";
162 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
167 my $out = IO::File->new("> $opts{out}") or die "Can't open $opts{out}: $!";
169 print $out $start, ( $opts{usewarnings} ? " -w" : "" ),
170 "\n#line ", ($headlines+1), "\n" unless $linedone;
171 print $out @file[$skiplines..$#file];
172 print $out $tail unless $taildone;
180 # Returns a string that can become [part of] a command line with
181 # proper quoting so that the subprocess sees this same list of args.
182 my ($self, @args) = @_;
187 if ( /^[^\s*?!\$<>;|'"\[\]\{\}]+$/ ) {
191 # XXX this will obviously have to improve - is there already a
192 # core module lying around that does proper quoting?
194 push @quoted, qq("$_");
198 return join " ", @quoted;
202 sub split_like_shell {
203 # As it turns out, Windows command-parsing is very different from
204 # Unix command-parsing. Double-quotes mean different things,
205 # backslashes don't necessarily mean escapes, and so on. So we
206 # can't use Text::ParseWords::shellwords() to break a command string
207 # into words. The algorithm below was bashed out by Randy and Ken
208 # (mostly Randy), and there are a lot of regression tests, so we
209 # should feel free to adjust if desired.
211 (my $self, local $_) = @_;
213 return @$_ if defined() && UNIVERSAL::isa($_, 'ARRAY');
216 return @argv unless defined() && length();
219 my( $i, $quote_mode ) = ( 0, 0 );
221 while ( $i < length() ) {
223 my $ch = substr( $_, $i , 1 );
224 my $next_ch = substr( $_, $i+1, 1 );
226 if ( $ch eq '\\' && $next_ch eq '"' ) {
229 } elsif ( $ch eq '\\' && $next_ch eq '\\' ) {
232 } elsif ( $ch eq '"' && $next_ch eq '"' && $quote_mode ) {
233 $quote_mode = !$quote_mode;
236 } elsif ( $ch eq '"' && $next_ch eq '"' && !$quote_mode &&
237 ( $i + 2 == length() ||
238 substr( $_, $i + 2, 1 ) eq ' ' )
239 ) { # for cases like: a"" => [ 'a' ]
243 } elsif ( $ch eq '"' ) {
244 $quote_mode = !$quote_mode;
245 } elsif ( $ch eq ' ' && !$quote_mode ) {
246 push( @argv, $arg ) if $arg;
248 ++$i while substr( $_, $i + 1, 1 ) eq ' ';
256 push( @argv, $arg ) if defined( $arg ) && length( $arg );
261 # system(@cmd) does not like having double-quotes in it on Windows.
262 # So we quote them and run it as a single command.
264 my ($self, @cmd) = @_;
266 my $cmd = $self->_quote_args(@cmd);
267 my $status = system($cmd);
268 if ($status and $! =~ /Argument list too long/i) {
269 my $env_entries = '';
270 foreach (sort keys %ENV) { $env_entries .= "$_=>".length($ENV{$_})."; " }
271 warn "'Argument list' was 'too long', env lengths are $env_entries";
283 Module::Build::Platform::Windows - Builder class for Windows platforms
287 The sole purpose of this module is to inherit from
288 C<Module::Build::Base> and override a few methods. Please see
289 L<Module::Build> for the docs.
293 Ken Williams <kwilliams@cpan.org>, Randy W. Sims <RandyS@ThePierianSpring.org>
297 perl(1), Module::Build(3)