#!/usr/bin/env perl
######################################################################
#
# Copyright 2003-2020 by Wilson Snyder. This program is free software; you
# can redistribute it and/or modify it under the terms of either the GNU
# Lesser General Public License Version 3 or the Perl Artistic License
# Version 2.0.
#
######################################################################

require 5.006_001;
use warnings;
use Getopt::Long;
use FindBin qw($RealBin $RealScript);
use IO::File;
use Pod::Usage;
use Cwd qw(abs_path getcwd);

use strict;
use vars qw($Debug @Opt_Verilator_Sw);

#######################################################################
#######################################################################
# main

autoflush STDOUT 1;
autoflush STDERR 1;

$Debug = 0;

# No arguments can't do anything useful.  Give help
if ($#ARGV < 0) {
    pod2usage(-exitstatus=>2, -verbose=>0);
}

# We sneak a look at the flags so we can do some pre-environment checks
# All flags will hit verilator...
foreach my $sw (@ARGV) {
    $sw = "'$sw'" if $sw =~ m![^---a-zA-Z0-9_/\\:.+]!;
    push @Opt_Verilator_Sw, $sw;
}

Getopt::Long::config("no_auto_abbrev","pass_through");
if (! GetOptions (
          # Major operating modes
          "help"        => \&usage,
          "debug:s"     => \&debug,
          # "version!"  => \&version,   # Also passthru'ed
          # Additional parameters
          "<>"          => sub {},      # Ignored
    )) {
    pod2usage(-exitstatus=>2, -verbose=>0);
}

# Normal, non gdb
run(verilator_coverage_bin()
    ." ".join(' ',@Opt_Verilator_Sw));

#----------------------------------------------------------------------

sub usage {
    pod2usage(-verbose=>2, -exitval=>0, -output=>\*STDOUT);
}

sub debug {
    shift;
    my $level = shift;
    $Debug = $level||3;
}

#######################################################################
#######################################################################
# Builds

sub verilator_coverage_bin {
    my $bin = "";
    # Use VERILATOR_ROOT if defined, else assume verilator_bin is in the search path
    my $basename = ($ENV{VERILATOR_COVERAGE_BIN}
                    || "verilator_coverage_bin_dbg");
    if (defined($ENV{VERILATOR_ROOT})) {
        my $dir = $ENV{VERILATOR_ROOT};
        if (-x "$dir/bin/$basename"
            || -x "$dir/bin/$basename.exe") {  # From a "make install" into VERILATOR_ROOT
            $bin = "$dir/bin/$basename";
        } else {
            $bin = "$dir/$basename";  # From pointing to kit directory
        }
    } else {
        if (-x "$RealBin/$basename"
            || -x "$RealBin/$basename.exe") {
            $bin = "$RealBin/$basename";  # From path/to/verilator with verilator_bin installed
        } else {
            $bin = $basename;  # Find in PATH
        }
        # Note we don't look under bin/$basename which would be right if running
        # in the kit dir. Running that would likely break, since
        # VERILATOR_ROOT wouldn't be set and Verilator won't find internal files.
    }
    return $bin;
}

#######################################################################
#######################################################################
# Utilities

sub run {
    # Run command, check errors
    my $command = shift;
    $! = undef;  # Cleanup -x
    print "\t$command\n" if $Debug>=3;
    system($command);
    my $status = $?;
    if ($status) {
        if ($! =~ /no such file or directory/i) {
            warn "%Error: verilator_coverage: Misinstalled, or VERILATOR_ROOT might need to be in environment\n";
        }
        if ($Debug) {  # For easy rerunning
            warn "%Error: export VERILATOR_ROOT=".($ENV{VERILATOR_ROOT}||"")."\n";
            warn "%Error: $command\n";
        }
        if ($status & 127) {
            if (($status & 127) == 8 || ($status & 127) == 11) { # SIGFPA or SIGSEGV
                warn "%Error: Verilator_coverage internal fault, sorry.\n" if !$Debug;
            } elsif (($status & 127) == 6) {  # SIGABRT
                warn "%Error: Verilator_coverage aborted.\n" if !$Debug;
            } else {
                warn "%Error: Verilator_coverage threw signal $status.\n" if !$Debug;
            }
        }
        if ($status != 256 || $Debug) {  # i.e. not normal exit(1)
            warn "%Error: Command Failed $command\n";
        }
        exit $! if $!;  # errno
        exit $? >> 8 if $? >> 8;  # child exit status
        exit 255;  # last resort
    }
}

