###################################################################### use FindBin qw($RealBin); use lib "$RealBin/blib/arch"; use lib "$RealBin/blib/lib"; use lib "$RealBin"; use Getopt::Long; use IO::File; use Pod::Usage; use Verilog::Preproc; use Verilog::Getopt; use strict; use vars qw ($Debug $VERSION); $VERSION = '3.305'; ###################################################################### # main $Debug = 0; my $opt_output_filename = undef; my $opt_blank=1; my $opt_dump_defines; my @opt_files; my @opt_pp_flags; autoflush STDOUT 1; autoflush STDERR 1; # Option parsing my $Opt = new Verilog::Getopt(); @ARGV = $Opt->parameter(@ARGV); # 先用verilog::Getopt扫描命令行 Getopt::Long::config ("no_auto_abbrev"); if (! GetOptions ( "help" => /&usage, "debug" => /&debug, "o=s" => /$opt_output_filename, "blank!" => /$opt_blank, "comment!" => sub { push @opt_pp_flags, (keep_comments=>$_[1]); }, #把keep_comment=>$comment放入数组中 "dump-defines!" => /$opt_dump_defines, "line!" => sub { push @opt_pp_flags, (line_directives=>$_[1]); }, # $_[1} 代表 索引的值 "pedantic!" => sub { push @opt_pp_flags, (pedantic=>$_[1]); }, "simple!" => sub { if ($_[1]) { push @opt_pp_flags, (keep_comments=>0, line_directives=>0, ); $opt_blank=0; } }, "version" => sub { print "Version $VERSION/n"; exit(0); }, "<>" => /¶meter, )) { die "%Error: Bad usage, try 'vppreproc --help'/n"; } if (!@opt_files) { die "%Error: vppreproc: No input filenames specified./n"; } my $fh = IO::File->new; if ($opt_output_filename) { #指定输出文件,否则输出到STDOUT $fh->open(">$opt_output_filename") or die "%Error: $! $opt_output_filename/n"; } else { $fh->open(">-") or die; } my $vp = Verilog::Preproc->new(@opt_pp_flags, options=>$Opt,); #根据选项,建立preprocessor $vp->debug($Debug) if $Debug; foreach my $file (@opt_files) { $vp->open($file); #对打开的文件进行preprocessor处理 # Alternatively, use $vp->getall for better performance while (defined (my $line = $vp->getline())) { #对处理后的每一行取出,再进一步处理 next if !$opt_blank && $line =~ /^/s*[/n]?$/; print $fh $line unless $opt_dump_defines; } } if ($opt_dump_defines) { foreach my $name ($Opt->define_names_sorted) { #对在命令行中指出的define,抽取出 name, 参数, 值。 打印到屏幕。 my $par = $Opt->defparams($name); $par="" if !$par; my $value = $Opt->defvalue($name); printf "`define %s%s %s/n", $name,$par,$value; } } exit (0); ###################################################################### sub usage { print "Version $VERSION/n"; pod2usage(-verbose=>2, -exitval => 2); exit (1); } sub debug { $Debug = 1; } sub parameter { my $param = shift; if ($param =~ /^--?/) { die "%Error: vppreproc: Unknown parameter: $param/n"; } else { push @opt_files, "$param"; # Must quote to convert Getopt to string, bug298 } }