llvm.org GIT mirror llvm / 9344eb7
Add script 'UpdateCMakeLists.pl'. This script can be used (manually) to update the list of files in the CMakeLists.txt files. It works by scanning (starting from the working directory from which it is run) all subdirectories containing CMakeLists.txt files. It then updates the list of C and C++ files in the 'add_llvm_library' and 'add_clang_library' lists. Sources file lists are emitted in lexical order. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75833 91177308-0d34-0410-b5e6-96231b3b80d8 Ted Kremenek 10 years ago
1 changed file(s) with 118 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/env perl
1 use strict;
2 use File::Find;
3 use File::Copy;
4 use Digest::MD5;
5
6 my @fileTypes = ("cpp", "c");
7 my %dirFiles;
8 my %dirCMake;
9
10 sub GetFiles {
11 my $dir = shift;
12 my $x = $dirFiles{$dir};
13 if (!defined $x) {
14 $x = [];
15 $dirFiles{$dir} = $x;
16 }
17 return $x;
18 }
19
20 sub ProcessFile {
21 my $file = $_;
22 my $dir = $File::Find::dir;
23 # Record if a CMake file was found.
24 if ($file eq "CMakeLists.txt") {
25 $dirCMake{$dir} = $File::Find::name;
26 return 0;
27 }
28 # Grab the extension of the file.
29 $file =~ /\.([^.]+)$/;
30 my $ext = $1;
31 my $files;
32 foreach my $x (@fileTypes) {
33 if ($ext eq $x) {
34 if (!defined $files) {
35 $files = GetFiles($dir);
36 }
37 push @$files, $file;
38 return 0;
39 }
40 }
41 return 0;
42 }
43
44 sub EmitCMakeList {
45 my $dir = shift;
46 my $files = $dirFiles{$dir};
47
48 if (!defined $files) {
49 return;
50 }
51
52 foreach my $file (sort @$files) {
53 print OUT " ";
54 print OUT $file;
55 print OUT "\n";
56 }
57 }
58
59 sub UpdateCMake {
60 my $cmakeList = shift;
61 my $dir = shift;
62 my $cmakeListNew = $cmakeList . ".new";
63 open(IN, $cmakeList);
64 open(OUT, ">", $cmakeListNew);
65 my $foundLibrary = 0;
66
67 while() {
68 if (!$foundLibrary) {
69 print OUT $_;
70 if (/^add_clang_library\(/ || /^add_llvm_library\(/) {
71 $foundLibrary = 1;
72 EmitCMakeList($dir);
73 }
74 }
75 else {
76 if (/\)/) {
77 print OUT $_;
78 $foundLibrary = 0;
79 }
80 }
81 }
82
83 close(IN);
84 close(OUT);
85
86 open(FILE, $cmakeList) or
87 die("Cannot open $cmakeList when computing digest\n");
88 binmode FILE;
89 my $digestA = Digest::MD5->new->addfile(*FILE)->hexdigest;
90 close(FILE);
91
92 open(FILE, $cmakeListNew) or
93 die("Cannot open $cmakeListNew when computing digest\n");
94 binmode FILE;
95 my $digestB = Digest::MD5->new->addfile(*FILE)->hexdigest;
96 close(FILE);
97
98 if ($digestA != $digestB) {
99 move($cmakeListNew, $cmakeList);
100 return 1;
101 }
102
103 unlink($cmakeListNew);
104 return 0;
105 }
106
107 sub UpdateCMakeFiles {
108 foreach my $dir (sort keys %dirCMake) {
109 if (UpdateCMake($dirCMake{$dir}, $dir)) {
110 print "Updated: $dir\n";
111 }
112 }
113 }
114
115 find({ wanted => \&ProcessFile, follow => 1 }, '.');
116 UpdateCMakeFiles();
117