2
0

Variable.pm 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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::Variable;
  13. use 5.006;
  14. use strict;
  15. use Carp;
  16. use Automake::Channels;
  17. use Automake::ChannelDefs;
  18. use Automake::Configure_ac;
  19. use Automake::Item;
  20. use Automake::VarDef;
  21. use Automake::Condition qw (TRUE FALSE);
  22. use Automake::DisjConditions;
  23. use Automake::General 'uniq';
  24. use Automake::Wrap 'makefile_wrap';
  25. require Exporter;
  26. use vars '@ISA', '@EXPORT', '@EXPORT_OK';
  27. @ISA = qw/Automake::Item Exporter/;
  28. @EXPORT = qw (err_var msg_var msg_cond_var reject_var
  29. var rvar vardef rvardef
  30. variables
  31. scan_variable_expansions check_variable_expansions
  32. variable_delete
  33. variables_dump
  34. set_seen
  35. require_variables
  36. variable_value
  37. output_variables
  38. transform_variable_recursively);
  39. =head1 NAME
  40. Automake::Variable - support for variable definitions
  41. =head1 SYNOPSIS
  42. use Automake::Variable;
  43. use Automake::VarDef;
  44. # Defining a variable.
  45. Automake::Variable::define($varname, $owner, $type,
  46. $cond, $value, $comment,
  47. $where, $pretty)
  48. # Looking up a variable.
  49. my $var = var $varname;
  50. if ($var)
  51. {
  52. ...
  53. }
  54. # Looking up a variable that is assumed to exist.
  55. my $var = rvar $varname;
  56. # The list of conditions where $var has been defined.
  57. # ($var->conditions is an Automake::DisjConditions,
  58. # $var->conditions->conds is a list of Automake::Condition.)
  59. my @conds = $var->conditions->conds
  60. # Access to the definition in Condition $cond.
  61. # $def is an Automake::VarDef.
  62. my $def = $var->def ($cond);
  63. if ($def)
  64. {
  65. ...
  66. }
  67. # When the conditional definition is assumed to exist, use
  68. my $def = $var->rdef ($cond);
  69. =head1 DESCRIPTION
  70. This package provides support for Makefile variable definitions.
  71. An C<Automake::Variable> is a variable name associated to possibly
  72. many conditional definitions. These definitions are instances
  73. of C<Automake::VarDef>.
  74. Therefore obtaining the value of a variable under a given
  75. condition involves two lookups. One to look up the variable,
  76. and one to look up the conditional definition:
  77. my $var = var $name;
  78. if ($var)
  79. {
  80. my $def = $var->def ($cond);
  81. if ($def)
  82. {
  83. return $def->value;
  84. }
  85. ...
  86. }
  87. ...
  88. When it is known that the variable and the definition
  89. being looked up exist, the above can be simplified to
  90. return var ($name)->def ($cond)->value; # Do not write this.
  91. but is better written
  92. return rvar ($name)->rdef ($cond)->value;
  93. or even
  94. return rvardef ($name, $cond)->value;
  95. The I<r> variants of the C<var>, C<def>, and C<vardef> methods add an
  96. extra test to ensure that the lookup succeeded, and will diagnose
  97. failures as internal errors (with a message which is much more
  98. informative than Perl's warning about calling a method on a
  99. non-object).
  100. =cut
  101. my $_VARIABLE_CHARACTERS = '[.A-Za-z0-9_@]+';
  102. my $_VARIABLE_PATTERN = '^' . $_VARIABLE_CHARACTERS . "\$";
  103. my $_VARIABLE_RECURSIVE_PATTERN =
  104. '^([.A-Za-z0-9_@]|\$[({]' . $_VARIABLE_CHARACTERS . '[})]?)+' . "\$";
  105. # The order in which variables should be output. (May contain
  106. # duplicates -- only the first occurrence matters.)
  107. my @_var_order;
  108. # This keeps track of all variables defined by &_gen_varname.
  109. # $_gen_varname{$base} is a hash for all variables defined with
  110. # prefix '$base'. Values stored in this hash are the variable names.
  111. # Keys have the form "(COND1)VAL1(COND2)VAL2..." where VAL1 and VAL2
  112. # are the values of the variable for condition COND1 and COND2.
  113. my %_gen_varname = ();
  114. # $_gen_varname_n{$base} is the number of variables generated by
  115. # _gen_varname() for $base. This is not the same as keys
  116. # %{$_gen_varname{$base}} because %_gen_varname may also contain
  117. # variables not generated by _gen_varname.
  118. my %_gen_varname_n = ();
  119. # Declare the macros that define known variables, so we can
  120. # hint the user if she try to use one of these variables.
  121. # Macros accessible via aclocal.
  122. my %_am_macro_for_var =
  123. (
  124. CCAS => 'AM_PROG_AS',
  125. CCASFLAGS => 'AM_PROG_AS',
  126. EMACS => 'AM_PATH_LISPDIR',
  127. GCJ => 'AM_PROG_GCJ',
  128. LEX => 'AM_PROG_LEX',
  129. LIBTOOL => 'LT_INIT',
  130. lispdir => 'AM_PATH_LISPDIR',
  131. pkgpyexecdir => 'AM_PATH_PYTHON',
  132. pkgpythondir => 'AM_PATH_PYTHON',
  133. pyexecdir => 'AM_PATH_PYTHON',
  134. PYTHON => 'AM_PATH_PYTHON',
  135. pythondir => 'AM_PATH_PYTHON',
  136. );
  137. # Macros shipped with Autoconf.
  138. my %_ac_macro_for_var =
  139. (
  140. ALLOCA => 'AC_FUNC_ALLOCA',
  141. CC => 'AC_PROG_CC',
  142. CFLAGS => 'AC_PROG_CC',
  143. CXX => 'AC_PROG_CXX',
  144. CXXFLAGS => 'AC_PROG_CXX',
  145. F77 => 'AC_PROG_F77',
  146. FFLAGS => 'AC_PROG_F77',
  147. FC => 'AC_PROG_FC',
  148. FCFLAGS => 'AC_PROG_FC',
  149. OBJC => 'AC_PROG_OBJC',
  150. OBJCFLAGS => 'AC_PROG_OBJC',
  151. OBJCXX => 'AC_PROG_OBJCXX',
  152. OBJCXXFLAGS => 'AC_PROG_OBJCXX',
  153. RANLIB => 'AC_PROG_RANLIB',
  154. UPC => 'AM_PROG_UPC',
  155. UPCFLAGS => 'AM_PROG_UPC',
  156. YACC => 'AC_PROG_YACC',
  157. );
  158. # The name of the configure.ac file.
  159. my $configure_ac;
  160. # Variables that can be overridden without complaint from -Woverride
  161. my %_silent_variable_override =
  162. (AM_MAKEINFOHTMLFLAGS => 1,
  163. AR => 1,
  164. ARFLAGS => 1,
  165. DEJATOOL => 1,
  166. JAVAC => 1,
  167. JAVAROOT => 1);
  168. # Count of helper variables used to implement conditional '+='.
  169. my $_appendvar;
  170. # Each call to C<Automake::Variable::traverse_recursively> gets an
  171. # unique label. This is used to detect recursively defined variables.
  172. my $_traversal = 0;
  173. =head2 Error reporting functions
  174. In these functions, C<$var> can be either a variable name, or
  175. an instance of C<Automake::Variable>.
  176. =over 4
  177. =item C<err_var ($var, $message, [%options])>
  178. Uncategorized errors about variables.
  179. =cut
  180. sub err_var ($$;%)
  181. {
  182. msg_var ('error', @_);
  183. }
  184. =item C<msg_cond_var ($channel, $cond, $var, $message, [%options])>
  185. Messages about conditional variable.
  186. =cut
  187. sub msg_cond_var ($$$$;%)
  188. {
  189. my ($channel, $cond, $var, $msg, %opts) = @_;
  190. my $v = ref ($var) ? $var : rvar ($var);
  191. msg $channel, $v->rdef ($cond)->location, $msg, %opts;
  192. }
  193. =item C<msg_var ($channel, $var, $message, [%options])>
  194. Messages about variables.
  195. =cut
  196. sub msg_var ($$$;%)
  197. {
  198. my ($channel, $var, $msg, %opts) = @_;
  199. my $v = ref ($var) ? $var : rvar ($var);
  200. # Don't know which condition is concerned. Pick any.
  201. my $cond = $v->conditions->one_cond;
  202. msg_cond_var $channel, $cond, $v, $msg, %opts;
  203. }
  204. =item C<$bool = reject_var ($varname, $error_msg)>
  205. Bail out with C<$error_msg> if a variable with name C<$varname> has
  206. been defined.
  207. Return true iff C<$varname> is defined.
  208. =cut
  209. sub reject_var ($$)
  210. {
  211. my ($var, $msg) = @_;
  212. my $v = var ($var);
  213. if ($v)
  214. {
  215. err_var $v, $msg;
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. =back
  221. =head2 Administrative functions
  222. =over 4
  223. =item C<Automake::Variable::hook ($varname, $fun)>
  224. Declare a function to be called whenever a variable
  225. named C<$varname> is defined or redefined.
  226. C<$fun> should take two arguments: C<$type> and C<$value>.
  227. When type is C<''> or <':'>, C<$value> is the value being
  228. assigned to C<$varname>. When C<$type> is C<'+'>, C<$value>
  229. is the value being appended to C<$varname>.
  230. =cut
  231. use vars '%_hooks';
  232. sub hook ($$)
  233. {
  234. my ($var, $fun) = @_;
  235. $_hooks{$var} = $fun;
  236. }
  237. =item C<variables ([$suffix])>
  238. Returns the list of all L<Automake::Variable> instances. (I.e., all
  239. variables defined so far.) If C<$suffix> is supplied, return only
  240. the L<Automake::Variable> instances that ends with C<_$suffix>.
  241. =cut
  242. use vars '%_variable_dict', '%_primary_dict';
  243. sub variables (;$)
  244. {
  245. my ($suffix) = @_;
  246. my @vars = ();
  247. if ($suffix)
  248. {
  249. if (exists $_primary_dict{$suffix})
  250. {
  251. @vars = values %{$_primary_dict{$suffix}};
  252. }
  253. }
  254. else
  255. {
  256. @vars = values %_variable_dict;
  257. }
  258. # The behaviour of the 'sort' built-in is undefined in scalar
  259. # context, hence we need an ad-hoc handling for such context.
  260. return wantarray ? sort { $a->name cmp $b->name } @vars : scalar @vars;
  261. }
  262. =item C<Automake::Variable::reset>
  263. The I<forget all> function. Clears all know variables and reset some
  264. other internal data.
  265. =cut
  266. sub reset ()
  267. {
  268. %_variable_dict = ();
  269. %_primary_dict = ();
  270. $_appendvar = 0;
  271. @_var_order = ();
  272. %_gen_varname = ();
  273. %_gen_varname_n = ();
  274. $_traversal = 0;
  275. }
  276. =item C<var ($varname)>
  277. Return the C<Automake::Variable> object for the variable
  278. named C<$varname> if defined. Return 0 otherwise.
  279. =cut
  280. sub var ($)
  281. {
  282. my ($name) = @_;
  283. return $_variable_dict{$name} if exists $_variable_dict{$name};
  284. return 0;
  285. }
  286. =item C<vardef ($varname, $cond)>
  287. Return the C<Automake::VarDef> object for the variable named
  288. C<$varname> if defined in condition C<$cond>. Return false
  289. if the condition or the variable does not exist.
  290. =cut
  291. sub vardef ($$)
  292. {
  293. my ($name, $cond) = @_;
  294. my $var = var $name;
  295. return $var && $var->def ($cond);
  296. }
  297. # Create the variable if it does not exist.
  298. # This is used only by other functions in this package.
  299. sub _cvar ($)
  300. {
  301. my ($name) = @_;
  302. my $v = var $name;
  303. return $v if $v;
  304. return _new Automake::Variable $name;
  305. }
  306. =item C<rvar ($varname)>
  307. Return the C<Automake::Variable> object for the variable named
  308. C<$varname>. Abort with an internal error if the variable was not
  309. defined.
  310. The I<r> in front of C<var> stands for I<required>. One
  311. should call C<rvar> to assert the variable's existence.
  312. =cut
  313. sub rvar ($)
  314. {
  315. my ($name) = @_;
  316. my $v = var $name;
  317. prog_error ("undefined variable $name\n" . &variables_dump)
  318. unless $v;
  319. return $v;
  320. }
  321. =item C<rvardef ($varname, $cond)>
  322. Return the C<Automake::VarDef> object for the variable named
  323. C<$varname> if defined in condition C<$cond>. Abort with an internal
  324. error if the condition or the variable does not exist.
  325. =cut
  326. sub rvardef ($$)
  327. {
  328. my ($name, $cond) = @_;
  329. return rvar ($name)->rdef ($cond);
  330. }
  331. =back
  332. =head2 Methods
  333. C<Automake::Variable> is a subclass of C<Automake::Item>. See
  334. that package for inherited methods.
  335. Here are the methods specific to the C<Automake::Variable> instances.
  336. Use the C<define> function, described latter, to create such objects.
  337. =over 4
  338. =cut
  339. # Create Automake::Variable objects. This is used
  340. # only in this file. Other users should use
  341. # the "define" function.
  342. sub _new ($$)
  343. {
  344. my ($class, $name) = @_;
  345. my $self = Automake::Item::new ($class, $name);
  346. $self->{'scanned'} = 0;
  347. $self->{'last-append'} = []; # helper variable for last conditional append.
  348. $_variable_dict{$name} = $self;
  349. if ($name =~ /_([[:alnum:]]+)$/)
  350. {
  351. $_primary_dict{$1}{$name} = $self;
  352. }
  353. return $self;
  354. }
  355. # _check_ambiguous_condition ($SELF, $COND, $WHERE)
  356. # -------------------------------------------------
  357. # Check for an ambiguous conditional. This is called when a variable
  358. # is being defined conditionally. If we already know about a
  359. # definition that is true under the same conditions, then we have an
  360. # ambiguity.
  361. sub _check_ambiguous_condition ($$$)
  362. {
  363. my ($self, $cond, $where) = @_;
  364. my $var = $self->name;
  365. my ($message, $ambig_cond) = $self->conditions->ambiguous_p ($var, $cond);
  366. # We allow silent variables to be overridden silently,
  367. # by either silent or non-silent variables.
  368. my $def = $self->def ($ambig_cond);
  369. if ($message && $def->pretty != VAR_SILENT)
  370. {
  371. msg 'syntax', $where, "$message ...", partial => 1;
  372. msg_var ('syntax', $var, "... '$var' previously defined here");
  373. verb ($self->dump);
  374. }
  375. }
  376. =item C<$bool = $var-E<gt>check_defined_unconditionally ([$parent, $parent_cond])>
  377. Warn if the variable is conditionally defined. C<$parent> is the name
  378. of the parent variable, and C<$parent_cond> the condition of the parent
  379. definition. These two variables are used to display diagnostics.
  380. =cut
  381. sub check_defined_unconditionally ($;$$)
  382. {
  383. my ($self, $parent, $parent_cond) = @_;
  384. if (!$self->conditions->true)
  385. {
  386. if ($parent)
  387. {
  388. msg_cond_var ('unsupported', $parent_cond, $parent,
  389. "automake does not support conditional definition of "
  390. . $self->name . " in $parent");
  391. }
  392. else
  393. {
  394. msg_var ('unsupported', $self,
  395. "automake does not support " . $self->name
  396. . " being defined conditionally");
  397. }
  398. }
  399. }
  400. =item C<$str = $var-E<gt>output ([@conds])>
  401. Format all the definitions of C<$var> if C<@cond> is not specified,
  402. else only that corresponding to C<@cond>.
  403. =cut
  404. sub output ($@)
  405. {
  406. my ($self, @conds) = @_;
  407. @conds = $self->conditions->conds
  408. unless @conds;
  409. my $res = '';
  410. my $name = $self->name;
  411. foreach my $cond (@conds)
  412. {
  413. my $def = $self->def ($cond);
  414. prog_error ("unknown condition '" . $cond->human . "' for '"
  415. . $self->name . "'")
  416. unless $def;
  417. next
  418. if $def->pretty == VAR_SILENT;
  419. $res .= $def->comment;
  420. my $val = $def->raw_value;
  421. my $equals = $def->type eq ':' ? ':=' : '=';
  422. my $str = $cond->subst_string;
  423. if ($def->pretty == VAR_ASIS)
  424. {
  425. my $output_var = "$name $equals $val";
  426. $output_var =~ s/^/$str/meg;
  427. $res .= "$output_var\n";
  428. }
  429. elsif ($def->pretty == VAR_PRETTY)
  430. {
  431. # Suppress escaped new lines. &makefile_wrap will
  432. # add them back, maybe at other places.
  433. $val =~ s/\\$//mg;
  434. my $wrap = makefile_wrap ("$str$name $equals", "$str\t",
  435. split (' ', $val));
  436. # If the last line of the definition is made only of
  437. # @substitutions@, append an empty variable to make sure it
  438. # cannot be substituted as a blank line (that would confuse
  439. # HP-UX Make).
  440. $wrap = makefile_wrap ("$str$name $equals", "$str\t",
  441. split (' ', $val), '$(am__empty)')
  442. if $wrap =~ /\n(\s*@\w+@)+\s*$/;
  443. $res .= $wrap;
  444. }
  445. else # ($def->pretty == VAR_SORTED)
  446. {
  447. # Suppress escaped new lines. &makefile_wrap will
  448. # add them back, maybe at other places.
  449. $val =~ s/\\$//mg;
  450. $res .= makefile_wrap ("$str$name $equals", "$str\t",
  451. sort (split (' ' , $val)));
  452. }
  453. }
  454. return $res;
  455. }
  456. =item C<@values = $var-E<gt>value_as_list ($cond, [$parent, $parent_cond])>
  457. Get the value of C<$var> as a list, given a specified condition,
  458. without recursing through any subvariables.
  459. C<$cond> is the condition of interest. C<$var> does not need
  460. to be defined for condition C<$cond> exactly, but it needs
  461. to be defined for at most one condition implied by C<$cond>.
  462. C<$parent> and C<$parent_cond> designate the name and the condition
  463. of the parent variable, i.e., the variable in which C<$var> is
  464. being expanded. These are used in diagnostics.
  465. For example, if C<A> is defined as "C<foo $(B) bar>" in condition
  466. C<TRUE>, calling C<rvar ('A')->value_as_list (TRUE)> will return
  467. C<("foo", "$(B)", "bar")>.
  468. =cut
  469. sub value_as_list ($$;$$)
  470. {
  471. my ($self, $cond, $parent, $parent_cond) = @_;
  472. my @result;
  473. # Get value for given condition
  474. my $onceflag;
  475. foreach my $vcond ($self->conditions->conds)
  476. {
  477. if ($vcond->true_when ($cond))
  478. {
  479. # If there is more than one definitions of $var matching
  480. # $cond then we are in trouble: tell the user we need a
  481. # paddle. Continue by merging results from all conditions,
  482. # although it doesn't make much sense.
  483. $self->check_defined_unconditionally ($parent, $parent_cond)
  484. if $onceflag;
  485. $onceflag = 1;
  486. my $val = $self->rdef ($vcond)->value;
  487. push @result, split (' ', $val);
  488. }
  489. }
  490. return @result;
  491. }
  492. =item C<@values = $var-E<gt>value_as_list_recursive ([%options])>
  493. Return the contents of C<$var> as a list, split on whitespace. This
  494. will recursively follow C<$(...)> and C<${...}> inclusions. It
  495. preserves C<@...@> substitutions.
  496. C<%options> is a list of option for C<Variable::traverse_recursively>
  497. (see this method). The most useful is C<cond_filter>:
  498. $var->value_as_list_recursive (cond_filter => $cond)
  499. will return the contents of C<$var> and any subvariable in all
  500. conditions implied by C<$cond>.
  501. C<%options> can also carry options specific to C<value_as_list_recursive>.
  502. Presently, the only such option is C<location =E<gt> 1> which instructs
  503. C<value_as_list_recursive> to return a list of C<[$location, @values]> pairs.
  504. =cut
  505. sub value_as_list_recursive ($;%)
  506. {
  507. my ($var, %options) = @_;
  508. return $var->traverse_recursively
  509. (# Construct [$location, $value] pairs if requested.
  510. sub {
  511. my ($var, $val, $cond, $full_cond) = @_;
  512. return [$var->rdef ($cond)->location, $val] if $options{'location'};
  513. return $val;
  514. },
  515. # Collect results.
  516. sub {
  517. my ($var, $parent_cond, @allresults) = @_;
  518. return map { my ($cond, @vals) = @$_; @vals } @allresults;
  519. },
  520. %options);
  521. }
  522. =item C<$bool = $var-E<gt>has_conditional_contents>
  523. Return 1 if C<$var> or one of its subvariable was conditionally
  524. defined. Return 0 otherwise.
  525. =cut
  526. sub has_conditional_contents ($)
  527. {
  528. my ($self) = @_;
  529. # Traverse the variable recursively until we
  530. # find a variable defined conditionally.
  531. # Use 'die' to abort the traversal, and pass it '$full_cond'
  532. # to we can find easily whether the 'eval' block aborted
  533. # because we found a condition, or for some other error.
  534. eval
  535. {
  536. $self->traverse_recursively
  537. (sub
  538. {
  539. my ($subvar, $val, $cond, $full_cond) = @_;
  540. die $full_cond if ! $full_cond->true;
  541. return ();
  542. },
  543. sub { return (); });
  544. };
  545. if ($@)
  546. {
  547. return 1 if ref ($@) && $@->isa ("Automake::Condition");
  548. # Propagate other errors.
  549. die;
  550. }
  551. return 0;
  552. }
  553. =item C<$string = $var-E<gt>dump>
  554. Return a string describing all we know about C<$var>.
  555. For debugging.
  556. =cut
  557. sub dump ($)
  558. {
  559. my ($self) = @_;
  560. my $text = $self->name . ": \n {\n";
  561. foreach my $vcond ($self->conditions->conds)
  562. {
  563. $text .= " " . $vcond->human . " => " . $self->rdef ($vcond)->dump;
  564. }
  565. $text .= " }\n";
  566. return $text;
  567. }
  568. =back
  569. =head2 Utility functions
  570. =over 4
  571. =item C<@list = scan_variable_expansions ($text)>
  572. Return the list of variable names expanded in C<$text>. Note that
  573. unlike some other functions, C<$text> is not split on spaces before we
  574. check for subvariables.
  575. =cut
  576. sub scan_variable_expansions ($)
  577. {
  578. my ($text) = @_;
  579. my @result = ();
  580. # Strip comments.
  581. $text =~ s/#.*$//;
  582. # Record each use of ${stuff} or $(stuff) that does not follow a $.
  583. while ($text =~ /(?<!\$)\$(?:\{([^\}]*)\}|\(([^\)]*)\))/g)
  584. {
  585. my $var = $1 || $2;
  586. # The occurrence may look like $(string1[:subst1=[subst2]]) but
  587. # we want only 'string1'.
  588. $var =~ s/:[^:=]*=[^=]*$//;
  589. push @result, $var;
  590. }
  591. return @result;
  592. }
  593. =item C<check_variable_expansions ($text, $where)>
  594. Check variable expansions in C<$text> and warn about any name that
  595. does not conform to POSIX. C<$where> is the location of C<$text>
  596. for the error message.
  597. =cut
  598. sub check_variable_expansions ($$)
  599. {
  600. my ($text, $where) = @_;
  601. # Catch expansion of variables whose name does not conform to POSIX.
  602. foreach my $var (scan_variable_expansions ($text))
  603. {
  604. if ($var !~ /$_VARIABLE_PATTERN/o)
  605. {
  606. # If the variable name contains a space, it's likely
  607. # to be a GNU make extension (such as $(addsuffix ...)).
  608. # Mention this in the diagnostic.
  609. my $gnuext = "";
  610. $gnuext = "\n(probably a GNU make extension)" if $var =~ / /;
  611. # Accept recursive variable expansions if so desired
  612. # (we hope they are rather portable in practice).
  613. if ($var =~ /$_VARIABLE_RECURSIVE_PATTERN/o)
  614. {
  615. msg ('portability-recursive', $where,
  616. "$var: non-POSIX recursive variable expansion$gnuext");
  617. }
  618. else
  619. {
  620. msg ('portability', $where, "$var: non-POSIX variable name$gnuext");
  621. }
  622. }
  623. }
  624. }
  625. =item C<Automake::Variable::define($varname, $owner, $type, $cond, $value, $comment, $where, $pretty)>
  626. Define or append to a new variable.
  627. C<$varname>: the name of the variable being defined.
  628. C<$owner>: owner of the variable (one of C<VAR_MAKEFILE>,
  629. C<VAR_CONFIGURE>, or C<VAR_AUTOMAKE>, defined by L<Automake::VarDef>).
  630. Variables can be overridden, provided the new owner is not weaker
  631. (C<VAR_AUTOMAKE> < C<VAR_CONFIGURE> < C<VAR_MAKEFILE>).
  632. C<$type>: the type of the assignment (C<''> for C<FOO = bar>,
  633. C<':'> for C<FOO := bar>, and C<'+'> for C<'FOO += bar'>).
  634. C<$cond>: the C<Condition> in which C<$var> is being defined.
  635. C<$value>: the value assigned to C<$var> in condition C<$cond>.
  636. C<$comment>: any comment (C<'# bla.'>) associated with the assignment.
  637. Comments from C<+=> assignments stack with comments from the last C<=>
  638. assignment.
  639. C<$where>: the C<Location> of the assignment.
  640. C<$pretty>: whether C<$value> should be pretty printed (one of
  641. C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, or C<VAR_SORTED>, defined
  642. by by L<Automake::VarDef>). C<$pretty> applies only to real
  643. assignments. I.e., it does not apply to a C<+=> assignment (except
  644. when part of it is being done as a conditional C<=> assignment).
  645. This function will all run any hook registered with the C<hook>
  646. function.
  647. =cut
  648. sub define ($$$$$$$$)
  649. {
  650. my ($var, $owner, $type, $cond, $value, $comment, $where, $pretty) = @_;
  651. prog_error "$cond is not a reference"
  652. unless ref $cond;
  653. prog_error "$where is not a reference"
  654. unless ref $where;
  655. prog_error "pretty argument missing"
  656. unless defined $pretty && ($pretty == VAR_ASIS
  657. || $pretty == VAR_PRETTY
  658. || $pretty == VAR_SILENT
  659. || $pretty == VAR_SORTED);
  660. error $where, "bad characters in variable name '$var'"
  661. if $var !~ /$_VARIABLE_PATTERN/o;
  662. # ':='-style assignments are not acknowledged by POSIX. Moreover it
  663. # has multiple meanings. In GNU make or BSD make it means "assign
  664. # with immediate expansion", while in OSF make it is used for
  665. # conditional assignments.
  666. msg ('portability', $where, "':='-style assignments are not portable")
  667. if $type eq ':';
  668. check_variable_expansions ($value, $where);
  669. # If there's a comment, make sure it is \n-terminated.
  670. if ($comment)
  671. {
  672. chomp $comment;
  673. $comment .= "\n";
  674. }
  675. else
  676. {
  677. $comment = '';
  678. }
  679. my $self = _cvar $var;
  680. my $def = $self->def ($cond);
  681. my $new_var = $def ? 0 : 1;
  682. # Additional checks for Automake definitions.
  683. if ($owner == VAR_AUTOMAKE && ! $new_var)
  684. {
  685. # An Automake variable must be consistently defined with the same
  686. # sign by Automake.
  687. if ($def->type ne $type && $def->owner == VAR_AUTOMAKE)
  688. {
  689. error ($def->location,
  690. "Automake variable '$var' was set with '"
  691. . $def->type . "=' here ...", partial => 1);
  692. error ($where, "... and is now set with '$type=' here.");
  693. prog_error ("Automake variable assignments should be consistently\n"
  694. . "defined with the same sign");
  695. }
  696. # If Automake tries to override a value specified by the user,
  697. # just don't let it do.
  698. if ($def->owner != VAR_AUTOMAKE)
  699. {
  700. if (! exists $_silent_variable_override{$var})
  701. {
  702. my $condmsg = ($cond == TRUE
  703. ? '' : (" in condition '" . $cond->human . "'"));
  704. msg_cond_var ('override', $cond, $var,
  705. "user variable '$var' defined here$condmsg ...",
  706. partial => 1);
  707. msg ('override', $where,
  708. "... overrides Automake variable '$var' defined here");
  709. }
  710. verb ("refusing to override the user definition of:\n"
  711. . $self->dump ."with '" . $cond->human . "' => '$value'");
  712. return;
  713. }
  714. }
  715. # Differentiate assignment types.
  716. # 1. append (+=) to a variable defined for current condition
  717. if ($type eq '+' && ! $new_var)
  718. {
  719. $def->append ($value, $comment);
  720. $self->{'last-append'} = [];
  721. # Only increase owners. A VAR_CONFIGURE variable augmented in a
  722. # Makefile.am becomes a VAR_MAKEFILE variable.
  723. $def->set_owner ($owner, $where->clone)
  724. if $owner > $def->owner;
  725. }
  726. # 2. append (+=) to a variable defined for *another* condition
  727. elsif ($type eq '+' && ! $self->conditions->false)
  728. {
  729. # * Generally, $cond is not TRUE. For instance:
  730. # FOO = foo
  731. # if COND
  732. # FOO += bar
  733. # endif
  734. # In this case, we declare an helper variable conditionally,
  735. # and append it to FOO:
  736. # FOO = foo $(am__append_1)
  737. # @COND_TRUE@am__append_1 = bar
  738. # Of course if FOO is defined under several conditions, we add
  739. # $(am__append_1) to each definitions.
  740. #
  741. # * If $cond is TRUE, we don't need the helper variable. E.g., in
  742. # if COND1
  743. # FOO = foo1
  744. # else
  745. # FOO = foo2
  746. # endif
  747. # FOO += bar
  748. # we can add bar directly to all definition of FOO, and output
  749. # @COND_TRUE@FOO = foo1 bar
  750. # @COND_FALSE@FOO = foo2 bar
  751. my $lastappend = [];
  752. # Do we need an helper variable?
  753. if ($cond != TRUE)
  754. {
  755. # Can we reuse the helper variable created for the previous
  756. # append? (We cannot reuse older helper variables because
  757. # we must preserve the order of items appended to the
  758. # variable.)
  759. my $condstr = $cond->string;
  760. my $key = "$var:$condstr";
  761. my ($appendvar, $appendvarcond) = @{$self->{'last-append'}};
  762. if ($appendvar && $condstr eq $appendvarcond)
  763. {
  764. # Yes, let's simply append to it.
  765. $var = $appendvar;
  766. $owner = VAR_AUTOMAKE;
  767. $self = var ($var);
  768. $def = $self->rdef ($cond);
  769. $new_var = 0;
  770. }
  771. else
  772. {
  773. # No, create it.
  774. my $num = ++$_appendvar;
  775. my $hvar = "am__append_$num";
  776. $lastappend = [$hvar, $condstr];
  777. &define ($hvar, VAR_AUTOMAKE, '+',
  778. $cond, $value, $comment, $where, $pretty);
  779. # Now HVAR is to be added to VAR.
  780. $comment = '';
  781. $value = "\$($hvar)";
  782. }
  783. }
  784. # Add VALUE to all definitions of SELF.
  785. foreach my $vcond ($self->conditions->conds)
  786. {
  787. # We have a bit of error detection to do here.
  788. # This:
  789. # if COND1
  790. # X = Y
  791. # endif
  792. # X += Z
  793. # should be rejected because X is not defined for all conditions
  794. # where '+=' applies.
  795. my $undef_cond = $self->not_always_defined_in_cond ($cond);
  796. if (! $undef_cond->false)
  797. {
  798. error ($where,
  799. "cannot apply '+=' because '$var' is not defined "
  800. . "in\nthe following conditions:\n "
  801. . join ("\n ", map { $_->human } $undef_cond->conds)
  802. . "\neither define '$var' in these conditions,"
  803. . " or use\n'+=' in the same conditions as"
  804. . " the definitions.");
  805. }
  806. else
  807. {
  808. &define ($var, $owner, '+', $vcond, $value, $comment,
  809. $where, $pretty);
  810. }
  811. }
  812. $self->{'last-append'} = $lastappend;
  813. }
  814. # 3. first assignment (=, :=, or +=)
  815. else
  816. {
  817. # There must be no previous value unless the user is redefining
  818. # an Automake variable or an AC_SUBST variable for an existing
  819. # condition.
  820. _check_ambiguous_condition ($self, $cond, $where)
  821. unless (!$new_var
  822. && (($def->owner == VAR_AUTOMAKE && $owner != VAR_AUTOMAKE)
  823. || $def->owner == VAR_CONFIGURE));
  824. # Never decrease an owner.
  825. $owner = $def->owner
  826. if ! $new_var && $owner < $def->owner;
  827. # Assignments to a macro set its location. We don't adjust
  828. # locations for '+='. Ideally I suppose we would associate
  829. # line numbers with random bits of text.
  830. $def = new Automake::VarDef ($var, $value, $comment, $where->clone,
  831. $type, $owner, $pretty);
  832. $self->set ($cond, $def);
  833. push @_var_order, $var;
  834. }
  835. # Call any defined hook. This helps to update some internal state
  836. # *while* parsing the file. For instance the handling of SUFFIXES
  837. # requires this (see var_SUFFIXES_trigger).
  838. &{$_hooks{$var}}($type, $value) if exists $_hooks{$var};
  839. }
  840. =item C<variable_delete ($varname, [@conds])>
  841. Forget about C<$varname> under the conditions C<@conds>, or completely
  842. if C<@conds> is empty.
  843. =cut
  844. sub variable_delete ($@)
  845. {
  846. my ($var, @conds) = @_;
  847. if (!@conds)
  848. {
  849. delete $_variable_dict{$var};
  850. }
  851. else
  852. {
  853. for my $cond (@conds)
  854. {
  855. delete $_variable_dict{$var}{'defs'}{$cond};
  856. }
  857. }
  858. if ($var =~ /_([[:alnum:]]+)$/)
  859. {
  860. delete $_primary_dict{$1}{$var};
  861. }
  862. }
  863. =item C<$str = variables_dump>
  864. Return a string describing all we know about all variables.
  865. For debugging.
  866. =cut
  867. sub variables_dump ()
  868. {
  869. my $text = "all variables:\n{\n";
  870. foreach my $var (variables())
  871. {
  872. $text .= $var->dump;
  873. }
  874. $text .= "}\n";
  875. return $text;
  876. }
  877. =item C<$var = set_seen ($varname)>
  878. =item C<$var = $var-E<gt>set_seen>
  879. Mark all definitions of this variable as examined, if the variable
  880. exists. See L<Automake::VarDef::set_seen>.
  881. Return the C<Variable> object if the variable exists, or 0
  882. otherwise (i.e., as the C<var> function).
  883. =cut
  884. sub set_seen ($)
  885. {
  886. my ($self) = @_;
  887. $self = ref $self ? $self : var $self;
  888. return 0 unless $self;
  889. for my $c ($self->conditions->conds)
  890. {
  891. $self->rdef ($c)->set_seen;
  892. }
  893. return $self;
  894. }
  895. =item C<$count = require_variables ($where, $reason, $cond, @variables)>
  896. Make sure that each supplied variable is defined in C<$cond>.
  897. Otherwise, issue a warning showing C<$reason> (C<$reason> should be
  898. the reason why these variables are required, for instance C<'option foo
  899. used'>). If we know which macro can define this variable, hint the
  900. user. Return the number of undefined variables.
  901. =cut
  902. sub require_variables ($$$@)
  903. {
  904. my ($where, $reason, $cond, @vars) = @_;
  905. my $res = 0;
  906. $reason .= ' but ' unless $reason eq '';
  907. $configure_ac = find_configure_ac
  908. unless defined $configure_ac;
  909. VARIABLE:
  910. foreach my $var (@vars)
  911. {
  912. # Nothing to do if the variable exists.
  913. next VARIABLE
  914. if vardef ($var, $cond);
  915. my $text = "$reason'$var' is undefined\n";
  916. my $v = var $var;
  917. if ($v)
  918. {
  919. my $undef_cond = $v->not_always_defined_in_cond ($cond);
  920. next VARIABLE
  921. if $undef_cond->false;
  922. $text .= ("in the following conditions:\n "
  923. . join ("\n ", map { $_->human } $undef_cond->conds)
  924. . "\n");
  925. }
  926. ++$res;
  927. if (exists $_am_macro_for_var{$var})
  928. {
  929. my $mac = $_am_macro_for_var{$var};
  930. $text .= " The usual way to define '$var' is to add "
  931. . "'$mac'\n to '$configure_ac' and run 'aclocal' and "
  932. . "'autoconf' again.";
  933. # aclocal will not warn about undefined macros unless it
  934. # starts with AM_.
  935. $text .= "\n If '$mac' is in '$configure_ac', make sure\n"
  936. . " its definition is in aclocal's search path."
  937. unless $mac =~ /^AM_/;
  938. }
  939. elsif (exists $_ac_macro_for_var{$var})
  940. {
  941. $text .= " The usual way to define '$var' is to add "
  942. . "'$_ac_macro_for_var{$var}'\n to '$configure_ac' and "
  943. . "run 'autoconf' again.";
  944. }
  945. error $where, $text, uniq_scope => US_GLOBAL;
  946. }
  947. return $res;
  948. }
  949. =item C<$count = $var->requires_variables ($reason, @variables)>
  950. Same as C<require_variables>, but a method of Automake::Variable.
  951. C<@variables> should be defined in the same conditions as C<$var> is
  952. defined.
  953. =cut
  954. sub requires_variables ($$@)
  955. {
  956. my ($var, $reason, @args) = @_;
  957. my $res = 0;
  958. for my $cond ($var->conditions->conds)
  959. {
  960. $res += require_variables ($var->rdef ($cond)->location, $reason,
  961. $cond, @args);
  962. }
  963. return $res;
  964. }
  965. =item C<variable_value ($var)>
  966. Get the C<TRUE> value of a variable, warn if the variable is
  967. conditionally defined. C<$var> can be either a variable name
  968. or a C<Automake::Variable> instance (this allows calls such
  969. as C<$var-E<gt>variable_value>).
  970. =cut
  971. sub variable_value ($)
  972. {
  973. my ($var) = @_;
  974. my $v = ref ($var) ? $var : var ($var);
  975. return () unless $v;
  976. $v->check_defined_unconditionally;
  977. my $d = $v->def (TRUE);
  978. return $d ? $d->value : "";
  979. }
  980. =item C<$str = output_variables>
  981. Format definitions for all variables.
  982. =cut
  983. sub output_variables ()
  984. {
  985. my $res = '';
  986. # We output variables it in the same order in which they were
  987. # defined (skipping duplicates).
  988. my @vars = uniq @_var_order;
  989. # Output all the Automake variables. If the user changed one,
  990. # then it is now marked as VAR_CONFIGURE or VAR_MAKEFILE.
  991. foreach my $var (@vars)
  992. {
  993. my $v = rvar $var;
  994. foreach my $cond ($v->conditions->conds)
  995. {
  996. $res .= $v->output ($cond)
  997. if $v->rdef ($cond)->owner == VAR_AUTOMAKE;
  998. }
  999. }
  1000. # Now dump the user variables that were defined.
  1001. foreach my $var (@vars)
  1002. {
  1003. my $v = rvar $var;
  1004. foreach my $cond ($v->conditions->conds)
  1005. {
  1006. $res .= $v->output ($cond)
  1007. if $v->rdef ($cond)->owner != VAR_AUTOMAKE;
  1008. }
  1009. }
  1010. return $res;
  1011. }
  1012. =item C<$var-E<gt>traverse_recursively (&fun_item, &fun_collect, [cond_filter =E<gt> $cond_filter], [inner_expand =E<gt> 1], [skip_ac_subst =E<gt> 1])>
  1013. Split the value of the Automake::Variable C<$var> on space, and
  1014. traverse its components recursively.
  1015. If C<$cond_filter> is an C<Automake::Condition>, process any
  1016. conditions which are true when C<$cond_filter> is true. Otherwise,
  1017. process all conditions.
  1018. We distinguish two kinds of items in the content of C<$var>.
  1019. Terms that look like C<$(foo)> or C<${foo}> are subvariables
  1020. and cause recursion. Other terms are assumed to be filenames.
  1021. Each time a filename is encountered, C<&fun_item> is called with the
  1022. following arguments:
  1023. ($var, -- the Automake::Variable we are currently
  1024. traversing
  1025. $val, -- the item (i.e., filename) to process
  1026. $cond, -- the Condition for the $var definition we are
  1027. examining (ignoring the recursion context)
  1028. $full_cond) -- the full Condition, taking into account
  1029. conditions inherited from parent variables
  1030. during recursion
  1031. If C<inner_expand> is set, variable references occurring in filename
  1032. (as in C<$(BASE).ext>) are expanded before the filename is passed to
  1033. C<&fun_item>.
  1034. If C<skip_ac_subst> is set, Autoconf @substitutions@ will be skipped,
  1035. i.e., C<&fun_item> will never be called for them.
  1036. C<&fun_item> may return a list of items, they will be passed to
  1037. C<&fun_store> later on. Define C<&fun_item> or @<&fun_store> as
  1038. C<undef> when they serve no purpose.
  1039. Once all items of a variable have been processed, the result (of the
  1040. calls to C<&fun_items>, or of recursive traversals of subvariables)
  1041. are passed to C<&fun_collect>. C<&fun_collect> receives three
  1042. arguments:
  1043. ($var, -- the variable being traversed
  1044. $parent_cond, -- the Condition inherited from parent
  1045. variables during recursion
  1046. @condlist) -- a list of [$cond, @results] pairs
  1047. where each $cond appear only once, and @result
  1048. are all the results for this condition.
  1049. Typically you should do C<$cond->merge ($parent_cond)> to recompute
  1050. the C<$full_cond> associated to C<@result>. C<&fun_collect> may
  1051. return a list of items, that will be used as the result of
  1052. C<Automake::Variable::traverse_recursively> (the top-level, or its
  1053. recursive calls).
  1054. =cut
  1055. # Contains a stack of 'from' and 'to' parts of variable
  1056. # substitutions currently in force.
  1057. my @_substfroms;
  1058. my @_substtos;
  1059. sub traverse_recursively ($&&;%)
  1060. {
  1061. ++$_traversal;
  1062. @_substfroms = ();
  1063. @_substtos = ();
  1064. my ($var, $fun_item, $fun_collect, %options) = @_;
  1065. my $cond_filter = $options{'cond_filter'};
  1066. my $inner_expand = $options{'inner_expand'};
  1067. my $skip_ac_subst = $options{'skip_ac_subst'};
  1068. return $var->_do_recursive_traversal ($var,
  1069. $fun_item, $fun_collect,
  1070. $cond_filter, TRUE, $inner_expand,
  1071. $skip_ac_subst)
  1072. }
  1073. # The guts of Automake::Variable::traverse_recursively.
  1074. sub _do_recursive_traversal ($$&&$$$$)
  1075. {
  1076. my ($var, $parent, $fun_item, $fun_collect, $cond_filter, $parent_cond,
  1077. $inner_expand, $skip_ac_subst) = @_;
  1078. $var->set_seen;
  1079. if ($var->{'scanned'} == $_traversal)
  1080. {
  1081. err_var $var, "variable '" . $var->name() . "' recursively defined";
  1082. return ();
  1083. }
  1084. $var->{'scanned'} = $_traversal;
  1085. my @allresults = ();
  1086. my $cond_once = 0;
  1087. foreach my $cond ($var->conditions->conds)
  1088. {
  1089. if (ref $cond_filter)
  1090. {
  1091. # Ignore conditions that don't match $cond_filter.
  1092. next if ! $cond->true_when ($cond_filter);
  1093. # If we found out several definitions of $var
  1094. # match $cond_filter then we are in trouble.
  1095. # Tell the user we don't support this.
  1096. $var->check_defined_unconditionally ($parent, $parent_cond)
  1097. if $cond_once;
  1098. $cond_once = 1;
  1099. }
  1100. my @result = ();
  1101. my $full_cond = $cond->merge ($parent_cond);
  1102. my @to_process = $var->value_as_list ($cond, $parent, $parent_cond);
  1103. while (@to_process)
  1104. {
  1105. my $val = shift @to_process;
  1106. # If $val is a variable (i.e. ${foo} or $(bar), not a filename),
  1107. # handle the sub variable recursively.
  1108. # (Backslashes before '}' and ')' within brackets are here to
  1109. # please Emacs's indentation.)
  1110. if ($val =~ /^\$\{([^\}]*)\}$/ || $val =~ /^\$\(([^\)]*)\)$/)
  1111. {
  1112. my $subvarname = $1;
  1113. # If the user uses a losing variable name, just ignore it.
  1114. # This isn't ideal, but people have requested it.
  1115. next if ($subvarname =~ /\@.*\@/);
  1116. # See if the variable is actually a substitution reference
  1117. my ($from, $to);
  1118. # This handles substitution references like ${foo:.a=.b}.
  1119. if ($subvarname =~ /^([^:]*):([^=]*)=(.*)$/o)
  1120. {
  1121. $subvarname = $1;
  1122. $to = $3;
  1123. $from = quotemeta $2;
  1124. }
  1125. my $subvar = var ($subvarname);
  1126. # Don't recurse into undefined variables.
  1127. next unless $subvar;
  1128. push @_substfroms, $from;
  1129. push @_substtos, $to;
  1130. my @res = $subvar->_do_recursive_traversal ($parent,
  1131. $fun_item,
  1132. $fun_collect,
  1133. $cond_filter,
  1134. $full_cond,
  1135. $inner_expand,
  1136. $skip_ac_subst);
  1137. push (@result, @res);
  1138. pop @_substfroms;
  1139. pop @_substtos;
  1140. next;
  1141. }
  1142. # Try to expand variable references inside filenames such as
  1143. # '$(NAME).txt'. We do not handle ':.foo=.bar'
  1144. # substitutions, but it would make little sense to use this
  1145. # here anyway.
  1146. elsif ($inner_expand
  1147. && ($val =~ /\$\{([^\}]*)\}/ || $val =~ /\$\(([^\)]*)\)/))
  1148. {
  1149. my $subvarname = $1;
  1150. my $subvar = var $subvarname;
  1151. if ($subvar)
  1152. {
  1153. # Replace the reference by its value, and reschedule
  1154. # for expansion.
  1155. foreach my $c ($subvar->conditions->conds)
  1156. {
  1157. if (ref $cond_filter)
  1158. {
  1159. # Ignore conditions that don't match $cond_filter.
  1160. next if ! $c->true_when ($cond_filter);
  1161. # If we found out several definitions of $var
  1162. # match $cond_filter then we are in trouble.
  1163. # Tell the user we don't support this.
  1164. $subvar->check_defined_unconditionally ($var,
  1165. $full_cond)
  1166. if $cond_once;
  1167. $cond_once = 1;
  1168. }
  1169. my $subval = $subvar->rdef ($c)->value;
  1170. $val =~ s/\$\{$subvarname\}/$subval/g;
  1171. $val =~ s/\$\($subvarname\)/$subval/g;
  1172. unshift @to_process, split (' ', $val);
  1173. }
  1174. next;
  1175. }
  1176. # We do not know any variable with this name. Fall through
  1177. # to filename processing.
  1178. }
  1179. elsif ($skip_ac_subst && $val =~ /^\@.+\@$/)
  1180. {
  1181. next;
  1182. }
  1183. if ($fun_item) # $var is a filename we must process
  1184. {
  1185. my $substnum=$#_substfroms;
  1186. while ($substnum >= 0)
  1187. {
  1188. $val =~ s/$_substfroms[$substnum]$/$_substtos[$substnum]/
  1189. if defined $_substfroms[$substnum];
  1190. $substnum -= 1;
  1191. }
  1192. # Make sure you update the doc of
  1193. # Automake::Variable::traverse_recursively
  1194. # if you change the prototype of &fun_item.
  1195. my @transformed = &$fun_item ($var, $val, $cond, $full_cond);
  1196. push (@result, @transformed);
  1197. }
  1198. }
  1199. push (@allresults, [$cond, @result]) if @result;
  1200. }
  1201. # We only care about _recursive_ variable definitions. The user
  1202. # is free to use the same variable several times in the same definition.
  1203. $var->{'scanned'} = -1;
  1204. return ()
  1205. unless $fun_collect;
  1206. # Make sure you update the doc of Automake::Variable::traverse_recursively
  1207. # if you change the prototype of &fun_collect.
  1208. return &$fun_collect ($var, $parent_cond, @allresults);
  1209. }
  1210. # _hash_varname ($VAR)
  1211. # --------------------
  1212. # Compute the key associated $VAR in %_gen_varname.
  1213. # See _gen_varname() below.
  1214. sub _hash_varname ($)
  1215. {
  1216. my ($var) = @_;
  1217. my $key = '';
  1218. foreach my $cond ($var->conditions->conds)
  1219. {
  1220. my @values = $var->value_as_list ($cond);
  1221. $key .= "($cond)@values";
  1222. }
  1223. return $key;
  1224. }
  1225. # _hash_values (@VALUES)
  1226. # ----------------------
  1227. # Hash @VALUES for %_gen_varname. @VALUES should be a list
  1228. # of pairs: ([$cond, @values], [$cond, @values], ...).
  1229. # See _gen_varname() below.
  1230. sub _hash_values (@)
  1231. {
  1232. my $key = '';
  1233. foreach my $pair (@_)
  1234. {
  1235. my ($cond, @values) = @$pair;
  1236. $key .= "($cond)@values";
  1237. }
  1238. return $key;
  1239. }
  1240. # ($VARNAME, $GENERATED)
  1241. # _gen_varname ($BASE, @DEFINITIONS)
  1242. # ----------------------------------
  1243. # Return a variable name starting with $BASE, that will be
  1244. # used to store definitions @DEFINITIONS.
  1245. # @DEFINITIONS is a list of pair [$COND, @OBJECTS].
  1246. #
  1247. # If we already have a $BASE-variable containing @DEFINITIONS, reuse
  1248. # it and set $GENERATED to 0. Otherwise construct a new name and set
  1249. # $GENERATED to 1.
  1250. #
  1251. # This way, we avoid combinatorial explosion of the generated
  1252. # variables. Especially, in a Makefile such as:
  1253. #
  1254. # | if FOO1
  1255. # | A1=1
  1256. # | endif
  1257. # |
  1258. # | if FOO2
  1259. # | A2=2
  1260. # | endif
  1261. # |
  1262. # | ...
  1263. # |
  1264. # | if FOON
  1265. # | AN=N
  1266. # | endif
  1267. # |
  1268. # | B=$(A1) $(A2) ... $(AN)
  1269. # |
  1270. # | c_SOURCES=$(B)
  1271. # | d_SOURCES=$(B)
  1272. #
  1273. # The generated c_OBJECTS and d_OBJECTS will share the same variable
  1274. # definitions.
  1275. #
  1276. # This setup can be the case of a testsuite containing lots (>100) of
  1277. # small C programs, all testing the same set of source files.
  1278. sub _gen_varname ($@)
  1279. {
  1280. my $base = shift;
  1281. my $key = _hash_values @_;
  1282. return ($_gen_varname{$base}{$key}, 0)
  1283. if exists $_gen_varname{$base}{$key};
  1284. my $num = 1 + ($_gen_varname_n{$base} || 0);
  1285. $_gen_varname_n{$base} = $num;
  1286. my $name = "${base}_${num}";
  1287. $_gen_varname{$base}{$key} = $name;
  1288. return ($name, 1);
  1289. }
  1290. =item C<$resvar = transform_variable_recursively ($var, $resvar, $base, $nodefine, $where, &fun_item, [%options])>
  1291. =item C<$resvar = $var-E<gt>transform_variable_recursively ($resvar, $base, $nodefine, $where, &fun_item, [%options])>
  1292. Traverse C<$var> recursively, and create a C<$resvar> variable in
  1293. which each filename in C<$var> have been transformed using
  1294. C<&fun_item>. (C<$var> may be a variable name in the first syntax.
  1295. It must be an C<Automake::Variable> otherwise.)
  1296. Helper variables (corresponding to sub-variables of C<$var>) are
  1297. created as needed, using C<$base> as prefix.
  1298. Arguments are:
  1299. $var source variable to traverse
  1300. $resvar resulting variable to define
  1301. $base prefix to use when naming subvariables of $resvar
  1302. $nodefine if true, traverse $var but do not define any variable
  1303. (this assumes &fun_item has some useful side-effect)
  1304. $where context into which variable definitions are done
  1305. &fun_item a transformation function -- see the documentation
  1306. of &fun_item in Automake::Variable::traverse_recursively.
  1307. This returns the string C<"\$($RESVAR)">.
  1308. C<%options> is a list of options to pass to
  1309. C<Variable::traverse_recursively> (see this method).
  1310. =cut
  1311. sub transform_variable_recursively ($$$$$&;%)
  1312. {
  1313. my ($var, $resvar, $base, $nodefine, $where, $fun_item, %options) = @_;
  1314. $var = ref $var ? $var : rvar $var;
  1315. my $res = $var->traverse_recursively
  1316. ($fun_item,
  1317. # The code that defines the variable holding the result
  1318. # of the recursive transformation of a subvariable.
  1319. sub {
  1320. my ($subvar, $parent_cond, @allresults) = @_;
  1321. # If no definition is required, return anything: the result is
  1322. # not expected to be used, only the side effect of $fun_item
  1323. # should matter.
  1324. return 'report-me' if $nodefine;
  1325. # Cache $subvar, so that we reuse it if @allresults is the same.
  1326. my $key = _hash_varname $subvar;
  1327. $_gen_varname{$base}{$key} = $subvar->name;
  1328. # Find a name for the variable, unless this is the top-variable
  1329. # for which we want to use $resvar.
  1330. my ($varname, $generated) =
  1331. ($var != $subvar) ? _gen_varname ($base, @allresults) : ($resvar, 1);
  1332. # Define the variable if we are not reusing a previously
  1333. # defined variable. At the top-level, we can also avoid redefining
  1334. # the variable if it already contains the same values.
  1335. if ($generated
  1336. && !($varname eq $var->name && $key eq _hash_values @allresults))
  1337. {
  1338. # If the new variable is the source variable, we assume
  1339. # we are trying to override a user variable. Delete
  1340. # the old variable first.
  1341. variable_delete ($varname) if $varname eq $var->name;
  1342. # Define an empty variable in condition TRUE if there is no
  1343. # result.
  1344. @allresults = ([TRUE, '']) unless @allresults;
  1345. # Define the rewritten variable in all conditions not
  1346. # already covered by user definitions.
  1347. foreach my $pair (@allresults)
  1348. {
  1349. my ($cond, @result) = @$pair;
  1350. my $var = var $varname;
  1351. my @conds = ($var
  1352. ? $var->not_always_defined_in_cond ($cond)->conds
  1353. : $cond);
  1354. foreach (@conds)
  1355. {
  1356. define ($varname, VAR_AUTOMAKE, '', $_, "@result",
  1357. '', $where, VAR_PRETTY);
  1358. }
  1359. }
  1360. }
  1361. set_seen $varname;
  1362. return "\$($varname)";
  1363. },
  1364. %options);
  1365. return $res;
  1366. }
  1367. =back
  1368. =head1 SEE ALSO
  1369. L<Automake::VarDef>, L<Automake::Condition>,
  1370. L<Automake::DisjConditions>, L<Automake::Location>.
  1371. =cut
  1372. 1;
  1373. ### Setup "GNU" style for perl-mode and cperl-mode.
  1374. ## Local Variables:
  1375. ## perl-indent-level: 2
  1376. ## perl-continued-statement-offset: 2
  1377. ## perl-continued-brace-offset: 0
  1378. ## perl-brace-offset: 0
  1379. ## perl-brace-imaginary-offset: 0
  1380. ## perl-label-offset: -2
  1381. ## cperl-indent-level: 2
  1382. ## cperl-brace-offset: 0
  1383. ## cperl-continued-brace-offset: 0
  1384. ## cperl-label-offset: -2
  1385. ## cperl-extra-newline-before-brace: t
  1386. ## cperl-merge-trailing-else: nil
  1387. ## cperl-continued-statement-offset: 2
  1388. ## End: