20 my $has_cpan_perl_releases;
23 if (eval { require CPAN::Perl::Releases; 1 }) {
24 print "Will use CPAN::Perl::Releases\n";
25 $has_cpan_perl_releases = 1;
30 getopts('ft:m:', \%opts);
32 my $cpan_mirror = 'cpan.cpantesters.org';
39 local $SIG{'INT'} = sub { exit 1 };
48 my ($path, $indent, $keep) = @args{qw<path indent keep>};
50 die "Path $path already exists" if -e $path;
51 File::Path::mkpath($path);
55 indent => $indent || 0,
62 eval "sub $_ { \$_[0]->{$_} }; 1" or die $@ for qw<path indent>;
65 sub keep { @_ > 1 ? $_[0]->{keep} = $_[1] : $_[0]->{keep} }
70 return if $self->keep;
72 my $path = $self->path;
73 return unless -e $path;
75 my $indent = $self->indent;
76 $indent = ' ' x (2 * $indent);
78 print "${indent}Cleaning up path $path... ";
79 File::Path::remove_tree($path);
85 my $num_version = shift;
87 my $obj = version->parse($num_version);
88 my $pretty_version = $obj->normal;
89 $pretty_version =~ s/^v?//;
91 my ($int, $frac) = split /\./, $num_version, 2;
93 die 'Wrong fractional part' if length $frac > 6;
94 $frac .= '0' x (6 - length $frac);
96 "$int$frac" => [ $num_version, $pretty_version ];
99 my %perls = map key_version($_),
100 grep "$_" >= '5.010001',
101 keys %Module::CoreList::released;
106 my $start = [ Time::HiRes::gettimeofday ];
108 my $ff = File::Fetch->new(uri => $uri);
109 my $file = $ff->fetch(to => $to) or die "Could not fetch $uri: " . $ff->error;
111 my $elapsed = Time::HiRes::tv_interval($start);
113 return $file, $elapsed;
116 sub perl_archive_for {
121 if ($has_cpan_perl_releases) {
122 my $tarballs = CPAN::Perl::Releases::perl_tarballs($version);
124 if (defined $tarballs) {
125 $path = $tarballs->{'tar.gz'};
128 my $uri = "http://search.cpan.org/dist/perl-$version";
131 fetch_uri($uri => \$_);
133 if (m{id/(([^/])/\2([^/])/\2\3[^/]*/perl-\Q$version\E\.tar\.(?:gz|bz2))}) {
139 my ($file) = ($path =~ m{([^/]*)$});
140 return "http://$cpan_mirror/authors/id/$path", $file;
142 die "Could not infer the archive for perl $version";
147 my ($size, $seconds) = @_;
149 my $speed = $size / $seconds;
152 while ($speed >= 1024) {
157 $speed = sprintf '%.02f', $speed;
159 my $unit = ('', 'K', 'M', 'G', 'T', 'P')[$order] . 'B/s';
161 return $speed, $unit;
167 open my $fh, '>', $file or die "Can't open $file for writing: $!";
170 File::Path::mkpath($target) unless -e $target;
172 my $tmp_dir = File::Spec->catdir($target, 'tmp');
174 sub fetch_source_file {
175 my ($file, $version, $dir) = @_;
177 my $INDENT = ' ' x 4;
179 print "${INDENT}Looking for the full name of the perl archive... ";
180 my ($archive_uri, $archive_file) = perl_archive_for($version);
181 print "$archive_uri\n";
183 if (-e File::Spec->catfile($tmp_dir, $archive_file)) {
184 print "${INDENT}$archive_file was already fetched\n";
186 print "${INDENT}Fetching $archive_uri... ";
187 ($archive_file, my $elapsed) = fetch_uri($archive_uri => $tmp_dir);
188 my ($speed, $unit) = bandwidth(-s $archive_file, $elapsed);
189 print "done at $speed$unit\n";
192 my $extract_path = File::Spec->catfile($tmp_dir, "perl-$version");
193 if (-e $extract_path) {
194 print "${INDENT}$archive_file was already extracted\n";
196 print "${INDENT}Extracting $archive_file... ";
197 my $ae = Archive::Extract->new(archive => $archive_file);
198 $ae->extract(to => $tmp_dir)
199 or die "Could not extract $archive_file: " . $ae->error;
200 $extract_path = $ae->extract_path;
204 File::Path::mkpath($dir) unless -e $dir;
205 print "${INDENT}Copying $file to $dir... ";
206 my $src = File::Spec->catfile($extract_path, $file);
207 my $dst = File::Spec->catfile($dir, $file);
209 File::Copy::copy($src => $dst) or die "Can't copy $src to $dst: $!";
214 print "not needed\n";
220 my %expected_chunks = (
225 ('COMP_NODE_HOOK') x 3,
234 my ($line, $file) = @_;
236 if ($line =~ /#\s*include\s+"INTERN\.h"/) {
237 push @{$patched_chunks{$file}}, 're_defs';
238 return "#include \"re_defs.h\"\n";
239 } elsif ($line =~ /^(\s*)RExC_rxi\s*=\s*ri\s*;\s*$/) {
240 push @{$patched_chunks{$file}}, 'COMP_BEGIN_HOOK';
241 return $line, "$1REH_CALL_COMP_BEGIN_HOOK(pRExC_state->rx);\n";
242 } elsif ($line =~ /FILL_ADVANCE_NODE(_ARG)?\(\s*([^\s,\)]+)/) {
243 my $shift = $1 ? 2 : 1;
244 push @{$patched_chunks{$file}}, 'COMP_NODE_HOOK';
245 return $line, " REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, ($2) - $shift);\n"
246 } elsif ($line =~ /end node insert/) {
247 push @{$patched_chunks{$file}}, 'COMP_NODE_HOOK';
248 return $line, " REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, convert);\n";
249 } elsif ($line =~ /&PL_core_reg_engine/) {
250 $line =~ s/&PL_core_reg_engine\b/&reh_regexp_engine/g;
258 my ($line, $file) = @_;
260 if ($line =~ /#\s*include\s+"perl\.h"/) {
261 push @{$patched_chunks{$file}}, 're_defs';
262 return $line, "#include \"re_defs.h\"\n";
263 } elsif ($line =~ /^\s*reenter_switch:\s*$/) {
264 push @{$patched_chunks{$file}}, 'EXEC_NODE_HOOK';
265 return "\tREH_CALL_EXEC_NODE_HOOK(rex, scan, reginfo, st);\n", $line;
272 'regcomp.c' => \&patch_regcomp,
273 'regexec.c' => \&patch_regexec,
276 sub patch_source_file {
277 my ($src, $dst) = @_;
279 my $file = (File::Spec->splitpath($src))[2];
281 $dst = File::Spec->catfile($dst, $file);
284 my $mangler = $manglers{$file};
286 File::Copy::copy($src => $dst) or die "Can't copy $src to $dst: $!";
290 open my $in, '<', $src or die "Can't open $src for reading: $!";
291 open my $out, '>', $dst or die "Can't open $dst for writing: $!";
293 while (defined(my $line = <$in>)) {
294 print $out $mangler->($line, $dst);
297 my $patched_chunks = join ' ', @{$patched_chunks{$dst}};
298 my $expected_chunks = join ' ', @{$expected_chunks{$file}};
299 unless ($patched_chunks eq $expected_chunks) {
300 die "File $dst was not properly patched (got \"$patched_chunks\", expected \"$expected_chunks\")\n";
306 for my $tag (sort { $a <=> $b } keys %perls) {
307 my ($num_version, $pretty_version) = @{$perls{$tag}};
309 my $dir = File::Spec->catdir($target, $tag);
311 print "Working on perl $pretty_version\n";
313 my $tmp_guard = Guard::Path->new(path => $tmp_dir);
315 my $orig_dir = File::Spec->catdir($dir, 'orig');
317 my @files = qw<regcomp.c regexec.c>;
318 push @files, 'dquote_static.c' if $num_version >= 5.013_006;
319 push @files, 'inline_invlist.c' if $num_version >= 5.017_004;
320 for my $file (@files) {
321 my $orig_file = File::Spec->catfile($orig_dir, $file);
323 print " Already have original $file\n";
325 print " Need to get original $file\n";
326 fetch_source_file($file, $pretty_version => $orig_dir);
330 if (not $opts{f} and -e File::Spec->catfile($dir, $file)) {
331 print " Already have patched $file\n";
333 print " Need to patch $file... ";
334 my $res = patch_source_file($orig_file => $dir);
335 print $res ? "done\n" : "nothing to do\n";
342 print 'Updating MANIFEST... ';
346 open my $in, '<', 'MANIFEST' or die "Can't open MANIFEST for reading: $!";
347 @manifest_files = grep !m{^src/.*\.c$}, <$in>;
350 my @source_files = map "$_\n", glob 'src/*/*.c';
352 open my $out, '>', 'MANIFEST' or die "Can't open MANIFEST for writing: $!";
353 print $out sort @manifest_files, @source_files;