#!/usr/bin/perl # # usage: fix-includes file_to_add.h before_file.h # $toadd = shift @ARGV; $before = shift @ARGV; # build the dependency list system("./tools/makedep -fdeps -Iinclude -Idlls/ddraw `find . -name \\*.h`"); open DEPS, "deps" or die "cannot open deps"; while () { chomp; while (/\\$/) { chop; $_ .= ; } my ($obj,@deps) = split; for (my $i = 0; $i <= $#deps; $i++) { $deps[$i] =~ s/.*\///; } my $header = $deps[0]; foreach my $f (@deps) { $includes{$header}{$f} = 1; } } close DEPS; unlink "deps"; foreach $file (@ARGV) { open IN, "$file" or die "cannot open $file"; @lines = (); $added = 0; while () { chomp; if (/^\s*\#\s*include\s+([<"])(.*)[>"]/) { my $quote = $1; my $incl = $2; if ($includes{$incl}{$toadd}) { # the header includes the one we want last unless $added; # not added yet, nothing to do next if ($incl eq $toadd); # don't add it twice } elsif ($includes{$incl}{$before}) { push @lines, ($quote eq "<") ? "#include <$toadd>\n" : "#include \"$toadd\"\n" unless $added; $added = 1; } } push @lines, $_ . "\n"; } close IN; if ($added) { open OUT, ">$file.new" or die "cannot create $file.new"; print OUT @lines; close OUT; print "changed $file\n"; system "chmod --reference=$file $file.new"; system "diff -u $file $file.new"; rename "$file.new", "$file"; } }