common.tmpl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. {- # -*- Mode: perl -*-
  2. use File::Basename;
  3. # A cache of objects for which a recipe has already been generated
  4. my %cache;
  5. # resolvedepends and reducedepends work in tandem to make sure
  6. # there are no duplicate dependencies and that they are in the
  7. # right order. This is especially used to sort the list of
  8. # libraries that a build depends on.
  9. sub extensionlesslib {
  10. my @result = map { $_ =~ /(\.a)?$/; $` } @_;
  11. return @result if wantarray;
  12. return $result[0];
  13. }
  14. sub resolvedepends {
  15. my $thing = shift;
  16. my $extensionlessthing = extensionlesslib($thing);
  17. my @listsofar = @_; # to check if we're looping
  18. my @list = @{$unified_info{depends}->{$thing} //
  19. $unified_info{depends}->{$extensionlessthing}};
  20. my @newlist = ();
  21. if (scalar @list) {
  22. foreach my $item (@list) {
  23. my $extensionlessitem = extensionlesslib($item);
  24. # It's time to break off when the dependency list starts looping
  25. next if grep { extensionlesslib($_) eq $extensionlessitem } @listsofar;
  26. push @newlist, $item, resolvedepends($item, @listsofar, $item);
  27. }
  28. }
  29. @newlist;
  30. }
  31. sub reducedepends {
  32. my @list = @_;
  33. my @newlist = ();
  34. my %replace = ();
  35. while (@list) {
  36. my $item = shift @list;
  37. my $extensionlessitem = extensionlesslib($item);
  38. if (grep { $extensionlessitem eq extensionlesslib($_) } @list) {
  39. if ($item ne $extensionlessitem) {
  40. # If this instance of the library is explicitly static, we
  41. # prefer that to any shared library name, since it must have
  42. # been done on purpose.
  43. $replace{$extensionlessitem} = $item;
  44. }
  45. } else {
  46. push @newlist, $item;
  47. }
  48. }
  49. map { $replace{$_} // $_; } @newlist;
  50. }
  51. # is_installed checks if a given file will be installed (i.e. they are
  52. # not defined _NO_INST in build.info)
  53. sub is_installed {
  54. my $product = shift;
  55. if (grep { $product eq $_ }
  56. map { (@{$unified_info{install}->{$_}}) }
  57. keys %{$unified_info{install}}) {
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. # dogenerate is responsible for producing all the recipes that build
  63. # generated source files. It recurses in case a dependency is also a
  64. # generated source file.
  65. sub dogenerate {
  66. my $src = shift;
  67. return "" if $cache{$src};
  68. my $obj = shift;
  69. my $bin = shift;
  70. my %opts = @_;
  71. if ($unified_info{generate}->{$src}) {
  72. die "$src is generated by Configure, should not appear in build file\n"
  73. if ref $unified_info{generate}->{$src} eq "";
  74. my $script = $unified_info{generate}->{$src}->[0];
  75. $OUT .= generatesrc(src => $src,
  76. product => $bin,
  77. generator => $unified_info{generate}->{$src},
  78. generator_incs => $unified_info{includes}->{$script},
  79. generator_deps => $unified_info{depends}->{$script},
  80. deps => $unified_info{depends}->{$src},
  81. incs => [ @{$unified_info{includes}->{$obj}},
  82. @{$unified_info{includes}->{$bin}} ],
  83. defs => [ @{$unified_info{defines}->{$obj}},
  84. @{$unified_info{defines}->{$bin}} ],
  85. %opts);
  86. foreach (@{$unified_info{depends}->{$src}}) {
  87. dogenerate($_, $obj, $bin, %opts);
  88. }
  89. }
  90. $cache{$src} = 1;
  91. }
  92. # doobj is responsible for producing all the recipes that build
  93. # object files as well as dependency files.
  94. sub doobj {
  95. my $obj = shift;
  96. return "" if $cache{$obj};
  97. my $bin = shift;
  98. my %opts = @_;
  99. if (@{$unified_info{sources}->{$obj}}) {
  100. $OUT .= src2obj(obj => $obj,
  101. product => $bin,
  102. srcs => $unified_info{sources}->{$obj},
  103. deps => $unified_info{depends}->{$obj},
  104. incs => [ @{$unified_info{includes}->{$obj}},
  105. @{$unified_info{includes}->{$bin}} ],
  106. defs => [ @{$unified_info{defines}->{$obj}},
  107. @{$unified_info{defines}->{$bin}} ],
  108. %opts);
  109. foreach ((@{$unified_info{sources}->{$obj}},
  110. @{$unified_info{depends}->{$obj}})) {
  111. dogenerate($_, $obj, $bin, %opts);
  112. }
  113. }
  114. $cache{$obj} = 1;
  115. }
  116. # dolib is responsible for building libraries. It will call
  117. # obj2shlib is shared libraries are produced, and obj2lib in all
  118. # cases. It also makes sure all object files for the library are
  119. # built.
  120. sub dolib {
  121. my $lib = shift;
  122. return "" if $cache{$lib};
  123. unless ($disabled{shared} || $lib =~ /\.a$/) {
  124. my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
  125. $OUT .= $obj2shlib->(lib => $lib,
  126. objs => $unified_info{shared_sources}->{$lib},
  127. deps => [ reducedepends(resolvedepends($lib)) ],
  128. installed => is_installed($lib));
  129. foreach ((@{$unified_info{shared_sources}->{$lib}},
  130. @{$unified_info{sources}->{$lib}})) {
  131. # If this is somehow a compiled object, take care of it that way
  132. # Otherwise, it might simply be generated
  133. if (defined $unified_info{sources}->{$_}) {
  134. doobj($_, $lib, intent => "shlib", installed => is_installed($lib));
  135. } else {
  136. dogenerate($_, undef, undef, intent => "lib");
  137. }
  138. }
  139. }
  140. $OUT .= obj2lib(lib => $lib,
  141. objs => [ @{$unified_info{sources}->{$lib}} ]);
  142. foreach (@{$unified_info{sources}->{$lib}}) {
  143. doobj($_, $lib, intent => "lib", installed => is_installed($lib));
  144. }
  145. $cache{$lib} = 1;
  146. }
  147. # doengine is responsible for building engines. It will call
  148. # obj2dso, and also makes sure all object files for the library
  149. # are built.
  150. sub doengine {
  151. my $lib = shift;
  152. return "" if $cache{$lib};
  153. $OUT .= obj2dso(lib => $lib,
  154. objs => $unified_info{shared_sources}->{$lib},
  155. deps => [ resolvedepends($lib) ],
  156. installed => is_installed($lib));
  157. foreach (@{$unified_info{shared_sources}->{$lib}}) {
  158. # If this is somehow a compiled object, take care of it that way
  159. # Otherwise, it might simply be generated
  160. if (defined $unified_info{sources}->{$_}) {
  161. doobj($_, $lib, intent => "dso", installed => is_installed($lib));
  162. } else {
  163. dogenerate($_, undef, $lib, intent => "dso");
  164. }
  165. }
  166. $cache{$lib} = 1;
  167. }
  168. # dobin is responsible for building programs. It will call obj2bin,
  169. # and also makes sure all object files for the library are built.
  170. sub dobin {
  171. my $bin = shift;
  172. return "" if $cache{$bin};
  173. my $deps = [ reducedepends(resolvedepends($bin)) ];
  174. $OUT .= obj2bin(bin => $bin,
  175. objs => [ @{$unified_info{sources}->{$bin}} ],
  176. deps => $deps,
  177. installed => is_installed($bin));
  178. foreach (@{$unified_info{sources}->{$bin}}) {
  179. doobj($_, $bin, intent => "bin", installed => is_installed($bin));
  180. }
  181. $cache{$bin} = 1;
  182. }
  183. # dobin is responsible for building scripts from templates. It will
  184. # call in2script.
  185. sub doscript {
  186. my $script = shift;
  187. return "" if $cache{$script};
  188. $OUT .= in2script(script => $script,
  189. sources => $unified_info{sources}->{$script},
  190. installed => is_installed($script));
  191. $cache{$script} = 1;
  192. }
  193. sub dodir {
  194. my $dir = shift;
  195. return "" if !exists(&generatedir) or $cache{$dir};
  196. $OUT .= generatedir(dir => $dir,
  197. deps => $unified_info{dirinfo}->{$dir}->{deps},
  198. %{$unified_info{dirinfo}->{$_}->{products}});
  199. $cache{$dir} = 1;
  200. }
  201. # Start with populating the cache with all the overrides
  202. %cache = map { $_ => 1 } @{$unified_info{overrides}};
  203. # Build mandatory generated headers
  204. foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
  205. # Build all known libraries, engines, programs and scripts.
  206. # Everything else will be handled as a consequence.
  207. foreach (@{$unified_info{libraries}}) { dolib($_); }
  208. foreach (@{$unified_info{engines}}) { doengine($_); }
  209. foreach (@{$unified_info{programs}}) { dobin($_); }
  210. foreach (@{$unified_info{scripts}}) { doscript($_); }
  211. foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
  212. # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
  213. # they are added here.
  214. $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
  215. -}