1 package Module::Build::Platform::Windows;
10 use Module::Build::Base;
13 @ISA = qw(Module::Build::Base);
16 sub manpage_separator {
20 sub have_forkpipe { 0 }
22 sub ACTION_realclean {
25 $self->SUPER::ACTION_realclean();
27 my $basename = basename($0);
28 $basename =~ s/(?:\.bat)?$//i;
30 if ( $basename eq $self->build_script ) {
31 if ( $self->build_bat ) {
32 my $full_progname = $0;
33 $full_progname =~ s/(?:\.bat)?$/.bat/i;
35 # Vodoo required to have a batch file delete itself without error;
36 # Syntax differs between 9x & NT: the later requires a null arg (???)
38 my $null_arg = (Win32::IsWinNT()) ? '""' : '';
39 my $cmd = qq(start $null_arg /min "\%comspec\%" /c del "$full_progname");
41 my $fh = IO::File->new(">> $basename.bat")
42 or die "Can't create $basename.bat: $!";
46 $self->delete_filetree($self->build_script . '.bat');
54 $self->SUPER::make_executable(@_);
56 foreach my $script (@_) {
59 if ( $script =~ /\.(bat|cmd)$/ ) {
60 $self->SUPER::make_executable($script);
63 # Perl script that needs to be wrapped in a batch script
66 if ( $script eq $self->build_script ) {
67 $opts{ntargs} = q(-x -S %0 --build_bat %*);
68 $opts{otherargs} = q(-x -S "%0" --build_bat %1 %2 %3 %4 %5 %6 %7 %8 %9);
71 my $out = eval {$self->pl2bat(in => $script, update => 1, %opts)};
73 $self->log_warn("WARNING: Unable to convert file '$script' to an executable script:\n$@");
75 $self->SUPER::make_executable($out);
81 # This routine was copied almost verbatim from the 'pl2bat' utility
82 # distributed with perl. It requires too much vodoo with shell quoting
83 # differences and shortcomings between the various flavors of Windows
84 # to reliably shell out
89 # NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
90 $opts{ntargs} = '-x -S %0 %*' unless exists $opts{ntargs};
91 $opts{otherargs} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $opts{otherargs};
93 $opts{stripsuffix} = '/\\.plx?/' unless exists $opts{stripsuffix};
94 $opts{stripsuffix} = ($opts{stripsuffix} =~ m{^/([^/]*[^/\$]|)\$?/?$} ? $1 : "\Q$opts{stripsuffix}\E");
96 unless (exists $opts{out}) {
97 $opts{out} = $opts{in};
98 $opts{out} =~ s/$opts{stripsuffix}$//oi;
99 $opts{out} .= '.bat' unless $opts{in} =~ /\.bat$/i or $opts{in} =~ /^-$/;
103 \@rem = '--*-Perl-*--
105 if "%OS%" == "Windows_NT" goto WinNT
106 perl $opts{otherargs}
110 if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
111 if %errorlevel% == 9009 echo You do not have Perl in your PATH.
112 if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
118 my $headlines = 2 + ($head =~ tr/\n/\n/);
119 my $tail = "\n__END__\n:endofperl\n";
126 my $start = $Config{startperl};
127 $start = "#!perl" unless $start =~ /^#!.*perl/;
129 my $in = IO::File->new("< $opts{in}") or die "Can't open $opts{in}: $!";
133 foreach my $line ( @file ) {
135 if ( $line =~ /^:endofperl\b/ ) {
136 if (!exists $opts{update}) {
137 warn "$opts{in} has already been converted to a batch file!\n";
142 if ( not $linedone and $line =~ /^#!.*perl/ ) {
143 if (exists $opts{update}) {
144 $skiplines = $linenum - 1;
145 $line .= "#line ".(1+$headlines)."\n";
147 $line .= "#line ".($linenum+$headlines)."\n";
151 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
156 my $out = IO::File->new("> $opts{out}") or die "Can't open $opts{out}: $!";
158 print $out $start, ( $opts{usewarnings} ? " -w" : "" ),
159 "\n#line ", ($headlines+1), "\n" unless $linedone;
160 print $out @file[$skiplines..$#file];
161 print $out $tail unless $taildone;
168 sub split_like_shell {
169 # As it turns out, Windows command-parsing is very different from
170 # Unix command-parsing. Double-quotes mean different things,
171 # backslashes don't necessarily mean escapes, and so on. So we
172 # can't use Text::ParseWords::shellwords() to break a command string
173 # into words. The algorithm below was bashed out by Randy and Ken
174 # (mostly Randy), and there are a lot of regression tests, so we
175 # should feel free to adjust if desired.
177 (my $self, local $_) = @_;
179 return @$_ if defined() && UNIVERSAL::isa($_, 'ARRAY');
182 return @argv unless defined() && length();
185 my( $i, $quote_mode ) = ( 0, 0 );
187 while ( $i < length() ) {
189 my $ch = substr( $_, $i , 1 );
190 my $next_ch = substr( $_, $i+1, 1 );
192 if ( $ch eq '\\' && $next_ch eq '"' ) {
195 } elsif ( $ch eq '\\' && $next_ch eq '\\' ) {
198 } elsif ( $ch eq '"' && $next_ch eq '"' && $quote_mode ) {
199 $quote_mode = !$quote_mode;
202 } elsif ( $ch eq '"' && $next_ch eq '"' && !$quote_mode &&
203 ( $i + 2 == length() ||
204 substr( $_, $i + 2, 1 ) eq ' ' )
205 ) { # for cases like: a"" => [ 'a' ]
209 } elsif ( $ch eq '"' ) {
210 $quote_mode = !$quote_mode;
211 } elsif ( $ch eq ' ' && !$quote_mode ) {
212 push( @argv, $arg ) if $arg;
214 ++$i while substr( $_, $i + 1, 1 ) eq ' ';
222 push( @argv, $arg ) if defined( $arg ) && length( $arg );
232 Module::Build::Platform::Windows - Builder class for Windows platforms
236 The sole purpose of this module is to inherit from
237 C<Module::Build::Base> and override a few methods. Please see
238 L<Module::Build> for the docs.
242 Ken Williams <ken@cpan.org>, Randy W. Sims <RandyS@ThePierianSpring.org>
246 perl(1), Module::Build(3)