summaryrefslogtreecommitdiff
path: root/gnu/llvm/utils/UpdateCMakeLists.pl
blob: c896ea839be88521f74ea749b97b87c1a4b7595a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env perl
use strict;
use File::Find;
use File::Copy;
use Digest::MD5;

my @fileTypes = ("cpp", "c");
my %dirFiles;
my %dirCMake;

sub GetFiles {
  my $dir = shift;
  my $x = $dirFiles{$dir};  
  if (!defined $x) {
    $x = [];
    $dirFiles{$dir} = $x;
  }  
  return $x;
}

sub ProcessFile {
  my $file = $_;
  my $dir = $File::Find::dir;
  # Record if a CMake file was found.
  if ($file eq "CMakeLists.txt") {
    $dirCMake{$dir} = $File::Find::name;
    return 0;
  }
  # Grab the extension of the file.
  $file =~ /\.([^.]+)$/;
  my $ext = $1;
  my $files;
  foreach my $x (@fileTypes) {
    if ($ext eq $x) {
      if (!defined $files) {
        $files = GetFiles($dir);
      }
      push @$files, $file;
      return 0;
    }
  }
  return 0;
}

sub EmitCMakeList {
  my $dir = shift;
  my $files = $dirFiles{$dir};
  
  if (!defined $files) {
    return;
  }
  
  foreach my $file (sort @$files) {
    print OUT "  ";
    print OUT $file;
    print OUT "\n";
  }  
}

sub UpdateCMake {
  my $cmakeList = shift;
  my $dir = shift;
  my $cmakeListNew = $cmakeList . ".new";
  open(IN, $cmakeList);
  open(OUT, ">", $cmakeListNew);
  my $foundLibrary = 0;
  
  while(<IN>) {
    if (!$foundLibrary) {
      print OUT $_;
      if (/^add_[^_]+_library\(/ || /^add_llvm_target\(/ || /^add_[^_]+_executable\(/) {
        $foundLibrary = 1;
        EmitCMakeList($dir);
      }
    }
    else {
      if (/\)/) {
        print OUT $_;
        $foundLibrary = 0;
      }
    }
  }

  close(IN);
  close(OUT);

  open(FILE, $cmakeList) or
    die("Cannot open $cmakeList when computing digest\n");
  binmode FILE;
  my $digestA = Digest::MD5->new->addfile(*FILE)->hexdigest;
  close(FILE);
    
  open(FILE, $cmakeListNew) or
    die("Cannot open $cmakeListNew when computing digest\n");
  binmode FILE;
  my $digestB = Digest::MD5->new->addfile(*FILE)->hexdigest;
  close(FILE);
  
  if ($digestA ne $digestB) {
    move($cmakeListNew, $cmakeList);
    return 1;    
  }
  
  unlink($cmakeListNew);
  return 0;
}

sub UpdateCMakeFiles {
  foreach my $dir (sort keys %dirCMake) {
    if (UpdateCMake($dirCMake{$dir}, $dir)) {
      print "Updated: $dir\n";
    }
  }
}

find({ wanted => \&ProcessFile, follow => 1 }, '.');
UpdateCMakeFiles();