allow coverage testing to test only .c code coverage
[imager.git] / imcover.perl
CommitLineData
1ef586b1
TC
1#!perl -w
2use strict;
3use Config;
4use ExtUtils::Manifest 'maniread';
86c50c46 5use Cwd;
7fcc2ea8
TC
6use Getopt::Long;
7
8my @tests;
9my $verbose;
6aadff55 10my $nodc;
7fcc2ea8 11GetOptions("t|test=s" => \@tests,
6aadff55 12 "n" => \$nodc,
7fcc2ea8
TC
13 "v" => \$verbose)
14 or die;
1ef586b1
TC
15
16my $make = $Config{make};
17# if there's a way to make with profiling for a recursive build like
18# Imager I don't see how
19if (-f 'Makefile') {
7fcc2ea8 20 run("$make clean");
1ef586b1 21}
7fcc2ea8
TC
22run("cover -delete");
23run("perl Makefile.PL --coverage")
1ef586b1 24 and die;
7fcc2ea8 25run("$make 'OTHERLDFLAGS=-ftest-coverage -fprofile-arcs'")
1ef586b1
TC
26 and die;
27
86c50c46 28{
bdea445e 29 local $ENV{DEVEL_COVER_OPTIONS} = "-db," . getcwd() . "/cover_db,-coverage,statement,branch,condition,subroutine";
6aadff55
TC
30 my $makecmd = "$make test TEST_VERBOSE=1";
31 $makecmd .= " HARNESS_PERL_SWITCHES=-MDevel::Cover" unless $nodc;
7fcc2ea8
TC
32 if (@tests) {
33 $makecmd .= " TEST_FILES='@tests'";
34 }
35 run($makecmd);
86c50c46 36}
1ef586b1
TC
37
38# build gcov files
39my $mani = maniread();
40# split by directory
41my %paths;
42for my $filename (keys %$mani) {
43 next unless $filename =~ /\.(xs|c|im)$/;
44 if ($filename =~ m!^(\w+)/(\w+\.\w+)$!) {
45 push @{$paths{$1}}, $2;
46 }
47 else {
48 push @{$paths{''}}, $filename;
49 }
50}
51
52for my $path (keys %paths) {
53 if ($path) {
7fcc2ea8 54 run("cd $path ; gcov -abc @{$paths{$path}} ; cd ..");
1ef586b1
TC
55 }
56 else {
7fcc2ea8 57 run("gcov -abc @{$paths{$path}}");
1ef586b1
TC
58 }
59 my $dir = $path ? $path : '.';
60 for my $file (@{$paths{$path}}) {
7fcc2ea8 61 run("gcov2perl $dir/$file.gcov");
1ef586b1
TC
62 }
63}
64
65my @dbs = "cover_db", map "$_/cover_db", grep $_, keys %paths;
7fcc2ea8
TC
66# we already ran gcov
67run("cover -nogcov -ignore_re '^t/'");
1ef586b1 68
7fcc2ea8
TC
69sub run {
70 my $cmd = shift;
71
72 print "Running: $cmd\n" if $verbose;
73 return system $cmd;
74}