common.tmpl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 resolvedepends {
  10. my $thing = shift;
  11. my @listsofar = @_; # to check if we're looping
  12. my @list = @{$unified_info{depends}->{$thing}};
  13. my @newlist = ();
  14. if (scalar @list) {
  15. foreach my $item (@list) {
  16. # It's time to break off when the dependency list starts looping
  17. next if grep { $_ eq $item } @listsofar;
  18. push @newlist, $item, resolvedepends($item, @listsofar, $item);
  19. }
  20. }
  21. @newlist;
  22. }
  23. sub reducedepends {
  24. my @list = @_;
  25. my @newlist = ();
  26. while (@list) {
  27. my $item = shift @list;
  28. push @newlist, $item
  29. unless grep { $item eq $_ } @list;
  30. }
  31. @newlist;
  32. }
  33. # dogenerate is responsible for producing all the recipes that build
  34. # generated source files. It recurses in case a dependency is also a
  35. # generated source file.
  36. sub dogenerate {
  37. my $src = shift;
  38. return "" if $cache{$src};
  39. my $obj = shift;
  40. my $bin = shift;
  41. my %opts = @_;
  42. if ($unified_info{generate}->{$src}) {
  43. die "$src is generated by Configure, should not appear in build file\n"
  44. if ref $unified_info{generate}->{$src} eq "";
  45. my $script = $unified_info{generate}->{$src}->[0];
  46. $OUT .= generatesrc(src => $src,
  47. generator => $unified_info{generate}->{$src},
  48. generator_incs => $unified_info{includes}->{$script},
  49. generator_deps => $unified_info{depends}->{$script},
  50. deps => $unified_info{depends}->{$src},
  51. incs => $unified_info{includes}->{$obj},
  52. %opts);
  53. foreach (@{$unified_info{depends}->{$src}}) {
  54. dogenerate($_, $obj, $bin, %opts);
  55. }
  56. }
  57. $cache{$src} = 1;
  58. }
  59. # doobj is responsible for producing all the recipes that build
  60. # object files as well as dependency files.
  61. sub doobj {
  62. my $obj = shift;
  63. return "" if $cache{$obj};
  64. (my $obj_no_o = $obj) =~ s|\.o$||;
  65. my $bin = shift;
  66. my %opts = @_;
  67. if (@{$unified_info{sources}->{$obj}}) {
  68. $OUT .= src2obj(obj => $obj_no_o,
  69. srcs => $unified_info{sources}->{$obj},
  70. deps => $unified_info{depends}->{$obj},
  71. incs => $unified_info{includes}->{$obj},
  72. %opts);
  73. foreach ((@{$unified_info{sources}->{$obj}},
  74. @{$unified_info{depends}->{$obj}})) {
  75. dogenerate($_, $obj, $bin, %opts);
  76. }
  77. }
  78. $cache{$obj} = 1;
  79. }
  80. # dolib is responsible for building libraries. It will call
  81. # libobj2shlib is shared libraries are produced, and obj2lib in all
  82. # cases. It also makes sure all object files for the library are
  83. # built.
  84. sub dolib {
  85. my $lib = shift;
  86. return "" if $cache{$lib};
  87. unless ($disabled{shared}) {
  88. my %ordinals =
  89. $unified_info{ordinals}->{$lib}
  90. ? (ordinals => $unified_info{ordinals}->{$lib}) : ();
  91. $OUT .= libobj2shlib(shlib => $unified_info{sharednames}->{$lib},
  92. lib => $lib,
  93. objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
  94. (@{$unified_info{sources}->{$lib}},
  95. @{$unified_info{shared_sources}->{$lib}}) ],
  96. deps => [ reducedepends(resolvedepends($lib)) ],
  97. %ordinals);
  98. foreach (@{$unified_info{shared_sources}->{$lib}}) {
  99. doobj($_, $lib, intent => "lib");
  100. }
  101. }
  102. $OUT .= obj2lib(lib => $lib,
  103. objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
  104. @{$unified_info{sources}->{$lib}} ]);
  105. foreach (@{$unified_info{sources}->{$lib}}) {
  106. doobj($_, $lib, intent => "lib");
  107. }
  108. $cache{$lib} = 1;
  109. }
  110. # doengine is responsible for building engines. It will call
  111. # obj2dso, and also makes sure all object files for the library
  112. # are built.
  113. sub doengine {
  114. my $lib = shift;
  115. return "" if $cache{$lib};
  116. $OUT .= obj2dso(lib => $lib,
  117. objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
  118. (@{$unified_info{sources}->{$lib}},
  119. @{$unified_info{shared_sources}->{$lib}}) ],
  120. deps => [ resolvedepends($lib) ]);
  121. foreach ((@{$unified_info{sources}->{$lib}},
  122. @{$unified_info{shared_sources}->{$lib}})) {
  123. doobj($_, $lib, intent => "dso");
  124. }
  125. $cache{$lib} = 1;
  126. }
  127. # dobin is responsible for building programs. It will call obj2bin,
  128. # and also makes sure all object files for the library are built.
  129. sub dobin {
  130. my $bin = shift;
  131. return "" if $cache{$bin};
  132. my $deps = [ reducedepends(resolvedepends($bin)) ];
  133. $OUT .= obj2bin(bin => $bin,
  134. objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
  135. @{$unified_info{sources}->{$bin}} ],
  136. deps => $deps);
  137. foreach (@{$unified_info{sources}->{$bin}}) {
  138. doobj($_, $bin, intent => "bin");
  139. }
  140. $cache{$bin} = 1;
  141. }
  142. # dobin is responsible for building scripts from templates. It will
  143. # call in2script.
  144. sub doscript {
  145. my $script = shift;
  146. return "" if $cache{$script};
  147. $OUT .= in2script(script => $script,
  148. sources => $unified_info{sources}->{$script});
  149. $cache{$script} = 1;
  150. }
  151. sub dodir {
  152. my $dir = shift;
  153. return "" if !exists(&generatedir) or $cache{$dir};
  154. $OUT .= generatedir(dir => $dir,
  155. deps => $unified_info{dirinfo}->{$dir}->{deps},
  156. %{$unified_info{dirinfo}->{$_}->{products}});
  157. $cache{$dir} = 1;
  158. }
  159. # Start with populating the cache with all the overrides
  160. %cache = map { $_ => 1 } @{$unified_info{overrides}};
  161. # For convenience collect information regarding directories where
  162. # files are generated, those generated files and the end product
  163. # they end up in where applicable. Then, add build rules for those
  164. # directories
  165. if (exists &generatedir) {
  166. my %loopinfo = ( "dso" => [ @{$unified_info{engines}} ],
  167. "lib" => [ @{$unified_info{libraries}} ],
  168. "bin" => [ @{$unified_info{programs}} ],
  169. "script" => [ @{$unified_info{scripts}} ] );
  170. foreach my $type (keys %loopinfo) {
  171. foreach my $product (@{$loopinfo{$type}}) {
  172. my %dirs = ();
  173. my $pd = dirname($product);
  174. # We already have a "test" target, and the current directory
  175. # is just silly to make a target for
  176. $dirs{$pd} = 1 unless $pd eq "test" || $pd eq ".";
  177. foreach (@{$unified_info{sources}->{$product}}) {
  178. my $d = dirname($_);
  179. # We don't want to create targets for source directories
  180. # when building out of source
  181. next if ($config{sourcedir} ne $config{builddir}
  182. && $d =~ m|^\Q$config{sourcedir}\E|);
  183. # We already have a "test" target, and the current directory
  184. # is just silly to make a target for
  185. next if $d eq "test" || $d eq ".";
  186. $dirs{$d} = 1;
  187. push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
  188. if $d ne $pd;
  189. }
  190. foreach (keys %dirs) {
  191. push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
  192. $product;
  193. }
  194. }
  195. }
  196. }
  197. # Build mandatory generated headers
  198. foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
  199. # Build all known libraries, engines, programs and scripts.
  200. # Everything else will be handled as a consequence.
  201. foreach (@{$unified_info{libraries}}) { dolib($_); }
  202. foreach (@{$unified_info{engines}}) { doengine($_); }
  203. foreach (@{$unified_info{programs}}) { dobin($_); }
  204. foreach (@{$unified_info{scripts}}) { doscript($_); }
  205. foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
  206. # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
  207. # they are added here.
  208. $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
  209. -}