2
0

common.tmpl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. generator => $unified_info{generate}->{$src},
  77. generator_incs => $unified_info{includes}->{$script},
  78. generator_deps => $unified_info{depends}->{$script},
  79. deps => $unified_info{depends}->{$src},
  80. incs => $unified_info{includes}->{$obj},
  81. %opts);
  82. foreach (@{$unified_info{depends}->{$src}}) {
  83. dogenerate($_, $obj, $bin, %opts);
  84. }
  85. }
  86. $cache{$src} = 1;
  87. }
  88. # doobj is responsible for producing all the recipes that build
  89. # object files as well as dependency files.
  90. sub doobj {
  91. my $obj = shift;
  92. return "" if $cache{$obj};
  93. my $bin = shift;
  94. my %opts = @_;
  95. if (@{$unified_info{sources}->{$obj}}) {
  96. $OUT .= src2obj(obj => $obj,
  97. product => $bin,
  98. srcs => $unified_info{sources}->{$obj},
  99. deps => $unified_info{depends}->{$obj},
  100. incs => $unified_info{includes}->{$obj},
  101. %opts);
  102. foreach ((@{$unified_info{sources}->{$obj}},
  103. @{$unified_info{depends}->{$obj}})) {
  104. dogenerate($_, $obj, $bin, %opts);
  105. }
  106. }
  107. $cache{$obj} = 1;
  108. }
  109. # dolib is responsible for building libraries. It will call
  110. # libobj2shlib is shared libraries are produced, and obj2lib in all
  111. # cases. It also makes sure all object files for the library are
  112. # built.
  113. sub dolib {
  114. my $lib = shift;
  115. return "" if $cache{$lib};
  116. unless ($disabled{shared} || $lib =~ /\.a$/) {
  117. $OUT .= libobj2shlib(shlib => $unified_info{sharednames}->{$lib},
  118. lib => $lib,
  119. objs => [ @{$unified_info{shared_sources}->{$lib}},
  120. @{$unified_info{sources}->{$lib}} ],
  121. deps => [ reducedepends(resolvedepends($lib)) ],
  122. installed => is_installed($lib));
  123. foreach ((@{$unified_info{shared_sources}->{$lib}},
  124. @{$unified_info{sources}->{$lib}})) {
  125. # If this is somehow a compiled object, take care of it that way
  126. # Otherwise, it might simply be generated
  127. if (defined $unified_info{sources}->{$_}) {
  128. doobj($_, $lib, intent => "lib", installed => is_installed($lib));
  129. } else {
  130. dogenerate($_, undef, undef, intent => "lib");
  131. }
  132. }
  133. }
  134. $OUT .= obj2lib(lib => $lib,
  135. objs => [ @{$unified_info{sources}->{$lib}} ]);
  136. foreach (@{$unified_info{sources}->{$lib}}) {
  137. doobj($_, $lib, intent => "lib", installed => is_installed($lib));
  138. }
  139. $cache{$lib} = 1;
  140. }
  141. # doengine is responsible for building engines. It will call
  142. # obj2dso, and also makes sure all object files for the library
  143. # are built.
  144. sub doengine {
  145. my $lib = shift;
  146. return "" if $cache{$lib};
  147. $OUT .= obj2dso(lib => $lib,
  148. objs => [ @{$unified_info{sources}->{$lib}},
  149. @{$unified_info{shared_sources}->{$lib}} ],
  150. deps => [ resolvedepends($lib) ],
  151. installed => is_installed($lib));
  152. foreach ((@{$unified_info{sources}->{$lib}},
  153. @{$unified_info{shared_sources}->{$lib}})) {
  154. doobj($_, $lib, intent => "dso", installed => is_installed($lib));
  155. }
  156. $cache{$lib} = 1;
  157. }
  158. # dobin is responsible for building programs. It will call obj2bin,
  159. # and also makes sure all object files for the library are built.
  160. sub dobin {
  161. my $bin = shift;
  162. return "" if $cache{$bin};
  163. my $deps = [ reducedepends(resolvedepends($bin)) ];
  164. $OUT .= obj2bin(bin => $bin,
  165. objs => [ @{$unified_info{sources}->{$bin}} ],
  166. deps => $deps,
  167. installed => is_installed($bin));
  168. foreach (@{$unified_info{sources}->{$bin}}) {
  169. doobj($_, $bin, intent => "bin", installed => is_installed($bin));
  170. }
  171. $cache{$bin} = 1;
  172. }
  173. # dobin is responsible for building scripts from templates. It will
  174. # call in2script.
  175. sub doscript {
  176. my $script = shift;
  177. return "" if $cache{$script};
  178. $OUT .= in2script(script => $script,
  179. sources => $unified_info{sources}->{$script},
  180. installed => is_installed($script));
  181. $cache{$script} = 1;
  182. }
  183. sub dodir {
  184. my $dir = shift;
  185. return "" if !exists(&generatedir) or $cache{$dir};
  186. $OUT .= generatedir(dir => $dir,
  187. deps => $unified_info{dirinfo}->{$dir}->{deps},
  188. %{$unified_info{dirinfo}->{$_}->{products}});
  189. $cache{$dir} = 1;
  190. }
  191. # Start with populating the cache with all the overrides
  192. %cache = map { $_ => 1 } @{$unified_info{overrides}};
  193. # For convenience collect information regarding directories where
  194. # files are generated, those generated files and the end product
  195. # they end up in where applicable. Then, add build rules for those
  196. # directories
  197. if (exists &generatedir) {
  198. my %loopinfo = ( "dso" => [ @{$unified_info{engines}} ],
  199. "lib" => [ @{$unified_info{libraries}} ],
  200. "bin" => [ @{$unified_info{programs}} ],
  201. "script" => [ @{$unified_info{scripts}} ] );
  202. foreach my $type (keys %loopinfo) {
  203. foreach my $product (@{$loopinfo{$type}}) {
  204. my %dirs = ();
  205. my $pd = dirname($product);
  206. # We already have a "test" target, and the current directory
  207. # is just silly to make a target for
  208. $dirs{$pd} = 1 unless $pd eq "test" || $pd eq ".";
  209. foreach (@{$unified_info{sources}->{$product}}) {
  210. my $d = dirname($_);
  211. # We don't want to create targets for source directories
  212. # when building out of source
  213. next if ($config{sourcedir} ne $config{builddir}
  214. && $d =~ m|^\Q$config{sourcedir}\E|);
  215. # We already have a "test" target, and the current directory
  216. # is just silly to make a target for
  217. next if $d eq "test" || $d eq ".";
  218. $dirs{$d} = 1;
  219. push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
  220. if $d ne $pd;
  221. }
  222. foreach (keys %dirs) {
  223. push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
  224. $product;
  225. }
  226. }
  227. }
  228. }
  229. # Build mandatory generated headers
  230. foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
  231. # Build all known libraries, engines, programs and scripts.
  232. # Everything else will be handled as a consequence.
  233. foreach (@{$unified_info{libraries}}) { dolib($_); }
  234. foreach (@{$unified_info{engines}}) { doengine($_); }
  235. foreach (@{$unified_info{programs}}) { dobin($_); }
  236. foreach (@{$unified_info{scripts}}) { doscript($_); }
  237. foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
  238. # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
  239. # they are added here.
  240. $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
  241. -}