1. fix bug by ensuring we start at the llvm source root.
[oota-llvm.git] / utils / userloc.pl
1 #!/usr/bin/perl -w
2 #
3 # Program:  userloc.pl
4 #
5 # Synopsis: This program uses "cvs annotate" to get a summary of how many lines
6 #           of code the various developres are responsible for. It takes one
7 #           argument, the directory to process. If the argument is not specified
8 #           then the cwd is used. The directory must be an LLVM tree checked out
9 #           from cvs. 
10 #
11 # Syntax:   userloc.pl [-tag=tag|-html... <directory>...
12 #
13 # Options:
14 #           -tag=tag
15 #               Use "tag" to select the revision (as per cvs -r option)
16 #           -filedetails
17 #               Report details about lines of code in each file for each user
18 #           -html
19 #               Generate HTML output instead of text output
20 # Directories:
21 #   The directories passed after the options should be relative paths to
22 #   directories of interest from the top of the llvm source tree, e.g. "lib"
23 #   or "include", etc.
24
25 die "Usage userloc.pl [-tag=tag|-html] <directories>..." 
26   if ($#ARGV < 0);
27
28 my $tag = "";
29 my $html = 0;
30 my $debug = 0;
31 my $filedetails = "";
32 while ( defined($ARGV[0]) && substr($ARGV[0],0,1) eq '-' )
33 {
34   if ($ARGV[0] =~ /-tag=.*/) {
35     $tag = $ARGV[0];
36     $tag =~ s#-tag=(.*)#$1#;
37   } elsif ($ARGV[0] =~ /-filedetails/) {
38     $filedetails = 1;
39   } elsif ($ARGV[0] eq "-html") {
40     $html = 1;
41   } elsif ($ARGV[0] eq "-debug") {
42     $debug = 1;
43   } else {
44     die "Invalid option: $ARGV[0]";
45   }
46   shift;
47 }
48
49 chomp(my $srcroot = `llvm-config --src-root`);
50 chdir($srcroot);
51 my $llvmdo = "$srcroot/utils/llvmdo";
52 my %Stats;
53 my %FileStats;
54
55 my $annotate = "cvs -z6 annotate -lf ";
56 if (length($tag) > 0)
57 {
58   $annotate = $annotate . " -r" . $tag;
59 }
60
61 sub GetCVSFiles
62 {
63   my $d = $_[0];
64   my $files ="";
65   open FILELIST, 
66     "$llvmdo -dirs \"$d\" -code-only echo |" || die "Can't get list of files with llvmdo";
67   while ( defined($line = <FILELIST>) ) {
68     chomp($file = $line);
69     print "File: $file\n" if ($debug);
70     $files = "$files $file";
71   }
72   return $files;
73 }
74
75 sub ScanDir
76 {
77   my $Dir = $_[0];
78   my $files = GetCVSFiles($Dir);
79
80   open (DATA,"$annotate $files 2>&1 |")
81     || die "Can't read cvs annotation data";
82
83   my $curfile = "";
84   while ( defined($line = <DATA>) )
85   {
86     chomp($line);
87     if ($line =~ '^Annotations for.*') {
88       $curfile = $line;
89       $curfile =~ s#^Annotations for ([[:print:]]*)#$1#;
90       print "Scanning: $curfile\n" if ($debug);
91     } elsif ($line =~ /^[0-9.]*[ \t]*\([^)]*\):/) {
92       $uname = $line;
93       $uname =~ s#^[0-9.]*[ \t]*\(([a-zA-Z0-9_.-]*) [^)]*\):.*#$1#;
94       $Stats{$uname}++;
95       if ($filedetails) {
96         $FileStats{$uname} = {} unless exists $FileStats{$uname};
97         ${$FileStats{$uname}}{$curfile}++;
98       }
99     }
100   }
101   close DATA;
102 }
103
104 sub printStats
105 {
106   my $dir = $_[0];
107   my $hash = $_[1];
108   my $user;
109   my $total = 0;
110
111   foreach $user (keys %Stats) { $total += $Stats{$user}; }
112
113   if ($html) { 
114     print "<p>Total Source Lines: $total<br/></p>\n";
115     print "<table>";
116     print " <tr><th style=\"text-align:right\">LOC</th>\n";
117     print " <th style=\"text-align:right\">\%LOC</th>\n";
118     print " <th style=\"text-align:left\">User</th>\n";
119     print "</tr>\n";
120   }
121
122   foreach $user ( sort keys %Stats )
123   {
124     my $v = $Stats{$user};
125     if (defined($v))
126     {
127       if ($html) {
128         printf "<tr><td style=\"text-align:right\">%d</td><td style=\"text-align:right\">(%4.1f%%)</td><td style=\"text-align:left\">", $v, (100.0/$total)*$v;
129         if ($filedetails) {
130           print "<a href=\"#$user\">$user</a></td></tr>";
131         } else {
132           print $user,"</td></tr>";
133         }
134       } else {
135         printf "%8d  (%4.1f%%)  %s\n", $v, (100.0/$total)*$v, $user;
136       }
137     }
138   }
139   print "</table>\n" if ($html);
140
141   if ($filedetails) {
142     foreach $user (sort keys %FileStats) {
143       my $total = 0;
144       foreach $file (sort keys %{$FileStats{$user}}) { 
145         $total += ${$FileStats{$user}}{$file}
146       }
147       if ($html) {
148         print "<table><tr><th style=\"text-align:left\" colspan=\"3\"><a name=\"$user\">$user</a></th></tr>\n";
149       } else {
150         print $user,":\n";
151       }
152       foreach $file (sort keys %{$FileStats{$user}}) {
153         my $v = ${$FileStats{$user}}{$file};
154         if ($html) { 
155           printf "<tr><td style=\"text-align:right\">&nbsp;&nbsp;%d</td><td
156           style=\"text-align:right\">&nbsp;%4.1f%%</td><td
157           style=\"text-align:left\">%s</td></tr>",$v, (100.0/$total)*$v,$file;
158         } else {
159           printf "%8d  (%4.1f%%)  %s\n", $v, (100.0/$total)*$v, $file;
160         }
161       }
162       if ($html) { print "</table>\n"; }
163     }
164   }
165 }
166
167
168 if ($html)
169 {
170 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";
171 print "<html>\n<head>\n";
172 print "  <title>LLVM LOC Based On CVS Annotation</title>\n";
173 print "  <link rel=\"stylesheet\" href=\"llvm.css\" type=\"text/css\"/>\n";
174 print "</head>\n";
175 print "<body><div class=\"doc_title\">LLVM LOC Based On CVS Annotation</div>\n";
176 print "<p>This document shows the total lines of code per user in each\n";
177 print "LLVM directory. Lines of code are attributed by the user that last\n";
178 print "committed the line. This does not necessarily reflect authorship.</p>\n";
179 }
180
181 my @DIRS;
182 if ($#ARGV > 0) {
183   @DIRS = @ARGV;
184 } else {
185   push @DIRS, 'include';
186   push @DIRS, 'lib';
187   push @DIRS, 'tools';
188   push @DIRS, 'runtime';
189   push @DIRS, 'docs';
190   push @DIRS, 'test';
191   push @DIRS, 'utils';
192   push @DIRS, 'examples';
193   push @DIRS, 'projects/Stacker';
194   push @DIRS, 'projects/sample';
195   push @DIRS, 'autoconf';
196 }
197   
198 for $Index ( 0 .. $#DIRS) { 
199   print "Scanning Dir: $DIRS[$Index]\n" if ($debug);
200   ScanDir($DIRS[$Index]); 
201 }
202
203 printStats;
204
205 print "</body></html>\n" if ($html) ;