2
0

Rule.pm 24 KB

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