/build/mergedep.pl

http://github.com/digego/extempore · Perl · 86 lines · 45 code · 10 blank · 31 comment · 5 complexity · 771315ed53a32ea24a9b727f974f568c MD5 · raw file

  1. #!/usr/bin/perl
  2. # Copyright 2003 Bryan Ford
  3. # Distributed under the GNU General Public License.
  4. #
  5. # Usage: mergedep <main-depfile> [<new-depfiles> ...]
  6. #
  7. # This script merges the contents of all <new-depfiles> specified
  8. # on the command line into the single file <main-depfile>,
  9. # which may or may not previously exist.
  10. # Dependencies in the <new-depfiles> will override
  11. # any existing dependencies for the same targets in <main-depfile>.
  12. # The <new-depfiles> are deleted after <main-depfile> is updated.
  13. #
  14. # The <new-depfiles> are typically generated by GCC with the -MD option,
  15. # and the <main-depfile> is typically included from a Makefile,
  16. # as shown here for GNU 'make':
  17. #
  18. # .deps: $(wildcard *.d)
  19. # perl mergedep $@ $^
  20. # -include .deps
  21. #
  22. # This script properly handles multiple dependencies per <new-depfile>,
  23. # including dependencies having no target,
  24. # so it is compatible with GCC3's -MP option.
  25. #
  26. sub readdeps {
  27. my $filename = shift;
  28. open(DEPFILE, $filename) or return 0;
  29. while (<DEPFILE>) {
  30. if (/([^:]*):([^\\:]*)([\\]?)$/) {
  31. my $target = $1;
  32. my $deplines = $2;
  33. my $slash = $3;
  34. while ($slash ne '') {
  35. $_ = <DEPFILE>;
  36. defined($_) or die
  37. "Unterminated dependency in $filename";
  38. /(^[ \t][^\\]*)([\\]?)$/ or die
  39. "Bad continuation line in $filename";
  40. $deplines = "$deplines\\\n$1";
  41. $slash = $2;
  42. }
  43. #print "DEPENDENCY [[$target]]: [[$deplines]]\n";
  44. $dephash{$target} = $deplines;
  45. } elsif (/^[#]?[ \t]*$/) {
  46. # ignore blank lines and comments
  47. } else {
  48. die "Bad dependency line in $filename: $_";
  49. }
  50. }
  51. close DEPFILE;
  52. return 1;
  53. }
  54. if ($#ARGV < 0) {
  55. print "Usage: mergedep <main-depfile> [<new-depfiles> ..]\n";
  56. exit(1);
  57. }
  58. %dephash = ();
  59. # Read the main dependency file
  60. $maindeps = $ARGV[0];
  61. readdeps($maindeps);
  62. # Read and merge in the new dependency files
  63. foreach $i (1 .. $#ARGV) {
  64. readdeps($ARGV[$i]) or die "Can't open $ARGV[$i]";
  65. }
  66. # Update the main dependency file
  67. open(DEPFILE, ">$maindeps.tmp") or die "Can't open output file $maindeps.tmp";
  68. foreach $target (keys %dephash) {
  69. print DEPFILE "$target:$dephash{$target}";
  70. }
  71. close DEPFILE;
  72. rename("$maindeps.tmp", "$maindeps") or die "Can't overwrite $maindeps";
  73. # Finally, delete the new dependency files
  74. foreach $i (1 .. $#ARGV) {
  75. unlink($ARGV[$i]) or print "Error removing $ARGV[$i]\n";
  76. }