1 package Module::Build::Platform::Windows;
10 use Module::Build::Base;
13 @ISA = qw(Module::Build::Base);
16 sub manpage_separator {
20 sub ACTION_realclean {
23 $self->SUPER::ACTION_realclean();
25 my $basename = basename($0);
26 $basename =~ s/(?:\.bat)?$//i;
28 if ( $basename eq $self->build_script ) {
29 if ( $self->build_bat ) {
30 my $full_progname = $0;
31 $full_progname =~ s/(?:\.bat)?$/.bat/i;
33 # Vodoo required to have a batch file delete itself without error;
34 # Syntax differs between 9x & NT: the later requires a null arg (???)
36 my $null_arg = (Win32::IsWinNT()) ? '""' : '';
37 my $cmd = qq(start $null_arg /min "\%comspec\%" /c del "$full_progname");
39 my $fh = IO::File->new(">> $basename.bat")
40 or die "Can't create $basename.bat: $!";
44 $self->delete_filetree($self->build_script . '.bat');
52 $self->SUPER::make_executable(@_);
54 foreach my $script (@_) {
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);
61 my $out = eval {$self->pl2bat(in => $script, update => 1, %opts)};
63 $self->log_warn("WARNING: Unable to convert file '$script' to an executable script:\n$@");
65 $self->SUPER::make_executable($out);
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
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};
82 $opts{stripsuffix} = '/\\.plx?/' unless exists $opts{stripsuffix};
83 $opts{stripsuffix} = ($opts{stripsuffix} =~ m{^/([^/]*[^/\$]|)\$?/?$} ? $1 : "\Q$opts{stripsuffix}\E");
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} =~ /^-$/;
94 if "%OS%" == "Windows_NT" goto WinNT
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
107 my $headlines = 2 + ($head =~ tr/\n/\n/);
108 my $tail = "\n__END__\n:endofperl\n";
115 my $start = $Config{startperl};
116 $start = "#!perl" unless $start =~ /^#!.*perl/;
118 my $in = IO::File->new("< $opts{in}") or die "Can't open $opts{in}: $!";
122 foreach my $line ( @file ) {
124 if ( $line =~ /^:endofperl\b/ ) {
125 if (!exists $opts{update}) {
126 warn "$opts{in} has already been converted to a batch file!\n";
131 if ( not $linedone and $line =~ /^#!.*perl/ ) {
132 if (exists $opts{update}) {
133 $skiplines = $linenum - 1;
134 $line .= "#line ".(1+$headlines)."\n";
136 $line .= "#line ".($linenum+$headlines)."\n";
140 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
145 my $out = IO::File->new("> $opts{out}") or die "Can't open $opts{out}: $!";
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;
157 sub 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.
166 (my $self, local $_) = @_;
168 return @$_ if defined() && UNIVERSAL::isa($_, 'ARRAY');
171 return @argv unless defined() && length();
174 my( $i, $quote_mode ) = ( 0, 0 );
176 while ( $i < length() ) {
178 my $ch = substr( $_, $i , 1 );
179 my $next_ch = substr( $_, $i+1, 1 );
181 if ( $ch eq '\\' && $next_ch eq '"' ) {
184 } elsif ( $ch eq '\\' && $next_ch eq '\\' ) {
187 } elsif ( $ch eq '"' && $next_ch eq '"' && $quote_mode ) {
188 $quote_mode = !$quote_mode;
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' ]
198 } elsif ( $ch eq '"' ) {
199 $quote_mode = !$quote_mode;
200 } elsif ( $ch eq ' ' && !$quote_mode ) {
201 push( @argv, $arg ) if $arg;
203 ++$i while substr( $_, $i + 1, 1 ) eq ' ';
211 push( @argv, $arg ) if defined( $arg ) && length( $arg );
221 Module::Build::Platform::Windows - Builder class for Windows platforms
225 The sole purpose of this module is to inherit from
226 C<Module::Build::Base> and override a few methods. Please see
227 L<Module::Build> for the docs.
231 Ken Williams <ken@cpan.org>, Randy W. Sims <RandyS@ThePierianSpring.org>
235 perl(1), Module::Build(3)