| 1 | package Makefile::Parser::GmakeDB; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | #use Smart::Comments '####'; | ||
| 7 | #use Smart::Comments '###', '####'; | ||
| 8 | use List::Util qw( first ); | ||
| 9 | use List::MoreUtils qw( none ); | ||
| 10 | use MDOM::Document::Gmake; | ||
| 11 | use Makefile::AST; | ||
| 12 | |||
| 13 | our $VERSION = '0.215'; | ||
| 14 | |||
| 15 | # XXX This should not be hard-coded this way... | ||
| 16 | our @Suffixes = ( | ||
| 17 | '.out', | ||
| 18 | '.a', | ||
| 19 | '.ln', | ||
| 20 | '.o', | ||
| 21 | '.c', | ||
| 22 | '.cc', | ||
| 23 | '.C', | ||
| 24 | '.cpp', | ||
| 25 | '.p', | ||
| 26 | '.f', | ||
| 27 | '.F', | ||
| 28 | '.r', | ||
| 29 | '.y', | ||
| 30 | '.l', | ||
| 31 | '.s', | ||
| 32 | '.S', | ||
| 33 | '.mod', | ||
| 34 | '.sym', | ||
| 35 | '.def', | ||
| 36 | '.h', | ||
| 37 | '.info', | ||
| 38 | '.dvi', | ||
| 39 | '.tex', | ||
| 40 | '.texinfo', | ||
| 41 | '.texi', | ||
| 42 | '.txinfo', | ||
| 43 | '.w', | ||
| 44 | '.ch', | ||
| 45 | '.web', | ||
| 46 | '.sh', | ||
| 47 | '.elc', | ||
| 48 | '.el' | ||
| 49 | ); | ||
| 50 | |||
| 51 | # need a better place for this sub: | ||
| 52 | sub solve_escaped ($) { | ||
| 53 | my $ref = shift; | ||
| 54 | $$ref =~ s/\\ ([\#\\:\n])/$1/gx; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _match_suffix ($@); | ||
| 58 | |||
| 59 | sub _match_suffix ($@) { | ||
| 60 | my ($target, $full_match) = @_; | ||
| 61 | ## $target | ||
| 62 | ## $full_match | ||
| 63 | if ($full_match) { | ||
| 64 | return first { $_ eq $target } @Suffixes; | ||
| 65 | } else { | ||
| 66 | my ($fst, $snd); | ||
| 67 | for my $suffix (@Suffixes) { | ||
| 68 | my $len = length($suffix); | ||
| 69 | ## prefix 1: substr($target, 0, $len) | ||
| 70 | ## prefix 2: $suffix | ||
| 71 | if (substr($target, 0, $len) eq $suffix) { | ||
| 72 | $fst = $suffix; | ||
| 73 | ## first suffix recognized: $suffix | ||
| 74 | ## suffix 1: substr($target, $len) | ||
| 75 | $snd = _match_suffix(substr($target, $len), 1); | ||
| 76 | ## $snd | ||
| 77 | next if !defined $snd; | ||
| 78 | return ($fst, $snd); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | return undef; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | sub parse ($$) { | ||
| 86 | shift; | ||
| 87 | my $ast = Makefile::AST->new; | ||
| 88 | my $dom = MDOM::Document::Gmake->new(shift); | ||
| 89 | my ($var_origin, $orig_lineno, $orig_file); | ||
| 90 | my $rule; # The last rule in the context | ||
| 91 | my ($not_a_target, $directive); | ||
| 92 | my $db_section = 'null'; | ||
| 93 | my $next_var_lineno = 0; # lineno for the next var assignment | ||
| 94 | for my $elem ($dom->elements) { | ||
| 95 | ## elem class: $elem->class | ||
| 96 | ## elem lineno: $elem->lineno | ||
| 97 | ## NEXT VAR LINENO: $next_var_lineno | ||
| 98 | ## CURRENT LINENO: $elem->lineno | ||
| 99 | if ($elem =~ /^# Variables$/) { | ||
| 100 | ### Setting DB section to 'var': $elem->content | ||
| 101 | $db_section = 'var'; | ||
| 102 | next; | ||
| 103 | } | ||
| 104 | if ($elem =~ /^# (?:Implicit Rules|Directives|Files)$/) { | ||
| 105 | ### Setting DB section to 'rule': $elem->content | ||
| 106 | $db_section = 'rule'; | ||
| 107 | next; | ||
| 108 | } | ||
| 109 | if ($elem =~ /^# (?:Pattern-specific Variable Values)$/) { | ||
| 110 | ### Setting DB section to 'patspec': $elem->content | ||
| 111 | $db_section = 'patspec'; | ||
| 112 | next; | ||
| 113 | } | ||
| 114 | if ($directive and $elem->class !~ /Directive$/) { | ||
| 115 | # XXX yes, this is hacky | ||
| 116 | ### pushing value to value: $elem | ||
| 117 | push @{ $directive->{value} }, $elem->clone; | ||
| 118 | next; | ||
| 119 | } | ||
| 120 | next if $elem->isa('MDOM::Token::Whitespace'); | ||
| 121 | if ($db_section eq 'var' and $elem->isa('MDOM::Assignment')) { | ||
| 122 | ## Found assignment: $elem->source | ||
| 123 | if (!$var_origin) { | ||
| 124 | my $lineno = $elem->lineno; | ||
| 125 | die "ERROR: line $lineno: No flavor found for the assignment"; | ||
| 126 | } else { | ||
| 127 | my $lhs = $elem->lhs; | ||
| 128 | my $rhs = $elem->rhs; | ||
| 129 | my $op = $elem->op; | ||
| 130 | |||
| 131 | my $flavor; | ||
| 132 | if ($op eq '=') { | ||
| 133 | $flavor = 'recursive'; | ||
| 134 | } elsif ($op eq ':=') { | ||
| 135 | $flavor = 'simple'; | ||
| 136 | } else { | ||
| 137 | # XXX add support for ?= and += | ||
| 138 | die "Unknown op: $op"; | ||
| 139 | } | ||
| 140 | my $name = join '', @$lhs; # XXX solve refs? | ||
| 141 | my @value_tokens = map { $_->clone } @$rhs; | ||
| 142 | #map { $_ = "$_" } @$rhs; | ||
| 143 | ## LHS: $name | ||
| 144 | ## RHS: $rhs | ||
| 145 | my $var = Makefile::AST::Variable->new({ | ||
| 146 | name => $name, | ||
| 147 | flavor => $flavor, | ||
| 148 | origin => $var_origin, | ||
| 149 | value => \@value_tokens, | ||
| 150 | lineno => $orig_lineno, | ||
| 151 | file => $orig_file, | ||
| 152 | }); | ||
| 153 | $ast->add_var($var); | ||
| 154 | undef $var_origin; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | elsif ($elem =~ /^#\s+(automatic|makefile|default|environment|command line)/) { | ||
| 158 | $var_origin = $1; | ||
| 159 | $var_origin = 'file' if $var_origin eq 'makefile'; | ||
| 160 | $next_var_lineno = $elem->lineno + 1; | ||
| 161 | } | ||
| 162 | elsif ($elem =~ /^# `(\S+)' directive \(from `(\S+)', line (\d+)\)/) { | ||
| 163 | ($var_origin, $orig_file, $orig_lineno) = ($1, $2, $3); | ||
| 164 | $next_var_lineno = $elem->lineno + 1; | ||
| 165 | ### directive origin: $var_origin | ||
| 166 | ### directive lineno: $orig_lineno | ||
| 167 | } | ||
| 168 | elsif ($elem =~ /^#\s+.*\(from `(\S+)', line (\d+)\)/) { | ||
| 169 | ($orig_file, $orig_lineno) = ($1, $2); | ||
| 170 | ## lineno: $orig_lineno | ||
| 171 | } | ||
| 172 | elsif ($db_section eq 'rule' and $elem =~ /^# Not a target:$/) { | ||
| 173 | $not_a_target = 1; | ||
| 174 | } | ||
| 175 | elsif ($elem =~ /^# Implicit\/static pattern stem: `(\S+)'/) { | ||
| 176 | #### Setting pattern stem for solved implicit rule: $1 | ||
| 177 | $rule->{stem} = $1; | ||
| 178 | } | ||
| 179 | elsif ($db_section eq 'rule' and $elem =~ /^# Also makes: (.*)/) { | ||
| 180 | my @other_targets = split /\s+/, $1; | ||
| 181 | $rule->{other_targets} = \@other_targets; | ||
| 182 | #### Setting other targets: @other_targets | ||
| 183 | } | ||
| 184 | elsif ($db_section ne 'var' and | ||
| 185 | $next_var_lineno == $elem->lineno and | ||
| 186 | $elem =~ /^# (\S.*?) (:=|\?=|\+=|=) (.*)/) { | ||
| 187 | #die "HERE!"; | ||
| 188 | my ($name, $op, $value) = ($1, $2, $3); | ||
| 189 | # XXX tokenize the $value here? | ||
| 190 | if (!$rule) { | ||
| 191 | die "error: target/parttern-specific variables found where there is no rule in the context"; | ||
| 192 | } | ||
| 193 | my $flavor; | ||
| 194 | if ($op eq ':=') { | ||
| 195 | $flavor = 'simple'; | ||
| 196 | } else { | ||
| 197 | # XXX we should treat '?=' and '+=' specifically here? | ||
| 198 | $flavor = 'recursive'; | ||
| 199 | } | ||
| 200 | #### Adding local variable: $name | ||
| 201 | my $handle = sub { | ||
| 202 | my $ast = shift; | ||
| 203 | my $old_value = $ast->eval_var_value($name); | ||
| 204 | #warn "VALUE!!! $value"; | ||
| 205 | $value = "$old_value $value" if $op eq '+='; | ||
| 206 | my $var = Makefile::AST::Variable->new({ | ||
| 207 | name => $name, | ||
| 208 | flavor => $flavor, | ||
| 209 | origin => $var_origin, | ||
| 210 | value => $flavor eq 'recursive' ? $value : [$value], | ||
| 211 | lineno => $orig_lineno, | ||
| 212 | file => $orig_file, | ||
| 213 | }); | ||
| 214 | $ast->enter_pad(); | ||
| 215 | $ast->add_var($var); | ||
| 216 | }; | ||
| 217 | $ast->add_pad_trigger($rule->target => $handle); | ||
| 218 | undef $var_origin; | ||
| 219 | } | ||
| 220 | elsif ($db_section ne 'var' and $elem->isa('MDOM::Rule::Simple')) { | ||
| 221 | ### Found rule: $elem->source | ||
| 222 | ### not a target? : $not_a_target | ||
| 223 | if ($rule) { | ||
| 224 | # The db output tends to produce | ||
| 225 | # trailing empty commands, so we remove it: | ||
| 226 | if ($rule->{commands}->[-1] and | ||
| 227 | $rule->{commands}->[-1] eq "\n") { | ||
| 228 | pop @{ $rule->{commands} }; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | if ($not_a_target) { | ||
| 232 | $not_a_target = 0; | ||
| 233 | next; | ||
| 234 | } | ||
| 235 | my $targets = $elem->targets; | ||
| 236 | my $colon = $elem->colon; | ||
| 237 | my $normal_prereqs = $elem->normal_prereqs; | ||
| 238 | my $order_prereqs = $elem->order_prereqs; | ||
| 239 | my $command = $elem->command; | ||
| 240 | |||
| 241 | ## Target (raw): $targets | ||
| 242 | ## Prereq (raw): $prereqs | ||
| 243 | |||
| 244 | my $target = join '', @$targets; | ||
| 245 | my @order_prereqs = split /\s+/, join '', @$order_prereqs; | ||
| 246 | my @normal_prereqs = split /\s+/, join '', @$normal_prereqs; | ||
| 247 | |||
| 248 | # Solve escaped chars: | ||
| 249 | solve_escaped(\$target); | ||
| 250 | map { solve_escaped(\$_) } @normal_prereqs, @order_prereqs; | ||
| 251 | @order_prereqs = grep { | ||
| 252 | my $value = $_; | ||
| 253 | none { $_ eq $value } @normal_prereqs | ||
| 254 | } @order_prereqs if @normal_prereqs; | ||
| 255 | |||
| 256 | #### Target: $target | ||
| 257 | ### Normal Prereqs: @normal_prereqs | ||
| 258 | ### Order-only Prereqs: @order_prereqs | ||
| 259 | |||
| 260 | #map { $_ = "$_" } @normal_prereqs, @order_prereqs; | ||
| 261 | # XXX suffix rules allow order-only prepreqs? not sure... | ||
| 262 | if ($target !~ /\s/ and $target !~ /\%/ and !@normal_prereqs and !@order_prereqs) { | ||
| 263 | ## try to recognize suffix rule: $target | ||
| 264 | my ($fst, $snd); | ||
| 265 | $fst = _match_suffix($target, 1); | ||
| 266 | if (!defined $fst) { | ||
| 267 | ($fst, $snd) = _match_suffix($target); | ||
| 268 | ## got first: $fst | ||
| 269 | ## got second: $snd | ||
| 270 | if (defined $fst) { | ||
| 271 | ## found suffix rule/2: $target | ||
| 272 | $target = '%' . $snd; | ||
| 273 | @normal_prereqs = ('%' . $fst); | ||
| 274 | } | ||
| 275 | } else { | ||
| 276 | ## found suffix rule rule/1: $target | ||
| 277 | $target = '%' . $fst; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | my $rule_struct = { | ||
| 281 | order_prereqs => [], | ||
| 282 | normal_prereqs => \@normal_prereqs, | ||
| 283 | order_prereqs => \@order_prereqs, | ||
| 284 | commands => [defined $command ? $command : ()], | ||
| 285 | colon => $colon, | ||
| 286 | target => $target, | ||
| 287 | }; | ||
| 288 | if ($target =~ /\%/) { | ||
| 289 | ## implicit rule found: $target | ||
| 290 | my $targets = [split /\s+/, $target]; | ||
| 291 | $rule_struct->{targets} = $targets, | ||
| 292 | $rule = Makefile::AST::Rule::Implicit->new($rule_struct); | ||
| 293 | $ast->add_implicit_rule($rule) if $db_section eq 'rule'; | ||
| 294 | } else { | ||
| 295 | $rule = Makefile::AST::Rule->new($rule_struct); | ||
| 296 | $ast->add_explicit_rule($rule) if $db_section eq 'rule'; | ||
| 297 | } | ||
| 298 | } elsif ($elem->isa('MDOM::Command')) { | ||
| 299 | ## Found command: $elem | ||
| 300 | if (!$rule) { | ||
| 301 | die "error: line " . $elem->lineno . | ||
| 302 | ": Command not allowed here"; | ||
| 303 | } else { | ||
| 304 | #my @tokens = map { "$_" } $elem->elements; | ||
| 305 | #my @tokens = $elem | ||
| 306 | #shift @tokens if $tokens[0] eq "\t"; | ||
| 307 | #pop @tokens if $tokens[-1] eq "\n"; | ||
| 308 | #push @{ $rule->{commands} }, \@tokens; | ||
| 309 | ## parser: CMD: $elem | ||
| 310 | my $first = $elem->first_element; | ||
| 311 | ## $first | ||
| 312 | $elem->remove_child($first) | ||
| 313 | if $first->class eq 'MDOM::Token::Separator'; | ||
| 314 | ### elem source: $elem->source | ||
| 315 | #if ($elem->source eq "\n") { | ||
| 316 | # die "Matched!"; | ||
| 317 | #} | ||
| 318 | ## lineno2: $orig_lineno | ||
| 319 | $elem->{lineno} = $orig_lineno if $orig_lineno; | ||
| 320 | $rule->add_command($elem->clone); # XXX why clone? | ||
| 321 | ### Command added: $elem->content | ||
| 322 | } | ||
| 323 | } elsif ($elem->class =~ /MDOM::Directive/) { | ||
| 324 | ### directive name: $elem->name | ||
| 325 | ### directive value: $elem->value | ||
| 326 | if ($elem->name eq 'define') { | ||
| 327 | # XXX set lineno to $orig_lineno here? | ||
| 328 | $directive = { | ||
| 329 | name => $elem->value, | ||
| 330 | value => [], # needs to be fed later | ||
| 331 | flavor => 'recursive', | ||
| 332 | origin => $var_origin, | ||
| 333 | lineno => $orig_lineno, | ||
| 334 | file => $orig_file, | ||
| 335 | }; | ||
| 336 | next; | ||
| 337 | } | ||
| 338 | if ($elem->name eq 'endef') { | ||
| 339 | ### parsed a define directive: $directive | ||
| 340 | # trim the trailing new lines in the value: | ||
| 341 | #warn "HERE!!! "; | ||
| 342 | #warn quotemeta($directive->{value}->[-1]); | ||
| 343 | my $last = $directive->{value}->[-1]; | ||
| 344 | if ("$last" =~ /^\s*$/s) { | ||
| 345 | pop @{ $directive->{value} } | ||
| 346 | |||
| 347 | } elsif ($last->can('last_element')) { | ||
| 348 | my $last_elem = $last->last_element; | ||
| 349 | #warn "LAST: '$last'\n"; | ||
| 350 | if ($last_elem and "$last_elem" =~ /^\s*$/s) { | ||
| 351 | $last->remove_child($last_elem); | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | my $var = Makefile::AST::Variable->new($directive); | ||
| 356 | $ast->add_var($var); | ||
| 357 | undef $var_origin; | ||
| 358 | undef $directive; | ||
| 359 | |||
| 360 | } else { | ||
| 361 | warn "warning: line " . $elem->lineno . | ||
| 362 | ": Unknown directive: " . $elem->source; | ||
| 363 | } | ||
| 364 | } elsif ($elem->class =~ /Unknown/) { | ||
| 365 | # XXX Note that output from $(info ...) may skew up stdout | ||
| 366 | # XXX This hack is used to make features/conditionals.t pass | ||
| 367 | print $elem if $elem eq "success\n"; | ||
| 368 | # XXX The 'hello, world' hack to used to make sanity/func-refs.t pass | ||
| 369 | warn "warning: line " . $elem->lineno . | ||
| 370 | ": Unknown GNU make database struct: " . | ||
| 371 | $elem->source | ||
| 372 | if $elem !~ /hello.*world/ and | ||
| 373 | $elem ne "success\n"; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | { | ||
| 377 | my $default = $ast->eval_var_value('.DEFAULT_GOAL'); | ||
| 378 | ## default goal's value: $var | ||
| 379 | $ast->default_goal($default) if $default; | ||
| 380 | ### DEFAULT GOAL: $ast->default_goal | ||
| 381 | |||
| 382 | my $rule = $ast->apply_explicit_rules('.PHONY'); | ||
| 383 | if ($rule) { | ||
| 384 | ### PHONY RULE: $rule | ||
| 385 | ### phony targets: @{ $rule->normal_prereqs } | ||
| 386 | for my $phony (@{ $rule->normal_prereqs }) { | ||
| 387 | $ast->set_phony_target($phony); | ||
| 388 | } | ||
| 389 | } | ||
| 390 | ## foo var: $ast->get_var('foo') | ||
| 391 | } | ||
| 392 | $ast; | ||
| 393 | } | ||
| 394 | |||
| 395 | 1; | ||
| 396 | __END__ |