common.tmpl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. # dogenerate is responsible for producing all the recipes that build
  52. # generated source files. It recurses in case a dependency is also a
  53. # generated source file.
  54. sub dogenerate {
  55. my $src = shift;
  56. return "" if $cache{$src};
  57. my $obj = shift;
  58. my $bin = shift;
  59. my %opts = @_;
  60. if ($unified_info{generate}->{$src}) {
  61. die "$src is generated by Configure, should not appear in build file\n"
  62. if ref $unified_info{generate}->{$src} eq "";
  63. my $script = $unified_info{generate}->{$src}->[0];
  64. $OUT .= generatesrc(src => $src,
  65. product => $bin,
  66. generator => $unified_info{generate}->{$src},
  67. generator_incs => $unified_info{includes}->{$script},
  68. generator_deps => $unified_info{depends}->{$script},
  69. deps => $unified_info{depends}->{$src},
  70. incs => [ @{$unified_info{includes}->{$obj}},
  71. @{$unified_info{includes}->{$bin}} ],
  72. defs => [ @{$unified_info{defines}->{$obj}},
  73. @{$unified_info{defines}->{$bin}} ],
  74. %opts);
  75. foreach (@{$unified_info{depends}->{$src}}) {
  76. dogenerate($_, $obj, $bin, %opts);
  77. }
  78. }
  79. $cache{$src} = 1;
  80. }
  81. # doobj is responsible for producing all the recipes that build
  82. # object files as well as dependency files.
  83. sub doobj {
  84. my $obj = shift;
  85. return "" if $cache{$obj};
  86. my $bin = shift;
  87. my %opts = @_;
  88. if (@{$unified_info{sources}->{$obj}}) {
  89. $OUT .= src2obj(obj => $obj,
  90. product => $bin,
  91. srcs => $unified_info{sources}->{$obj},
  92. deps => $unified_info{depends}->{$obj},
  93. incs => [ @{$unified_info{includes}->{$obj}},
  94. @{$unified_info{includes}->{$bin}} ],
  95. defs => [ @{$unified_info{defines}->{$obj}},
  96. @{$unified_info{defines}->{$bin}} ],
  97. %opts);
  98. foreach ((@{$unified_info{sources}->{$obj}},
  99. @{$unified_info{depends}->{$obj}})) {
  100. dogenerate($_, $obj, $bin, %opts);
  101. }
  102. }
  103. $cache{$obj} = 1;
  104. }
  105. # dolib is responsible for building libraries. It will call
  106. # obj2shlib is shared libraries are produced, and obj2lib in all
  107. # cases. It also makes sure all object files for the library are
  108. # built.
  109. sub dolib {
  110. my $lib = shift;
  111. return "" if $cache{$lib};
  112. unless ($disabled{shared} || $lib =~ /\.a$/) {
  113. my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
  114. $OUT .= $obj2shlib->(lib => $lib,
  115. attrs => $unified_info{attributes}->{$lib},
  116. objs => $unified_info{shared_sources}->{$lib},
  117. deps => [ reducedepends(resolvedepends($lib)) ]);
  118. foreach ((@{$unified_info{shared_sources}->{$lib}},
  119. @{$unified_info{sources}->{$lib}})) {
  120. # If this is somehow a compiled object, take care of it that way
  121. # Otherwise, it might simply be generated
  122. if (defined $unified_info{sources}->{$_}) {
  123. doobj($_, $lib, intent => "shlib",
  124. attrs => $unified_info{attributes}->{$lib});
  125. } else {
  126. dogenerate($_, undef, undef, intent => "lib");
  127. }
  128. }
  129. }
  130. $OUT .= obj2lib(lib => $lib,
  131. attrs => $unified_info{attributes}->{$lib},
  132. objs => [ @{$unified_info{sources}->{$lib}} ]);
  133. foreach (@{$unified_info{sources}->{$lib}}) {
  134. doobj($_, $lib, intent => "lib",
  135. attrs => $unified_info{attributes}->{$lib});
  136. }
  137. $cache{$lib} = 1;
  138. }
  139. # domodule is responsible for building modules. It will call
  140. # obj2dso, and also makes sure all object files for the library
  141. # are built.
  142. sub domodule {
  143. my $lib = shift;
  144. return "" if $cache{$lib};
  145. $OUT .= obj2dso(lib => $lib,
  146. attrs => $unified_info{attributes}->{$lib},
  147. objs => $unified_info{sources}->{$lib},
  148. deps => [ resolvedepends($lib) ]);
  149. foreach (@{$unified_info{sources}->{$lib}}) {
  150. # If this is somehow a compiled object, take care of it that way
  151. # Otherwise, it might simply be generated
  152. if (defined $unified_info{sources}->{$_}) {
  153. doobj($_, $lib, intent => "dso",
  154. attrs => $unified_info{attributes}->{$lib});
  155. } else {
  156. dogenerate($_, undef, $lib, intent => "dso");
  157. }
  158. }
  159. $cache{$lib} = 1;
  160. }
  161. # dobin is responsible for building programs. It will call obj2bin,
  162. # and also makes sure all object files for the library are built.
  163. sub dobin {
  164. my $bin = shift;
  165. return "" if $cache{$bin};
  166. my $deps = [ reducedepends(resolvedepends($bin)) ];
  167. $OUT .= obj2bin(bin => $bin,
  168. attrs => $unified_info{attributes}->{$bin},
  169. objs => [ @{$unified_info{sources}->{$bin}} ],
  170. deps => $deps);
  171. foreach (@{$unified_info{sources}->{$bin}}) {
  172. doobj($_, $bin, intent => "bin",
  173. attrs => $unified_info{attributes}->{$bin});
  174. }
  175. $cache{$bin} = 1;
  176. }
  177. # dobin is responsible for building scripts from templates. It will
  178. # call in2script.
  179. sub doscript {
  180. my $script = shift;
  181. return "" if $cache{$script};
  182. $OUT .= in2script(script => $script,
  183. attrs => $unified_info{attributes}->{$script},
  184. sources => $unified_info{sources}->{$script});
  185. $cache{$script} = 1;
  186. }
  187. sub dodir {
  188. my $dir = shift;
  189. return "" if !exists(&generatedir) or $cache{$dir};
  190. $OUT .= generatedir(dir => $dir,
  191. deps => $unified_info{dirinfo}->{$dir}->{deps},
  192. %{$unified_info{dirinfo}->{$_}->{products}});
  193. $cache{$dir} = 1;
  194. }
  195. # Start with populating the cache with all the overrides
  196. %cache = map { $_ => 1 } @{$unified_info{overrides}};
  197. # Build mandatory generated headers
  198. foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
  199. # Build all known libraries, modules, programs and scripts.
  200. # Everything else will be handled as a consequence.
  201. foreach (@{$unified_info{libraries}}) { dolib($_); }
  202. foreach (@{$unified_info{modules}}) { domodule($_); }
  203. foreach (@{$unified_info{programs}}) { dobin($_); }
  204. foreach (@{$unified_info{scripts}}) { doscript($_); }
  205. foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
  206. -}