| 1 | package Makefile::AST::Evaluator; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.215'; | ||
| 7 | |||
| 8 | #use Smart::Comments; | ||
| 9 | #use Smart::Comments '####'; | ||
| 10 | use File::stat; | ||
| 11 | use Class::Trigger qw(firing_rule); | ||
| 12 | |||
| 13 | # XXX put these globals to some better place | ||
| 14 | our ( | ||
| 15 | $Quiet, $JustPrint, $IgnoreErrors, | ||
| 16 | $AlwaysMake, $Question | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub new ($$) { | ||
| 20 | my $class = ref $_[0] ? ref shift : shift; | ||
| 21 | my $ast = shift; | ||
| 22 | return bless { | ||
| 23 | ast => $ast, | ||
| 24 | updated => {}, | ||
| 25 | mtime_cache => {}, # this is better for the AST? | ||
| 26 | parent_target => undef, | ||
| 27 | targets_making => {}, | ||
| 28 | required_targets => {}, | ||
| 29 | }, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub ast ($) { $_[0]->{ast} } | ||
| 33 | |||
| 34 | sub mark_as_updated ($$) { | ||
| 35 | my ($self, $target) = @_; | ||
| 36 | ### marking target as updated: $target | ||
| 37 | $self->{updated}->{$target} = 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | # XXX this should be moved to the AST | ||
| 41 | sub is_updated ($$) { | ||
| 42 | my ($self, $target) = @_; | ||
| 43 | $self->{updated}->{$target}; | ||
| 44 | } | ||
| 45 | |||
| 46 | # update the mtime cache with -M $file | ||
| 47 | sub update_mtime ($$@) { | ||
| 48 | my ($self, $file, $cache) = @_; | ||
| 49 | $cache ||= $self->{mtime_cache}; | ||
| 50 | if (-e $file) { | ||
| 51 | my $stat = stat $file or | ||
| 52 | die "$::MAKE: *** stat failed on $file: $!\n"; | ||
| 53 | ### set mtime for file: $file | ||
| 54 | ### mtime: $stat->mtime | ||
| 55 | return ($cache->{$file} = $stat->mtime); | ||
| 56 | } else { | ||
| 57 | ## file not found: $file | ||
| 58 | return ($cache->{$file} = undef); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | # get -M $file from cache (if any) or set the cache | ||
| 63 | # key-value pair otherwise | ||
| 64 | sub get_mtime ($$) { | ||
| 65 | my ($self, $file) = @_; | ||
| 66 | my $cache = $self->{mtime_cache}; | ||
| 67 | if (!exists $cache->{$file}) { | ||
| 68 | # set the cache | ||
| 69 | return $self->update_mtime($file, $cache); | ||
| 70 | } | ||
| 71 | return $cache->{$file}; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub set_required_target ($$) { | ||
| 75 | my ($self, $target) = @_; | ||
| 76 | $self->{required_targets}->{$target} = 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub is_required_target ($$) { | ||
| 80 | my ($self, $target) = @_; | ||
| 81 | $self->{required_targets}->{$target}; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub make ($$) { | ||
| 85 | my ($self, $target) = @_; | ||
| 86 | return 'UP_TO_DATE' | ||
| 87 | if $self->is_updated($target); | ||
| 88 | my $making = $self->{targets_making}; | ||
| 89 | if ($making->{$target}) { | ||
| 90 | warn "$::MAKE: Circular $target <- $target ". | ||
| 91 | "dependency dropped.\n"; | ||
| 92 | return 'UP_TO_DATE'; | ||
| 93 | } else { | ||
| 94 | $making->{$target} = 1; | ||
| 95 | } | ||
| 96 | my $retval; | ||
| 97 | my @rules = $self->ast->apply_explicit_rules($target); | ||
| 98 | ### number of explicit rules: scalar(@rules) | ||
| 99 | if (@rules == 0) { | ||
| 100 | ### no rule matched the target: $target | ||
| 101 | ### trying to make implicitly here... | ||
| 102 | my $ret = $self->make_implicitly($target); | ||
| 103 | delete $making->{$target}; | ||
| 104 | if (!$ret) { | ||
| 105 | return $self->make_by_rule($target => undef); | ||
| 106 | } else { | ||
| 107 | return $ret; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | # run the double-colon rules serially or run the | ||
| 111 | # single matched single-colon rule: | ||
| 112 | for my $rule (@rules) { | ||
| 113 | my $ret; | ||
| 114 | ### explicit rule for: $target | ||
| 115 | ### explicit rule: $rule->as_str | ||
| 116 | if (!$rule->has_command) { # XXX is this really necessary? | ||
| 117 | ### The explicit rule has no command, so | ||
| 118 | ### trying to make implicitly... | ||
| 119 | $ret = $self->make_implicitly($target); | ||
| 120 | $retval = $ret if !$retval || $ret eq 'REBUILT'; | ||
| 121 | } | ||
| 122 | $ret = $self->make_by_rule($target => $rule); | ||
| 123 | ### make_by_rule returned: $ret | ||
| 124 | $retval = $ret if !$retval || $ret eq 'REBUILT'; | ||
| 125 | } | ||
| 126 | delete $making->{$target}; | ||
| 127 | |||
| 128 | # postpone the timestamp propagation until all individual | ||
| 129 | # rules have been updated: | ||
| 130 | $self->update_mtime($target); | ||
| 131 | |||
| 132 | $self->mark_as_updated($target); | ||
| 133 | |||
| 134 | return $retval; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub make_implicitly ($$) { | ||
| 138 | my ($self, $target) = @_; | ||
| 139 | if ($self->ast->is_phony_target($target)) { | ||
| 140 | ### make_implicitly skipped target since it's phony: $target | ||
| 141 | return undef; | ||
| 142 | } | ||
| 143 | my $rule = $self->ast->apply_implicit_rules($target); | ||
| 144 | if (!$rule) { | ||
| 145 | return undef; | ||
| 146 | } | ||
| 147 | ### implicit rule: $rule->as_str | ||
| 148 | my $retval = $self->make_by_rule($target => $rule); | ||
| 149 | if ($retval eq 'REBUILT') { | ||
| 150 | for my $target ($rule->other_targets) { | ||
| 151 | $self->mark_as_updated($target); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | return $retval; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub make_by_rule ($$$) { | ||
| 158 | my ($self, $target, $rule) = @_; | ||
| 159 | ### make_by_rule (target): $target | ||
| 160 | return 'UP_TO_DATE' | ||
| 161 | if $self->is_updated($target) and $rule->colon eq ':'; | ||
| 162 | # XXX the parent should be passed via arguments or local vars | ||
| 163 | my $parent = $self->{parent_target}; | ||
| 164 | ## Retrieving parent target: $parent | ||
| 165 | if (!$rule) { | ||
| 166 | ## HERE! | ||
| 167 | ## exists? : -f $target | ||
| 168 | if (-f $target) { | ||
| 169 | return 'UP_TO_DATE'; | ||
| 170 | } else { | ||
| 171 | if ($self->is_required_target($target)) { | ||
| 172 | my $msg = | ||
| 173 | "$::MAKE: *** No rule to make target `$target'"; | ||
| 174 | if (defined $parent) { | ||
| 175 | $msg .= | ||
| 176 | ", needed by `$parent'"; | ||
| 177 | } | ||
| 178 | print STDERR "$msg."; | ||
| 179 | if ($Makefile::AST::Runtime) { | ||
| 180 | die " Stop.\n"; | ||
| 181 | } else { | ||
| 182 | warn " Ignored.\n"; | ||
| 183 | $self->mark_as_updated($target); | ||
| 184 | return 'UP_TO_DATE'; | ||
| 185 | } | ||
| 186 | } else { | ||
| 187 | return 'UP_TO_DATE'; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | ### make_by_rule (rule): $rule->as_str | ||
| 192 | ### stem: $rule->stem | ||
| 193 | |||
| 194 | # XXX solve pattern-specific variables here... | ||
| 195 | |||
| 196 | # enter pads for target-specific variables: | ||
| 197 | # XXX in order to solve '+=' and '?=', | ||
| 198 | # XXX we actually should NOT call enter pad | ||
| 199 | # XXX directly here... | ||
| 200 | my $saved_stack_len = $self->ast->pad_stack_len; | ||
| 201 | $self->ast->enter_pad($rule->target); | ||
| 202 | ## pad stack: $self->ast->{pad_stack}->[0] | ||
| 203 | |||
| 204 | my $target_mtime = $self->get_mtime($target); | ||
| 205 | my $out_of_date = | ||
| 206 | $self->ast->is_phony_target($target) || | ||
| 207 | !defined $target_mtime; | ||
| 208 | my $prereq_rebuilt; | ||
| 209 | ## Setting parent target to: $target | ||
| 210 | $self->{parent_target} = $target; | ||
| 211 | # process normal prereqs: | ||
| 212 | for my $prereq (@{ $rule->normal_prereqs }) { | ||
| 213 | # XXX handle order-only prepreqs here | ||
| 214 | ### processing prereq: $prereq | ||
| 215 | $self->set_required_target($prereq); | ||
| 216 | my $res = $self->make($prereq); | ||
| 217 | ### make returned: $res | ||
| 218 | if ($res and $res eq 'REBUILT') { | ||
| 219 | $out_of_date++; | ||
| 220 | $prereq_rebuilt++; | ||
| 221 | } elsif ($res and $res eq 'UP_TO_DATE') { | ||
| 222 | if (!$out_of_date) { | ||
| 223 | if ($self->get_mtime($prereq) > $target_mtime) { | ||
| 224 | ### prereq file is newer: $prereq | ||
| 225 | $out_of_date = 1; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } else { | ||
| 229 | die "make_by_rule: Unexpected returned value for prereq $prereq: $res"; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | # process order-only prepreqs: | ||
| 233 | for my $prereq (@{ $rule->order_prereqs }) { | ||
| 234 | ## process order-only prereq: $prereq | ||
| 235 | $self->set_required_target($prereq); | ||
| 236 | $self->make($prereq); | ||
| 237 | } | ||
| 238 | $self->{parent_target} = undef; | ||
| 239 | if ($AlwaysMake || $out_of_date) { | ||
| 240 | my @ast_cmds = $rule->prepare_commands($self->ast); | ||
| 241 | $self->call_trigger('firing_rule', $rule, \@ast_cmds); | ||
| 242 | if (!$Question) { | ||
| 243 | ### firing rule's commands: $rule->as_str | ||
| 244 | $rule->run_commands(@ast_cmds); | ||
| 245 | } | ||
| 246 | $self->mark_as_updated($rule->target) | ||
| 247 | if $rule->colon eq ':'; | ||
| 248 | if (my $others = $rule->other_targets) { | ||
| 249 | # mark "other targets" as updated too: | ||
| 250 | for my $other (@$others) { | ||
| 251 | ### marking "other target" as updated: $other | ||
| 252 | $self->mark_as_updated($other); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | $self->ast->leave_pad( | ||
| 256 | $self->ast->pad_stack_len - $saved_stack_len | ||
| 257 | ); | ||
| 258 | #### AST Commands: @ast_cmds | ||
| 259 | return 'REBUILT' | ||
| 260 | if @ast_cmds or $prereq_rebuilt; | ||
| 261 | } | ||
| 262 | $self->ast->leave_pad( | ||
| 263 | $self->ast->pad_stack_len - $saved_stack_len | ||
| 264 | ); | ||
| 265 | return 'UP_TO_DATE'; | ||
| 266 | } | ||
| 267 | |||
| 268 | 1; | ||
| 269 | __END__ |