UpdateCMakeLists.pl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env perl
  2. use strict;
  3. use File::Find;
  4. use File::Copy;
  5. use Digest::MD5;
  6. my @fileTypes = ("cpp", "c");
  7. my %dirFiles;
  8. my %dirCMake;
  9. sub GetFiles {
  10. my $dir = shift;
  11. my $x = $dirFiles{$dir};
  12. if (!defined $x) {
  13. $x = [];
  14. $dirFiles{$dir} = $x;
  15. }
  16. return $x;
  17. }
  18. sub ProcessFile {
  19. my $file = $_;
  20. my $dir = $File::Find::dir;
  21. # Record if a CMake file was found.
  22. if ($file eq "CMakeLists.txt") {
  23. $dirCMake{$dir} = $File::Find::name;
  24. return 0;
  25. }
  26. # Grab the extension of the file.
  27. $file =~ /\.([^.]+)$/;
  28. my $ext = $1;
  29. my $files;
  30. foreach my $x (@fileTypes) {
  31. if ($ext eq $x) {
  32. if (!defined $files) {
  33. $files = GetFiles($dir);
  34. }
  35. push @$files, $file;
  36. return 0;
  37. }
  38. }
  39. return 0;
  40. }
  41. sub EmitCMakeList {
  42. my $dir = shift;
  43. my $files = $dirFiles{$dir};
  44. if (!defined $files) {
  45. return;
  46. }
  47. foreach my $file (sort @$files) {
  48. print OUT " ";
  49. print OUT $file;
  50. print OUT "\n";
  51. }
  52. }
  53. sub UpdateCMake {
  54. my $cmakeList = shift;
  55. my $dir = shift;
  56. my $cmakeListNew = $cmakeList . ".new";
  57. open(IN, $cmakeList);
  58. open(OUT, ">", $cmakeListNew);
  59. my $foundLibrary = 0;
  60. while(<IN>) {
  61. if (!$foundLibrary) {
  62. print OUT $_;
  63. if (/^add_[^_]+_library\(/ || /^add_llvm_target\(/ || /^add_[^_]+_executable\(/) {
  64. $foundLibrary = 1;
  65. EmitCMakeList($dir);
  66. }
  67. }
  68. else {
  69. if (/\)/) {
  70. print OUT $_;
  71. $foundLibrary = 0;
  72. }
  73. }
  74. }
  75. close(IN);
  76. close(OUT);
  77. open(FILE, $cmakeList) or
  78. die("Cannot open $cmakeList when computing digest\n");
  79. binmode FILE;
  80. my $digestA = Digest::MD5->new->addfile(*FILE)->hexdigest;
  81. close(FILE);
  82. open(FILE, $cmakeListNew) or
  83. die("Cannot open $cmakeListNew when computing digest\n");
  84. binmode FILE;
  85. my $digestB = Digest::MD5->new->addfile(*FILE)->hexdigest;
  86. close(FILE);
  87. if ($digestA ne $digestB) {
  88. move($cmakeListNew, $cmakeList);
  89. return 1;
  90. }
  91. unlink($cmakeListNew);
  92. return 0;
  93. }
  94. sub UpdateCMakeFiles {
  95. foreach my $dir (sort keys %dirCMake) {
  96. if (UpdateCMake($dirCMake{$dir}, $dir)) {
  97. print "Updated: $dir\n";
  98. }
  99. }
  100. }
  101. find({ wanted => \&ProcessFile, follow => 1 }, '.');
  102. UpdateCMakeFiles();