tools: Compare file contents directly in Perl instead of invoking cmp.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2016-07-06 15:32:16 +09:00
parent ca5d6c73fe
commit af91122caf
2 changed files with 47 additions and 64 deletions

View File

@ -84,16 +84,14 @@ sub dirname($)
} }
# update a file if changed # update a file if changed
sub update_file($) sub update_file($$)
{ {
my $file = shift; my $file = shift;
my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null"; my $new = shift;
if (!$ret)
{ open FILE, ">$file.new" or die "cannot create $file.new";
unlink "$file.new"; print FILE $new;
} close FILE;
else
{
rename "$file.new", "$file"; rename "$file.new", "$file";
print "$file updated\n"; print "$file updated\n";
if ($file eq "configure.ac") if ($file eq "configure.ac")
@ -101,8 +99,6 @@ sub update_file($)
system "autoconf"; system "autoconf";
print "configure updated\n"; print "configure updated\n";
} }
}
return $ret;
} }
# replace some lines in a file between two markers # replace some lines in a file between two markers
@ -111,34 +107,28 @@ sub replace_in_file($$$@)
my $file = shift; my $file = shift;
my $start = shift; my $start = shift;
my $end = shift; my $end = shift;
my ($old, $new);
open NEW_FILE, ">$file.new" or die "cannot create $file.new";
if (defined($start))
{
open OLD_FILE, "$file" or die "cannot open $file"; open OLD_FILE, "$file" or die "cannot open $file";
while (<OLD_FILE>) while (<OLD_FILE>)
{ {
$old .= $_;
last if /$start/; last if /$start/;
print NEW_FILE $_; $new .= $_;
}
} }
print NEW_FILE @_; $new .= join "", @_;
if (defined($end)) my $skip = 1;
{
my $skip=1;
while (<OLD_FILE>) while (<OLD_FILE>)
{ {
print NEW_FILE $_ unless $skip; $old .= $_;
$new .= $_ unless $skip;
$skip = 0 if /$end/; $skip = 0 if /$end/;
} }
}
close OLD_FILE if defined($start); close OLD_FILE;
close NEW_FILE; update_file($file, $new) if $old ne $new;
return update_file($file);
} }
# replace all source variables in a makefile # replace all source variables in a makefile
@ -147,14 +137,14 @@ sub replace_makefile_variables($)
my $file = shift; my $file = shift;
my $make = $makefiles{$file}; my $make = $makefiles{$file};
my $source_vars_regexp = join "|", @source_vars; my $source_vars_regexp = join "|", @source_vars;
my $modified = 0;
my %replaced; my %replaced;
my $old;
open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new"; my $new;
open OLD_FILE, "$file.in" or die "cannot open $file.in"; open OLD_FILE, "$file.in" or die "cannot open $file.in";
while (<OLD_FILE>) while (<OLD_FILE>)
{ {
$old .= $_;
if (/^\s*($source_vars_regexp)(\s*)=/) if (/^\s*($source_vars_regexp)(\s*)=/)
{ {
# try to preserve formatting # try to preserve formatting
@ -178,6 +168,7 @@ sub replace_makefile_variables($)
{ {
$_ = <OLD_FILE>; $_ = <OLD_FILE>;
last unless $_; last unless $_;
$old .= $_;
$old_str .= $_; $old_str .= $_;
} }
my $new_str = ""; my $new_str = "";
@ -188,18 +179,17 @@ sub replace_makefile_variables($)
elsif ($multiline) elsif ($multiline)
{ {
$new_str = "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n"; $new_str = "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
print NEW_FILE $new_str; $new .= $new_str;
} }
else else
{ {
$new_str = "$var$spaces= @values\n"; $new_str = "$var$spaces= @values\n";
print NEW_FILE $new_str; $new .= $new_str;
} }
$modified = 1 if ($old_str ne $new_str);
$replaced{$var} = 1; $replaced{$var} = 1;
next; next;
} }
print NEW_FILE $_; $new .= $_;
} }
foreach my $var (@source_vars) foreach my $var (@source_vars)
{ {
@ -207,13 +197,10 @@ sub replace_makefile_variables($)
next unless defined ${$make}{"=$var"}; next unless defined ${$make}{"=$var"};
my @values = @{${$make}{"=$var"}}; my @values = @{${$make}{"=$var"}};
next unless @values; next unless @values;
print NEW_FILE "\n$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n"; $new .= "\n$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
$modified = 1;
} }
close OLD_FILE; close OLD_FILE;
close NEW_FILE; update_file("$file.in", $new) if $old ne $new;
return update_file("$file.in") if $modified;
unlink "$file.in.new";
} }
# parse the specified makefile and load the variables # parse the specified makefile and load the variables

View File

@ -263,21 +263,17 @@ foreach my $arg (@ARGV)
elsif ($arg eq "-d") { $show_duplicates = 1; } elsif ($arg eq "-d") { $show_duplicates = 1; }
} }
sub update_file($) # update a file if changed
sub update_file($$)
{ {
my $file = shift; my $file = shift;
my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null"; my $new = shift;
if (!$ret)
{ open FILE, ">$file.new" or die "cannot create $file.new";
unlink "$file.new"; print FILE $new;
} close FILE;
else
{
#system "diff -u $file $file.new";
rename "$file.new", "$file"; rename "$file.new", "$file";
print "$file updated\n"; print "$file updated\n";
}
return $ret;
} }
# parse a spec file line # parse a spec file line
@ -319,11 +315,12 @@ sub update_spec_file($)
my $name = shift; my $name = shift;
my $file = "dlls/$name/$name.spec"; my $file = "dlls/$name/$name.spec";
my %stubs; my %stubs;
my ($old, $new);
open SPEC, "<$file" or die "cannot open $file"; open SPEC, "<$file" or die "cannot open $file";
open NEW, ">$file.new" or die "cannot create $file.new";
while (<SPEC>) while (<SPEC>)
{ {
$old .= $_;
chomp; chomp;
my $commented_out = 0; my $commented_out = 0;
@ -389,11 +386,10 @@ sub update_spec_file($)
$_ .= $descr{comment} || ""; $_ .= $descr{comment} || "";
done: done:
print NEW "$_\n"; $new .= "$_\n";
} }
close SPEC; close SPEC;
close NEW; update_file( $file, $new ) if $old ne $new;
update_file( $file );
} }
sub sync_spec_files(@) sub sync_spec_files(@)