1# $Id: Rule.pm,v 1.121 2012/03/04 13:56:35 pfeiffer Exp $ 2use strict qw(vars subs); 3 4package Mpp::Rule; 5 6use Mpp::Event qw(when_done wait_for); 7use Mpp::File; 8use Mpp::FileOpt; 9use Mpp::Text; 10use Mpp::BuildCheck::exact_match; 11use Mpp::Lexer; 12use Mpp::Cmds; 13use Mpp::Scanner; 14 15our $unsafe; # Perl code was performed, that might leave files open or chdir. 16 17=head1 NAME 18 19Rule -- Stores information about a build rule 20 21=head1 USAGE 22 23 my $rule = new Mpp::Rule "targets", "dependencies", 24 "command"[, $makefile, $makefile_line]; 25 26=head1 DESCRIPTION 27 28A rule object contains all the information necessary to make a specific file. 29 30$makefile is a pointer to the structure returned by load_makefile. 31$makefile_line is an ASCII string that is used if an error message occurs 32while expanding variables in this rule. 33 34This does not actually place the rule into the Mpp::File hierarchy. 35 36=cut 37 38sub new { 39 #my ($class, $targets, $dependencies, $command, $makefile, $source_line, $build_check_method, $signature_method) = @_; 40 bless { TARGET_STRING => $_[1], 41 DEPENDENCY_STRING => $_[2], 42 COMMAND_STRING => $_[3], 43 MAKEFILE => $_[4], 44 LOAD_IDX => $_[4]{LOAD_IDX}, 45 RULE_SOURCE => $_[5] 46 }, $_[0]; 47 # Make the rule object. 48} 49 50# 51# Return the directory that this rule should be executed in. 52# 53sub build_cwd { $_[0]{MAKEFILE}{CWD} } 54 55# 56# Return a build cache associated with this rule, if any. 57# 58# A build cache may be specified for each rule, or for a whole makefile, 59# or for all makefiles (on the command line). 60# 61sub build_cache { 62 exists $_[0]{BUILD_CACHE} ? $_[0]{BUILD_CACHE} : 63 exists $_[0]{MAKEFILE}{BUILD_CACHE} ? $_[0]{MAKEFILE}{BUILD_CACHE} : 64 $Mpp::BuildCache::global; 65} 66 67# 68# Set the build cache for this rule. 69# 70sub set_build_cache { 71 $_[0]{BUILD_CACHE} = $_[1]; 72} 73 74# 75# Return the makefile that made this rule: 76# 77sub makefile { $_[0]{MAKEFILE} } 78 79# 80# This subroutine is called to find all the targets and dependencies of the 81# rule. It does so by expanding the dependency and target string, and 82# lexically analysing the action, which in turn parses the commands, which 83# scans some types of input files, like C for #include statements. 84# 85# Usage: 86# ($all_targets, $all_dependencies, $action_string, $env_deps) = 87# $rule->find_all_targets_dependencies($oinfo); 88# 89# Where all_targets is a reference to a list of target object info structs, 90# all_dependencies is a reference to a list of dependency object info structs, 91# $action_string is the final build command after expanding all make 92# variables, and $env_deps is a reference to a hash mapping environmental 93# dependencies to current values. $oinfo is the requested target, which is 94# used only to determine where to look for cached scan information. 95# 96my $action_prefix = qr/(?:[-+\@]\s*|noecho\s+|ignore_error\s+)*/; 97sub find_all_targets_dependencies { 98 my ($self, $oinfo, $dont_scan) = @_; 99 100 my $build_cwd = $self->build_cwd; # Access the default directory. 101 102 my %all_targets; # A hash of all targets, so we can tell quickly 103 # whether we already know about a given target. 104 local $Mpp::Subs::rule = $self; # Set this up so that subroutines can find the 105 # rule that is currently being expanded. 106 107 local $self->{ALL_TARGETS} = \%all_targets; 108 # Store this so it can be found easily but 109 # also goes away automatically. 110 111 my %all_dependencies; # A hash of all dependencies, so we can tell 112 # whether we already know about a given 113 # dependency. 114 local $self->{ALL_DEPENDENCIES} = \%all_dependencies; 115 116 my %env_dependencies; # A hash of dependencies on the environment. 117 local $self->{ENV_DEPENDENCIES} = \%env_dependencies; 118 119 my @explicit_dependencies; # The ones that were listed in the dependency 120 # list or explicitly somewhere else in the 121 # makefile. 122 local $self->{EXPLICIT_DEPENDENCIES} = \@explicit_dependencies; 123 124 my @explicit_targets; # The ones that were listed and not inferred 125 # from the command. 126 127 local $self->{EXPLICIT_TARGETS} = \@explicit_targets; 128 129 my @extra_dependencies; 130 local $self->{EXTRA_DEPENDENCIES} = \@extra_dependencies; 131 132 my $makefile = $self->{MAKEFILE}; 133 134# 135# Get the full list of explicit targets: 136# 137 my $target_string = 138 $makefile->expand_text($self->{TARGET_STRING}, $self->{RULE_SOURCE}); 139 140 141 for( split_on_whitespace $target_string ) { 142 my $tinfo = file_info unquote(), $build_cwd; 143 push @explicit_targets, $tinfo; 144 $self->add_target( $tinfo ); 145 } 146 147# This is a good time to expand the dispatch rule option, if any: 148 if($self->{DISPATCH}) { 149 $self->{DISPATCH} = $makefile->expand_text( $self->{DISPATCH}, $self->{RULE_SOURCE} ); 150 $self->{DISPATCH} =~ s/^\s*//; 151 $self->{DISPATCH} =~ s/\s*$//; 152 delete $self->{DISPATCH} unless $self->{DISPATCH}; 153 } 154 155# 156# Get the full list of explicit dependencies: 157# 158 my $dependency_string = 159 $makefile->expand_text($self->{DEPENDENCY_STRING}, $self->{RULE_SOURCE}); 160 # Get the list of files. 161 162 for( split_on_whitespace $dependency_string ) { 163 push @explicit_dependencies, /[\[\*\?]/ ? # Is it a wildcard? 164 Mpp::Glob::zglob_fileinfo( unquote(), $build_cwd ) : 165 file_info( unquote(), $build_cwd ); 166 } 167 168# 169# Get the explicit environmental dependencies: 170# 171 if(exists $self->{ENV_DEPENDENCY_STRING}) { 172 my $env_dependency_string = $makefile->expand_text( 173 $self->{ENV_DEPENDENCY_STRING}, $self->{RULE_SOURCE} 174 ); 175 $self->add_env_dependency( unquote ) 176 for split_on_whitespace $env_dependency_string; 177 } 178 179 push @explicit_dependencies, @extra_dependencies; 180 # Extra dependencies go at the end of the 181 # list. 182 183 foreach (@explicit_dependencies) { 184 $self->add_dependency($_); # Make sure we know about each dependency. 185 } 186 187# 188# Now expand the command string. This must be done last because it 189# can depend on both the explicit targets and the explicit dependencies. 190# 191 my $perl = 0; # split-pattern always 2nd in list, even if 1st is empty 192 my $command_string = join '', map { 193 if (!($perl = !$perl)) # so this one is perl, and next won't be :-) 194 { 195 $_; 196 } 197 elsif (@explicit_targets && $explicit_targets[0]{PRIVATE_VARS}) 198 { 199 local $Mpp::Makefile::private = $explicit_targets[0]; 200 # Temporarily set up target-specific variables, 201 # if there actually are any. 202 $makefile->expand_text($_, $self->{RULE_SOURCE}); 203 # Get the text of the command. 204 } # Turn off the target-specific variables. 205 else { 206 $makefile->expand_text($_, $self->{RULE_SOURCE}); 207 # Get the text of the command. 208 } 209 } split /^(${action_prefix}perl\s*\{(?s:\{.*?\}\})?.*\n?)/m, $self->{COMMAND_STRING}; 210 211 $command_string =~ s/^\s+//; # Strip out leading and trailing whitespace 212 $command_string =~ s/\s+$//; # so we don't trigger unnecessary rebuilds 213 # quite as often. 214 215 $self->{COMMAND_STRING} = $command_string; # Remember expansion for later. 216 217 # In $dont_scan mode (--final-rule-only), don't try to build any 218 # dependencies. We assume that the user knows they're up to date. The 219 # build info won't show then either, so it will look out-of-date next time 220 # around. 221 if($dont_scan) { 222 return (\@explicit_targets, [], $command_string, \%env_dependencies); 223 } 224 225 # Try to get the scanner results from the build info, and failing that 226 # scan from scratch. (Well, not completely from scratch, because the cached 227 # lists of include directives can still be used if they're up-to-date.) 228 if( my $msg = $self->load_scaninfo( $oinfo, $command_string, \@explicit_dependencies )) { 229 Mpp::log LEX_RULE => $oinfo, $msg 230 if $Mpp::log_level; 231 unless( eval { $self->lexer->lex_rule( $command_string, $self ) } ) { 232 die $@ if $@ && $@ ne "SCAN_FAILED\n"; 233 $self->{SCAN_FAILED} ||= 1; 234 } # Look for any additional dependencies (or 235 # targets) that we didn't know about. 236 } else { 237 Mpp::log SCAN_CACHED => $oinfo 238 if $Mpp::log_level; 239 } 240 241 for my $dep ( @explicit_dependencies, values %all_dependencies ) { 242 Mpp::File::mark_build_info_for_update $dep 243 if $dep->{BUILD_INFO} && delete $dep->{BUILD_INFO}{RESCAN}; 244 # From mppr -- we're done with this now. 245 } 246 247# 248# For some reason, the #@!$@#% Linux kernel makefiles have a file that 249# depends on itself. This looks like a simple mistake, but I want this 250# to work properly on the Linux kernel, so we explicitly remove dependencies 251# that are equal to the target. 252# 253 foreach (values %all_targets) { 254 if( $all_dependencies{int()} ) { 255 delete $all_dependencies{int()}; # Remove it from the dependency list. 256 my $warn_flag = 0; 257 for (my $idx = 0; $idx < @explicit_dependencies; ++$idx) { 258 if ($explicit_dependencies[$idx] == $_) { # Was it an explicit dependency? 259 splice(@explicit_dependencies, $idx, 1); # Remove it. 260 $warn_flag++ or 261 warn '`' . absolute_filename( $_ ) . "' depends on itself; circular dependency removed\n"; 262 } 263 } 264 265 } 266 } 267 268# 269# Make a list of the targets and the dependencies, first listing the explicitly 270# specified ones, and then listing the implicit ones later. It's confusing 271# (and breaks some makefiles) if dependencies are built in a different order 272# than they are specified. 273# 274 delete @all_targets{int()} for @explicit_targets; 275 delete $all_dependencies{int()} for @explicit_dependencies; 276 277 ([ @explicit_targets, values %all_targets ], 278 [ @explicit_dependencies, values %all_dependencies ], 279 $command_string, \%env_dependencies); 280} 281 282=head2 get_tagname 283 284 $real_tag = $rule->get_tagname($tag) 285 286Returns a unique tag name for $tag. 287See L</"load_scaninfo">. 288 289Since a rule can have multiple commands with the same include tag, the 290requested tag might not match the actual tag that gets returned. 291 292=cut 293 294sub get_tagname { 295 my ($self, $tag) = @_; 296 return undef unless defined $tag; 297 die if $tag eq ''; 298 my $real_tag = $tag; 299 my $suffix = 0; 300 while(exists $self->{META_DEPS}{$real_tag}) { 301 $real_tag = $tag . ++$suffix; 302 } 303 $self->{META_DEPS}{$real_tag}={}; 304 $real_tag; 305} 306 307=head2 add_include_dir 308 309 $rule->add_include_dir($real_tag, $dirname); 310 311Add an include directory for scanner caching. 312See L</"load_scaninfo">. 313 314$dirname is a name relative to build_cwd. 315 316=cut 317 318sub add_include_dir { 319 my ($self, $tag, $dir, $front) = @_; 320 die "unregistered tag $tag" unless $self->{META_DEPS}{$tag}; 321 die unless $tag; 322 $self->{INCLUDE_PATHS}{$tag} ||= []; 323 if($front) { 324 unshift(@{$self->{INCLUDE_PATHS}{$tag}}, $dir); 325 } 326 else { 327 push(@{$self->{INCLUDE_PATHS}{$tag}}, $dir); 328 } 329 $self->{INCLUDE_PATHS_REL}{$tag} = 1 unless defined $dir; 330} 331 332=head2 add_include_suffix 333 334 $rule->add_include_suffix($real_tag, $suffix); 335 336Add an include suffix list for scanner caching. 337See L</"load_scaninfo">. 338 339=cut 340 341sub add_include_suffix_list { 342 my( $self, $tag, $sfxs ) = @_; 343 die "unregistered tag $tag" unless $self->{META_DEPS}{$tag}; 344 $self->{INCLUDE_SFXS}{$tag} = $sfxs; 345} 346 347=head2 add_meta_dependency 348 349 $rule->add_meta_dependency($real_tag, $src, $name, $finfo); 350 351Add an include file for scanner caching. 352Return value is TRUE iff the meta dependency failed to build. 353See L</"load_scaninfo">. 354 355$name is the requested name relative to the directory named by $src 356(relative to the build directory). 357If $real_tag is undef, then it is treated as a path including only ".". 358$src is ignored if $name is an absolute path or none of the directories in 359the include path are undefined. 360If $src is undef, then the build directory is used. 361 362The return value is zero on success (including if the file could not be found), 363nonzero on build failure. 364 365=cut 366 367sub fix_file_ { 368 my $name = $_[0]; 369 die unless defined($name) && $name ne '' && $name !~ m@^/+$@; 370 $name =~ s@/+$@@; 371 $name; 372} 373sub add_any_dependency_ { 374 my ($self, $key, $tag, $src, $name, $finfo) = @_; 375 $tag='' unless defined $tag; 376 # We won't care where the file was included from if it's not using a tag, 377 # if the tag it's using doesn't care about the source directory, or if 378 # the file was included with an absolute path. 379 $src='' unless defined($src) && $tag ne '' && 380 $self->{INCLUDE_PATHS_REL}{$tag} && $name !~ m@^/@; 381 $src=~s@/*$@/@ unless $src eq ''; 382 die "unregistered tag $tag" unless $tag eq '' || $self->{META_DEPS}{$tag}; 383 $self->{$key}{$tag}{$src}{fix_file_($name)} = 1; 384 if($finfo) { 385 $self->add_dependency($finfo); 386 return wait_for Mpp::build $finfo if $key eq 'META_DEPS'; 387 } 388 0; # success 389} 390sub add_any_dependency_if_exists_ { 391 my ($self, $key, $tag, $src, $name, $finfo, $tinfo) = @_; 392 393 # TBD: We should return undef if work would have to be done in order 394 # to build or re-build $finfo. This would require the equivalent of the 395 # GNU make -q option, which we don't have (yet). 396 397 # We need to recover from errors while figuring out where to build the 398 # dependency, because we might need to build Makeppfile's while searching, 399 # and we might fail to build one that we'll wind up not actually needing. 400 local $Mpp::keep_going = 1; 401 if($finfo && !Mpp::File::exists_or_can_be_built_or_remove $finfo ) { 402 return undef; 403 } 404 Mpp::log CACHED_DEP => $finfo, $tinfo 405 if $Mpp::log_level && $finfo; 406 !&add_any_dependency_; 407} 408sub add_meta_dependency { 409 splice @_, 1, 0, 'META_DEPS'; 410 &add_any_dependency_; 411} 412 413=head2 add_implicit_dependency 414 415 $rule->add_implicit_dependency($real_tag, $src, $name, $finfo); 416 417Add a dependency file for scanner caching. 418See L</"load_scaninfo">. 419 420This works just like add_meta_dependency, except that the rule doesn't 421have to be re-scanned by virtue of an implicit dependency being out of date. 422 423=cut 424 425sub add_implicit_dependency { 426 splice @_, 1, 0, 'IMPLICIT_DEPS'; 427 &add_any_dependency_; 428} 429 430=head2 add_implicit_target 431 432 $rule->add_implicit_target($name); 433 434Add a target file for scanner caching. 435See L</"load_scaninfo">. 436 437=cut 438 439sub add_implicit_target { 440 my ($self, $name) = @_; 441 $self->{IMPLICIT_TARGETS}{fix_file_($name)} = 1; 442 my $finfo = file_info($name, $self->build_cwd); 443 $self->add_target($finfo); 444 $finfo; 445} 446 447=head2 add_implicit_env_dependency 448 449 $rule->add_implicit_env_dependency($name); 450 451Add an environmental dependency for scanner caching. 452See L</"load_scaninfo">. 453 454=cut 455 456sub add_implicit_env_dependency { 457 my ($self, $name) = @_; 458 $self->{IMPLICIT_ENV_DEPS}{$name} = 1; 459 $self->add_env_dependency($name); 460} 461 462=head2 set_signature_class 463 464 $rule->set_signature_class($name, $implicit, $method) 465 466Look up and cache the signature method. If $implicit don't replace a previously 467set one (when reading meta data). If $method is given, skip look up. 468 469=cut 470 471sub set_signature_class { 472 my( $self, $name, $implicit, $method, $override ) = @_; 473 unless( $self->{SIG_METHOD_NAME} && $self->{SIG_METHOD_OVERRIDE} && $implicit ) { 474 $method = Mpp::Signature::get $name, $self->{RULE_SOURCE} 475 or die "$self->{RULE_SOURCE}: invalid signature class $name\n"; 476 $self->{SIG_METHOD_NAME} = $name; 477 $self->{SIG_METHOD_IMPLICIT} = 1 if $implicit; 478 $self->{SIGNATURE_METHOD} = $method; 479 $self->{SIG_METHOD_OVERRIDE} = 1 if $override; # remember this against later implicit value 480 } 481 $method; 482} 483 484=head2 mark_scaninfo_uncacheable 485 486 $rule->mark_scaninfo_uncacheable 487 488Prohibit the rule from caching its scanner information. 489This is useful when the information gathered by one of the scanners that 490gets involved doesn't fit into the caching scheme, and that could cause 491build inaccuracies. 492 493=cut 494 495sub mark_scaninfo_uncacheable { 496 undef $_[0]{xSCANINFO_UNCACHEABLE}; 497} 498 499=head2 cache_scaninfo 500 501 $rule->cache_scaninfo(\@targets); 502 503Set build_info_string's for scanner caching for each of @targets. 504See L</"load_scaninfo">. 505 506Once transferred to the build_info cache, the information is deleted from 507$rule, in order to save memory. 508 509=cut 510 511sub clear_scaninfo_ { 512 delete @{$_[0]}{qw(SIG_METHOD_NAME SIG_METHOD_IMPLICIT INCLUDE_PATHS INCLUDE_PATHS_REL INCLUDE_SFXS 513 META_DEPS IMPLICIT_DEPS IMPLICIT_TARGETS IMPLICIT_ENV_DEPS)}; 514} 515sub cache_scaninfo { 516 my ($self, $targets) = @_; 517 518 die if exists $self->{SCAN_FAILED}; 519 unless($Mpp::nocache_scaninfo || exists $self->{xSCANINFO_UNCACHEABLE}) { 520 # Wipe out unnecessary info from IMPLICIT_DEPS: 521 while(my ($tag, $th) = each %{$self->{IMPLICIT_DEPS}}) { 522 $tag = $self->{META_DEPS}{$tag}; 523 while(my ($dir, $dh) = each %$th) { 524 $dir = $tag->{$dir}; 525 for my $name (keys %$dh) { 526 delete $dh->{$name} if exists $dir->{$name}; 527 } 528 } 529 } 530 531 # NOTE: Because the build info has to be valid at the next makepp run, 532 # it is important that the directory/file paths do *not* have symbolic 533 # links resolved, because otherwise there will be no hope of detecting 534 # that we need to rebuild because a symbolic link was retargeted. The 535 # exception to this rule is that the paths of directories containing the 536 # file from which another file was included can be resolved, because the 537 # dependency on the file that has the include directive will force a 538 # rebuild if its path contains a retargeted symbolic link (and we are 539 # able to detect that, which isn't the case yet). 540 my @implicits = 'IMPLICIT_TARGETS'; 541 push @implicits, 'IMPLICIT_ENV_DEPS' if %{$self->{IMPLICIT_ENV_DEPS} || {}}; 542 for my $tinfo (@$targets) { 543 save_build_info_( $self, $tinfo, 'SIG_METHOD_NAME' ); 544 save_build_info_( $self, $tinfo, 'SIG_METHOD_IMPLICIT' ); 545 save_build_info_tag_( $self, $tinfo, 'INCLUDE_PATHS', \&join1_ ); 546 save_build_info_tag_( $self, $tinfo, 'INCLUDE_SFXS', \&join1_ ); 547 save_build_info_tag_( $self, $tinfo, 'META_DEPS', \&join_dir_ ); 548 save_build_info_tag_( $self, $tinfo, 'IMPLICIT_DEPS', \&join_dir_ ); 549 save_build_info_( $self, $tinfo, $_, 550 sub { join1_([sort keys %{$_[0] || {}}]) } 551 ) for @implicits; 552 } 553 &Mpp::File::update_build_infos; 554 } 555 &clear_scaninfo_; 556} 557sub join1_ { 558 join "\01", map { defined($_) ? $_ : '' } @{$_[0]}; 559} 560sub join_dir_ { 561 my $hashref = $_[0] || {}; 562 my @result; 563 push @result, sort keys %{$hashref->{''}} 564 if $hashref->{''}; 565 for my $dir (sort keys %$hashref) { 566 if( $dir ne '' and my @keys = keys %{$hashref->{$dir}} ) { 567 push @result, $dir, sort @keys; 568 } 569 } 570 join "\01", @result; 571} 572sub save_build_info_ { 573 my ($self, $tinfo, $key, $sub) = @_; 574 my $value = $self->{$key} if exists $self->{$key}; 575 Mpp::File::set_build_info_string $tinfo,$key, $sub ? &$sub($value) : $value 576 if $value || $sub; 577} 578sub save_build_info_tag_ { 579 my( $self, $tinfo, $key, $sub ) = @_; 580 save_build_info_( $self, $tinfo, $key, 581 sub { 582 my $hashref = $_[0] || {}; 583 my @result; 584 for(sort keys %$hashref) { 585 my $item=&$sub($hashref->{$_}); 586 push @result, join1_ [$_, $item] if $item ne '' || $key eq 'INCLUDE_PATHS'; 587 # The latter can be empty on Win, if it includes only undef. 588 # This is not the same thing as an empty list, so store it. 589 } 590 join "\02", @result; 591 } 592 ); 593} 594 595=head2 load_scaninfo 596 597 $rule->load_scaninfo($tinfo, $command_string, $explicit_dependencies); 598 599Attempt to get all the scanned information about $rule from $tinfo's build 600info file. 601If this fails for whatever reason, including the scaninfo possibly being 602out of date, then the reason is returned, and $rule is left in its original 603state. 604This typically means that it must be re-scanned from scratch. 605Otherwise, 0 is returned. 606 607=cut 608 609sub load_scaninfo_single { 610 my( $self, $tinfo_version, $tinfo, $command_string, $explicit_dependencies ) = @_; 611 612 # Fetch the info, or give up 613 my( $cwd, $sig_method_name, $sig_method_implicit, $include_paths, $include_sfxs, $meta_deps, $implicit_deps, 614 $implicit_targets, $implicit_env_deps, $command, $rescan ) = 615 Mpp::File::build_info_string $tinfo_version, 616 qw(CWD SIG_METHOD_NAME SIG_METHOD_IMPLICIT INCLUDE_PATHS INCLUDE_SFXS META_DEPS IMPLICIT_DEPS 617 IMPLICIT_TARGETS IMPLICIT_ENV_DEPS COMMAND RESCAN); 618 return $rescan == 1 ? 'signed by makeppreplay' : 'built by makeppreplay' if $rescan; 619 620 return 'info not cached' unless defined $cwd && 621 defined $include_paths && 622 defined $meta_deps && defined $implicit_deps && 623 defined $implicit_targets; 624 625 # If the CWD changed, then we have to rescan in order to make sure that 626 # path names in the scaninfo are relative to the correct directory. 627 $cwd = file_info $cwd, $tinfo->{'..'}; 628 return 'the build directory changed' unless $cwd == $self->build_cwd; 629 630 # Early out for command changed. This is redundant, but it saves a lot of 631 # work if it fails, especially because loading the cached dependencies might 632 # entail building metadependencies. 633 return 'the build command changed' if $command_string ne $command; 634 635 my $name = $self->{SIG_METHOD_NAME} || ''; 636 if( $name ? $self->{SIG_METHOD_OVERRIDE} : !$sig_method_implicit ) { 637 # This will make files look like changed (even if they are not). 638 return 'signature method changed' if ($sig_method_name||'') ne $name; 639 } 640 my @sig_info; 641 if( $sig_method_name && $sig_method_implicit ) { 642 @sig_info = @$self{qw(SIG_METHOD_NAME SIG_METHOD_IMPLICIT SIGNATURE_METHOD)}; 643 local $self->{RULE_SOURCE} = $tinfo_version->{NAME}; 644 eval { set_signature_class $self, $sig_method_name, 1 }; 645 if( $@ ) { # Newer mpp may have stored unknown method 646 warn $@; 647 return 'build info signature'; 648 } 649 } 650 651 # Trump up a dummy scanner object to help us look for files. 652 my $scanner = Mpp::Scanner->new($self, '.'); 653 654 for( split /\02/, $include_paths ) { 655 my( $tag, @path ) = split /\01/, $_, -1; 656 Mpp::Scanner::add_include_dir $scanner, $tag, $_ eq '' ? undef : $_ 657 for @path; 658 } 659 for( split /\02/, $include_sfxs || '' ) { 660 my( $tag, @sfxs ) = split /\01/, $_, -1; 661 Mpp::Scanner::add_include_suffix_list $scanner, $tag, \@sfxs; 662 } 663 664 # Replay the implicit environmental dependencies. 665 my %saved_explicit_env_deps = %{$self->{ENV_DEPENDENCIES}}; 666 $self->add_implicit_env_dependency($_) 667 for split /\01/, $implicit_env_deps || ''; 668 669 # Save the existing list of dependencies. 670 my %saved_all_dependencies = %{$self->{ALL_DEPENDENCIES}}; 671 672 my %meta_dep_finfos; # Not used, but may be used in the future. 673 my $cant_build_deps; 674 { 675 # Update $self to reflect the cached dependencies: 676 my $key = 'META_DEPS'; 677 TAG: for( split( /\02/, $meta_deps ), undef, split( /\02/, $implicit_deps )) { 678 unless( defined ) { 679 $key = 'IMPLICIT_DEPS'; 680 next TAG; 681 } 682 my( $tag, @incs ) = split /\01/, $_, -1; 683 die unless defined $tag; 684 Mpp::Scanner::get_tagname $scanner, $tag if $tag ne ''; # make it a valid tag 685 686 # TBD: Ideally, we should remember which tags are marked should_find 687 # instead of assuming that those containing 'sys' & 'lib' aren't and all 688 # others are, but this will generate the correct warnings most of the 689 # time, and it's not the end of the world if it doesn't: 690 Mpp::Scanner::should_find $scanner, $tag if $tag !~ /sys|lib/; 691 692 my $dir = $cwd; 693 for my $item (@incs) { 694 if( $item =~ s@/$@@ ) { 695 $dir = file_info $item, $cwd; 696 } else { 697 my $finfo; 698 if($tag ne '') { 699 $finfo = Mpp::Scanner::find $scanner, undef, $tag, $item, $dir; 700 add_any_dependency_if_exists_( $self, $key, 701 $tag, relative_filename($dir,$cwd), $item, $finfo, $tinfo 702 ) or $cant_build_deps=1; 703 } else { 704 $finfo = file_info $item, $cwd; 705 add_any_dependency_if_exists_( $self, $key, 706 undef, undef, $item, $finfo, $tinfo 707 ) or $cant_build_deps=1; 708 } 709 last TAG if $cant_build_deps; 710 $meta_dep_finfos{$finfo} = 1 if $finfo && $key eq 'META_DEPS'; 711 } 712 } 713 } 714 } 715 716 # TBD: If you have a explicit dependency in the target list, then it gets 717 # removed after this method, but before the dependencies are stored in the 718 # build info, and therefore the dependency list will look out of date 719 # when it isn't. This only slows things down, only happens when the 720 # makefile is screwy, and we can fix it using $explicit_dependencies. 721 delete @{$self->{ALL_DEPENDENCIES}}{keys %{$self->{ALL_TARGETS}}}; 722 723 my $flush_scaninfo = $cant_build_deps; 724 unless($flush_scaninfo) { 725 my $rebuild_needed = $self->build_check_method->build_check( 726 $tinfo_version, 727 $self->sorted_dependencies([values %{$self->{ALL_DEPENDENCIES}}]), 728 $command_string, $cwd, $self->signature_method, 729 $self->{ENV_DEPENDENCIES} 730 ); 731 $flush_scaninfo ||= $rebuild_needed; 732 if($rebuild_needed && $rebuild_needed eq 'DEPENDENCIES') { 733 $flush_scaninfo = 0; 734 735 # The target is out of date only with respect to dependencies. If 736 # none of them are meta dependencies, then we don't need to re-scan. 737 my @outdated = $self->build_check_method->changed_dependencies( 738 $tinfo_version, $self->signature_method, $cwd, 739 values %{$self->{ALL_DEPENDENCIES}} 740 ); 741 742 for(@outdated) { 743 if($meta_dep_finfos{$_}) { 744 $flush_scaninfo = 1; 745 last; 746 } 747 } 748 } 749 } 750 751 if($flush_scaninfo) { 752 # Reverse the side effects of this method: 753 @$self{qw(SIG_METHOD_NAME SIG_METHOD_IMPLICIT SIGNATURE_METHOD)} = @sig_info 754 if @sig_info; 755 %{$self->{ALL_DEPENDENCIES}} = %saved_all_dependencies; 756 %{$self->{ENV_DEPENDENCIES}} = %saved_explicit_env_deps; 757 &clear_scaninfo_; 758 759 return $cant_build_deps ? "couldn't build dependencies" : 760 'meta dependencies are out of date'; 761 } 762 763 # Now we know that this is going to succeed, so go ahead and add the implicit 764 # targets without fear of having to backtrack. 765 for(split(/\01/, $implicit_targets)) { 766 $self->add_implicit_target($_); 767 } 768 769 0; 770} 771sub load_scaninfo { 772 return '--force_rescan option specified' if $Mpp::force_rescan; 773 774 my $self = shift; # Modify stack to pass on below 775 my ($tinfo) = @_; 776 777 Mpp::log SCAN_INFO => $tinfo 778 if $Mpp::log_level; 779 my $first_msg = $self->load_scaninfo_single( $tinfo, @_ ); 780 return 0 unless $first_msg; 781 if( exists $tinfo->{ALTERNATE_VERSIONS} ) { 782 for( @{$tinfo->{ALTERNATE_VERSIONS}} ) { 783 if( my $msg = $self->load_scaninfo_single( $_, @_ )) { 784 Mpp::log SCAN_INFO_NOT => $_, $msg 785 if $Mpp::log_level; 786 } else { 787 Mpp::log SCAN_INFO_FROM => $_ 788 if $Mpp::log_level; 789 return 0; 790 } 791 } 792 } 793 $first_msg; 794} 795 796sub sorted_dependencies { 797 my ($self, $all_dependencies) = @_; 798 my( %dependencies, %long_dependencies, %dups ); 799 my $build_cwd = $self->build_cwd; 800 for( @$all_dependencies ) { 801 my $name = $_->{NAME}; 802 if( $dups{$name} ) { 803 $long_dependencies{relative_filename( $_, $build_cwd )} = $_; 804 } elsif( $dependencies{$name} ) { 805 next if $_ == $dependencies{$name}; # Same dep explicitly given multiply 806 $dups{$name} = 1; 807 $long_dependencies{relative_filename( $dependencies{$name}, $build_cwd )} = $dependencies{$name}; 808 delete $dependencies{$name}; 809 $long_dependencies{relative_filename( $_, $build_cwd )} = $_; 810 } else { 811 $dependencies{$name} = $_; 812 } 813 } 814 [@dependencies{sort keys %dependencies}, @long_dependencies{sort keys %long_dependencies}]; 815 # Get a sorted list of dependencies. We need 816 # to have these in a predictable order so 817 # build info can be quickly compared. It's 818 # important to sort by the filename first 819 # rather than the directories, because if we 820 # use a repository, sometimes directories 821 # change but filenames don't (if absolute 822 # directory names are referenced in the build 823 # procedure), and we want the system not to 824 # force recompilation in this case. 825} 826 827=head2 $rule->execute($command_string, $all_targets, $all_dependencies) 828 829 $handle = $rule->execute($command_string, $all_targets, $all_dependencies); 830 831Executes the given command string, and returns a handle for the executing 832process (which may not have finished by the time we return). The command 833string may contain several shell commands separated by newlines. 834 835This is part of the Rule class so the default execute routine can be 836overridden. You can replace the execute routine with any complicated 837operation you want, provided it returns the appropriate kind of handles. 838 839=cut 840 841sub execute { 842 my ($self, $actions, $all_targets, $all_dependencies) = @_; # Name the arguments. 843 844 # Check for a symlink that doesn't need rebuilding, even though the linkee changed. 845 if( @$all_targets == 1 ) {{ # Extra block for last. 846 my( $cmd, $symlink ) = 847 exists $all_targets->[0]{TEMP_BUILD_INFO} ? @{$all_targets->[0]{TEMP_BUILD_INFO}}{qw(COMMAND SYMLINK)} : 848 last; 849 delete $all_targets->[0]{TEMP_BUILD_INFO}; # Save some memory 850 851 last unless $actions eq $cmd and # Rule or link no longer matches, this is the effective build check for symlinks. 852 $symlink eq (readlink( absolute_filename_nolink $all_targets->[0] ) || '') and 853 file_exists file_info $symlink, $all_targets->[0]{'..'}; 854 855 my $linkee = dereference $all_targets->[0]; 856 if( exists $linkee->{BUILD_INFO} ) { # Replicate content-based signatures -- build_target_done will mark it for update. 857 while( my( $key, $value ) = each %{$linkee->{BUILD_INFO}} ) { 858 $all_targets->[0]{BUILD_INFO}{$key} = $value 859 if $key ne 'DEP_SIGS' && Mpp::Signature::is_content_based $value; 860 } 861 } 862 $Mpp::n_files_changed--; # Undo count, since we're not really rebuilding it. 863 Mpp::log SYMLINK_KEEP => $all_targets->[0], $self->source 864 if $Mpp::log_level; 865 return; # Success, pretend we did rebuild it. 866 }} 867 868 869 Mpp::Recursive::setup_socket() if # Do we need to listen for recursive make? 870 defined $Mpp::Recursive::command && $actions =~ /\brecursive_makepp\b/; 871 872 my $build_cwd = $self->build_cwd; 873# 874# If we're not building in parallel, then we don't change the standard output 875# of the commands. In this case, if there's a directory change, we print 876# it out before executing the commands. 877# 878 &touched_filesystem; # In case there are side-effects 879 if( $Mpp::parallel_make ) { 880 881# 882# We're executing in parallel. We can't print the directory name initially, 883# and we have to redirect STDOUT, because we don't want to mix output from 884# different commands. 885# 886# At present, we direct the output to a file, and then print it when we're 887# done. 888# 889 890 my $tmpfile = "/tmp/makepp.$$" . substr rand, 1; 891 # Make the name of a temporary file. 892 # Don't use f_mktemp, which is more expensive. 893 # TODO when discontinuing support of 5.6: 894 # switch to: open my $fh, '+>', undef 895 # and dup $fh to STDOUT/ERR 896 my $proc_handle = new Mpp::Event::Process sub { 897# 898# Child process. Redirect our output to the temporary file and then start 899# the build. 900# 901 open STDOUT, '>', $tmpfile or 902 die "$Mpp::progname: can't redirect standard output to $tmpfile--$!\n"; 903 open STDERR, '>&STDOUT' or die "can't dup STDOUT\n"; 904 # Make stderr go to the same place. 905 open STDIN, '< /dev/null' or 906 die "$Mpp::progname: can't redirect standard input to /dev/null--$!\n"; 907 # Turns out we don't want to close STDIN, because then duping STDOUT 908 # causes fd 0 to be opened, rather than 2, so STDERR is actually file 909 # handle 0. This caused a weird bug where error messages were not 910 # displayed on parallel builds. 911 $self->execute_command($actions, $build_cwd, $all_targets, $all_dependencies); # Execute the action(s). 912 }; 913 914# 915# Gets executed in the parent process when the child has finished. 916# 917 my $end_handler = sub { 918 if( -s $tmpfile ) { # Is there anything? 919 print_build_cwd( $build_cwd ); # Display any directory change. 920 local $| = 1; # Must flush before copy. 921 File::Copy::copy( $tmpfile, \*STDOUT ); 922 } 923 unlink $tmpfile; # Get rid of it. 924 $_[0]; # Propagate the status. 925 }; 926 when_done $proc_handle, $end_handler, ERROR => $end_handler; 927 } else { # Not parallel. 928 print_build_cwd( $build_cwd ); # Display any directory change. 929 wait_for new Mpp::Event::Process sub { 930 $self->execute_command($actions, $build_cwd, $all_targets, $all_dependencies); # Execute the command. 931 }; # Wait for the process, because we don't want 932 # to get ahead and start scanning files we 933 # can't build yet. That could mix up output 934 # between make and the other process. 935 } 936} 937 938=head2 $rule->print_command 939 940Print out a command instead of executing it. This is used only when we 941are running with -n (dry run mode). 942 943=cut 944 945sub print_command { 946 my $cmd = $_[1]; # Access the command string. 947 $cmd =~ s/^$action_prefix//gmo; # Don't print prefix. 948 print "$cmd\n"; 949 undef; # No build handle. 950} 951 952=head2 $rule->append($rule) 953 954Add the targets and commands from another rule to this rule. This is used 955only when grokking double colon rules like this: 956 957 clean:: 958 $(RM) *.o 959 960 # Later in the makefile: 961 clean:: 962 $(RM) y.tab.c y.tab.h 963 964=cut 965 966sub append { 967 my ($self, $other) = @_; # Name the arguments. 968 969 $self->{DEPENDENCY_STRING} .= " $other->{DEPENDENCY_STRING}"; 970 $self->{COMMAND_STRING} .= $other->{COMMAND_STRING}; 971 $self->{RULE_SOURCE} .= ",$other->{RULE_SOURCE}"; 972 if(exists $other->{ENV_DEPENDENCY_STRING}) { 973 if(exists $self->{ENV_DEPENDENCY_STRING}) { 974 $self->{ENV_DEPENDENCY_STRING} .= " $other->{ENV_DEPENDENCY_STRING}"; 975 } 976 else { 977 $self->{ENV_DEPENDENCY_STRING} = $other->{ENV_DEPENDENCY_STRING}; 978 } 979 } 980} 981 982# Returns the action (2nd arg) split on newline, with prefixes (the stuff that 983# doesn't get executed by the sub-shell) split off. The returned list repeatedly 984# has the following 4 elements: (flags, perl, &, action, ..., (undef) x 4) 985sub split_actions { 986 pos( $_[1] ) = 0; 987 $_[1] =~ /\G\s*/gc; 988 $_[1] =~ /\G($action_prefix)(?:(?:make)?perl\s*(\{(?s:\{.*?\}\})?.*)|(&)?(.*))\s*/cgmo; 989 # 1 1 2 2 3 3 4 4 990} 991 992=head2 $rule->setup_environment() 993 994Sets up the environment for the rule's makefile. 995 996=cut 997 998sub setup_environment { 999 my $makefile = $_[0]{MAKEFILE}; 1000 shift; 1001 $makefile->setup_environment( @_ ); 1002} 1003 1004use POSIX (); 1005sub exec_or_die { 1006 my( $action, $silent ) = @_; 1007 my $result = 254 << 8; 1008 if( $silent || !$Mpp::profile ) { 1009 { exec format_exec_args $action } 1010 # Then execute it and don't return. 1011 # This discards the extra make process (and 1012 # probably saves lots of memory). 1013 print STDERR "exec $action failed--$!\n"; # Should never get here, braces eliminate warning. 1014 } else { 1015 $result = system format_exec_args $action; 1016 print STDERR "system $action failed--$!\n" 1017 if $result == -1; 1018 Mpp::print_profile_end( $action ) if $Mpp::profile; 1019 } 1020 close $_ for @Mpp::close_fhs; 1021 Mpp::suicide Mpp::signame $result & 0x7F 1022 if $result & 0x7F; 1023 POSIX::_exit $result ? ($result>>8 || 1) : 0; 1024} 1025 1026# 1027# The following internal subroutine executes each action. Arguments: 1028# a) The directory that we're supposed to execute this from. 1029# b) The lines of actions to execute. 1030# c) A hash of variables that must be exported to the environment, and their 1031# values. 1032# d) The environment to use (not including the exports). 1033# 1034# At this point, STDOUT and STDERR should be set up to wherever the command 1035# is supposed to output to. 1036# 1037my $true = (-x '/bin/true') ? '/bin/true' : '/usr/bin/true'; 1038sub execute_command { 1039 my( $self, undef, $build_cwd, $all_targets, $all_dependencies ) = @_; # Name the arguments. [1] goes to split_actions. 1040 1041 # On Windows, native actions don't fork, and so we have to continue to 1042 # defer signals so that we can reliably mark failed targets as such -- 1043 # even though this is dangerous because there is no way to stop infinite 1044 # loops! For system commands, we unblock 'INT' and 'QUIT' so that the 1045 # action will be stoppable, and we'll still be able to mark them because 1046 # while the system is running they are blocked by the makepp process 1047 # automatically. 1048 &Mpp::reset_signal_handlers if !Mpp::is_windows; # unless constant gives a warning in some variants of 5.6 1049 chdir $build_cwd; # Move to the correct directory. 1050 $self->{MAKEFILE}->setup_environment; 1051 1052 $Mpp::Recursive::socket_name and # Pass info about recursive make. 1053 $ENV{MAKEPP_SOCKET} = $Mpp::Recursive::socket_name; 1054 1055 $Mpp::preexecute_rule_hook and &$Mpp::preexecute_rule_hook($self); 1056 1057# 1058# Now execute each action. We exec the action if it is the last in the series 1059# and we don't need to ignore the return code; otherwise, we call system() 1060# instead. This means we start an extra process in those cases, but it's too 1061# tricky to avoid it in such comparatively rare situations. 1062# 1063 my( $flags, $perl, $command, $action, @actions ) = &split_actions; # Get the commands to execute with flags. 1064 $#actions -= 4; # Eliminate bogus undefs. 1065 1066 local $unsafe; 1067 my $maybe_open; 1068 while( 1 ) { 1069 next unless 1070 defined $action && $action =~ /\S/ || defined $perl && $perl =~ /\S/; 1071 if( $unsafe ) { 1072 $build_cwd = absolute_filename $build_cwd if ref $build_cwd; 1073 CORE::chdir $build_cwd; 1074 undef $unsafe; 1075 $maybe_open = 1; 1076 } 1077 1078# 1079# Grok the @ and - in front of the command. 1080# 1081 my $silent_flag = $Mpp::quiet_flag || $flags =~ /\@|noecho/; 1082 my $error_abort = $flags !~ /-|ignore_error/; 1083 1084 if( defined $perl or defined $command ) { 1085 $File::chdir = 1; # External command might change our dir. 1086 Mpp::print_profile( defined $perl ? "perl $perl" : "&$action" ) unless $silent_flag; 1087 # This prints out makeperl as perl. That is correct, 1088 # because it has already been expanded to plain perl code. 1089 local $Mpp::Subs::rule = $self; 1090 local $self->{EXPLICIT_TARGETS} = $all_targets; 1091 local $self->{EXPLICIT_DEPENDENCIES} = $all_dependencies; 1092 if( defined $perl ) { 1093 $unsafe = 1; 1094 # NOTE: $self->{RULE_SOURCE} is the line at which the rule started, 1095 # which is not the same as the line at which the perl action started. 1096 # To get the real line number, you have to add the number of lines 1097 # into the rule in which the perl actions appears. Although this is 1098 # misleading, it's better than nothing because it usually gets you 1099 # very close. It's hard to fix. 1100 eval { Mpp::Subs::eval_or_die $perl, $self->{MAKEFILE}, $self->{RULE_SOURCE} }; 1101 if( $@ ) { 1102 Mpp::print_error( 'perl action: '.$@ ); 1103 return 1 if $error_abort; 1104 } 1105 } else { 1106 my $comment = index_ignoring_quotes $action, '#'; 1107 my( $cmd, @args ) = 1108 unquote_split_on_whitespace $comment > -1 ? substr $action, 0, $comment : $action; 1109 eval { 1110 if( defined &{$self->{MAKEFILE}{PACKAGE} . "::c_$cmd"} ) { # Function from makefile? 1111 $unsafe = 1; 1112 local $0 = $cmd; 1113 &{$self->{MAKEFILE}{PACKAGE} . "::c_$0"}( @args ); 1114 } elsif( defined &{"Mpp::Cmds::c_$cmd"} ) { # Builtin Function? 1115 local $0 = $cmd; 1116 &{"Mpp::Cmds::c_$0"}( @args ); 1117 } else { 1118 $unsafe = 1; 1119 Mpp::Subs::run( $cmd, @args ); 1120 } 1121 }; 1122 if( $@ ) { 1123 for( my $error = $@ ) { # Make a modifiable copy. 1124 s/\(eval \d+\)(?:\[.*?\])? line \d+/\`$self->{RULE_SOURCE}\'/g; 1125 s/^$cmd: //; 1126 print STDERR "makepp: &$cmd: $_"; 1127 } 1128 &Mpp::flush_log; 1129 return 1 if $error_abort; 1130 } 1131 } 1132 Mpp::print_profile_end( defined $perl ? "perl $perl" : "&$action" ) if $Mpp::profile && !$silent_flag; 1133 next; # Process the next action. 1134 } 1135 1136 if($self->{DISPATCH}) { 1137 $action =~ s/\'/\'\\\'\'/g; 1138 $action = $self->{DISPATCH} . " sh -c '$action'"; 1139 } 1140 Mpp::print_profile( $action ) unless $silent_flag; 1141 if( Mpp::is_windows > 0 ) { # Can't fork / exec on native Windows. 1142 { 1143 # NOTE: In Cygwin, TERM and HUP are automatically unblocked in the 1144 # system process, but INT and QUIT are not -- they are just ignored 1145 # in the calling process. There is a race here where an INT or 1146 # QUIT can sneak in, but it's quite unlikely. 1147 local @SIG{'INT', 'QUIT'} = ( 'DEFAULT' ) x 2; 1148 system format_exec_args $action; 1149 } 1150 $error_abort and $? and return ($?/256) || 1; # Quit if an error. 1151 Mpp::print_profile_end( $action ) if $Mpp::profile && !$silent_flag; 1152 next; # Process the next action. 1153 } 1154 1155 undef $unsafe; # Because exec() closes and fork() flushes 1156 # filehandles, and external cmd can't chdir. 1157 my $nop = $Mpp::Text::N[0]; # Reuse any old function that does nothing, to 1158 # save allocating a new sub. 1159 if ($error_abort && !@actions) { # Is this the last action? 1160 $SIG{__WARN__} = $nop; # Suppress annoying warning message here if the 1161 # exec fails. 1162 exec_or_die $action, $silent_flag; 1163 # Then execute it and don't return. 1164 # This discards the extra make process (and 1165 # probably saves lots of memory). 1166 } else { # We have more to do after the process 1167 # finishes, so we need to keep running 1168 # after the process starts. 1169# 1170# We used to do with with system(), but system has two serious drawbacks: 1171# 1) A nasty message is printed if the first word is not a valid shell 1172# command, and the perl process aborts, apparently without setting the 1173# correct exit code. 1174# 2) On Linux, system() blocks signals, which means that makepp wouldn't 1175# respond to ^C or other things like that. (See the man page.) 1176# 1177 &Mpp::flush_log if Mpp::is_perl_5_6; 1178 my $pid = fork; 1179 unless ($pid) { # Executed in child process: 1180 $SIG{__WARN__} = $nop; # Suppress annoying warning message here if 1181 # the exec fails. 1182 exec_or_die $action, $silent_flag; 1183 } 1184 1185 $pid == -1 and die "fork failed--$!\n"; 1186 wait; # Wait for it to finish. 1187 $error_abort && $? and exit( int( $? / 256 ) || 255 ); 1188 # Quit on an error. $status/256 is the exit 1189 # status of the process, but if the system() 1190 # call failed for some reason, that may be 0. 1191 } 1192 } continue { 1193 last unless @actions; 1194 ($flags, $perl, $command, $action) = splice @actions, 0, 4; 1195 } 1196 1197 Mpp::is_windows > 0 and return 0; # If we didn't fork, we'll always get here. 1198 1199 # Close filehandles that may have been left opened by a Perl command. 1200 if( $maybe_open || $unsafe ) { 1201 if( Mpp::is_perl_5_6 ) { 1202 close $_ for @Mpp::close_fhs, Mpp::MAKEPP ? values %{$self->{MAKEFILE}{PACKAGE}.'::'} : (); 1203 } 1204 exec $true; # Close open filehandles w/o doing garbage collection 1205 warn "failed to exec `$true'--$!"; 1206 # If that didn't work, then do a close on every symbol in the makefile's 1207 # package. This is usually good enough, but not if the makefile used an 1208 # IO handle in another package. 1209 close $_ for values %{$self->{MAKEFILE}{PACKAGE}.'::'}; 1210 } 1211 1212 close $_ for @Mpp::close_fhs; 1213 POSIX::_exit 0; # If we get here, it means that the last 1214 # command in the series was executed with 1215 # ignore_error. 1216} 1217 1218# 1219# This subroutine prints out a message if we've changed directories. This 1220# message is important for programs like emacs that look through the compiler 1221# output--it helps them find the directory that the file containing the errors 1222# is located in. 1223# 1224our $last_build_cwd = 0; # Comparable to a ref. 1225sub print_build_cwd { 1226 my $build_cwd = $_[0]; 1227 if( $last_build_cwd != $build_cwd ) { # Different from previous or no previous? 1228 if( $Mpp::print_directory ) { 1229 print "$Mpp::progname: Leaving directory `$last_build_cwd->{FULLNAME}'\n" 1230 if $last_build_cwd; # Don't let the directory stack fill up. 1231 print "$Mpp::progname: Entering directory `" . &absolute_filename . "'\n"; 1232 } 1233 $last_build_cwd = $build_cwd; 1234 } 1235} 1236 1237=head2 build_check_method 1238 1239 $rule->build_check_method; 1240 1241Gets the build check method for this particular rule. 1242 1243=cut 1244 1245sub build_check_method { $_[0]{BUILD_CHECK_METHOD} || $Mpp::BuildCheck::default } 1246 1247=head2 set_build_check_method 1248 1249 $rule->set_build_check_method(method); 1250 1251Sets the build check method to be the given method. 1252 1253=cut 1254 1255sub set_build_check_method { 1256 $_[0]{BUILD_CHECK_METHOD} = $_[1]; 1257} 1258 1259=head2 $rule->signature_method 1260 1261 my $sig_method = $rule->signature_method; 1262 1263Returns the signature method to use to calculate signatures for files. 1264 1265The default method is the file time + file size (see Mpp/Signature.pm for 1266details). 1267 1268=cut 1269 1270sub signature_method { $_[0]{SIGNATURE_METHOD} || $Mpp::Signature::signature } 1271 1272=head2 $rule->lexer 1273 1274 $rule->lexer() 1275 1276Returns the rule lexer object, creating it if it doesn't already exist. 1277 1278=cut 1279 1280sub lexer { 1281 $_[0]{LEXER_OBJ} ||= # Is the lexer already cached? 1282 do { 1283 my $lexer = $_[0]{LEXER}; 1284 $lexer ? # Was one explicitly set? 1285 &$lexer() : # Yes: generate it 1286 new Mpp::Lexer; # No: Use the default 1287 }; 1288} 1289 1290=head2 $rule->source 1291 1292 $source_string = $rule->source; 1293 1294Returns a description of where this rule was encountered, suitable for error 1295messages. 1296 1297=cut 1298 1299sub source { $_[0]{RULE_SOURCE} } 1300*name = \&source; # Alias for Mpp::log 1301 1302# 1303# This subroutine is called to add a possibly new dependency to the list of 1304# all dependencies for the current rule. 1305# 1306# Arguments: 1307# $rule->add_dependency($object_info); 1308# 1309# This should only be called from subroutines which are called by 1310# find_all_targets_dependencies. This basically means it should only be 1311# called by the command parsers. 1312# 1313sub add_dependency { 1314 $_[0]{ALL_DEPENDENCIES}{int $_[1]} ||= $_[1]; 1315 # Store it if we didn't already know about it. 1316} 1317 1318# This subroutine is called to add a possibly new environmental dependency, 1319# and to store the value as if it were rebuilt in the rule. 1320# 1321# $rule->add_env_dependency($var_name); 1322# 1323# $var_name can also be "$name in $var", in which case we split $var on /:/ 1324# and look for a file called $name in each directory, taking the first 1325# directory name we find as the "current value". 1326# 1327sub add_env_dependency { 1328 my ($rule, $var_name) = @_; 1329 return if $rule->{ENV_DEPENDENCIES}{$var_name}; 1330 if( my( $name, $val ) = $var_name =~ /^(.+) in (\S+)$/) { 1331 if( $val eq 'PATH' ) { 1332 my $found = Mpp::Subs::f_find_program( $name, $rule->{MAKEFILE}, $rule->{SOURCE}, 1 ); 1333print $found," XX\n"; 1334 return $rule->{ENV_DEPENDENCIES}{$var_name} = Mpp::Subs::f_dir_noslash( $found, $rule->{MAKEFILE}, $rule->{SOURCE} ) 1335 if $found; 1336 } else { 1337 for( split /:/, $rule->{MAKEFILE}->get_env( $val ) || '' ) { 1338 next unless $_; 1339 return $rule->{ENV_DEPENDENCIES}{$var_name} = $_ 1340 if Mpp::File::exists_or_can_be_built file_info $name, file_info $_, $rule->{MAKEFILE}{CWD}; 1341 } 1342 } 1343 return $rule->{ENV_DEPENDENCIES}{$var_name} = ''; 1344 } 1345 my $val = $rule->{MAKEFILE}->get_env($var_name); 1346 $val = 'defined' if defined($val) && $val eq ''; 1347 $val = '' unless defined($val); 1348 $rule->{ENV_DEPENDENCIES}{$var_name} = $val; 1349} 1350 1351sub expand_additional_deps { 1352 my ($self, $oinfo) = @_; 1353 if ($oinfo->{ADDITIONAL_DEPENDENCIES}) { # Any additional explicit dependencies? 1354 my @dep_infos; 1355 foreach (@{$oinfo->{ADDITIONAL_DEPENDENCIES}}) { 1356 my ($dep_str, $makefile, $makefile_line) = @$_; 1357 # Name the components of the stored info. 1358 for( split_on_whitespace $makefile->expand_text( $dep_str, $makefile_line ) ) { 1359 push @dep_infos, /[\[\*\?]/ ? # Is it a wildcard? 1360 Mpp::Glob::zglob_fileinfo( unquote(), $makefile->{CWD} ) : 1361 # Get which files this is referring to. 1362 file_info( unquote(), $makefile->{CWD} ); 1363 } 1364 } 1365 return @dep_infos; 1366 } 1367 (); 1368} 1369 1370# 1371# This subroutine is called to add a possibly new target to the list of targets 1372# for the current rule. 1373# 1374# Arguments: 1375# $rule->add_target($object_info); 1376# 1377# This should only be called from subroutines which are called by 1378# find_all_targets_dependencies. This basically means it should only be 1379# called by the command parsers. 1380# 1381sub add_target { 1382 my ($self, $oinfo) = @_; 1383 1384 return if $self->{ALL_TARGETS}{int $oinfo}; # Quit if we already knew about this. 1385 1386# 1387# This target may have dependencies which were explicitly listed in some other 1388# part of the makefile, as used to be common for specifying include files. 1389# For example, we might have a rule like: 1390# 1391# %.o: %.c 1392# compilation command 1393# 1394# xyz.o : abc.h def.h ghi.h 1395# 1396 my @dep_infos = $self->expand_additional_deps($oinfo); 1397 for( @dep_infos ) { $self->add_dependency($_) } 1398 # Add them to the dependency list. 1399 push @{$self->{EXTRA_DEPENDENCIES}}, @dep_infos; 1400 1401 $self->{ALL_TARGETS}{int $oinfo} = $oinfo; 1402} 1403 1404 1405 1406############################################################################### 1407# 1408# This is a replacement for the Rule when there is no actual rule specified 1409# in the file. Most of the subroutines in this package are dummies to 1410# replace the functionality in the Rule class. 1411# 1412package Mpp::DefaultRule; 1413 1414# 1415# Establish a default rule for a new file: 1416# 1417sub new { bless { TARGET => $_[1] }} 1418 1419sub build_cwd { $Mpp::File::CWD_INFO } 1420 1421*expand_additional_deps = \&Mpp::Rule::expand_additional_deps; 1422 1423sub find_all_targets_dependencies { 1424 my $target = $_[0]{TARGET}; # Get the target object info. 1425 ([$target], 1426 $target->{ADDITIONAL_DEPENDENCIES} ? [$_[0]->expand_additional_deps($target)] : [], 1427 # Are there dependencies for this file even 1428 # though there's no rule? 1429 # We have to expand the dependency list now. 1430 '', {}); 1431} 1432 1433# 1434# If we ever get to this subroutine, it means there's no way to build this 1435# file. 1436# 1437sub execute { 1438 Mpp::Event::when_done sub { 1439 my $target = $_[0]; 1440 if( $target->{TEMP_BUILD_INFO} ) { 1441 Mpp::print_error( 'Attempting to retain stale ', $target ); 1442 -1; # Return nonzero to indicate error. 1443 } elsif( $target->{ADDITIONAL_DEPENDENCIES} ) { 1444 # If it has additional dependencies, then 1445 # there was a rule, or at least we have to 1446 # pretend there was. 1447 #file_exists( $target ) and return 0; 1448 # If the file doesn't exist yet, we may 1449 # have to link it from a repository. 1450 # Mpp::build handles linking targets in from a repository after the rule 1451 # runs, so we don't have to do it here even if there are 1452 # ALTERNATE_VERSIONS. 1453 0; # Return success even if the file doesn't exist. 1454 # This is to handle things like 1455 # FORCE: 1456 # which are really just dummy phony targets. 1457 } elsif( exists $target->{xPHONY} ) { 1458 0; 1459 } else { 1460 Mpp::print_error( 'No rule to make ', $target ); 1461 -1; # Return nonzero to indicate error. 1462 } 1463 }, [$_[0]{TARGET}]; 1464} 1465 1466# 1467# This gets called whenever we access a file for which there is no rule 1468# when we are running with -n. In this case we don't want to do anything. 1469# 1470sub print_command { 1471 my $target = $_[0]{TARGET}; 1472 if ($target->{ADDITIONAL_DEPENDENCIES}) { 1473 return 0; # Handle case like 1474 # all: xyz 1475 # where there is no rule for all. 1476 } 1477 Mpp::print_error( 'No rule to make ', $target ); 1478 1479 -1; # Return nonzero to indicate error. 1480} 1481 1482# 1483# The signature method that we use causes the file to be remade if it doesn't 1484# exist, and leaves it alone if it does. 1485# 1486sub signature_method { $Mpp::Signature::signature } 1487 1488sub build_check_method { $Mpp::DefaultRule::BuildCheck::build_check_method_object } 1489 1490sub build_cache { undef } # Build cache disabled for objects with 1491 # no actions. Reused below where we also need a sub 1492 # that returns undef. 1493 1494sub source { 'default rule' } 1495*name = \&source; # Alias for Mpp::log 1496 1497*sorted_dependencies = \&Mpp::Rule::sorted_dependencies; 1498 1499# If there is no action, then there is nothing to scan, so presumably nothing 1500# was affected by scanning. Reuse any nop function. 1501*cache_scaninfo = $Mpp::Text::N[0]; 1502 1503 1504 1505package Mpp::DefaultRule::BuildCheck; 1506 1507our @ISA = 'Mpp::BuildCheck'; 1508 1509use Mpp::File; 1510 1511# 1512# This package implements signature checking for files for which there is no 1513# rule. We "rebuild" only if the file does not exist. This will cause the 1514# file to be linked in from a repository if we can find it somewhere. 1515# 1516 1517$Mpp::DefaultRule::BuildCheck::build_check_method_object = bless \@ISA; 1518 # Make a singleton object that defines our 1519 # special build_check method. 1520 1521sub build_check { 1522 my $tinfo = $_[1]; # Ignore self 1523 1524 return 'stale symbolic link' if exists $tinfo->{TEMP_BUILD_INFO}; 1525 1526 if( Mpp::File::is_stale $tinfo ) { 1527 if( defined $Mpp::Recursive::traditional || defined $Mpp::Recursive::hybrid ) { 1528 warn 'info: I hope stale `' . absolute_filename( $tinfo ) . "' was generated by a parallel \$(MAKE).\n"; 1529 } else { 1530 warn '`' . absolute_filename( $tinfo ) . "' is a stale file generated by makepp,\n" . 1531 " but there is no rule to build it any more. Makepp assumes that it\n" . 1532 " is now a source file. If this is correct, then you should add it\n" . 1533 " to your source control system (if any), and touch the file to make\n" . 1534 " this warning go away. Otherwise, you should remove the file,\n" . 1535 " because the targets that depend on it will not be reproducible.\n" . 1536 " makepp --rm-stale would automatically do this for you.\n"; 1537 } 1538 } 1539 1540 # It would be faster to check for existence first, but that doesn't work, 1541 # because then if the file were from a repository but the repository version 1542 # changed, then the file would originally exist, and then it gets deleted 1543 # by build_info_string(), which returns undef, making it look as though the 1544 # file is up-to-date, when in fact it doesn't even exist! 1545 defined( Mpp::File::build_info_string( $tinfo, 'FROM_REPOSITORY' )) || 1546 !file_exists( $tinfo ) || 1547 ( 1548 # If it used to be a generated file, but now we need to get it from a 1549 # repository, then it needs to be linked in, unless we're treating old 1550 # generated files as source. 1551 $Mpp::rm_stale_files && 1552 !$tinfo->{ADDITIONAL_DEPENDENCIES} && 1553 defined Mpp::File::build_info_string $tinfo, 'BUILD_SIGNATURE' 1554 ); 1555} 1556 1557# 1558# This subroutine is called when we're deciding whether to import the file 1559# from a repository. (It can be called when deciding whether to use a build 1560# cache, too, except that we've disabled build caches by not providing a 1561# build_cache_key function.) 1562# 1563*build_check_from_build_info = \&Mpp::DefaultRule::build_cache; 1564 # If a file exists in a repository, there's 1565 # no reason not to import it since there's 1566 # no build rule for this target. 1567 15681; 1569