📄 getopt.pm
字号:
sub file_path { my $self = shift; my $filename = shift; my $lookup_type = shift || 'all'; # return path to given filename using library directories & files, or undef # locations are cached, because -r can be a very slow operation defined $filename or carp "%Error: Undefined filename,"; return $self->{_file_path_cache}{$filename} if defined $self->{_file_path_cache}{$filename}; if (-r $filename && !-d $filename) { $self->{_file_path_cache}{$filename} = $filename; $self->depend_files($filename); return $filename; } # What paths to use? my @dirlist; if ($lookup_type eq 'module') { @dirlist = $self->module_dir(); } elsif ($lookup_type eq 'include') { @dirlist = $self->incdir(); } else { # all @dirlist = ($self->incdir(), $self->module_dir()); } # Expand any envvars in incdir/moduledir @dirlist = map {$self->file_substitute($_)} @dirlist; # Check each search path # We use both the incdir and moduledir. This isn't strictly correct, # but it's fairly silly to have to specify both all of the time. my %checked_dir = (); my %checked_file = (); foreach my $dir (@dirlist) { next if $checked_dir{$dir}; $checked_dir{$dir}=1; # -r can be quite slow # Check each postfix added to the file foreach my $postfix ("", @{$self->{libext}}) { my $found = "$dir/$filename$postfix"; next if $checked_file{$found}; $checked_file{$found}=1; # -r can be quite slow if (-r $found && !-d $found) { $self->{_file_path_cache}{$filename} = $found; $self->depend_files($found); return $found; } } } return $filename; # Let whoever needs it discover it doesn't exist}sub libext_matches { my $self = shift; my $filename = shift; return undef if !$filename; foreach my $postfix (@{$self->{libext}}) { my $re = quotemeta($postfix) . "\$"; return $filename if ($filename =~ /$re/); } return undef;}sub map_directories { my $self = shift; my $func = shift; # Execute map function on all directories listed in self. { my @newdir = $self->incdir(); @newdir = map {&{$func}} @newdir; $self->incdir(\@newdir); } { my @newdir = $self->module_dir(); @newdir = map {&{$func}} @newdir; $self->module_dir(\@newdir); }}######################################################################## Getopt functionssub defparams { my $self = shift; my $token = shift; my $val = $self->{defines}{$token}; if (!defined $val) { return undef; } elsif (ref $val) { return $val->[1]; # Has parameters, return param list } else { return 0; }}sub defvalue { my $self = shift; my $token = shift; my $val = $self->{defines}{$token}; (defined $val) or carp "%Warning: ".$self->fileline().": No definition for $token,"; if (ref $val) { return $val->[0]; # Has parameters, return just value } else { return $val; }}sub defvalue_nowarn { my $self = shift; my $token = shift; my $val = $self->{defines}{$token}; if (ref $val) { return $val->[0]; # Has parameters, return just value } else { return $val; }}sub define { my $self = shift; if (@_) { my $token = shift; my $value = shift; my $params = shift||""; print "Define $token $params= $value\n" if $Debug; my $oldval = $self->{defines}{$token}; my $oldparams; if (ref $oldval eq 'ARRAY') { ($oldval, $oldparams) = @{$oldval}; } if (defined $oldval && (($oldval ne $value) || (defined $oldparams && $oldparams ne $params)) && $self->{define_warnings}) { warn "%Warning: ".$self->fileline().": Redefining `$token\n"; } if ($params) { $self->{defines}{$token} = [$value, $params]; } else { $self->{defines}{$token} = $value; } }}sub undef { my $self = shift; my $token = shift; my $oldval = $self->{defines}{$token}; # We no longer warn about undefing something that doesn't exist, as other compilers don't #(defined $oldval or !$self->{define_warnings}) # or carp "%Warning: ".$self->fileline().": No definition to undef for $token,"; delete $self->{defines}{$token};}sub remove_defines { my $self = shift; my $sym = shift; my $val = "x"; while (defined $val) { last if $sym eq $val; (my $xsym = $sym) =~ s/^\`//; $val = $self->defvalue_nowarn($xsym); #Undef if not found $sym = $val if defined $val; } return $sym;}######################################################################### Package return1;__END__=pod=head1 NAMEVerilog::Getopt - Get Verilog command line options=head1 SYNOPSIS use Verilog::Getopt; my $opt = new Verilog::Getopt; $opt->parameter (qw( +incdir+standard_include_directory )); @ARGV = $opt->parameter (@ARGV); ... print "Path to foo.v is ", $opt->file_path('foo.v');=head1 DESCRIPTIONVerilog::Getopt provides standardized handling of options similar toVerilog/VCS and cc/GCC.=over 4=item $opt = Verilog::Getopt->new ( I<opts> )Create a new Getopt. If gcc_style=>0 is passed as a parameter, parsing ofGCC-like parameters is disabled. If vcs_style=>0 is passed as a parameter,parsing of VCS-like parameters is disabled.=item $self->file_path ( I<filename>, [I<lookup-type>] )Returns a new path to the filename, using the library directories andsearch paths to resolve the file. Optional lookup-type is'module','include', or 'all', to use only module_dirs, incdirs, or both forthe lookup.=item $self->get_parameters ( )Returns a list of parameters that when passed through $self->parameter()should result in the same state. Often this is used to form command linesfor downstream programs that also use Verilog::Getopt.=item $self->parameter ( \@params )Parses any recognized parameters in the referenced array, removing thestandard parameters and returning a array with all unparsed parameters.The below list shows the VCS-like parameters that are supported, and thefunctions that are called: +libext+I<ext>+I<ext>... libext (I<ext>) +incdir+I<dir> incdir (I<dir>) +define+I<var>[+=]I<value> define (I<var>,I<value>) +define+I<var> define (I<var>,undef) +librescan Ignored -f I<file> Parse parameters in file -v I<file> library (I<file>) -y I<dir> module_dir (I<dir>) all others Put in returned listThe below list shows the GCC-like parameters that are supported, and thefunctions that are called: -DI<var>=I<value> define (I<var>,I<value>) -DI<var> define (I<var>,undef) -UI<var> undefine (I<var>) -II<dir> incdir (I<dir>) -f I<file> Parse parameters in file all others Put in returned list=item $self->write_parameters_file ( I<filename> )Write the output from get_parameters to the specified file.=back=head1 ACCESSORS=over 4=item $self->define ( $token, $value )This method is called when a define is recognized. The default behaviorloads a hash that is used to fulfill define references. This function mayalso be called outside parsing to predefine values.=item $self->defparams ( $token )This method returns the parameter list of the define. This will be defined,but false, if the define does not have arguments.=item $self->defvalue ( $token )This method returns the value of a given define, or prints a warning.=item $self->defvalue_nowarn ( $token )This method returns the value of a given define, or undef.=item $self->depend_files ()Returns reference to list of filenames referenced with file_path, usefulfor creating dependency lists. With argument, adds that file. With listreference argument, sets the list to the argument.=item $self->file_abs ( $filename )Using the incdir and libext lists, convert the specified module or filename("foo") to a absolute filename ("include/dir/foo.v").=item $self->file_skip_special ( $filename )Return true if the filename is one that generally should be ignored whenrecursing directories, such as for example, ".", "CVS", and ".svn".=item $self->file_substitute ( $filename )Removes existing environment variables from the provided filename. Anyundefined variables are not substituted nor cause errors.=item $self->incdir ()Returns reference to list of include directories. With argument, adds thatdirectory.=item $self->libext ()Returns reference to list of library extensions. With argument, adds thatextension.=item $self->libext_matches (I<filename>)Returns true if the passed filename matches the libext.=item $self->library ()Returns reference to list of libraries. With argument, adds that library.=item $self->module_dir ()Returns reference to list of module directories. With argument, adds thatdirectory.=item $self->remove_defines ( $token )Return string with any definitions in the token removed.=item $self->undef ( $token )Deletes a hash element that is used to fulfill define references. Thisfunction may also be called outside parsing to erase a predefined value.=back=head1 DISTRIBUTIONVerilog-Perl is part of the L<http://www.veripool.org/> free Verilog EDAsoftware tool suite. The latest version is available from CPAN and fromL<http://www.veripool.org/verilog-perl>.Copyright 2000-2009 by Wilson Snyder. This package is free software; youcan redistribute it and/or modify it under the terms of either the GNULesser General Public License or the Perl Artistic License.=head1 AUTHORSWilson Snyder <wsnyder@wsnyder.org>=head1 SEE ALSOL<Verilog-Perl>,L<Verilog::Language>=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -