File Coverage

File:/tmp/automake/lib/Automake/Rule.pm
Coverage:91.6%

linestmtbrancondsubpodtimecode
1# Copyright (C) 2003, 2004, 2006, 2007, 2010 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
16package Automake::Rule;
17
1159
1159
1159
3430
1227
3949
use strict;
18
1159
1159
1159
4326
1332
3419
use Carp;
19
20
1159
1159
1159
4857
1367
3352
use Automake::Item;
21
1159
1159
1159
8694
1423
7287
use Automake::RuleDef;
22
1159
1159
1159
4508
1163
3019
use Automake::ChannelDefs;
23
1159
1159
1159
4505
1233
3660
use Automake::Channels;
24
1159
1159
1159
5677
1491
25163
use Automake::Options;
25
1159
1159
1159
5220
2501
11829
use Automake::Condition qw (TRUE FALSE);
26
1159
1159
1159
4532
1225
6858
use Automake::DisjConditions;
27require Exporter;
28
1159
1159
1159
3943
1179
4173
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 - 94
=head1 NAME

Automake::Rule - support for rules definitions

=head1 SYNOPSIS

  use Automake::Rule;
  use Automake::RuleDef;


=head1 DESCRIPTION

This package provides support for Makefile rule definitions.

An C<Automake::Rule> is a rule name associated to possibly
many conditional definitions.  These definitions are instances
of C<Automake::RuleDef>.

Therefore obtaining the value of a rule under a given
condition involves two lookups.  One to look up the rule,
and one to look up the conditional definition:

  my $rule = rule $name;
  if ($rule)
    {
      my $def = $rule->def ($cond);
      if ($def)
	{
	  return $def->location;
	}
      ...
    }
  ...

when it is known that the rule and the definition
being looked up exist, the above can be simplified to

  return rule ($name)->def ($cond)->location; # do not write this.

but is better written

  return rrule ($name)->rdef ($cond)->location;

or even

  return rruledef ($name, $cond)->location;

The I<r> variants of the C<rule>, C<def>, and C<ruledef> methods add
an extra test to ensure that the lookup succeeded, and will diagnose
failures as internal errors (with a message which is much more
informative than Perl's warning about calling a method on a
non-object).

=head2 Global variables

=over 4

=cut
95
96my $_SUFFIX_RULE_PATTERN =
97  '^(\.[a-zA-Z0-9_(){}$+@\-]+)(\.[a-zA-Z0-9_(){}$+@\-]+)' . "\$";
98
99# Suffixes found during a run.
100
1159
1159
1159
5126
1441
4670
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
1159
1159
1159
4184
1287
2899
use vars '$_suffix_rules_default';
105
106 - 114
=item C<%dependencies>

Holds the dependencies of targets which dependencies are factored.
Typically, C<.PHONY> will appear in plenty of F<*.am> files, but must
be output once.  Arguably all pure dependencies could be subject to
this factoring, but it is not unpleasant to have paragraphs in
Makefile: keeping related stuff altogether.

=cut
115
116
1159
1159
1159
4604
1288
2729
use vars '%dependencies';
117
118 - 123
=item <%actions>

Holds the factored actions.  Tied to C<%dependencies>, i.e., filled
only when keys exists in C<%dependencies>.

=cut
124
125
1159
1159
1159
4081
1236
3444
use vars '%actions';
126
127 - 153
=item <$suffix_rules>

This maps the source extension for all suffix rule seen to
a C<hash> whose keys are the possible output extensions.

Note that this is transitively closed by construction:
if we have
      exists $suffix_rules{$ext1}{$ext2}
   && exists $suffix_rules{$ext2}{$ext3}
then we also have
      exists $suffix_rules{$ext1}{$ext3}

So it's easy to check whether C<.foo> can be transformed to
C<.$(OBJEXT)> by checking whether
C<$suffix_rules{'.foo'}{'.$(OBJEXT)'}> exists.  This will work even if
transforming C<.foo> to C<.$(OBJEXT)> involves a chain of several
suffix rules.

The value of C<$suffix_rules{$ext1}{$ext2}> is a pair
C<[ $next_sfx, $dist ]> where C<$next_sfx> is target suffix
for the next rule to use to reach C<$ext2>, and C<$dist> the
distance to C<$ext2'>.

The content of this variable should be updated via the
C<register_suffix_rule> function.

=cut
154
155
1159
1159
1159
4263
1196
2561
use vars '$suffix_rules';
156
157 - 166
=item C<$KNOWN_EXTENSIONS_PATTERN>

Pattern that matches all know input extensions (i.e. extensions used
by the languages supported by Automake).  Using this pattern (instead
of `\..*$') to match extensions allows Automake to support dot-less
extensions.

New extensions should be registered with C<accept_extensions>.

=cut
167
168
1159
1159
1159
4077
1264
3410
use vars qw ($KNOWN_EXTENSIONS_PATTERN @_known_extensions_list);
169$KNOWN_EXTENSIONS_PATTERN = "";
170@_known_extensions_list = ();
171
172=back
173
174 - 185
=head2 Error reporting functions

In these functions, C<$rule> can be either a rule name, or
an instance of C<Automake::Rule>.

=over 4

=item C<err_rule ($rule, $message, [%options])>

Uncategorized errors about rules.

=cut
186
187sub err_rule ($$;%)
188{
189
1
1
4
  msg_rule ('error', @_);
190}
191
192 - 196
=item C<err_cond_rule ($cond, $rule, $message, [%options])>

Uncategorized errors about conditional rules.

=cut
197
198sub err_cond_rule ($$$;%)
199{
200
0
1
0
  msg_cond_rule ('error', @_);
201}
202
203 - 207
=item C<msg_cond_rule ($channel, $cond, $rule, $message, [%options])>

Messages about conditional rules.

=cut
208
209sub msg_cond_rule ($$$$;%)
210{
211
42
1
149
  my ($channel, $cond, $rule, $msg, %opts) = @_;
212
42
160
  my $r = ref ($rule) ? $rule : rrule ($rule);
213
42
213
  msg $channel, $r->rdef ($cond)->location, $msg, %opts;
214}
215
216 - 220
=item C<msg_rule ($channel, $targetname, $message, [%options])>

Messages about rules.

=cut
221
222sub msg_rule ($$$;%)
223{
224
1
1
3
  my ($channel, $rule, $msg, %opts) = @_;
225
1
6
  my $r = ref ($rule) ? $rule : rrule ($rule);
226  # Don't know which condition is concerned. Pick any.
227
1
3
  my $cond = $r->conditions->one_cond;
228
1
5
  msg_cond_rule ($channel, $cond, $r, $msg, %opts);
229}
230
231
232 - 239
=item C<$bool = reject_rule ($rule, $error_msg)>

Bail out with C<$error_msg> if a rule with name C<$rule> has been
defined.

Return true iff C<$rule> is defined.

=cut
240
241sub reject_rule ($$)
242{
243
15828
1
28704
  my ($rule, $msg) = @_;
244
15828
24600
  if (rule ($rule))
245    {
246
1
3
      err_rule $rule, $msg;
247
1
3
      return 1;
248    }
249
15827
32009
  return 0;
250}
251
252=back
253
254 - 263
=head2 Administrative functions

=over 4

=item C<accept_extensions (@exts)>

Update C<$KNOWN_EXTENSIONS_PATTERN> to recognize the extensions
listed C<@exts>.  Extensions should contain a dot if needed.

=cut
264
265sub accept_extensions (@)
266{
267
22555
1
44819
    push @_known_extensions_list, @_;
268
22555
391478
    $KNOWN_EXTENSIONS_PATTERN =
269        '(?:' . join ('|', map (quotemeta, @_known_extensions_list)) . ')';
270}
271
272 - 277
=item C<rules>

Return the list of all L<Automake::Rule> instances.  (I.e., all
rules defined so far.)

=cut
278
279
1159
1159
1159
4985
1366
3164
use vars '%_rule_dict';
280sub rules ()
281{
282
0
1
0
  return values %_rule_dict;
283}
284
285
286 - 291
=item C<register_action($target, $action)>

Append the C<$action> to C<$actions{$target}> taking care of special
cases.

=cut
292
293sub register_action ($$)
294{
295
91651
1
147414
  my ($target, $action) = @_;
296
91651
177642
  if ($actions{$target})
297    {
298
5890
33170
      $actions{$target} .= "\n$action" if $action;
299    }
300  else
301    {
302
85761
342351
      $actions{$target} = $action;
303    }
304}
305
306
307 - 312
=item C<Automake::Rule::reset>

The I<forget all> function.  Clears all know rules and reset some
other internal data.

=cut
313
314sub reset()
315{
316
1322
1
34454
  %_rule_dict = ();
317
1322
69014
  @_suffixes = ();
318  # The first time we initialize the variables,
319  # we save the value of $suffix_rules.
320
1322
4144
  if (defined $_suffix_rules_default)
321    {
322
203
501
      $suffix_rules = $_suffix_rules_default;
323    }
324  else
325    {
326
1119
3921
      $_suffix_rules_default = $suffix_rules;
327    }
328
329
1322
48299
  %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
1322
7562
  %actions = ();
383}
384
385 - 392
=item C<register_suffix_rule ($where, $src, $dest)>

Register a suffix rules defined on C<$where> that transform
files ending in C<$src> into files ending in C<$dest>.

This upgrades the C<$suffix_rules> variables.

=cut
393
394sub register_suffix_rule ($$$)
395{
396
77101
1
135629
  my ($where, $src, $dest) = @_;
397
398
77101
219246
  verb "Sources ending in $src become $dest";
399
77101
178111
  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
77101
357124
  $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
77101
150662
  if (exists $suffix_rules->{$src})
418    {
419
32822
90319
      $suffix_rules->{$src}{$dest} = [ $dest, 1 ];
420    }
421  else
422    {
423
44279
147772
      $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
77101
166217
  if (exists $suffix_rules->{$dest})
429    {
430
12907
12907
13009
33406
      for my $dest2 (keys %{$suffix_rules->{$dest}})
431        {
432
25740
58585
          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
25740
82478
          if (! exists $suffix_rules->{$src}{$dest2}
436              || $suffix_rules->{$src}{$dest2}[1] > $dist)
437            {
438
25604
79341
              $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
77101
77101
75651
219590
  my @dest2 = keys %{$suffix_rules->{$src}};
446
77101
249583
  for my $src2 (keys %$suffix_rules)
447    {
448
1606873
4007881
      if (exists $suffix_rules->{$src2}{$src})
449        {
450
3467
5284
          for my $dest2 (@dest2)
451            {
452
6882
14776
              my $dist = $suffix_rules->{$src}{$dest2} + 1;
453              # Overwrite an existing $src2->$dest2 path only if
454              # the path via $src is shorter.
455
6882
54937
              if (! exists $suffix_rules->{$src2}{$dest2}
456                  || $suffix_rules->{$src2}{$dest2}[1] > $dist)
457                {
458
54
253
                  $suffix_rules->{$src2}{$dest2} = [ $src, $dist ];
459                }
460            }
461        }
462    }
463}
464
465 - 470
=item C<$count = suffix_rules_count>

Return the number of suffix rules added while processing the current
F<Makefile> (excluding predefined suffix rules).

=cut
471
472sub suffix_rules_count ()
473{
474
1269
1
10723
  return (scalar keys %$suffix_rules) - (scalar keys %$_suffix_rules_default);
475}
476
477 - 481
=item C<@list = suffixes>

Return the list of known suffixes.

=cut
482
483sub suffixes ()
484{
485
1319
1
5915
  return @_suffixes;
486}
487
488 - 493
=item C<rule ($rulename)>

Return the C<Automake::Rule> object for the rule
named C<$rulename> if defined.  Return 0 otherwise.

=cut
494
495sub rule ($)
496{
497
305090
1
406114
  my ($name) = @_;
498  # Strip $(EXEEXT) from $name, so we can diagnose
499  # a clash if `ctags$(EXEEXT):' is redefined after `ctags:'.
500
305090
343097
  $name =~ s,\$\(EXEEXT\)$,,;
501
305090
1551255
  return $_rule_dict{$name} || 0;
502}
503
504 - 510
=item C<ruledef ($rulename, $cond)>

