Re: [patch] refine make regen to be more selective
[p5sagit/p5-mst-13.2.git] / regen_lib.pl
CommitLineData
9ad884cb 1#!/usr/bin/perl -w
2use strict;
3use vars qw($Is_W32 $Is_OS2 $Is_Cygwin $Is_NetWare $Needs_Write);
4use Config; # Remember, this is running using an existing perl
5
6# Common functions needed by the regen scripts
7
8$Is_W32 = $^O eq 'MSWin32';
9$Is_OS2 = $^O eq 'os2';
10$Is_Cygwin = $^O eq 'cygwin';
11$Is_NetWare = $Config{osname} eq 'NetWare';
12if ($Is_NetWare) {
13 $Is_W32 = 0;
14}
15
16$Needs_Write = $Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare;
17
b6b9a099 18eval "use Digest::MD5 'md5'; 1;"
19 or warn "Digest::MD5 unavailable, doing unconditional regen\n";
20
21sub cksum {
22 my $pl = shift;
23 my ($buf, $cksum);
24 local *FH;
25 if (open(FH, $pl)) {
26 local $/;
27 $buf = <FH>;
28 $cksum = defined &md5 ? md5($buf) : 0;
29 close FH;
30 } else {
31 warn "$0: $pl: $!\n";
32 }
33 return $cksum;
34}
35
9ad884cb 36sub safer_unlink {
37 my @names = @_;
38 my $cnt = 0;
39
40 my $name;
41 foreach $name (@names) {
42 next unless -e $name;
43 chmod 0777, $name if $Needs_Write;
44 ( CORE::unlink($name) and ++$cnt
45 or warn "Couldn't unlink $name: $!\n" );
46 }
47 return $cnt;
48}
49
50sub safer_rename_silent {
51 my ($from, $to) = @_;
52
53 # Some dosish systems can't rename over an existing file:
54 safer_unlink $to;
55 chmod 0600, $from if $Needs_Write;
56 rename $from, $to;
57}
58
b6b9a099 59sub safer_rename_always {
60 my ($from, $to) = @_;
61 safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
62}
63
9ad884cb 64sub safer_rename {
65 my ($from, $to) = @_;
b6b9a099 66
67 my $fc = cksum($from);
68 my $tc = cksum($to);
69
70 if ($fc and $fc eq $tc) {
71 warn "no changes between '$from' & '$to'\n";
72 safer_unlink($from);
73 return;
74 }
75 warn "changed '$from' to '$to'\n";
9ad884cb 76 safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
77}
781;