23 my $old_fh = select STDOUT;
28 my $has_cpan_perl_releases;
31 if (eval { require CPAN::Perl::Releases; 1 }) {
32 print "Will use CPAN::Perl::Releases\n";
33 $has_cpan_perl_releases = 1;
38 getopts('ft:m:', \%opts);
40 my $cpan_mirror = 'cpan.cpantesters.org';
47 local $SIG{'INT'} = sub { exit 1 };
56 my ($path, $indent, $keep) = @args{qw<path indent keep>};
58 die "Path $path already exists" if -e $path;
59 File::Path::mkpath($path);
63 indent => $indent || 0,
70 eval "sub $_ { \$_[0]->{$_} }; 1" or die $@ for qw<path indent>;
73 sub keep { @_ > 1 ? $_[0]->{keep} = $_[1] : $_[0]->{keep} }
78 return if $self->keep;
80 my $path = $self->path;
81 return unless -e $path;
83 my $indent = $self->indent;
84 $indent = ' ' x (2 * $indent);
86 print "${indent}Cleaning up path $path... ";
87 File::Path::remove_tree($path);
93 my $num_version = shift;
95 my $obj = version->parse($num_version);
96 my $pretty_version = $obj->normal;
97 $pretty_version =~ s/^v?//;
99 my ($int, $frac) = split /\./, $num_version, 2;
101 die 'Wrong fractional part' if length $frac > 6;
102 $frac .= '0' x (6 - length $frac);
104 "$int$frac" => [ $num_version, $pretty_version ];
107 my %perls = map key_version($_),
108 grep "$_" >= '5.010001',
109 keys %Module::CoreList::released;
112 package LWP::MyUserAgent;
115 BEGIN { push @ISA, 'LWP::UserAgent' }
121 my ($class, $cb) = @_;
123 my $ua = bless LWP::UserAgent->new, $class;
125 $ua->show_progress(1) if $cb;
127 my $tag = Scalar::Util::refaddr($ua);
135 my ($ua, $stat, $r) = @_;
137 my $tag = Scalar::Util::refaddr($ua);
142 if ($stat eq 'begin') {
144 } elsif ($stat eq 'end') {
146 } elsif ($stat eq 'tick') {
147 my $tick = ++$ticks{$tag};
148 $desc = qw<- \ | />[$tick % 4];
150 $desc = sprintf "%.01f%%", 100 * $stat;
161 my $tag = Scalar::Util::refaddr($ua);
171 my ($uri, $to, $cb) = @_;
173 my $start = [ Time::HiRes::gettimeofday ];
175 my $ua = LWP::MyUserAgent->new($cb);
176 my $res = $ua->get($uri);
177 die "Could not fetch $uri: " . $res->status_line unless $res->is_success;
180 my $file = (File::Spec::Unix->splitpath(URI->new($uri)->path))[2];
181 my $vol = (File::Spec->splitpath($here))[0];
182 $to = File::Spec->catdir($here, $to)
183 unless File::Spec->file_name_is_absolute($to);
184 $file = File::Spec->catpath($vol, $to, $file);
186 open my $fh, '>', $file or die "Can't open $file for writing: $!";
187 print $fh $res->content;
188 close $fh or die "Can't close $file: $!";
190 my $elapsed = Time::HiRes::tv_interval($start);
192 return $file, $elapsed;
195 sub perl_archive_for {
200 if ($has_cpan_perl_releases) {
201 my $tarballs = CPAN::Perl::Releases::perl_tarballs($version);
203 if (defined $tarballs) {
204 $path = $tarballs->{'tar.gz'};
207 my $uri = "http://search.cpan.org/dist/perl-$version";
210 fetch_uri($uri => \$_);
212 if (m{id/(([^/])/\2([^/])/\2\3[^/]*/perl-\Q$version\E\.tar\.(?:gz|bz2))}) {
218 my ($file) = ($path =~ m{([^/]*)$});
219 return "http://$cpan_mirror/authors/id/$path", $file;
221 die "Could not infer the archive for perl $version";
226 my ($size, $seconds) = @_;
228 my $speed = $size / $seconds;
231 while ($speed >= 1024) {
236 $speed = sprintf '%.02f', $speed;
238 my $unit = ('', 'K', 'M', 'G', 'T', 'P')[$order] . 'B/s';
240 return $speed, $unit;
246 open my $fh, '>', $file or die "Can't open $file for writing: $!";
249 File::Path::mkpath($target) unless -e $target;
251 my $tmp_dir = File::Spec->catdir($target, 'tmp');
253 sub fetch_source_file {
254 my ($file, $version, $dir) = @_;
256 my $INDENT = ' ' x 4;
258 print "${INDENT}Looking for the full name of the perl archive... ";
259 my ($archive_uri, $archive_file) = perl_archive_for($version);
260 print "$archive_uri\n";
262 if (-e File::Spec->catfile($tmp_dir, $archive_file)) {
263 print "${INDENT}$archive_file was already fetched\n";
265 print "${INDENT}Fetching $archive_uri...\n";
268 my ($stat, $desc) = @_;
269 $desc = '0%' if $stat eq 'begin';
270 my $len = length $desc;
272 if ($len > $maxlen) {
275 $extra = ' ' x ($maxlen - $len);
277 print "\r${INDENT} In progress... $desc$extra";
278 print "$extra\n" if $stat eq 'end';
280 ($archive_file, my $elapsed) = fetch_uri($archive_uri => $tmp_dir, $cb);
281 my ($speed, $unit) = bandwidth(-s $archive_file, $elapsed);
282 print "${INDENT} File downloaded at $speed$unit\n";
285 my $extract_path = File::Spec->catfile($tmp_dir, "perl-$version");
286 if (-e $extract_path) {
287 print "${INDENT}$archive_file was already extracted\n";
289 print "${INDENT}Extracting $archive_file... ";
290 my $ae = Archive::Extract->new(archive => $archive_file);
291 $ae->extract(to => $tmp_dir)
292 or die "Could not extract $archive_file: " . $ae->error;
293 $extract_path = $ae->extract_path;
297 File::Path::mkpath($dir) unless -e $dir;
298 print "${INDENT}Copying $file to $dir... ";
299 my $src = File::Spec->catfile($extract_path, $file);
300 my $dst = File::Spec->catfile($dir, $file);
302 File::Copy::copy($src => $dst) or die "Can't copy $src to $dst: $!";
307 print "not needed\n";
313 my %expected_chunks = (
318 ('COMP_NODE_HOOK') x 3,
327 my ($line, $file) = @_;
329 if ($line =~ /#\s*include\s+"INTERN\.h"/) {
330 push @{$patched_chunks{$file}}, 're_defs';
331 return "#include \"re_defs.h\"\n";
332 } elsif ($line =~ /^(\s*)RExC_rxi\s*=\s*ri\s*;\s*$/) {
333 push @{$patched_chunks{$file}}, 'COMP_BEGIN_HOOK';
334 return $line, "$1REH_CALL_COMP_BEGIN_HOOK(pRExC_state->rx);\n";
335 } elsif ($line =~ /FILL_ADVANCE_NODE(_ARG)?\(\s*([^\s,\)]+)/) {
336 my $shift = $1 ? 2 : 1;
337 push @{$patched_chunks{$file}}, 'COMP_NODE_HOOK';
338 return $line, " REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, ($2) - $shift);\n"
339 } elsif ($line =~ /end node insert/) {
340 push @{$patched_chunks{$file}}, 'COMP_NODE_HOOK';
341 return $line, " REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, convert);\n";
342 } elsif ($line =~ /&PL_core_reg_engine/) {
343 $line =~ s/&PL_core_reg_engine\b/&reh_regexp_engine/g;
351 my ($line, $file) = @_;
353 if ($line =~ /#\s*include\s+"perl\.h"/) {
354 push @{$patched_chunks{$file}}, 're_defs';
355 return $line, "#include \"re_defs.h\"\n";
356 } elsif ($line =~ /^\s*reenter_switch:\s*$/) {
357 push @{$patched_chunks{$file}}, 'EXEC_NODE_HOOK';
358 return "\tREH_CALL_EXEC_NODE_HOOK(rex, scan, reginfo, st);\n", $line;
365 'regcomp.c' => \&patch_regcomp,
366 'regexec.c' => \&patch_regexec,
369 sub patch_source_file {
370 my ($src, $dst) = @_;
372 my $file = (File::Spec->splitpath($src))[2];
374 $dst = File::Spec->catfile($dst, $file);
377 my $mangler = $manglers{$file};
379 File::Copy::copy($src => $dst) or die "Can't copy $src to $dst: $!";
383 open my $in, '<', $src or die "Can't open $src for reading: $!";
384 open my $out, '>', $dst or die "Can't open $dst for writing: $!";
386 while (defined(my $line = <$in>)) {
387 print $out $mangler->($line, $dst);
390 my $patched_chunks = join ' ', @{$patched_chunks{$dst}};
391 my $expected_chunks = join ' ', @{$expected_chunks{$file}};
392 unless ($patched_chunks eq $expected_chunks) {
393 die "File $dst was not properly patched (got \"$patched_chunks\", expected \"$expected_chunks\")\n";
399 for my $tag (sort { $a <=> $b } keys %perls) {
400 my ($num_version, $pretty_version) = @{$perls{$tag}};
402 my $dir = File::Spec->catdir($target, $tag);
404 print "Working on perl $pretty_version\n";
406 my $tmp_guard = Guard::Path->new(path => $tmp_dir);
408 my $orig_dir = File::Spec->catdir($dir, 'orig');
410 my @files = qw<regcomp.c regexec.c>;
411 push @files, 'dquote_static.c' if $num_version >= 5.013_006;
412 push @files, 'inline_invlist.c' if $num_version >= 5.017_004;
413 for my $file (@files) {
414 my $orig_file = File::Spec->catfile($orig_dir, $file);
416 print " Already have original $file\n";
418 print " Need to get original $file\n";
419 fetch_source_file($file, $pretty_version => $orig_dir);
423 if (not $opts{f} and -e File::Spec->catfile($dir, $file)) {
424 print " Already have patched $file\n";
426 print " Need to patch $file... ";
427 my $res = patch_source_file($orig_file => $dir);
428 print $res ? "done\n" : "nothing to do\n";
435 print 'Updating MANIFEST... ';
439 open my $in, '<', 'MANIFEST' or die "Can't open MANIFEST for reading: $!";
440 @manifest_files = grep !m{^src/.*\.c$}, <$in>;
443 my @source_files = map "$_\n", glob 'src/*/*.c';
445 open my $out, '>', 'MANIFEST' or die "Can't open MANIFEST for writing: $!";
446 print $out sort @manifest_files, @source_files;