Support more C++ file extensions for MSVC in the compile script.
[platform/upstream/automake.git] / lib / Automake / Rule.pm
1 # Copyright (C) 2003, 2004, 2006, 2007  Free Software Foundation, Inc.
2
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)
6 # any later version.
7
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.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package Automake::Rule;
17 use strict;
18 use Carp;
19
20 use Automake::Item;
21 use Automake::RuleDef;
22 use Automake::ChannelDefs;
23 use Automake::Channels;
24 use Automake::Options;
25 use Automake::Condition qw (TRUE FALSE);
26 use Automake::DisjConditions;
27 require Exporter;
28 use vars '@ISA', '@EXPORT', '@EXPORT_OK';
29 @ISA = qw/Automake::Item Exporter/;
30 @EXPORT = qw (reset register_suffix_rule suffix_rules_count
31               suffixes rules $suffix_rules $KNOWN_EXTENSIONS_PATTERN
32               depend %dependencies %actions register_action
33               accept_extensions
34               reject_rule msg_rule msg_cond_rule err_rule err_cond_rule
35               rule rrule ruledef rruledef);
36
37 =head1 NAME
38
39 Automake::Rule - support for rules definitions
40
41 =head1 SYNOPSIS
42
43   use Automake::Rule;
44   use Automake::RuleDef;
45
46
47 =head1 DESCRIPTION
48
49 This package provides support for Makefile rule definitions.
50
51 An C<Automake::Rule> is a rule name associated to possibly
52 many conditional definitions.  These definitions are instances
53 of C<Automake::RuleDef>.
54
55 Therefore obtaining the value of a rule under a given
56 condition involves two lookups.  One to look up the rule,
57 and one to look up the conditional definition:
58
59   my $rule = rule $name;
60   if ($rule)
61     {
62       my $def = $rule->def ($cond);
63       if ($def)
64         {
65           return $def->location;
66         }
67       ...
68     }
69   ...
70
71 when it is known that the rule and the definition
72 being looked up exist, the above can be simplified to
73
74   return rule ($name)->def ($cond)->location; # do not write this.
75
76 but is better written
77
78   return rrule ($name)->rdef ($cond)->location;
79
80 or even
81
82   return rruledef ($name, $cond)->location;
83
84 The I<r> variants of the C<rule>, C<def>, and C<ruledef> methods add
85 an extra test to ensure that the lookup succeeded, and will diagnose
86 failures as internal errors (with a message which is much more
87 informative than Perl's warning about calling a method on a
88 non-object).
89
90 =head2 Global variables
91
92 =over 4
93
94 =cut
95
96 my $_SUFFIX_RULE_PATTERN =
97   '^(\.[a-zA-Z0-9_(){}$+@\-]+)(\.[a-zA-Z0-9_(){}$+@\-]+)' . "\$";
98
99 # Suffixes found during a run.
100 use vars '@_suffixes';
101
102 # Same as $suffix_rules (declared below), but records only the
103 # default rules supplied by the languages Automake supports.
104 use vars '$_suffix_rules_default';
105
106 =item C<%dependencies>
107
108 Holds the dependencies of targets which dependencies are factored.
109 Typically, C<.PHONY> will appear in plenty of F<*.am> files, but must
110 be output once.  Arguably all pure dependencies could be subject to
111 this factoring, but it is not unpleasant to have paragraphs in
112 Makefile: keeping related stuff altogether.
113
114 =cut
115
116 use vars '%dependencies';
117
118 =item <%actions>
119
120 Holds the factored actions.  Tied to C<%dependencies>, i.e., filled
121 only when keys exists in C<%dependencies>.
122
123 =cut
124
125 use vars '%actions';
126
127 =item <$suffix_rules>
128
129 This maps the source extension for all suffix rule seen to
130 a C<hash> whose keys are the possible output extensions.
131
132 Note that this is transitively closed by construction:
133 if we have
134       exists $suffix_rules{$ext1}{$ext2}
135    && exists $suffix_rules{$ext2}{$ext3}
136 then we also have
137       exists $suffix_rules{$ext1}{$ext3}
138
139 So it's easy to check whether C<.foo> can be transformed to
140 C<.$(OBJEXT)> by checking whether
141 C<$suffix_rules{'.foo'}{'.$(OBJEXT)'}> exists.  This will work even if
142 transforming C<.foo> to C<.$(OBJEXT)> involves a chain of several
143 suffix rules.
144
145 The value of C<$suffix_rules{$ext1}{$ext2}> is a pair
146 C<[ $next_sfx, $dist ]> where C<$next_sfx> is target suffix
147 for the next rule to use to reach C<$ext2>, and C<$dist> the
148 distance to C<$ext2'>.
149
150 The content of this variable should be updated via the
151 C<register_suffix_rule> function.
152
153 =cut
154
155 use vars '$suffix_rules';
156
157 =item C<$KNOWN_EXTENSIONS_PATTERN>
158
159 Pattern that matches all know input extensions (i.e. extensions used
160 by the languages supported by Automake).  Using this pattern (instead
161 of `\..*$') to match extensions allows Automake to support dot-less
162 extensions.
163
164 New extensions should be registered with C<accept_extensions>.
165
166 =cut
167
168 use vars qw ($KNOWN_EXTENSIONS_PATTERN @_known_extensions_list);
169 $KNOWN_EXTENSIONS_PATTERN = "";
170 @_known_extensions_list = ();
171
172 =back
173
174 =head2 Error reporting functions
175
176 In these functions, C<$rule> can be either a rule name, or
177 an instance of C<Automake::Rule>.
178
179 =over 4
180
181 =item C<err_rule ($rule, $message, [%options])>
182
183 Uncategorized errors about rules.
184
185 =cut
186
187 sub err_rule ($$;%)
188 {
189   msg_rule ('error', @_);
190 }
191
192 =item C<err_cond_rule ($cond, $rule, $message, [%options])>
193
194 Uncategorized errors about conditional rules.
195
196 =cut
197
198 sub err_cond_rule ($$$;%)
199 {
200   msg_cond_rule ('error', @_);
201 }
202
203 =item C<msg_cond_rule ($channel, $cond, $rule, $message, [%options])>
204
205 Messages about conditional rules.
206
207 =cut
208
209 sub msg_cond_rule ($$$$;%)
210 {
211   my ($channel, $cond, $rule, $msg, %opts) = @_;
212   my $r = ref ($rule) ? $rule : rrule ($rule);
213   msg $channel, $r->rdef ($cond)->location, $msg, %opts;
214 }
215
216 =item C<msg_rule ($channel, $targetname, $message, [%options])>
217
218 Messages about rules.
219
220 =cut
221
222 sub msg_rule ($$$;%)
223 {
224   my ($channel, $rule, $msg, %opts) = @_;
225   my $r = ref ($rule) ? $rule : rrule ($rule);
226   # Don't know which condition is concerned.  Pick any.
227   my $cond = $r->conditions->one_cond;
228   msg_cond_rule ($channel, $cond, $r, $msg, %opts);
229 }
230
231
232 =item C<$bool = reject_rule ($rule, $error_msg)>
233
234 Bail out with C<$error_msg> if a rule with name C<$rule> has been
235 defined.
236
237 Return true iff C<$rule> is defined.
238
239 =cut
240
241 sub reject_rule ($$)
242 {
243   my ($rule, $msg) = @_;
244   if (rule ($rule))
245     {
246       err_rule $rule, $msg;
247       return 1;
248     }
249   return 0;
250 }
251
252 =back
253
254 =head2 Administrative functions
255
256 =over 4
257
258 =item C<accept_extensions (@exts)>
259
260 Update C<$KNOWN_EXTENSIONS_PATTERN> to recognize the extensions
261 listed C<@exts>.  Extensions should contain a dot if needed.
262
263 =cut
264
265 sub accept_extensions (@)
266 {
267     push @_known_extensions_list, @_;
268     $KNOWN_EXTENSIONS_PATTERN =
269         '(?:' . join ('|', map (quotemeta, @_known_extensions_list)) . ')';
270 }
271
272 =item C<rules>
273
274 Return the list of all L<Automake::Rule> instances.  (I.e., all
275 rules defined so far.)
276
277 =cut
278
279 use vars '%_rule_dict';
280 sub rules ()
281 {
282   return values %_rule_dict;
283 }
284
285
286 =item C<register_action($target, $action)>
287
288 Append the C<$action> to C<$actions{$target}> taking care of special
289 cases.
290
291 =cut
292
293 sub register_action ($$)
294 {
295   my ($target, $action) = @_;
296   if ($actions{$target})
297     {
298       $actions{$target} .= "\n$action" if $action;
299     }
300   else
301     {
302       $actions{$target} = $action;
303     }
304 }
305
306
307 =item C<Automake::Rule::reset>
308
309 The I<forget all> function.  Clears all know rules and reset some
310 other internal data.
311
312 =cut
313
314 sub reset()
315 {
316   %_rule_dict = ();
317   @_suffixes = ();
318   # The first time we initialize the variables,
319   # we save the value of $suffix_rules.
320   if (defined $_suffix_rules_default)
321     {
322       $suffix_rules = $_suffix_rules_default;
323     }
324   else
325     {
326       $_suffix_rules_default = $suffix_rules;
327     }
328
329   %dependencies =
330     (
331      # Texinfoing.
332      'dvi'      => [],
333      'dvi-am'   => [],
334      'pdf'      => [],
335      'pdf-am'   => [],
336      'ps'       => [],
337      'ps-am'    => [],
338      'info'     => [],
339      'info-am'  => [],
340      'html'     => [],
341      'html-am'  => [],
342
343      # Installing/uninstalling.
344      'install-data-am'      => [],
345      'install-exec-am'      => [],
346      'uninstall-am'         => [],
347
348      'install-man'          => [],
349      'uninstall-man'        => [],
350
351      'install-dvi'          => [],
352      'install-dvi-am'       => [],
353      'install-html'         => [],
354      'install-html-am'      => [],
355      'install-info'         => [],
356      'install-info-am'      => [],
357      'install-pdf'          => [],
358      'install-pdf-am'       => [],
359      'install-ps'           => [],
360      'install-ps-am'        => [],
361
362      'installcheck-am'      => [],
363
364      # Cleaning.
365      'clean-am'             => [],
366      'mostlyclean-am'       => [],
367      'maintainer-clean-am'  => [],
368      'distclean-am'         => [],
369      'clean'                => [],
370      'mostlyclean'          => [],
371      'maintainer-clean'     => [],
372      'distclean'            => [],
373
374      # Tarballing.
375      'dist-all'             => [],
376
377      # Phoning.
378      '.PHONY'               => [],
379      # Recursive install targets (so `make -n install' works for BSD Make).
380      '.MAKE'                => [],
381      );
382   %actions = ();
383 }
384
385 =item C<register_suffix_rule ($where, $src, $dest)>
386
387 Register a suffix rules defined on C<$where> that transform
388 files ending in C<$src> into files ending in C<$dest>.
389
390 This upgrades the C<$suffix_rules> variables.
391
392 =cut
393
394 sub register_suffix_rule ($$$)
395 {
396   my ($where, $src, $dest) = @_;
397
398   verb "Sources ending in $src become $dest";
399   push @_suffixes, $src, $dest;
400
401   # When transforming sources to objects, Automake uses the
402   # %suffix_rules to move from each source extension to
403   # `.$(OBJEXT)', not to `.o' or `.obj'.  However some people
404   # define suffix rules for `.o' or `.obj', so internally we will
405   # consider these extensions equivalent to `.$(OBJEXT)'.  We
406   # CANNOT rewrite the target (i.e., automagically replace `.o'
407   # and `.obj' by `.$(OBJEXT)' in the output), or warn the user
408   # that (s)he'd better use `.$(OBJEXT)', because Automake itself
409   # output suffix rules for `.o' or `.obj'...
410   $dest = '.$(OBJEXT)' if ($dest eq '.o' || $dest eq '.obj');
411
412   # Reading the comments near the declaration of $suffix_rules might
413   # help to understand the update of $suffix_rules that follows...
414
415   # Register $dest as a possible destination from $src.
416   # We might have the create the \hash.
417   if (exists $suffix_rules->{$src})
418     {
419       $suffix_rules->{$src}{$dest} = [ $dest, 1 ];
420     }
421   else
422     {
423       $suffix_rules->{$src} = { $dest => [ $dest, 1 ] };
424     }
425
426   # If we know how to transform $dest in something else, then
427   # we know how to transform $src in that "something else".
428   if (exists $suffix_rules->{$dest})
429     {
430       for my $dest2 (keys %{$suffix_rules->{$dest}})
431         {
432           my $dist = $suffix_rules->{$dest}{$dest2}[1] + 1;
433           # Overwrite an existing $src->$dest2 path only if
434           # the path via $dest which is shorter.
435           if (! exists $suffix_rules->{$src}{$dest2}
436               || $suffix_rules->{$src}{$dest2}[1] > $dist)
437             {
438               $suffix_rules->{$src}{$dest2} = [ $dest, $dist ];
439             }
440         }
441     }
442
443   # Similarly, any extension that can be derived into $src
444   # can be derived into the same extensions as $src can.
445   my @dest2 = keys %{$suffix_rules->{$src}};
446   for my $src2 (keys %$suffix_rules)
447     {
448       if (exists $suffix_rules->{$src2}{$src})
449         {
450           for my $dest2 (@dest2)
451             {
452               my $dist = $suffix_rules->{$src}{$dest2} + 1;
453               # Overwrite an existing $src2->$dest2 path only if
454               # the path via $src is shorter.
455               if (! exists $suffix_rules->{$src2}{$dest2}
456                   || $suffix_rules->{$src2}{$dest2}[1] > $dist)
457                 {
458                   $suffix_rules->{$src2}{$dest2} = [ $src, $dist ];
459                 }
460             }
461         }
462     }
463 }
464
465 =item C<$count = suffix_rules_count>
466
467 Return the number of suffix rules added while processing the current
468 F<Makefile> (excluding predefined suffix rules).
469
470 =cut
471
472 sub suffix_rules_count ()
473 {
474   return (scalar keys %$suffix_rules) - (scalar keys %$_suffix_rules_default);
475 }
476
477 =item C<@list = suffixes>
478
479 Return the list of known suffixes.
480
481 =cut
482
483 sub suffixes ()
484 {
485   return @_suffixes;
486 }
487
488 =item C<rule ($rulename)>
489
490 Return the C<Automake::Rule> object for the rule
491 named C<$rulename> if defined.   Return 0 otherwise.
492
493 =cut
494
495 sub rule ($)
496 {
497   my ($name) = @_;
498   # Strip $(EXEEXT) from $name, so we can diagnose
499   # a clash if `ctags$(EXEEXT):' is redefined after `ctags:'.
500   $name =~ s,\$\(EXEEXT\)$,,;
501   return $_rule_dict{$name} || 0;
502 }
503
504 =item C<ruledef ($rulename, $cond)>
505
506 Return the C<Automake::RuleDef> object for the rule named
507 C<$rulename> if defined in condition C<$cond>.  Return false
508 if the condition or the rule does not exist.
509
510 =cut
511
512 sub ruledef ($$)
513 {
514   my ($name, $cond) = @_;
515   my $rule = rule $name;
516   return $rule && $rule->def ($cond);
517 }
518
519 =item C<rrule ($rulename)
520
521 Return the C<Automake::Rule> object for the variable named
522 C<$rulename>.  Abort with an internal error if the variable was not
523 defined.
524
525 The I<r> in front of C<var> stands for I<required>.  One
526 should call C<rvar> to assert the rule's existence.
527
528 =cut
529
530 sub rrule ($)
531 {
532   my ($name) = @_;
533   my $r = rule $name;
534   prog_error ("undefined rule $name\n" . &rules_dump)
535     unless $r;
536   return $r;
537 }
538
539 =item C<rruledef ($varname, $cond)>
540
541 Return the C<Automake::RuleDef> object for the rule named
542 C<$rulename> if defined in condition C<$cond>.  Abort with an internal
543 error if the condition or the rule does not exist.
544
545 =cut
546
547 sub rruledef ($$)
548 {
549   my ($name, $cond) = @_;
550   return rrule ($name)->rdef ($cond);
551 }
552
553 # Create the variable if it does not exist.
554 # This is used only by other functions in this package.
555 sub _crule ($)
556 {
557   my ($name) = @_;
558   my $r = rule $name;
559   return $r if $r;
560   return _new Automake::Rule $name;
561 }
562
563 sub _new ($$)
564 {
565   my ($class, $name) = @_;
566
567   # Strip $(EXEEXT) from $name, so we can diagnose
568   # a clash if `ctags$(EXEEXT):' is redefined after `ctags:'.
569   (my $keyname = $name) =~ s,\$\(EXEEXT\)$,,;
570
571   my $self = Automake::Item::new ($class, $name);
572   $_rule_dict{$keyname} = $self;
573   return $self;
574 }
575
576
577 =item C<@conds = define ($rulename, $source, $owner, $cond, $where)>
578
579 Define a new rule.  C<$rulename> is the list of targets.  C<$source>
580 is the filename the rule comes from.  C<$owner> is the owner of the
581 rule (C<RULE_AUTOMAKE> or C<RULE_USER>).  C<$cond> is the
582 C<Automake::Condition> under which the rule is defined.  C<$where> is
583 the C<Automake::Location> where the rule is defined.
584
585 Returns a (possibly empty) list of C<Automake::Condition>s where the
586 rule's definition should be output.
587
588 =cut
589
590 sub define ($$$$$)
591 {
592   my ($target, $source, $owner, $cond, $where) = @_;
593
594   prog_error "$where is not a reference"
595     unless ref $where;
596   prog_error "$cond is not a reference"
597     unless ref $cond;
598
599   # Don't even think about defining a rule in condition FALSE.
600   return () if $cond == FALSE;
601
602   # For now `foo:' will override `foo$(EXEEXT):'.  This is temporary,
603   # though, so we emit a warning.
604   (my $noexe = $target) =~ s,\$\(EXEEXT\)$,,;
605   my $noexerule = rule $noexe;
606   my $tdef = $noexerule ? $noexerule->def ($cond) : undef;
607
608   if ($noexe ne $target
609       && $tdef
610       && $noexerule->name ne $target)
611     {
612       # The no-exeext option enables this feature.
613       if (! option 'no-exeext')
614         {
615           msg ('obsolete', $tdef->location,
616                "deprecated feature: target `$noexe' overrides "
617                . "`$noexe\$(EXEEXT)'\n"
618                . "change your target to read `$noexe\$(EXEEXT)'");
619           msg ('obsolete', $where, "target `$target' was defined here");
620         }
621       # Don't `return ()' now, as this might hide target clashes
622       # detected below.
623     }
624
625
626   # A GNU make-style pattern rule has a single "%" in the target name.
627   msg ('portability', $where,
628        "`%'-style pattern rules are a GNU make extension")
629     if $target =~ /^[^%]*%[^%]*$/;
630
631   # Diagnose target redefinitions.
632   if ($tdef)
633     {
634       my $oldowner  = $tdef->owner;
635       # Ok, it's the name target, but the name maybe different because
636       # `foo$(EXEEXT)' and `foo' have the same key in our table.
637       my $oldname = $tdef->name;
638
639       # Don't mention true conditions in diagnostics.
640       my $condmsg =
641         $cond == TRUE ? '' : " in condition `" . $cond->human . "'";
642
643       if ($owner == RULE_USER)
644         {
645           if ($oldowner == RULE_USER)
646             {
647               # Ignore `%'-style pattern rules.  We'd need the
648               # dependencies to detect duplicates, and they are
649               # already diagnosed as unportable by -Wportability.
650               if ($target !~ /^[^%]*%[^%]*$/)
651                 {
652                   ## FIXME: Presently we can't diagnose duplicate user rules
653                   ## because we don't distinguish rules with commands
654                   ## from rules that only add dependencies.  E.g.,
655                   ##   .PHONY: foo
656                   ##   .PHONY: bar
657                   ## is legitimate. (This is phony.test.)
658
659                   # msg ('syntax', $where,
660                   #      "redefinition of `$target'$condmsg...", partial => 1);
661                   # msg_cond_rule ('syntax', $cond, $target,
662                   #                "... `$target' previously defined here");
663                 }
664               # Return so we don't redefine the rule in our tables,
665               # don't check for ambiguous condition, etc.  The rule
666               # will be output anyway because &read_am_file ignore the
667               # return code.
668               return ();
669             }
670           else
671             {
672               # Since we parse the user Makefile.am before reading
673               # the Automake fragments, this condition should never happen.
674               prog_error ("user target `$target'$condmsg seen after Automake's"
675                           . " definition\nfrom " . $tdef->source);
676             }
677         }
678       else # $owner == RULE_AUTOMAKE
679         {
680           if ($oldowner == RULE_USER)
681             {
682               # -am targets listed in %dependencies support a -local
683               # variant.  If the user tries to override TARGET or
684               # TARGET-am for which there exists a -local variant,
685               # just tell the user to use it.
686               my $hint = 0;
687               my $noam = $target;
688               $noam =~ s/-am$//;
689               if (exists $dependencies{"$noam-am"})
690                 {
691                   $hint = "consider using $noam-local instead of $target";
692                 }
693
694               msg_cond_rule ('override', $cond, $target,
695                              "user target `$target' defined here"
696                              . "$condmsg...", partial => 1);
697               msg ('override', $where,
698                    "... overrides Automake target `$oldname' defined here",
699                    partial => $hint);
700               msg_cond_rule ('override', $cond, $target, $hint)
701                 if $hint;
702
703               # Don't overwrite the user definition of TARGET.
704               return ();
705             }
706           else # $oldowner == RULE_AUTOMAKE
707             {
708               # Automake should ignore redefinitions of its own
709               # rules if they came from the same file.  This makes
710               # it easier to process a Makefile fragment several times.
711               # However it's an error if the target is defined in many
712               # files.  E.g., the user might be using bin_PROGRAMS = ctags
713               # which clashes with our `ctags' rule.
714               # (It would be more accurate if we had a way to compare
715               # the *content* of both rules.  Then $targets_source would
716               # be useless.)
717               my $oldsource = $tdef->source;
718               return () if $source eq $oldsource && $target eq $oldname;
719
720               msg ('syntax', $where, "redefinition of `$target'$condmsg...",
721                    partial => 1);
722               msg_cond_rule ('syntax', $cond, $target,
723                              "... `$oldname' previously defined here");
724               return ();
725             }
726         }
727       # Never reached.
728       prog_error ("Unreachable place reached.");
729     }
730
731   # Conditions for which the rule should be defined.
732   my @conds = $cond;
733
734   # Check ambiguous conditional definitions.
735   my $rule = _crule $target;
736   my ($message, $ambig_cond) = $rule->conditions->ambiguous_p ($target, $cond);
737   if ($message)                 # We have an ambiguity.
738     {
739       if ($owner == RULE_USER)
740         {
741           # For user rules, just diagnose the ambiguity.
742           msg 'syntax', $where, "$message ...", partial => 1;
743           msg_cond_rule ('syntax', $ambig_cond, $target,
744                          "... `$target' previously defined here");
745           return ();
746         }
747       else
748         {
749           # FIXME: for Automake rules, we can't diagnose ambiguities yet.
750           # The point is that Automake doesn't propagate conditions
751           # everywhere.  For instance &handle_PROGRAMS doesn't care if
752           # bin_PROGRAMS was defined conditionally or not.
753           # On the following input
754           #   if COND1
755           #   foo:
756           #           ...
757           #   else
758           #   bin_PROGRAMS = foo
759           #   endif
760           # &handle_PROGRAMS will attempt to define a `foo:' rule
761           # in condition TRUE (which conflicts with COND1).  Fixing
762           # this in &handle_PROGRAMS and siblings seems hard: you'd
763           # have to explain &file_contents what to do with a
764           # condition.  So for now we do our best *here*.  If `foo:'
765           # was already defined in condition COND1 and we want to define
766           # it in condition TRUE, then define it only in condition !COND1.
767           # (See cond14.test and cond15.test for some test cases.)
768           @conds = $rule->not_always_defined_in_cond ($cond)->conds;
769
770           # No conditions left to define the rule.
771           # Warn, because our workaround is meaningless in this case.
772           if (scalar @conds == 0)
773             {
774               msg 'syntax', $where, "$message ...", partial => 1;
775               msg_cond_rule ('syntax', $ambig_cond, $target,
776                              "... `$target' previously defined here");
777               return ();
778             }
779         }
780     }
781
782   # Finally define this rule.
783   for my $c (@conds)
784     {
785       my $def = new Automake::RuleDef ($target, '', $where->clone,
786                                        $owner, $source);
787       $rule->set ($c, $def);
788     }
789
790   # We honor inference rules with multiple targets because many
791   # make support this and people use it.  However this is disallowed
792   # by POSIX.  We'll print a warning later.
793   my $target_count = 0;
794   my $inference_rule_count = 0;
795
796   for my $t (split (' ', $target))
797     {
798       ++$target_count;
799       # Check if the rule is a suffix rule: either it's a rule for
800       # two known extensions...
801       if ($t =~ /^($KNOWN_EXTENSIONS_PATTERN)($KNOWN_EXTENSIONS_PATTERN)$/
802           # ...or it's a rule with unknown extensions (i.e., the rule
803           # looks like `.foo.bar:' but `.foo' or `.bar' are not
804           # declared in SUFFIXES and are not known language
805           # extensions).  Automake will complete SUFFIXES from
806           # @suffixes automatically (see handle_footer).
807
808
809           || ($t =~ /$_SUFFIX_RULE_PATTERN/o && accept_extensions($1)))
810         {
811           ++$inference_rule_count;
812           register_suffix_rule ($where, $1, $2);
813         }
814     }
815
816   # POSIX allows multiple targets before the colon, but disallows
817   # definitions of multiple inference rules.  It's also
818   # disallowed to mix plain targets with inference rules.
819   msg ('portability', $where,
820        "Inference rules can have only one target before the colon (POSIX).")
821     if $inference_rule_count > 0 && $target_count > 1;
822
823   return @conds;
824 }
825
826 =item C<depend ($target, @deps)>
827
828 Adds C<@deps> to the dependencies of target C<$target>.  This should
829 be used only with factored targets (those appearing in
830 C<%dependees>).
831
832 =cut
833
834 sub depend ($@)
835 {
836   my ($category, @dependees) = @_;
837   push (@{$dependencies{$category}}, @dependees);
838 }
839
840 =back
841
842 =head1 SEE ALSO
843
844 L<Automake::RuleDef>, L<Automake::Condition>,
845 L<Automake::DisjConditions>, L<Automake::Location>.
846
847 =cut
848
849 1;
850
851 ### Setup "GNU" style for perl-mode and cperl-mode.
852 ## Local Variables:
853 ## perl-indent-level: 2
854 ## perl-continued-statement-offset: 2
855 ## perl-continued-brace-offset: 0
856 ## perl-brace-offset: 0
857 ## perl-brace-imaginary-offset: 0
858 ## perl-label-offset: -2
859 ## cperl-indent-level: 2
860 ## cperl-brace-offset: 0
861 ## cperl-continued-brace-offset: 0
862 ## cperl-label-offset: -2
863 ## cperl-extra-newline-before-brace: t
864 ## cperl-merge-trailing-else: nil
865 ## cperl-continued-statement-offset: 2
866 ## End: