2
0

common.tmpl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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->(shlib => $unified_info{sharednames}->{$lib},
  126. lib => $lib,
  127. objs => $unified_info{shared_sources}->{$lib},
  128. deps => [ reducedepends(resolvedepends($lib)) ],
  129. installed => is_installed($lib));
  130. foreach ((@{$unified_info{shared_sources}->{$lib}},
  131. @{$unified_info{sources}->{$lib}})) {
  132. # If this is somehow a compiled object, take care of it that way
  133. # Otherwise, it might simply be generated
  134. if (defined $unified_info{sources}->{$_}) {
  135. doobj($_, $lib, intent => "shlib", installed => is_installed($lib));
  136. } else {
  137. dogenerate($_, undef, undef, intent => "lib");
  138. }
  139. }
  140. }
  141. $OUT .= obj2lib(lib => $lib,
  142. objs => [ @{$unified_info{sources}->{$lib}} ]);
  143. foreach (@{$unified_info{sources}->{$lib}}) {
  144. doobj($_, $lib, intent => "lib", installed => is_installed($lib));
  145. }
  146. $cache{$lib} = 1;
  147. }
  148. # doengine is responsible for building engines. It will call
  149. # obj2dso, and also makes sure all object files for the library
  150. # are built.
  151. sub doengine {
  152. my $lib = shift;
  153. return "" if $cache{$lib};
  154. $OUT .= obj2dso(lib => $lib,
  155. objs => $unified_info{shared_sources}->{$lib},
  156. deps => [ resolvedepends($lib) ],
  157. installed => is_installed($lib));
  158. foreach (@{$unified_info{shared_sources}->{$lib}}) {
  159. # If this is somehow a compiled object, take care of it that way
  160. # Otherwise, it might simply be generated
  161. if (defined $unified_info{sources}->{$_}) {
  162. doobj($_, $lib, intent => "dso", installed => is_installed($lib));
  163. } else {
  164. dogenerate($_, undef, $lib, intent => "dso");
  165. }
  166. }
  167. $cache{$lib} = 1;
  168. }
  169. # dobin is responsible for building programs. It will call obj2bin,
  170. # and also makes sure all object files for the library are built.
  171. sub dobin {
  172. my $bin = shift;
  173. return "" if $cache{$bin};
  174. my $deps = [ reducedepends(resolvedepends($bin)) ];
  175. $OUT .= obj2bin(bin => $bin,
  176. objs => [ @{$unified_info{sources}->{$bin}} ],
  177. deps => $deps,
  178. installed => is_installed($bin));
  179. foreach (@{$unified_info{sources}->{$bin}}) {
  180. doobj($_, $bin, intent => "bin", installed => is_installed($bin));
  181. }
  182. $cache{$bin} = 1;
  183. }
  184. # dobin is responsible for building scripts from templates. It will
  185. # call in2script.
  186. sub doscript {
  187. my $script = shift;
  188. return "" if $cache{$script};
  189. $OUT .= in2script(script => $script,
  190. sources => $unified_info{sources}->{$script},
  191. installed => is_installed($script));
  192. $cache{$script} = 1;
  193. }
  194. sub dodir {
  195. my $dir = shift;
  196. return "" if !exists(&generatedir) or $cache{$dir};
  197. $OUT .= generatedir(dir => $dir,
  198. deps => $unified_info{dirinfo}->{$dir}->{deps},
  199. %{$unified_info{dirinfo}->{$_}->{products}});
  200. $cache{$dir} = 1;
  201. }
  202. # Start with populating the cache with all the overrides
  203. %cache = map { $_ => 1 } @{$unified_info{overrides}};
  204. # Build mandatory generated headers
  205. foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
  206. # Build all known libraries, engines, programs and scripts.
  207. # Everything else will be handled as a consequence.
  208. foreach (@{$unified_info{libraries}}) { dolib($_); }
  209. foreach (@{$unified_info{engines}}) { doengine($_); }
  210. foreach (@{$unified_info{programs}}) { dobin($_); }
  211. foreach (@{$unified_info{scripts}}) { doscript($_); }
  212. foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
  213. # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
  214. # they are added here.
  215. $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
  216. -}