From d07b201048ef6556e64da7ff0c4d3e3e70f23359 Mon Sep 17 00:00:00 2001 From: Chad Elliott Date: Fri, 17 May 2019 09:45:55 -0500 Subject: Fri May 17 14:42:45 UTC 2019 Chad Elliott * modules/Creator.pm: * modules/IARProjectCreator.pm: * modules/IARWorkspaceCreator.pm: If a project type requires relative paths, locate a relative path when replacing $() variables if at all possible. * modules/ProjectCreator.pm: * modules/TemplateParser.pm: * templates/iar.mpd: A new template function, 'extension', can be used to get the extension of a file. Additionally, non-template files can now be obtained through the custom input file interface. --- modules/Creator.pm | 63 ++++++++++++++++++++++++++++++++++++++---- modules/IARProjectCreator.pm | 6 ++++ modules/IARWorkspaceCreator.pm | 6 ++++ modules/ProjectCreator.pm | 16 +++++++++++ modules/TemplateParser.pm | 16 ++++++++++- 5 files changed, 101 insertions(+), 6 deletions(-) (limited to 'modules') diff --git a/modules/Creator.pm b/modules/Creator.pm index 1a0050cd..1d70dabe 100644 --- a/modules/Creator.pm +++ b/modules/Creator.pm @@ -1027,12 +1027,41 @@ sub get_outdir { } +sub aggressively_replace { + my($self, $icwd, $val) = @_; + my $count = 0; + my $wd = $icwd; + my $ival = ($self->{'case_tolerant'} ? lc($val) : $val); + + ## Search back up the directories until we either find a match or we + ## run out of directories. + while($wd =~ s/[^\/]+[\/]?$//) { + ## We have gone up one directory + $count++; + + ## Make a regular expression and see if we have found a match + ## with our provided directory value. + my $re = $self->escape_regex_special($wd); + if ($ival =~ /^($re)/) { + ## We have found how it is relative. Now make the relative path + ## and return it. + my $prefix = $1; + my $suffix = substr($val, length($prefix)); + return ('../' x $count) . $suffix; + } + } + + ## We never found a match + return undef; +} + sub expand_variables { my($self, $value, $rel, $expand_template, $scopes, $expand, $warn) = @_; my $cwd = $self->getcwd(); my $start = 0; my $forward_slashes = $self->{'convert_slashes'} || $self->{'requires_forward_slashes'}; + my $aggrep = $self->aggressive_relative_replacement(); ## Fix up the value for Windows switch the \\'s to / $cwd =~ s/\\/\//g if ($forward_slashes); @@ -1100,13 +1129,31 @@ sub expand_variables { ## instead of leaving it we will expand it. But, we will only ## get into this section if this is the secondary attempt to ## replace the variable (indicated by the $warn boolean). - $val =~ s/\//\\/g if ($self->{'convert_slashes'}); - substr($value, $start) =~ s/\$\([^)]+\)/$val/; - $whole = $val; + my $aggressive_rel; + if ($aggrep && + ($aggressive_rel = $self->aggressively_replace($icwd, $val))) { + $aggressive_rel =~ s/\//\\/g if ($self->{'convert_slashes'}); + substr($value, $start) =~ s/\$\([^)]+\)/$aggressive_rel/; + $whole = $aggressive_rel; + } + else { + $val =~ s/\//\\/g if ($self->{'convert_slashes'}); + substr($value, $start) =~ s/\$\([^)]+\)/$val/; + $whole = $val; + } } else { - my $loc = index(substr($value, $start), $whole); - $start += $loc if ($loc > 0); + my $aggressive_rel; + if ($aggrep && + ($aggressive_rel = $self->aggressively_replace($icwd, $val))) { + $aggressive_rel =~ s/\//\\/g if ($self->{'convert_slashes'}); + substr($value, $start) =~ s/\$\([^)]+\)/$aggressive_rel/; + $whole = $aggressive_rel; + } + else { + my $loc = index(substr($value, $start), $whole); + $start += $loc if ($loc > 0); + } } } } @@ -1285,6 +1332,12 @@ sub get_secondary_relative_values { } +sub aggressive_relative_replacement { + #my $self = shift; + return 0; +} + + sub convert_all_variables { #my $self = shift; return 0; diff --git a/modules/IARProjectCreator.pm b/modules/IARProjectCreator.pm index aa16ee8f..e509f613 100644 --- a/modules/IARProjectCreator.pm +++ b/modules/IARProjectCreator.pm @@ -75,4 +75,10 @@ sub get_cmdsep_symbol { } +sub aggressive_relative_replacement { + #my $self = shift; + return 1; +} + + 1; diff --git a/modules/IARWorkspaceCreator.pm b/modules/IARWorkspaceCreator.pm index ff17ae31..53e98ea6 100644 --- a/modules/IARWorkspaceCreator.pm +++ b/modules/IARWorkspaceCreator.pm @@ -70,4 +70,10 @@ sub post_workspace { } +sub aggressive_relative_replacement { + #my $self = shift; + return 1; +} + + 1; diff --git a/modules/ProjectCreator.pm b/modules/ProjectCreator.pm index 3a33609f..9bf06973 100644 --- a/modules/ProjectCreator.pm +++ b/modules/ProjectCreator.pm @@ -4617,6 +4617,22 @@ sub get_custom_value { } } } + elsif ($cmd eq 'non_template_output_files') { + # Generate non-template output files based on $based + if (defined $self->{'custom_output_files'}) { + $value = []; + foreach my $file (@{$self->{'custom_output_files'}->{$based}}) { + my $template = 0; + foreach my $ext (@{$self->{'valid_components'}->{'template_files'}}) { + if ($file =~ /$ext$/) { + $template = 1; + last; + } + } + push(@$value, $file) if (!$template); + } + } + } elsif ($cmd eq 'inputexts') { my @array = @{$self->{'valid_components'}->{$based}}; foreach my $val (@array) { diff --git a/modules/TemplateParser.pm b/modules/TemplateParser.pm index 723e524a..39f9762e 100644 --- a/modules/TemplateParser.pm +++ b/modules/TemplateParser.pm @@ -92,6 +92,7 @@ my %keywords = ('if' => 0, 'deref' => 0, 'set' => 0, 'is_relative' => $get_type|$doif_type|$get_combined_type, + 'extension' => $get_type, ); my %target_type_vars = ('type_is_static' => 1, @@ -388,7 +389,7 @@ sub get_value { if (!defined $value && $name =~ /^(.*)\->(\w+)/) { my $pre = $1; my $post = $2; - my $base = $self->get_value($pre); + my $base = $self->get_value($pre); if (defined $base) { $value = $self->{'prjc'}->get_special_value( @@ -2014,6 +2015,19 @@ sub handle_is_relative { } +sub get_extension { + my($self, $name) = @_; + my $val = $self->get_value_with_default($name); + return ($val =~ /(\.[^\.]+)$/ ? $1 : ''); +} + + +sub handle_extension { + my($self, $name) = @_; + $self->append_current($self->get_extension($name)); +} + + sub prepare_parameters { my($self, $prefix) = @_; my $input = $self->get_value($prefix . '->input_file'); -- cgit v1.2.1