common.tmpl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. {- # -*- Mode: perl -*-
  2. use File::Basename;
  3. my $debug_resolvedepends = $ENV{BUILDFILE_DEBUG_DEPENDS};
  4. my $debug_rules = $ENV{BUILDFILE_DEBUG_RULES};
  5. # A cache of objects for which a recipe has already been generated
  6. my %cache;
  7. # collectdepends, expanddepends and reducedepends work together to make
  8. # sure there are no duplicate or weak dependencies and that they are in
  9. # the right order. This is used to sort the list of libraries that a
  10. # build depends on.
  11. sub extensionlesslib {
  12. my @result = map { $_ =~ /(\.a)?$/; $` } @_;
  13. return @result if wantarray;
  14. return $result[0];
  15. }
  16. # collectdepends dives into the tree of dependencies and returns
  17. # a list of all the non-weak ones.
  18. sub collectdepends {
  19. return () unless @_;
  20. my $thing = shift;
  21. my $extensionlessthing = extensionlesslib($thing);
  22. my @listsofar = @_; # to check if we're looping
  23. my @list = @{$unified_info{depends}->{$thing} //
  24. $unified_info{depends}->{$extensionlessthing}};
  25. my @newlist = ();
  26. print STDERR "DEBUG[collectdepends] $thing > ", join(' ', @listsofar), "\n"
  27. if $debug_resolvedepends;
  28. foreach my $item (@list) {
  29. my $extensionlessitem = extensionlesslib($item);
  30. # It's time to break off when the dependency list starts looping
  31. next if grep { extensionlesslib($_) eq $extensionlessitem } @listsofar;
  32. # Don't add anything here if the dependency is weak
  33. next if defined $unified_info{attributes}->{depends}->{$thing}->{$item}->{'weak'};
  34. my @resolved = collectdepends($item, @listsofar, $item);
  35. push @newlist, $item, @resolved;
  36. }
  37. print STDERR "DEBUG[collectdepends] $thing < ", join(' ', @newlist), "\n"
  38. if $debug_resolvedepends;
  39. @newlist;
  40. }
  41. # expanddepends goes through a list of stuff, checks if they have any
  42. # dependencies, and adds them at the end of the current position if
  43. # they aren't already present later on.
  44. sub expanddepends {
  45. my @after = ( @_ );
  46. print STDERR "DEBUG[expanddepends]> ", join(' ', @after), "\n"
  47. if $debug_resolvedepends;
  48. my @before = ();
  49. while (@after) {
  50. my $item = shift @after;
  51. print STDERR "DEBUG[expanddepends]\\ ", join(' ', @before), "\n"
  52. if $debug_resolvedepends;
  53. print STDERR "DEBUG[expanddepends] - ", $item, "\n"
  54. if $debug_resolvedepends;
  55. my @middle = (
  56. $item,
  57. map {
  58. my $x = $_;
  59. my $extlessx = extensionlesslib($x);
  60. if (grep { $extlessx eq extensionlesslib($_) } @before
  61. and
  62. !grep { $extlessx eq extensionlesslib($_) } @after) {
  63. print STDERR "DEBUG[expanddepends] + ", $x, "\n"
  64. if $debug_resolvedepends;
  65. ( $x )
  66. } else {
  67. print STDERR "DEBUG[expanddepends] ! ", $x, "\n"
  68. if $debug_resolvedepends;
  69. ()
  70. }
  71. } @{$unified_info{depends}->{$item} // []}
  72. );
  73. print STDERR "DEBUG[expanddepends] = ", join(' ', @middle), "\n"
  74. if $debug_resolvedepends;
  75. print STDERR "DEBUG[expanddepends]/ ", join(' ', @after), "\n"
  76. if $debug_resolvedepends;
  77. push @before, @middle;
  78. }
  79. print STDERR "DEBUG[expanddepends]< ", join(' ', @before), "\n"
  80. if $debug_resolvedepends;
  81. @before;
  82. }
  83. # reducedepends looks through a list, and checks if each item is
  84. # repeated later on. If it is, the earlier copy is dropped.
  85. sub reducedepends {
  86. my @list = @_;
  87. print STDERR "DEBUG[reducedepends]> ", join(' ', @list), "\n"
  88. if $debug_resolvedepends;
  89. my @newlist = ();
  90. my %replace = ();
  91. while (@list) {
  92. my $item = shift @list;
  93. my $extensionlessitem = extensionlesslib($item);
  94. if (grep { $extensionlessitem eq extensionlesslib($_) } @list) {
  95. if ($item ne $extensionlessitem) {
  96. # If this instance of the library is explicitly static, we
  97. # prefer that to any shared library name, since it must have
  98. # been done on purpose.
  99. $replace{$extensionlessitem} = $item;
  100. }
  101. } else {
  102. push @newlist, $item;
  103. }
  104. }
  105. @newlist = map { $replace{$_} // $_; } @newlist;
  106. print STDERR "DEBUG[reducedepends]< ", join(' ', @newlist), "\n"
  107. if $debug_resolvedepends;
  108. @newlist;
  109. }
  110. # Do it all
  111. # This takes multiple inputs and combine them into a single list of
  112. # interdependent things. The returned value will include all the input.
  113. # Callers are responsible for taking away the things they are building.
  114. sub resolvedepends {
  115. print STDERR "DEBUG[resolvedepends] START (", join(', ', @_), ")\n"
  116. if $debug_resolvedepends;
  117. my @all =
  118. reducedepends(expanddepends(map { ( $_, collectdepends($_) ) } @_));
  119. print STDERR "DEBUG[resolvedepends] END (", join(', ', @_), ") : ",
  120. join(',', map { "\n $_" } @all), "\n"
  121. if $debug_resolvedepends;
  122. @all;
  123. }
  124. # dogenerate is responsible for producing all the recipes that build
  125. # generated source files. It recurses in case a dependency is also a
  126. # generated source file.
  127. sub dogenerate {
  128. my $src = shift;
  129. return "" if $cache{$src};
  130. my $obj = shift;
  131. my $bin = shift;
  132. my %opts = @_;
  133. if ($unified_info{generate}->{$src}) {
  134. die "$src is generated by Configure, should not appear in build file\n"
  135. if ref $unified_info{generate}->{$src} eq "";
  136. my $script = $unified_info{generate}->{$src}->[0];
  137. $OUT .= generatesrc(src => $src,
  138. product => $bin,
  139. generator => $unified_info{generate}->{$src},
  140. generator_incs => $unified_info{includes}->{$script},
  141. generator_deps => $unified_info{depends}->{$script},
  142. deps => $unified_info{depends}->{$src},
  143. incs => [ defined $obj
  144. ? @{$unified_info{includes}->{$obj}}
  145. : (),
  146. defined $bin
  147. ? @{$unified_info{includes}->{$bin}}
  148. : () ],
  149. defs => [ defined $obj
  150. ? @{$unified_info{defines}->{$obj}}
  151. : (),
  152. defined $bin
  153. ? @{$unified_info{defines}->{$bin}}
  154. : () ],
  155. %opts);
  156. foreach (@{$unified_info{depends}->{$src}}) {
  157. dogenerate($_, $obj, $bin, %opts);
  158. }
  159. }
  160. $cache{$src} = 1;
  161. }
  162. # doobj is responsible for producing all the recipes that build
  163. # object files as well as dependency files.
  164. sub doobj {
  165. my $obj = shift;
  166. return "" if $cache{$obj};
  167. my $bin = shift;
  168. my %opts = @_;
  169. if (@{$unified_info{sources}->{$obj}}) {
  170. my @srcs = @{$unified_info{sources}->{$obj}};
  171. my @deps = @{$unified_info{depends}->{$obj}};
  172. my @incs = ( @{$unified_info{includes}->{$obj}},
  173. @{$unified_info{includes}->{$bin}} );
  174. my @defs = ( @{$unified_info{defines}->{$obj}},
  175. @{$unified_info{defines}->{$bin}} );
  176. print STDERR "DEBUG[doobj] \@srcs for $obj ($bin) : ",
  177. join(",", map { "\n $_" } @srcs), "\n"
  178. if $debug_rules;
  179. print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
  180. join(",", map { "\n $_" } @deps), "\n"
  181. if $debug_rules;
  182. print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
  183. join(",", map { "\n $_" } @incs), "\n"
  184. if $debug_rules;
  185. print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
  186. join(",", map { "\n $_" } @defs), "\n"
  187. if $debug_rules;
  188. print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
  189. join(",", map { "\n $_ = $opts{$_}" } sort keys %opts), "\n"
  190. if $debug_rules;
  191. $OUT .= src2obj(obj => $obj, product => $bin,
  192. srcs => [ @srcs ], deps => [ @deps ],
  193. incs => [ @incs ], defs => [ @defs ],
  194. %opts);
  195. foreach ((@{$unified_info{sources}->{$obj}},
  196. @{$unified_info{depends}->{$obj}})) {
  197. dogenerate($_, $obj, $bin, %opts);
  198. }
  199. }
  200. $cache{$obj} = 1;
  201. }
  202. # Helper functions to grab all applicable intermediary files.
  203. # This is particularly useful when a library is given as source
  204. # rather than a dependency. In that case, we consider it to be a
  205. # container with object file references, or possibly references
  206. # to further libraries to pilfer in the same way.
  207. sub getsrclibs {
  208. my $section = shift;
  209. # For all input, see if it sources static libraries. If it does,
  210. # return them together with the result of a recursive call.
  211. map { ( $_, getsrclibs($section, $_) ) }
  212. grep { $_ =~ m|\.a$| }
  213. map { @{$unified_info{$section}->{$_} // []} }
  214. @_;
  215. }
  216. sub getlibobjs {
  217. my $section = shift;
  218. # For all input, see if it's an intermediary file (library or object).
  219. # If it is, collect the result of a recursive call, or if that returns
  220. # an empty list, the element itself. Return the result.
  221. map {
  222. my @x = getlibobjs($section, @{$unified_info{$section}->{$_}});
  223. @x ? @x : ( $_ );
  224. }
  225. grep { defined $unified_info{$section}->{$_} }
  226. @_;
  227. }
  228. # dolib is responsible for building libraries. It will call
  229. # obj2shlib if shared libraries are produced, and obj2lib in all
  230. # cases. It also makes sure all object files for the library are
  231. # built.
  232. sub dolib {
  233. my $lib = shift;
  234. return "" if $cache{$lib};
  235. my %attrs = %{$unified_info{attributes}->{libraries}->{$lib}};
  236. my @deps = ( resolvedepends(getsrclibs('sources', $lib)) );
  237. # We support two types of objs, those who are specific to this library
  238. # (they end up in @objs) and those that we get indirectly, via other
  239. # libraries (they end up in @foreign_objs). We get the latter any time
  240. # someone has done something like this in build.info:
  241. # SOURCE[libfoo.a]=libbar.a
  242. # The indirect object files must be kept in a separate array so they
  243. # don't get rebuilt unnecessarily (and with incorrect auxiliary
  244. # information).
  245. #
  246. # Object files can't be collected commonly for shared and static
  247. # libraries, because we contain their respective object files in
  248. # {shared_sources} and {sources}, and because the implications are
  249. # slightly different for each library form.
  250. #
  251. # We grab all these "foreign" object files recursively with getlibobjs().
  252. unless ($disabled{shared} || $lib =~ /\.a$/) {
  253. my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
  254. # If this library sources other static libraries and those
  255. # libraries are marked {noinst}, there's no need to include
  256. # all of their object files. Instead, we treat those static
  257. # libraries as dependents alongside any other library this
  258. # one depends on, and let symbol resolution do its job.
  259. my @sourced_libs = ();
  260. my @objs = ();
  261. my @foreign_objs = ();
  262. my @deps = ();
  263. foreach (@{$unified_info{shared_sources}->{$lib}}) {
  264. if ($_ !~ m|\.a$|) {
  265. push @objs, $_;
  266. } elsif ($unified_info{attributes}->{libraries}->{$_}->{noinst}) {
  267. push @deps, $_;
  268. } else {
  269. push @deps, getsrclibs('sources', $_);
  270. push @foreign_objs, getlibobjs('sources', $_);
  271. }
  272. }
  273. @deps = ( grep { $_ ne $lib } resolvedepends($lib, @deps) );
  274. print STDERR "DEBUG[dolib:shlib] \%attrs for $lib : ", ,
  275. join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
  276. if %attrs && $debug_rules;
  277. print STDERR "DEBUG[dolib:shlib] \@deps for $lib : ",
  278. join(",", map { "\n $_" } @deps), "\n"
  279. if @deps && $debug_rules;
  280. print STDERR "DEBUG[dolib:shlib] \@objs for $lib : ",
  281. join(",", map { "\n $_" } @objs), "\n"
  282. if @objs && $debug_rules;
  283. print STDERR "DEBUG[dolib:shlib] \@foreign_objs for $lib : ",
  284. join(",", map { "\n $_" } @foreign_objs), "\n"
  285. if @foreign_objs && $debug_rules;
  286. $OUT .= $obj2shlib->(lib => $lib,
  287. attrs => { %attrs },
  288. objs => [ @objs, @foreign_objs ],
  289. deps => [ @deps ]);
  290. foreach (@objs) {
  291. # If this is somehow a compiled object, take care of it that way
  292. # Otherwise, it might simply be generated
  293. if (defined $unified_info{sources}->{$_}) {
  294. if($_ =~ /\.a$/) {
  295. dolib($_);
  296. } else {
  297. doobj($_, $lib, intent => "shlib", attrs => { %attrs });
  298. }
  299. } else {
  300. dogenerate($_, undef, undef, intent => "lib");
  301. }
  302. }
  303. }
  304. {
  305. # When putting static libraries together, we cannot rely on any
  306. # symbol resolution, so for all static libraries used as source for
  307. # this one, as well as other libraries they depend on, we simply
  308. # grab all their object files unconditionally,
  309. # Symbol resolution will happen when any program, module or shared
  310. # library is linked with this one.
  311. my @objs = ();
  312. my @sourcedeps = ();
  313. my @foreign_objs = ();
  314. foreach (@{$unified_info{sources}->{$lib}}) {
  315. if ($_ !~ m|\.a$|) {
  316. push @objs, $_;
  317. } else {
  318. push @sourcedeps, $_;
  319. }
  320. }
  321. @sourcedeps = ( grep { $_ ne $lib } resolvedepends(@sourcedeps) );
  322. print STDERR "DEBUG[dolib:lib] : \@sourcedeps for $_ : ",
  323. join(",", map { "\n $_" } @sourcedeps), "\n"
  324. if @sourcedeps && $debug_rules;
  325. @foreign_objs = getlibobjs('sources', @sourcedeps);
  326. print STDERR "DEBUG[dolib:lib] \%attrs for $lib : ", ,
  327. join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
  328. if %attrs && $debug_rules;
  329. print STDERR "DEBUG[dolib:lib] \@objs for $lib : ",
  330. join(",", map { "\n $_" } @objs), "\n"
  331. if @objs && $debug_rules;
  332. print STDERR "DEBUG[dolib:lib] \@foreign_objs for $lib : ",
  333. join(",", map { "\n $_" } @foreign_objs), "\n"
  334. if @foreign_objs && $debug_rules;
  335. $OUT .= obj2lib(lib => $lib, attrs => { %attrs },
  336. objs => [ @objs, @foreign_objs ]);
  337. foreach (@objs) {
  338. doobj($_, $lib, intent => "lib", attrs => { %attrs });
  339. }
  340. }
  341. $cache{$lib} = 1;
  342. }
  343. # domodule is responsible for building modules. It will call
  344. # obj2dso, and also makes sure all object files for the library
  345. # are built.
  346. sub domodule {
  347. my $module = shift;
  348. return "" if $cache{$module};
  349. my %attrs = %{$unified_info{attributes}->{modules}->{$module}};
  350. my @objs = @{$unified_info{sources}->{$module}};
  351. my @deps = ( grep { $_ ne $module }
  352. resolvedepends($module) );
  353. print STDERR "DEBUG[domodule] \%attrs for $module :",
  354. join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
  355. if $debug_rules;
  356. print STDERR "DEBUG[domodule] \@objs for $module : ",
  357. join(",", map { "\n $_" } @objs), "\n"
  358. if $debug_rules;
  359. print STDERR "DEBUG[domodule] \@deps for $module : ",
  360. join(",", map { "\n $_" } @deps), "\n"
  361. if $debug_rules;
  362. $OUT .= obj2dso(module => $module,
  363. attrs => { %attrs },
  364. objs => [ @objs ],
  365. deps => [ @deps ]);
  366. foreach (@{$unified_info{sources}->{$module}}) {
  367. # If this is somehow a compiled object, take care of it that way
  368. # Otherwise, it might simply be generated
  369. if (defined $unified_info{sources}->{$_}) {
  370. doobj($_, $module, intent => "dso", attrs => { %attrs });
  371. } else {
  372. dogenerate($_, undef, $module, intent => "dso");
  373. }
  374. }
  375. $cache{$module} = 1;
  376. }
  377. # dobin is responsible for building programs. It will call obj2bin,
  378. # and also makes sure all object files for the library are built.
  379. sub dobin {
  380. my $bin = shift;
  381. return "" if $cache{$bin};
  382. my %attrs = %{$unified_info{attributes}->{programs}->{$bin}};
  383. my @objs = @{$unified_info{sources}->{$bin}};
  384. my @deps = ( grep { $_ ne $bin } resolvedepends($bin) );
  385. print STDERR "DEBUG[dobin] \%attrs for $bin : ",
  386. join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
  387. if %attrs && $debug_rules;
  388. print STDERR "DEBUG[dobin] \@objs for $bin : ",
  389. join(",", map { "\n $_" } @objs), "\n"
  390. if @objs && $debug_rules;
  391. print STDERR "DEBUG[dobin] \@deps for $bin : ",
  392. join(",", map { "\n $_" } @deps), "\n"
  393. if @deps && $debug_rules;
  394. $OUT .= obj2bin(bin => $bin,
  395. attrs => { %attrs },
  396. objs => [ @objs ],
  397. deps => [ @deps ]);
  398. foreach (@objs) {
  399. doobj($_, $bin, intent => "bin", attrs => { %attrs });
  400. }
  401. $cache{$bin} = 1;
  402. }
  403. # dobin is responsible for building scripts from templates. It will
  404. # call in2script.
  405. sub doscript {
  406. my $script = shift;
  407. return "" if $cache{$script};
  408. $OUT .= in2script(script => $script,
  409. attrs => $unified_info{attributes}->{$script},
  410. sources => $unified_info{sources}->{$script});
  411. $cache{$script} = 1;
  412. }
  413. sub dodir {
  414. my $dir = shift;
  415. return "" if !exists(&generatedir) or $cache{$dir};
  416. $OUT .= generatedir(dir => $dir,
  417. deps => $unified_info{dirinfo}->{$dir}->{deps},
  418. %{$unified_info{dirinfo}->{$_}->{products}});
  419. $cache{$dir} = 1;
  420. }
  421. # dodocs is responsible for building documentation from .pods.
  422. # It will call generatesrc.
  423. sub dodocs {
  424. my $type = shift;
  425. my $section = shift;
  426. foreach my $doc (@{$unified_info{"${type}docs"}->{$section}}) {
  427. next if $cache{$doc};
  428. $OUT .= generatesrc(src => $doc,
  429. generator => $unified_info{generate}->{$doc});
  430. foreach ((@{$unified_info{depends}->{$doc}})) {
  431. dogenerate($_, undef, undef, %opts);
  432. }
  433. $cache{$doc} = 1;
  434. }
  435. }
  436. # Start with populating the cache with all the overrides
  437. %cache = map { $_ => 1 } @{$unified_info{overrides}};
  438. # Build mandatory generated headers
  439. foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
  440. # Build all known libraries, modules, programs and scripts.
  441. # Everything else will be handled as a consequence.
  442. foreach (@{$unified_info{libraries}}) { dolib($_); }
  443. foreach (@{$unified_info{modules}}) { domodule($_); }
  444. foreach (@{$unified_info{programs}}) { dobin($_); }
  445. foreach (@{$unified_info{scripts}}) { doscript($_); }
  446. foreach (sort keys %{$unified_info{htmldocs}}) { dodocs('html', $_); }
  447. foreach (sort keys %{$unified_info{mandocs}}) { dodocs('man', $_); }
  448. foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
  449. -}