2005-04-18 20:34:27 +04:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
# Currently, we implement only the directory option
|
|
|
|
# The following features have been requested for
|
|
|
|
#
|
2015-06-24 06:59:57 +03:00
|
|
|
# 1. Get the name of the directory and produce a
|
2005-04-18 20:34:27 +04:00
|
|
|
# statistic for all the files which have been touched
|
2015-06-24 06:59:57 +03:00
|
|
|
# in all the subdirectories starting from that
|
2005-04-18 20:34:27 +04:00
|
|
|
# directory
|
|
|
|
#
|
|
|
|
# 2. Since gcov spits out a statistic for all the header
|
2015-06-24 06:59:57 +03:00
|
|
|
# files included by all the source files, we need to
|
2005-04-18 20:34:27 +04:00
|
|
|
# aggregate them somehow into a single file. This might
|
|
|
|
# have to be done manually by going through all the lines
|
2015-06-24 06:59:57 +03:00
|
|
|
# which have been executed in that file since a header
|
2005-04-18 20:34:27 +04:00
|
|
|
# file may have multiple inclusions meaning that they
|
|
|
|
# might have multiple .gcov files in different directories
|
|
|
|
#
|
|
|
|
# 3. Support command line parsing of arguments
|
|
|
|
#
|
|
|
|
# 4. Support giving a listing of all the files which have
|
|
|
|
# been covered less than a certain percentage, say for
|
|
|
|
# eg., all files which have been covered less that 5%
|
|
|
|
#
|
|
|
|
|
|
|
|
use strict;
|
2005-04-19 04:35:59 +04:00
|
|
|
use Switch;
|
2007-01-17 17:21:23 +03:00
|
|
|
use File::Basename;
|
2005-04-19 04:35:59 +04:00
|
|
|
|
|
|
|
#global variables
|
|
|
|
my $index = 0;
|
|
|
|
my $num_args = scalar(@ARGV);
|
|
|
|
my $percentage = 0.0;
|
|
|
|
my $ret1 = 0;
|
|
|
|
my $ret2 = 0;
|
|
|
|
my $dir_list_given = 0;
|
|
|
|
my $req_list_given = 0;
|
|
|
|
|
2007-01-17 17:21:23 +03:00
|
|
|
$ret1 = open(DIR_FILE, "dir_list.txt");
|
|
|
|
$ret2 = open(REQ_FILE, "req_list.txt");
|
2005-04-19 04:35:59 +04:00
|
|
|
|
|
|
|
if ($ret1 < 0 || $ret2 < 0) {
|
|
|
|
print "ERROR: opening dir_list.txt or req_list.txt\n";
|
|
|
|
exit(3);
|
|
|
|
}
|
2005-04-18 21:35:00 +04:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
# check the error condition
|
|
|
|
if (0 == ($num_args % 2)) {
|
2005-04-18 21:35:00 +04:00
|
|
|
} else {
|
2005-04-19 04:35:59 +04:00
|
|
|
print "ERROR: the number of arguments has to be even\n";
|
|
|
|
exit(3);
|
2005-04-18 20:34:27 +04:00
|
|
|
}
|
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
# process the arguments
|
|
|
|
while($num_args > 0) {
|
|
|
|
switch ($ARGV[$index]) {
|
2015-06-24 06:59:57 +03:00
|
|
|
case "-d" {
|
2005-04-19 04:35:59 +04:00
|
|
|
print DIR_FILE $ARGV[$index+1];
|
|
|
|
$index += 2;
|
2015-06-24 06:59:57 +03:00
|
|
|
$num_args -= 2;
|
2005-04-19 04:35:59 +04:00
|
|
|
$dir_list_given = 1;
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
case "-f" {
|
2005-04-19 04:35:59 +04:00
|
|
|
my $filename = `find . -name $ARGV[$index+1]`;
|
|
|
|
print REQ_FILE $filename;
|
|
|
|
$index += 2;
|
2015-06-24 06:59:57 +03:00
|
|
|
$num_args -= 2;
|
2005-04-19 04:35:59 +04:00
|
|
|
$req_list_given = 1;
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
case "-p" {
|
2005-04-19 04:35:59 +04:00
|
|
|
$percentage = $ARGV[$index];
|
|
|
|
$index += 2;
|
2015-06-24 06:59:57 +03:00
|
|
|
$num_args -= 2;
|
2005-04-19 04:35:59 +04:00
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
else {
|
2005-04-19 04:35:59 +04:00
|
|
|
print "ERROR: Incorrect command line option\n";
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-18 21:14:29 +04:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
close(DIR_FILE);
|
|
|
|
close(REQ_FILE);
|
2005-04-18 21:14:29 +04:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
# when nothing is specified, everything defaults to just "src"
|
|
|
|
if (0 == $dir_list_given && 0 == $req_list_given) {
|
|
|
|
print "WARN: No directory or file has been requested\ndefaulting to src directory\n";
|
|
|
|
system("echo src > dir_list.txt");
|
|
|
|
$dir_list_given = 1;
|
|
|
|
}
|
2005-04-18 21:14:29 +04:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
#Now, we have both the requested file list and the directory list
|
|
|
|
#So simply call the procedure generate_stats with the required arguments
|
|
|
|
if (1 == $dir_list_given) {
|
|
|
|
get_file_list("./dir_list.txt",
|
|
|
|
"touched_files.txt",
|
|
|
|
"untouched_files.txt");
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
generate_stats("touched_files.txt", # file_list
|
|
|
|
"coverage_stats.txt",# generic coverage numbers
|
|
|
|
"percent_stats.txt", # files below a certain %
|
|
|
|
$percentage, # percentage below which we report
|
|
|
|
1); # 1 to report
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
if (1 == $req_list_given) {
|
|
|
|
generate_stats("req_list.txt", # file_list
|
|
|
|
"req_stats.txt",# generic coverage
|
|
|
|
"", # no percentage coverage required
|
|
|
|
0, # not required
|
|
|
|
0); # 0 to not report percentages
|
2005-04-18 21:35:00 +04:00
|
|
|
}
|
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
sub get_file_list {
|
|
|
|
my ($input, $touched, $untouched) = @_;
|
|
|
|
my $ret = open(DIRFILES, "< $input");
|
|
|
|
if ($ret < 0) {
|
|
|
|
print "ERROR: could not open directory listing\n";
|
|
|
|
exit(3);
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
while(<DIRFILES>) {
|
|
|
|
chomp();
|
|
|
|
my $c_files = `find $_ -name \"*.c\"`;
|
|
|
|
my $cc_files = `find $_ -name \"*.cc\"`;
|
2007-01-17 17:21:23 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
$cc_files =~ s/\.cc//g;
|
|
|
|
$c_files =~ s/\.c//g;
|
|
|
|
$c_files = $c_files . $cc_files;
|
|
|
|
my @C_FILES = split(/\n/, $c_files);
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2007-01-17 17:21:23 +03:00
|
|
|
my $da_files = `find $_ -name \"*.da\" -o -name \"*.gcda\"`;
|
|
|
|
$da_files =~ s/\.gcda//g;
|
2005-04-19 04:35:59 +04:00
|
|
|
$da_files =~ s/\.da//g;
|
|
|
|
my @DA_FILES = split(/\n/, $da_files);
|
2007-01-17 17:21:23 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
open (TEMP1, "> temp1");
|
|
|
|
open (TEMP2, "> temp2");
|
|
|
|
print TEMP1 $c_files;
|
|
|
|
print TEMP2 $da_files;
|
|
|
|
close(TEMP1);
|
2005-04-18 20:34:27 +04:00
|
|
|
close(TEMP2);
|
2015-06-24 06:59:57 +03:00
|
|
|
|
|
|
|
# Now do the manual diff
|
2005-04-19 04:35:59 +04:00
|
|
|
open(TEMP1, "< temp1");
|
2005-04-21 09:15:56 +04:00
|
|
|
open(UNTOUCHED_FILES, ">> $untouched");
|
|
|
|
open(TOUCHED_FILES, ">> $touched");
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
while(<TEMP1>) {
|
|
|
|
my $c_file = $_;
|
|
|
|
my $found = 0;
|
2007-01-17 17:21:23 +03:00
|
|
|
my $file_name;
|
|
|
|
my $dir_name;
|
|
|
|
my $file_ext;
|
|
|
|
my $search_file;
|
|
|
|
($file_name, $dir_name, $file_ext) = fileparse($c_file, ('\.c') );
|
|
|
|
|
|
|
|
# For our libtool build, every .gcda and .gcno file lands in the .libs dir.
|
|
|
|
$search_file = $dir_name . ".libs/" . $file_name;
|
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
open(TEMP2, "< temp2");
|
|
|
|
while(<TEMP2>) {
|
2007-01-17 17:21:23 +03:00
|
|
|
if ($search_file eq $_) {
|
2005-04-19 04:35:59 +04:00
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(TEMP2);
|
|
|
|
if ($found == 0) {
|
|
|
|
print UNTOUCHED_FILES $c_file;
|
|
|
|
} else {
|
|
|
|
chomp($c_file);
|
|
|
|
$c_file = $c_file . ".c\n";
|
|
|
|
print TOUCHED_FILES $c_file;
|
|
|
|
}
|
2005-04-18 20:34:27 +04:00
|
|
|
}
|
2005-04-19 04:35:59 +04:00
|
|
|
close(UNTOUCHED_FILES);
|
|
|
|
close(TOUCHED_FILES);
|
|
|
|
close(TEMP1);
|
|
|
|
system("rm temp1 temp2");
|
2005-04-18 20:34:27 +04:00
|
|
|
}
|
2005-04-19 04:35:59 +04:00
|
|
|
close(DIRFILES);
|
|
|
|
#Now to print the stats of all the files which are touched
|
|
|
|
system("sort $touched -o temp; uniq temp $touched");
|
|
|
|
system("sort $untouched -o temp; uniq temp $untouched");
|
2005-04-18 21:14:29 +04:00
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
|
|
|
|
# This is the function which generates the statistics and dumps it out
|
|
|
|
# to a file. Details are pretty straightforward at this point
|
|
|
|
sub generate_stats {
|
|
|
|
my ($input_file, $coverage_file, $percent_file, $percentage, $calculate) = @_;
|
|
|
|
my $k = 0;
|
|
|
|
my $l = 0;
|
|
|
|
|
2015-06-24 06:59:57 +03:00
|
|
|
open (INPUT, "< $input_file");
|
2005-04-19 04:35:59 +04:00
|
|
|
open (COVERAGE, "> $coverage_file");
|
|
|
|
if ($calculate == 1) {
|
|
|
|
open (PERCENT, "> percent_coverage.txt");
|
|
|
|
}
|
|
|
|
|
2007-01-17 17:21:23 +03:00
|
|
|
print COVERAGE "#Index Directory Filename Usage(%)\n";
|
2005-04-19 04:35:59 +04:00
|
|
|
print COVERAGE "#======================================================================================================\n";
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
if ($calculate == 1) {
|
2007-01-17 17:21:23 +03:00
|
|
|
print PERCENT "#Index Directory Filename Usage(%)\n";
|
2005-04-19 04:35:59 +04:00
|
|
|
print PERCENT "#======================================================================================================\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $average = 0.0;
|
|
|
|
my $num_files = `wc -l touched_files.txt`;
|
|
|
|
|
2007-01-17 17:21:23 +03:00
|
|
|
print "num_files:", $num_files;
|
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
while (<INPUT>) {
|
|
|
|
#generate the gcov file for this particular file
|
|
|
|
#1. Get the directory name and filename seperately
|
|
|
|
#2. Invoke gcov on the file
|
|
|
|
#3. Print the statistic onto a file
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
chomp();
|
|
|
|
my $full_name = $_;
|
2007-01-17 17:21:23 +03:00
|
|
|
my $dir_name;
|
|
|
|
my $file_name;
|
|
|
|
my $file_ext;
|
|
|
|
my $file_gcda;
|
|
|
|
my $found_file;
|
|
|
|
($file_name, $dir_name, $file_ext) = fileparse($full_name, ('\.c') );
|
|
|
|
$file_gcda = $file_name . ".gcda";
|
2005-04-19 04:35:59 +04:00
|
|
|
|
2007-01-17 17:21:23 +03:00
|
|
|
open(RESULT, "cd $dir_name; gcov $file_gcda -o .libs 2> /dev/null | ");
|
2005-04-19 04:35:59 +04:00
|
|
|
while (<RESULT>) {
|
2015-06-24 06:59:57 +03:00
|
|
|
if (/Creating/) { $found_file = 0; }
|
2005-04-19 04:35:59 +04:00
|
|
|
else {
|
2007-01-17 17:21:23 +03:00
|
|
|
# print "check: ", $_;
|
|
|
|
# Do not check including the file_extension; might be .c or .cc or .C
|
|
|
|
if (/^File '$file_name/) {
|
|
|
|
# print "Found File:\n", $_;
|
|
|
|
$found_file = 1;
|
|
|
|
}
|
|
|
|
|
2005-04-19 04:35:59 +04:00
|
|
|
#Now we are doing the right line. Search for this file
|
2007-01-17 17:21:23 +03:00
|
|
|
if (/^Lines/ && $found_file == 1) {
|
|
|
|
# print "Found Lines:\n", $_;
|
|
|
|
s/([\s,0-9]*\.[0-9]+\%)\.*/$1/;
|
2015-06-24 06:59:57 +03:00
|
|
|
my $val = $1;
|
2005-04-19 04:35:59 +04:00
|
|
|
$average += $val;
|
|
|
|
$k++;
|
2007-01-17 17:21:23 +03:00
|
|
|
my $print_string = sprintf("%4d %40s %40s %3.2f\n", $k, $dir_name, $file_name, $val);
|
2005-04-19 04:35:59 +04:00
|
|
|
if ($calculate == 1) {
|
2015-06-24 06:59:57 +03:00
|
|
|
if ($val <= $percentage) {
|
2005-04-19 04:35:59 +04:00
|
|
|
$l++;
|
2007-01-17 17:21:23 +03:00
|
|
|
my $zero_string = sprintf("%4d %40s %40s %3.2f\n", $l, $dir_name, $file_name, $val);
|
2005-04-19 04:35:59 +04:00
|
|
|
print PERCENT $zero_string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print COVERAGE $print_string;
|
2007-01-17 17:21:23 +03:00
|
|
|
|
|
|
|
# Need to detect the next round
|
|
|
|
$found_file = 0;
|
2005-04-18 20:34:27 +04:00
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
}
|
2005-04-19 04:35:59 +04:00
|
|
|
}
|
|
|
|
close(RESULT);
|
|
|
|
}
|
2005-04-21 09:18:12 +04:00
|
|
|
if ($num_files != 0){
|
|
|
|
print COVERAGE "==============================================================\n";
|
|
|
|
print COVERAGE "Average coverage was: ", $average/$num_files, " \n";
|
|
|
|
print COVERAGE "==============================================================\n";
|
|
|
|
}
|
2005-04-19 04:35:59 +04:00
|
|
|
close(INPUT);
|
|
|
|
close(COVERAGE);
|
|
|
|
if ($calculate == 1) {
|
|
|
|
close(PERCENT);
|
2005-04-18 20:34:27 +04:00
|
|
|
}
|
|
|
|
}
|