1 # Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 package Automake
::Rule
;
23 use Automake
::RuleDef
;
24 use Automake
::ChannelDefs
;
25 use Automake
::Channels
;
26 use Automake
::Options
;
27 use Automake
::Condition qw
(TRUE FALSE
);
28 use Automake
::DisjConditions
;
30 use vars
'@ISA', '@EXPORT', '@EXPORT_OK';
31 @ISA = qw
/Automake::Item Exporter/;
32 @EXPORT = qw
(reset register_suffix_rule next_in_suffix_chain
33 suffixes rules
$KNOWN_EXTENSIONS_PATTERN
34 depend
%dependencies %actions register_action
36 reject_rule msg_rule msg_cond_rule err_rule err_cond_rule
37 rule rrule ruledef rruledef
);
41 Automake::Rule - support for rules definitions
46 use Automake::RuleDef;
51 This package provides support for Makefile rule definitions.
53 An C<Automake::Rule> is a rule name associated to possibly
54 many conditional definitions. These definitions are instances
55 of C<Automake::RuleDef>.
57 Therefore obtaining the value of a rule under a given
58 condition involves two lookups. One to look up the rule,
59 and one to look up the conditional definition:
61 my $rule = rule $name;
64 my $def = $rule->def ($cond);
67 return $def->location;
73 when it is known that the rule and the definition
74 being looked up exist, the above can be simplified to
76 return rule ($name)->def ($cond)->location; # do not write this.
80 return rrule ($name)->rdef ($cond)->location;
84 return rruledef ($name, $cond)->location;
86 The I<r> variants of the C<rule>, C<def>, and C<ruledef> methods add
87 an extra test to ensure that the lookup succeeded, and will diagnose
88 failures as internal errors (with a message which is much more
89 informative than Perl's warning about calling a method on a
92 =head2 Global variables
98 my $_SUFFIX_RULE_PATTERN =
99 '^(\.[a-zA-Z0-9_(){}$+@\-]+)(\.[a-zA-Z0-9_(){}$+@\-]+)' . "\$";
102 my @_known_extensions_list = ();
105 # See comments in the implementation of the 'next_in_suffix_chain()'
106 # variable for details.
109 # Same as $suffix_rules, but records only the default rules
110 # supplied by the languages Automake supports.
111 my %_suffix_rules_builtin;
113 =item C<%dependencies>
115 Holds the dependencies of targets which dependencies are factored.
116 Typically, C<.PHONY> will appear in plenty of F<*.am> files, but must
117 be output once. Arguably all pure dependencies could be subject to
118 this factoring, but it is not unpleasant to have paragraphs in
119 Makefile: keeping related stuff altogether.
123 use vars
'%dependencies';
127 Holds the factored actions. Tied to C<%dependencies>, i.e., filled
128 only when keys exists in C<%dependencies>.
134 =item C<$KNOWN_EXTENSIONS_PATTERN>
136 Pattern that matches all know input extensions (i.e. extensions used
137 by the languages supported by Automake). Using this pattern (instead
138 of '\..*$') to match extensions allows Automake to support dot-less
141 New extensions should be registered with C<accept_extensions>.
145 use vars qw
($KNOWN_EXTENSIONS_PATTERN);
146 $KNOWN_EXTENSIONS_PATTERN = "";
150 =head2 Error reporting functions
152 In these functions, C<$rule> can be either a rule name, or
153 an instance of C<Automake::Rule>.
157 =item C<err_rule ($rule, $message, [%options])>
159 Uncategorized errors about rules.
165 msg_rule
('error', @_);
168 =item C<err_cond_rule ($cond, $rule, $message, [%options])>
170 Uncategorized errors about conditional rules.
174 sub err_cond_rule
($$$;%)
176 msg_cond_rule
('error', @_);
179 =item C<msg_cond_rule ($channel, $cond, $rule, $message, [%options])>
181 Messages about conditional rules.
185 sub msg_cond_rule
($$$$;%)
187 my ($channel, $cond, $rule, $msg, %opts) = @_;
188 my $r = ref ($rule) ?
$rule : rrule
($rule);
189 msg
$channel, $r->rdef ($cond)->location, $msg, %opts;
192 =item C<msg_rule ($channel, $targetname, $message, [%options])>
194 Messages about rules.
200 my ($channel, $rule, $msg, %opts) = @_;
201 my $r = ref ($rule) ?
$rule : rrule
($rule);
202 # Don't know which condition is concerned. Pick any.
203 my $cond = $r->conditions->one_cond;
204 msg_cond_rule
($channel, $cond, $r, $msg, %opts);
208 =item C<$bool = reject_rule ($rule, $error_msg)>
210 Bail out with C<$error_msg> if a rule with name C<$rule> has been
213 Return true iff C<$rule> is defined.
219 my ($rule, $msg) = @_;
222 err_rule
$rule, $msg;
230 =head2 Administrative functions
234 =item C<accept_extensions (@exts)>
236 Update C<$KNOWN_EXTENSIONS_PATTERN> to recognize the extensions
237 listed in C<@exts>. Extensions should contain a dot if needed.
241 sub accept_extensions
(@
)
243 push @_known_extensions_list, @_;
244 $KNOWN_EXTENSIONS_PATTERN =
245 '(?:' . join ('|', map (quotemeta, @_known_extensions_list)) . ')';
250 Return the list of all L<Automake::Rule> instances. (I.e., all
251 rules defined so far.)
257 return values %_rule_dict;
261 =item C<register_action($target, $action)>
263 Append the C<$action> to C<$actions{$target}> taking care of special
268 sub register_action
($$)
270 my ($target, $action) = @_;
271 if ($actions{$target})
273 $actions{$target} .= "\n$action" if $action;
277 $actions{$target} = $action;
282 =item C<Automake::Rule::reset>
284 The I<forget all> function. Clears all known rules and resets some
293 %_suffix_rules = %_suffix_rules_builtin;
309 # Installing/uninstalling.
310 'install-data-am' => [],
311 'install-exec-am' => [],
312 'uninstall-am' => [],
315 'uninstall-man' => [],
318 'install-dvi-am' => [],
319 'install-html' => [],
320 'install-html-am' => [],
321 'install-info' => [],
322 'install-info-am' => [],
324 'install-pdf-am' => [],
326 'install-ps-am' => [],
328 'installcheck-am' => [],
332 'mostlyclean-am' => [],
333 'maintainer-clean-am' => [],
334 'distclean-am' => [],
337 'maintainer-clean' => [],
345 # Recursive install targets (so "make -n install" works for BSD Make).
351 =item C<next_in_suffix_chain ($ext1, $ext2)>
353 Return the target suffix for the next rule to use to reach C<$ext2>
354 from C<$ext1>, or C<undef> if no such rule exists.
358 sub next_in_suffix_chain
($$)
360 my ($ext1, $ext2) = @_;
361 return undef unless (exists $_suffix_rules{$ext1} and
362 exists $_suffix_rules{$ext1}{$ext2});
363 return $_suffix_rules{$ext1}{$ext2}[0];
366 =item C<register_suffix_rule ($where, $src, $dest)>
368 Register a suffix rule defined on C<$where> that transforms
369 files ending in C<$src> into files ending in C<$dest>.
373 sub register_suffix_rule
($$$)
375 my ($where, $src, $dest) = @_;
376 my $suffix_rules = $where->{'position'} ? \
%_suffix_rules
377 : \
%_suffix_rules_builtin;
379 verb
"Sources ending in $src become $dest";
380 push @_suffixes, $src, $dest;
382 # When transforming sources to objects, Automake uses the
383 # %suffix_rules to move from each source extension to
384 # '.$(OBJEXT)', not to '.o' or '.obj'. However some people
385 # define suffix rules for '.o' or '.obj', so internally we will
386 # consider these extensions equivalent to '.$(OBJEXT)'. We
387 # CANNOT rewrite the target (i.e., automagically replace '.o'
388 # and '.obj' by '.$(OBJEXT)' in the output), or warn the user
389 # that (s)he'd better use '.$(OBJEXT)', because Automake itself
390 # output suffix rules for '.o' or '.obj' ...
391 $dest = '.$(OBJEXT)' if ($dest eq '.o' || $dest eq '.obj');
393 # ----------------------------------------------------------------------
394 # The $suffix_rules variable maps the source extension for all suffix
395 # rules seen to a hash whose keys are the possible output extensions.
397 # Note that this is transitively closed by construction:
400 # exists $suffix_rules{$ext1}{$ext2}
401 # && exists $suffix_rules{$ext2}{$ext3}
405 # exists $suffix_rules{$ext1}{$ext3}
407 # So it's easy to check whether '.foo' can be transformed to
408 # '.$(OBJEXT)' by checking whether $suffix_rules{'.foo'}{'.$(OBJEXT)'}
409 # exists. This will work even if transforming '.foo' to '.$(OBJEXT)'
410 # involves a chain of several suffix rules.
412 # The value of $suffix_rules{$ext1}{$ext2} is a pair [$next_sfx, $dist]
413 # where $next_sfx is target suffix for the next rule to use to reach
414 # $ext2, and $dist the distance to $ext2.
415 # ----------------------------------------------------------------------
417 # Register $dest as a possible destination from $src.
418 # We might have the create the \hash.
419 if (exists $suffix_rules->{$src})
421 $suffix_rules->{$src}{$dest} = [ $dest, 1 ];
425 $suffix_rules->{$src} = { $dest => [ $dest, 1 ] };
428 # If we know how to transform $dest in something else, then
429 # we know how to transform $src in that "something else".
430 if (exists $suffix_rules->{$dest})
432 for my $dest2 (keys %{$suffix_rules->{$dest}})
434 my $dist = $suffix_rules->{$dest}{$dest2}[1] + 1;
435 # Overwrite an existing $src->$dest2 path only if
436 # the path via $dest which is shorter.
437 if (! exists $suffix_rules->{$src}{$dest2}
438 || $suffix_rules->{$src}{$dest2}[1] > $dist)
440 $suffix_rules->{$src}{$dest2} = [ $dest, $dist ];
445 # Similarly, any extension that can be derived into $src
446 # can be derived into the same extensions as $src can.
447 my @dest2 = keys %{$suffix_rules->{$src}};
448 for my $src2 (keys %$suffix_rules)
450 if (exists $suffix_rules->{$src2}{$src})
452 for my $dest2 (@dest2)
454 my $dist = $suffix_rules->{$src}{$dest2} + 1;
455 # Overwrite an existing $src2->$dest2 path only if
456 # the path via $src is shorter.
457 if (! exists $suffix_rules->{$src2}{$dest2}
458 || $suffix_rules->{$src2}{$dest2}[1] > $dist)
460 $suffix_rules->{$src2}{$dest2} = [ $src, $dist ];
467 =item C<@list = suffixes>
469 Return the list of known suffixes.
478 =item C<rule ($rulename)>
480 Return the C<Automake::Rule> object for the rule
481 named C<$rulename> if defined. Return 0 otherwise.
488 # Strip $(EXEEXT) from $name, so we can diagnose
489 # a clash if 'ctags$(EXEEXT):' is redefined after 'ctags:'.
490 $name =~ s
,\
$\
(EXEEXT\
)$,,;
491 return $_rule_dict{$name} || 0;
494 =item C<ruledef ($rulename, $cond)>
496 Return the C<Automake::RuleDef> object for the rule named
497 C<$rulename> if defined in condition C<$cond>. Return false
498 if the condition or the rule does not exist.
504 my ($name, $cond) = @_;
505 my $rule = rule
$name;
506 return $rule && $rule->def ($cond);
509 =item C<rrule ($rulename)
511 Return the C<Automake::Rule> object for the variable named
512 C<$rulename>. Abort with an internal error if the variable was not
515 The I<r> in front of C<var> stands for I<required>. One
516 should call C<rvar> to assert the rule's existence.
524 prog_error
("undefined rule $name\n" . &rules_dump
)
529 =item C<rruledef ($varname, $cond)>
531 Return the C<Automake::RuleDef> object for the rule named
532 C<$rulename> if defined in condition C<$cond>. Abort with an internal
533 error if the condition or the rule does not exist.
539 my ($name, $cond) = @_;
540 return rrule
($name)->rdef ($cond);
543 # Create the variable if it does not exist.
544 # This is used only by other functions in this package.
550 return _new Automake
::Rule
$name;
555 my ($class, $name) = @_;
557 # Strip $(EXEEXT) from $name, so we can diagnose
558 # a clash if 'ctags$(EXEEXT):' is redefined after 'ctags:'.
559 (my $keyname = $name) =~ s
,\
$\
(EXEEXT\
)$,,;
561 my $self = Automake
::Item
::new
($class, $name);
562 $_rule_dict{$keyname} = $self;
566 sub _rule_defn_with_exeext_awareness
($$$)
568 my ($target, $cond, $where) = @_;
570 # For now 'foo:' will override 'foo$(EXEEXT):'. This is temporary,
571 # though, so we emit a warning.
572 (my $noexe = $target) =~ s/\$\(EXEEXT\)$//;
573 my $noexerule = rule
$noexe;
574 my $tdef = $noexerule ?
$noexerule->def ($cond) : undef;
576 if ($noexe ne $target
578 && $noexerule->name ne $target)
580 # The no-exeext option enables this feature.
581 if (! option
'no-exeext')
583 msg
('obsolete', $tdef->location,
584 "deprecated feature: target '$noexe' overrides "
585 . "'$noexe\$(EXEEXT)'\n"
586 . "change your target to read '$noexe\$(EXEEXT)'",
588 msg
('obsolete', $where, "target '$target' was defined here");
594 sub _maybe_warn_about_duplicated_target
($$$$$$)
596 my ($target, $tdef, $source, $owner, $cond, $where) = @_;
598 my $oldowner = $tdef->owner;
599 # Ok, it's the name target, but the name maybe different because
600 # 'foo$(EXEEXT)' and 'foo' have the same key in our table.
601 my $oldname = $tdef->name;
603 # Don't mention true conditions in diagnostics.
605 $cond == TRUE ?
'' : (" in condition '" . $cond->human . "'");
607 if ($owner == RULE_USER
)
609 if ($oldowner == RULE_USER
)
611 # Ignore '%'-style pattern rules. We'd need the
612 # dependencies to detect duplicates, and they are
613 # already diagnosed as unportable by -Wportability.
614 if ($target !~ /^[^%]*%[^%]*$/)
616 ## FIXME: Presently we can't diagnose duplicate user rules
617 ## because we don't distinguish rules with commands
618 ## from rules that only add dependencies. E.g.,
621 ## is legitimate. This is checked in the 'phony.sh' test.
623 # msg ('syntax', $where,
624 # "redefinition of '$target'$condmsg ...", partial => 1);
625 # msg_cond_rule ('syntax', $cond, $target,
626 # "... '$target' previously defined here");
631 # Since we parse the user Makefile.am before reading
632 # the Automake fragments, this condition should never happen.
633 prog_error
("user target '$target'$condmsg seen after Automake's"
634 . " definition\nfrom " . $tdef->source);
637 else # $owner == RULE_AUTOMAKE
639 if ($oldowner == RULE_USER
)
641 # -am targets listed in %dependencies support a -local
642 # variant. If the user tries to override TARGET or
643 # TARGET-am for which there exists a -local variant,
644 # just tell the user to use it.
648 if (exists $dependencies{"$noam-am"})
650 $hint = "consider using $noam-local instead of $target";
653 msg_cond_rule
('override', $cond, $target,
654 "user target '$target' defined here"
655 . "$condmsg ...", partial
=> 1);
656 msg
('override', $where,
657 "... overrides Automake target '$oldname' defined here",
659 msg_cond_rule
('override', $cond, $target, $hint)
662 else # $oldowner == RULE_AUTOMAKE
664 # Automake should ignore redefinitions of its own
665 # rules if they came from the same file. This makes
666 # it easier to process a Makefile fragment several times.
667 # However it's an error if the target is defined in many
668 # files. E.g., the user might be using bin_PROGRAMS = ctags
669 # which clashes with our 'ctags' rule.
670 # (It would be more accurate if we had a way to compare
671 # the *content* of both rules. Then $targets_source would
673 my $oldsource = $tdef->source;
674 if (not ($source eq $oldsource && $target eq $oldname))
677 $where, "redefinition of '$target'$condmsg ...",
679 msg_cond_rule
('syntax', $cond, $target,
680 "... '$oldname' previously defined here");
686 # Return the list of conditionals in which the rule was defined. In case
687 # an ambiguous conditional definition is detected, return the empty list.
688 sub _conditionals_for_rule
($$$$)
690 my ($rule, $owner, $cond, $where) = @_;
691 my $target = $rule->name;
693 my ($message, $ambig_cond) = $rule->conditions->ambiguous_p ($target, $cond);
695 return $cond if !$message; # No ambiguity.
697 if ($owner == RULE_USER
)
699 # For user rules, just diagnose the ambiguity.
700 msg
'syntax', $where, "$message ...", partial
=> 1;
701 msg_cond_rule
('syntax', $ambig_cond, $target,
702 "... '$target' previously defined here");
706 # FIXME: for Automake rules, we can't diagnose ambiguities yet.
707 # The point is that Automake doesn't propagate conditions
708 # everywhere. For instance &handle_PROGRAMS doesn't care if
709 # bin_PROGRAMS was defined conditionally or not.
710 # On the following input
717 # &handle_PROGRAMS will attempt to define a 'foo:' rule
718 # in condition TRUE (which conflicts with COND1). Fixing
719 # this in &handle_PROGRAMS and siblings seems hard: you'd
720 # have to explain &file_contents what to do with a
721 # condition. So for now we do our best *here*. If 'foo:'
722 # was already defined in condition COND1 and we want to define
723 # it in condition TRUE, then define it only in condition !COND1.
724 # (See cond14.sh and cond15.sh for some test cases.)
725 @conds = $rule->not_always_defined_in_cond ($cond)->conds;
727 # No conditions left to define the rule.
728 # Warn, because our workaround is meaningless in this case.
729 if (scalar @conds == 0)
731 msg
'syntax', $where, "$message ...", partial
=> 1;
732 msg_cond_rule
('syntax', $ambig_cond, $target,
733 "... '$target' previously defined here");
739 =item C<@conds = define ($rulename, $source, $owner, $cond, $where)>
741 Define a new rule. C<$rulename> is the list of targets. C<$source>
742 is the filename the rule comes from. C<$owner> is the owner of the
743 rule (C<RULE_AUTOMAKE> or C<RULE_USER>). C<$cond> is the
744 C<Automake::Condition> under which the rule is defined. C<$where> is
745 the C<Automake::Location> where the rule is defined.
747 Returns a (possibly empty) list of C<Automake::Condition>s where the
748 rule's definition should be output.
754 my ($target, $source, $owner, $cond, $where) = @_;
756 prog_error
"$where is not a reference"
758 prog_error
"$cond is not a reference"
761 # Don't even think about defining a rule in condition FALSE.
762 return () if $cond == FALSE
;
764 my $tdef = _rule_defn_with_exeext_awareness
($target, $cond, $where);
766 # A GNU make-style pattern rule has a single "%" in the target name.
767 msg
('portability', $where,
768 "'%'-style pattern rules are a GNU make extension")
769 if $target =~ /^[^%]*%[^%]*$/;
771 # See whether this is a duplicated target declaration.
774 # Diagnose invalid target redefinitions, if any. Note that some
775 # target redefinitions are valid (e.g., for multiple-targets
777 _maybe_warn_about_duplicated_target
($target, $tdef, $source,
778 $owner, $cond, $where);
779 # Return so we don't redefine the rule in our tables, don't check
780 # for ambiguous condition, etc. The rule will be output anyway
781 # because '&read_am_file' ignores the return code.
785 my $rule = _crule
$target;
787 # Conditions for which the rule should be defined. Due to some
788 # complications in the automake internals, this aspect is not as
789 # obvious as it might be, and in come cases this list must contain
790 # other entries in addition to '$cond'. See the comments in
791 # '_conditionals_for_rule' for a rationale.
792 my @conds = _conditionals_for_rule
($rule, $owner, $cond, $where);
794 # Stop if we had ambiguous conditional definitions.
795 return unless @conds;
797 # Finally define this rule.
800 my $def = new Automake
::RuleDef
($target, '', $where->clone,
802 $rule->set ($c, $def);
805 # We honor inference rules with multiple targets because many
806 # makes support this and people use it. However this is disallowed
807 # by POSIX. We'll print a warning later.
808 my $target_count = 0;
809 my $inference_rule_count = 0;
811 for my $t (split (' ', $target))
814 # Check if the rule is a suffix rule: either it's a rule for
815 # two known extensions...
816 if ($t =~ /^($KNOWN_EXTENSIONS_PATTERN)($KNOWN_EXTENSIONS_PATTERN)$/
817 # ...or it's a rule with unknown extensions (i.e., the rule
818 # looks like '.foo.bar:' but '.foo' or '.bar' are not
819 # declared in SUFFIXES and are not known language
820 # extensions). Automake will complete SUFFIXES from
821 # @suffixes automatically (see handle_footer).
822 || ($t =~ /$_SUFFIX_RULE_PATTERN/o && accept_extensions
($1)))
824 ++$inference_rule_count;
825 register_suffix_rule
($where, $1, $2);
829 # POSIX allows multiple targets before the colon, but disallows
830 # definitions of multiple inference rules. It's also
831 # disallowed to mix plain targets with inference rules.
832 msg
('portability', $where,
833 "inference rules can have only one target before the colon (POSIX)")
834 if $inference_rule_count > 0 && $target_count > 1;
839 =item C<depend ($target, @deps)>
841 Adds C<@deps> to the dependencies of target C<$target>. This should
842 be used only with factored targets (those appearing in
849 my ($category, @dependees) = @_;
850 push (@
{$dependencies{$category}}, @dependees);
857 L<Automake::RuleDef>, L<Automake::Condition>,
858 L<Automake::DisjConditions>, L<Automake::Location>.
864 ### Setup "GNU" style for perl-mode and cperl-mode.
866 ## perl-indent-level: 2
867 ## perl-continued-statement-offset: 2
868 ## perl-continued-brace-offset: 0
869 ## perl-brace-offset: 0
870 ## perl-brace-imaginary-offset: 0
871 ## perl-label-offset: -2
872 ## cperl-indent-level: 2
873 ## cperl-brace-offset: 0
874 ## cperl-continued-brace-offset: 0
875 ## cperl-label-offset: -2
876 ## cperl-extra-newline-before-brace: t
877 ## cperl-merge-trailing-else: nil
878 ## cperl-continued-statement-offset: 2