#######################################################################
#######################################################################
package main;
__END__

=pod

=head1 NAME

verilator_coverage - Verilator coverage analyzer

=head1 SYNOPSIS

    verilator_coverage --help
    verilator_coverage --version

    verilator_coverage --annotate <obj>

    verilator_coverage  -write merged.dat -read <datafiles>...

    verilator_coverage  -write-info merged.info -read <datafiles>...

Verilator_coverage processes Verilator coverage reports.

With --anotate, it reads the specified data file and generates annotated
source code with coverage metrics annotated.  If multiple coverage points
exist on the same line, additional lines will be inserted to report the
additional points.

Additional Verilog-standard arguments specify the search paths necessary to
find the source code that the coverage analysis was performed on.

To get correct coverage percentages, you may wish to read logs/coverage.dat
into Emacs and do a M-x keep-lines to include only those statistics of
interest.

For Verilog conditions that should never occur, you should add a $stop
statement.  This will remove the coverage during the next build.

=head1 ARGUMENTS

=over 4

=item I<filename>

Specify input data file, may be repeated to read multiple inputs.  If no
data file is specified, by default coverage.dat is read.

=item --annotate I<output_directory>

Sprcifies the directory name that source files with annotated coverage data
should be written to.

=item --annotate-all

Specifies all files should be shown.  By default, only those source files
which have low coverage are written to the output directory.

=item --annotate-min I<count>

Specifies the minimum occurrence count that should be flagged if the
coverage point does not include a specified threshold.  Defaults to 10.

=item --help

Displays this message and program version and exits.

=item --rank

Print an experimental report listing the relative importance of each test
in covering all of the coverage points.  The report shows "Covered" which
indicates the number of points that test covers; a test is considered to
cover a point if it has a bucket count of at least 1. The "rank" column has
a higher number t indicate the test is more important, and rank 0 means the
test does not need to be run to cover the points.  "RankPts" indicates the
number of coverage points this test will contribute to overall coverage if
all tests are run in the order of highest to lowest rank.

=item --unlink

When using --write to combine coverage data, unlink all input files after
the output has been created.

=item --version

Displays program version and exits.

=item --write I<filename>

Specifies the aggregate coverage results, summed across all the files,
should be written to the given filename in verilator_coverage data format.
This is useful in scripts to combine many sequential runs into one master
coverage file.

=item --write-info I<filename.info>

Specifies the aggregate coverage results, summed across all the files,
should be written to the given filename in C<lcov> .info format.
This may be used to use C<lcov> to aggregate or generate reports.

The info format loses data compared to the Verilator coverage data format;
the info will all forms of coverage converted to line style coverage, and
if there are multiple coverage points on a single line, the minimum
coverage across those points will be used to report coverage of the line.

=back

=head1 VERILOG ARGUMENTS

The following arguments are compatible with GCC, VCS and most Verilog
programs.

=over 4

=item +libext+I<ext>+I<ext>...

Defines the extensions for Verilog files.

=item +define+I<var>+I<value>
=item -DI<var>=I<value>

Defines the given variable.

=item +incdir+I<dir>
=item -II<dir>

Specifies a directory for finding include files.

=item -f I<file>

Specifies a file containing additional command line arguments.

=item -y I<dir>

Specifies a module search directory.

=back

=head1 DISTRIBUTION

The latest version is available from L<https://verilator.org>.

Copyright 2003-2020 by Wilson Snyder. This program is free software; you
can redistribute it and/or modify the Verilator internals under the terms
of either the GNU Lesser General Public License Version 3 or the Perl
Artistic License Version 2.0.

SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0

=head1 AUTHORS

Wilson Snyder <wsnyder@wsnyder.org>

=head1 SEE ALSO

C<verilator>, C<lcov>

L<verilator_coverage --help> which is the source for this document.

=cut

######################################################################