Return the C<Automake::RuleDef> object for the rule named
C<$rulename> if defined in condition C<$cond>.  Return false
if the condition or the rule does not exist.

=cut
511
512sub ruledef ($$)
513{
514
0
1
0
  my ($name, $cond) = @_;
515
0
0
  my $rule = rule $name;
516
0
0
  return $rule && $rule->def ($cond);
517}
518
519 - 528
=item C<rrule ($rulename)

Return the C<Automake::Rule> object for the variable named
C<$rulename>.  Abort with an internal error if the variable was not
defined.

The I<r> in front of C<var> stands for I<required>.  One
should call C<rvar> to assert the rule's existence.

=cut
529
530sub rrule ($)
531{
532
42
1
83
  my ($name) = @_;
533
42
80
  my $r = rule $name;
534
42
98
  prog_error ("undefined rule $name\n" . &rules_dump)
535    unless $r;
536
42
68
  return $r;
537}
538
539 - 545
=item C<rruledef ($varname, $cond)>

Return the C<Automake::RuleDef> object for the rule named
C<$rulename> if defined in condition C<$cond>.  Abort with an internal
error if the condition or the rule does not exist.

=cut
546
547sub rruledef ($$)
548{
549
0
1
0
  my ($name, $cond) = @_;
550
0
0
  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.
555sub _crule ($)
556{
557
104227
144847
  my ($name) = @_;
558
104227
148124
  my $r = rule $name;
559
104227
174174
  return $r if $r;
560
104206
246767
  return _new Automake::Rule $name;
561}
562
563sub _new ($$)
564{
565
104206
169377
  my ($class, $name) = @_;
566
567  # Strip $(EXEEXT) from $name, so we can diagnose
568  # a clash if `ctags$(EXEEXT):' is redefined after `ctags:'.
569
104206
147310
  (my $keyname = $name) =~ s,\$\(EXEEXT\)$,,;
570
571
104206
226508
  my $self = Automake::Item::new ($class, $name);
572
104206
200244
  $_rule_dict{$keyname} = $self;
573
104206
176833
  return $self;
574}
575
576
577 - 588
=item C<@conds = define ($rulename, $source, $owner, $cond, $where)>

Define a new rule.  C<$rulename> is the list of targets.  C<$source>
is the filename the rule comes from.  C<$owner> is the owner of the
rule (C<RULE_AUTOMAKE> or C<RULE_USER>).  C<$cond> is the
C<Automake::Condition> under which the rule is defined.  C<$where> is
the C<Automake::Location> where the rule is defined.

Returns a (possibly empty) list of C<Automake::Condition>s where the
rule's definition should be output.

=cut
589
590sub define ($$$$$)
591{
592
174106
1
357638
  my ($target, $source, $owner, $cond, $where) = @_;
593
594
174106
331510
  prog_error "$where is not a reference"
595    unless ref $where;
596
174106
302961
  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
174106
415570
  return () if $cond == FALSE;
601
602  # For now `foo:' will override `foo$(EXEEXT):'. This is temporary,
603  # though, so we emit a warning.
604
104349
167981
  (my $noexe = $target) =~ s,\$\(EXEEXT\)$,,;
605
104349
164536
  my $noexerule = rule $noexe;
606
104349
175268
  my $tdef = $noexerule ? $noexerule->def ($cond) : undef;
607
608
104349
349570
  if ($noexe ne $target
609      && $tdef
610      && $noexerule->name ne $target)
611    {
612      # The no-exeext option enables this feature.
613
6
20
      if (! option 'no-exeext')
614        {
615
4
29
          msg ('obsolete', $tdef->location,
616               "deprecated feature: target `$noexe' overrides "
617               . "`$noexe\$(EXEEXT)'\n"
618               . "change your target to read `$noexe\$(EXEEXT)'",
619               partial => 1);
620
4
16
          msg ('obsolete', $where, "target `$target' was defined here");
621        }
622      # Don't `return ()' now, as this might hide target clashes
623      # detected below.
624    }
625
626
627  # A GNU make-style pattern rule has a single "%" in the target name.
628
104349
210628
  msg ('portability', $where,
629       "`%'-style pattern rules are a GNU make extension")
630    if $target =~ /^[^%]*%[^%]*$/;
631
632  # Diagnose target redefinitions.
633
104349
168796
  if ($tdef)
634    {
635
122
481
      my $oldowner = $tdef->owner;
636      # Ok, it's the name target, but the name maybe different because
637      # `foo$(EXEEXT)' and `foo' have the same key in our table.
638
122
362
      my $oldname = $tdef->name;
639
640      # Don't mention true conditions in diagnostics.
641
122
331
      my $condmsg =
642        $cond == TRUE ? '' : " in condition `" . $cond->human . "'";
643
644
122
234
      if ($owner == RULE_USER)
645        {
646
6
14
          if ($oldowner == RULE_USER)
647            {
648              # Ignore `%'-style pattern rules. We'd need the
649              # dependencies to detect duplicates, and they are
650              # already diagnosed as unportable by -Wportability.
651
6
35
              if ($target !~ /^[^%]*%[^%]*$/)
652                {
653                  ## FIXME: Presently we can't diagnose duplicate user rules
654                  ## because we don't distinguish rules with commands
655                  ## from rules that only add dependencies. E.g.,
656                  ## .PHONY: foo
657                  ## .PHONY: bar
658                  ## is legitimate. (This is phony.test.)
659
660                  # msg ('syntax', $where,
661                  # "redefinition of `$target'$condmsg ...", partial => 1);
662                  # msg_cond_rule ('syntax', $cond, $target,
663                  # "... `$target' previously defined here");
664                }
665              # Return so we don't redefine the rule in our tables,
666              # don't check for ambiguous condition, etc. The rule
667              # will be output anyway because &read_am_file ignore the
668              # return code.
669
6
18
              return ();
670            }
671          else
672            {
673              # Since we parse the user Makefile.am before reading
674              # the Automake fragments, this condition should never happen.
675
0
0
              prog_error ("user target `$target'$condmsg seen after Automake's"
676                          . " definition\nfrom " . $tdef->source);
677            }
678        }
679      else # $owner == RULE_AUTOMAKE
680        {
681
116
226
          if ($oldowner == RULE_USER)
682            {
683              # -am targets listed in %dependencies support a -local
684              # variant. If the user tries to override TARGET or
685              # TARGET-am for which there exists a -local variant,
686              # just tell the user to use it.
687
24
38
              my $hint = 0;
688
24
41
              my $noam = $target;
689
24
78
              $noam =~ s/-am$//;
690
24
92
              if (exists $dependencies{"$noam-am"})
691                {
692
10
27
                  $hint = "consider using $noam-local instead of $target";
693                }
694
695
24
153
              msg_cond_rule ('override', $cond, $target,
696                             "user target `$target' defined here"
697                             . "$condmsg ...", partial => 1);
698
24
118
              msg ('override', $where,
699                   "... overrides Automake target `$oldname' defined here",
700                   partial => $hint);
701
24
107
              msg_cond_rule ('override', $cond, $target, $hint)
702                if $hint;
703
704              # Don't overwrite the user definition of TARGET.
705
24
78
              return ();
706            }
707          else # $oldowner == RULE_AUTOMAKE
708            {
709              # Automake should ignore redefinitions of its own
710              # rules if they came from the same file. This makes
711              # it easier to process a Makefile fragment several times.
712              # However it's an error if the target is defined in many
713              # files. E.g., the user might be using bin_PROGRAMS = ctags
714              # which clashes with our `ctags' rule.
715              # (It would be more accurate if we had a way to compare
716              # the *content* of both rules. Then $targets_source would
717              # be useless.)
718
92
222
              my $oldsource = $tdef->source;
719
92
676
              return () if $source eq $oldsource && $target eq $oldname;
720
721
3
15
              msg ('syntax', $where, "redefinition of `$target'$condmsg ...",
722                   partial => 1);
723
3
20
              msg_cond_rule ('syntax', $cond, $target,
724                             "... `$oldname' previously defined here");
725
3
11
              return ();
726            }
727        }
728      # Never reached.
729
0
0
      prog_error ("unreachable place reached");
730    }
731
732  # Conditions for which the rule should be defined.
733
104227
147843
  my @conds = $cond;
734
735  # Check ambiguous conditional definitions.
736
104227
167791
  my $rule = _crule $target;
737
104227
260034
  my ($message, $ambig_cond) = $rule->conditions->ambiguous_p ($target, $cond);
738
104227
195624
  if ($message) # We have an ambiguity.
739    {
740
12
29
      if ($owner == RULE_USER)
741        {
742          # For user rules, just diagnose the ambiguity.
743
0
0
          msg 'syntax', $where, "$message ...", partial => 1;
744
0
0
          msg_cond_rule ('syntax', $ambig_cond, $target,
745                         "... `$target' previously defined here");
746
0
0
          return ();
747        }
748      else
749        {
750          # FIXME: for Automake rules, we can't diagnose ambiguities yet.
751          # The point is that Automake doesn't propagate conditions
752          # everywhere. For instance &handle_PROGRAMS doesn't care if
753          # bin_PROGRAMS was defined conditionally or not.
754          # On the following input
755          # if COND1
756          # foo:
757          # ...
758          # else
759          # bin_PROGRAMS = foo
760          # endif
761          # &handle_PROGRAMS will attempt to define a `foo:' rule
762          # in condition TRUE (which conflicts with COND1). Fixing
763          # this in &handle_PROGRAMS and siblings seems hard: you'd
764          # have to explain &file_contents what to do with a
765          # condition. So for now we do our best *here*. If `foo:'
766          # was already defined in condition COND1 and we want to define
767          # it in condition TRUE, then define it only in condition !COND1.
768          # (See cond14.test and cond15.test for some test cases.)
769
12
77
          @conds = $rule->not_always_defined_in_cond ($cond)->conds;
770
771          # No conditions left to define the rule.
772          # Warn, because our workaround is meaningless in this case.
773
12
46
          if (scalar @conds == 0)
774            {
775
4
21
              msg 'syntax', $where, "$message ...", partial => 1;
776
4
27
              msg_cond_rule ('syntax', $ambig_cond, $target,
777                             "... `$target' previously defined here");
778
4
20
              return ();
779            }
780        }
781    }
782
783  # Finally define this rule.
784
104223
147342
  for my $c (@conds)
785    {
786
104224
281109
      my $def = new Automake::RuleDef ($target, '', $where->clone,
787                                       $owner, $source);
788
104224
261871
      $rule->set ($c, $def);
789    }
790
791  # We honor inference rules with multiple targets because many
792  # make support this and people use it. However this is disallowed
793  # by POSIX. We'll print a warning later.
794
104223
129988
  my $target_count = 0;
795
104223
102186
  my $inference_rule_count = 0;
796
797
104223
220458
  for my $t (split (' ', $target))
798    {
799
105441
98377
      ++$target_count;
800      # Check if the rule is a suffix rule: either it's a rule for
801      # two known extensions...
802
105441
1335558
      if ($t =~ /^($KNOWN_EXTENSIONS_PATTERN)($KNOWN_EXTENSIONS_PATTERN)$/
803          # ...or it's a rule with unknown extensions (i.e., the rule
804          # looks like `.foo.bar:' but `.foo' or `.bar' are not
805          # declared in SUFFIXES and are not known language
806          # extensions). Automake will complete SUFFIXES from
807          # @suffixes automatically (see handle_footer).
808
809
810          || ($t =~ /$_SUFFIX_RULE_PATTERN/o && accept_extensions($1)))
811        {
812
1766
2290
          ++$inference_rule_count;
813
1766
5201
          register_suffix_rule ($where, $1, $2);
814        }
815    }
816
817  # POSIX allows multiple targets before the colon, but disallows
818  # definitions of multiple inference rules. It's also
819  # disallowed to mix plain targets with inference rules.
820
104223
298917
  msg ('portability', $where,
821       "inference rules can have only one target before the colon (POSIX)")
822    if $inference_rule_count > 0 && $target_count > 1;
823
824
104223
319832
  return @conds;
825}
826
827 - 833
=item C<depend ($target, @deps)>

Adds C<@deps> to the dependencies of target C<$target>.  This should
be used only with factored targets (those appearing in
C<%dependees>).

=cut
834
835sub depend ($@)
836{
837
97284
1
197675
  my ($category, @dependees) = @_;
838
97284
97284
91908
314363
  push (@{$dependencies{$category}}, @dependees);
839}
840
841=back
842
843 - 848
=head1 SEE ALSO

L<Automake::RuleDef>, L<Automake::Condition>,
L<Automake::DisjConditions>, L<Automake::Location>.

=cut
849
8501;
851
852### Setup "GNU" style for perl-mode and cperl-mode.
853## Local Variables:
854## perl-indent-level: 2
855## perl-continued-statement-offset: 2
856## perl-continued-brace-offset: 0
857## perl-brace-offset: 0
858## perl-brace-imaginary-offset: 0
859## perl-label-offset: -2
860## cperl-indent-level: 2
861## cperl-brace-offset: 0
862## cperl-continued-brace-offset: 0
863## cperl-label-offset: -2
864## cperl-extra-newline-before-brace: t
865## cperl-merge-trailing-else: nil
866## cperl-continued-statement-offset: 2
867## End: