Ensure libssl uses the new library context aware CT code
[openssl.git] / Configurations / common.tmpl
1 {- # -*- Mode: perl -*-
2
3  use File::Basename;
4
5  my $debug_resolvedepends = $ENV{BUILDFILE_DEBUG_DEPENDS};
6  my $debug_rules = $ENV{BUILDFILE_DEBUG_RULES};
7
8  # A cache of objects for which a recipe has already been generated
9  my %cache;
10
11  # collectdepends, expanddepends and reducedepends work together to make
12  # sure there are no duplicate or weak dependencies and that they are in
13  # the right order.  This is used to sort the list of libraries  that a
14  # build depends on.
15  sub extensionlesslib {
16      my @result = map { $_ =~ /(\.a)?$/; $` } @_;
17      return @result if wantarray;
18      return $result[0];
19  }
20
21  # collectdepends dives into the tree of dependencies and returns
22  # a list of all the non-weak ones.
23  sub collectdepends {
24      return () unless @_;
25
26      my $thing = shift;
27      my $extensionlessthing = extensionlesslib($thing);
28      my @listsofar = @_;    # to check if we're looping
29      my @list = @{$unified_info{depends}->{$thing} //
30                       $unified_info{depends}->{$extensionlessthing}};
31      my @newlist = ();
32
33      print STDERR "DEBUG[collectdepends] $thing > ", join(' ', @listsofar), "\n"
34          if $debug_resolvedepends;
35      foreach my $item (@list) {
36          my $extensionlessitem = extensionlesslib($item);
37          # It's time to break off when the dependency list starts looping
38          next if grep { extensionlesslib($_) eq $extensionlessitem } @listsofar;
39          # Don't add anything here if the dependency is weak
40          next if defined $unified_info{attributes}->{depends}->{$thing}->{$item}->{'weak'};
41          my @resolved = collectdepends($item, @listsofar, $item);
42          push @newlist, $item, @resolved;
43      }
44      print STDERR "DEBUG[collectdepends] $thing < ", join(' ', @newlist), "\n"
45          if $debug_resolvedepends;
46      @newlist;
47  }
48
49  # expanddepends goes through a list of stuff, checks if they have any
50  # dependencies, and adds them at the end of the current position if
51  # they aren't already present later on.
52  sub expanddepends {
53      my @after = ( @_ );
54      print STDERR "DEBUG[expanddepends]> ", join(' ', @after), "\n"
55          if $debug_resolvedepends;
56      my @before = ();
57      while (@after) {
58          my $item = shift @after;
59          print STDERR "DEBUG[expanddepends]\\  ", join(' ', @before), "\n"
60              if $debug_resolvedepends;
61          print STDERR "DEBUG[expanddepends] - ", $item, "\n"
62              if $debug_resolvedepends;
63          my @middle = (
64              $item,
65              map {
66                  my $x = $_;
67                  my $extlessx = extensionlesslib($x);
68                  if (grep { $extlessx eq extensionlesslib($_) } @before
69                      and
70                      !grep { $extlessx eq extensionlesslib($_) } @after) {
71                      print STDERR "DEBUG[expanddepends] + ", $x, "\n"
72                          if $debug_resolvedepends;
73                      ( $x )
74                  } else {
75                      print STDERR "DEBUG[expanddepends] ! ", $x, "\n"
76                          if $debug_resolvedepends;
77                      ()
78                  }
79              } @{$unified_info{depends}->{$item} // []}
80          );
81          print STDERR "DEBUG[expanddepends] = ", join(' ', @middle), "\n"
82              if $debug_resolvedepends;
83          print STDERR "DEBUG[expanddepends]/  ", join(' ', @after), "\n"
84              if $debug_resolvedepends;
85          push @before, @middle;
86      }
87      print STDERR "DEBUG[expanddepends]< ", join(' ', @before), "\n"
88          if $debug_resolvedepends;
89      @before;
90  }
91
92  # reducedepends looks through a list, and checks if each item is
93  # repeated later on.  If it is, the earlier copy is dropped.
94  sub reducedepends {
95      my @list = @_;
96      print STDERR "DEBUG[reducedepends]> ", join(' ', @list), "\n"
97          if $debug_resolvedepends;
98      my @newlist = ();
99      my %replace = ();
100      while (@list) {
101          my $item = shift @list;
102          my $extensionlessitem = extensionlesslib($item);
103          if (grep { $extensionlessitem eq extensionlesslib($_) } @list) {
104              if ($item ne $extensionlessitem) {
105                  # If this instance of the library is explicitly static, we
106                  # prefer that to any shared library name, since it must have
107                  # been done on purpose.
108                  $replace{$extensionlessitem} = $item;
109              }
110          } else {
111              push @newlist, $item;
112          }
113      }
114      @newlist = map { $replace{$_} // $_; } @newlist;
115      print STDERR "DEBUG[reducedepends]< ", join(' ', @newlist), "\n"
116          if $debug_resolvedepends;
117      @newlist;
118  }
119
120  # Do it all
121  # This takes multiple inputs and combine them into a single list of
122  # interdependent things.  The returned value will include all the input.
123  # Callers are responsible for taking away the things they are building.
124  sub resolvedepends {
125      print STDERR "DEBUG[resolvedepends] START (", join(', ', @_), ")\n"
126          if $debug_resolvedepends;
127      my @all =
128          reducedepends(expanddepends(map { ( $_, collectdepends($_) ) } @_));
129      print STDERR "DEBUG[resolvedepends] END (", join(', ', @_), ") : ",
130          join(',', map { "\n    $_" } @all), "\n"
131          if $debug_resolvedepends;
132      @all;
133  }
134
135  # dogenerate is responsible for producing all the recipes that build
136  # generated source files.  It recurses in case a dependency is also a
137  # generated source file.
138  sub dogenerate {
139      my $src = shift;
140      return "" if $cache{$src};
141      my $obj = shift;
142      my $bin = shift;
143      my %opts = @_;
144      if ($unified_info{generate}->{$src}) {
145          die "$src is generated by Configure, should not appear in build file\n"
146              if ref $unified_info{generate}->{$src} eq "";
147          my $script = $unified_info{generate}->{$src}->[0];
148          $OUT .= generatesrc(src => $src,
149                              product => $bin,
150                              generator => $unified_info{generate}->{$src},
151                              generator_incs => $unified_info{includes}->{$script},
152                              generator_deps => $unified_info{depends}->{$script},
153                              deps => $unified_info{depends}->{$src},
154                              incs => [ defined $obj
155                                            ? @{$unified_info{includes}->{$obj}}
156                                            : (),
157                                        defined $bin
158                                            ? @{$unified_info{includes}->{$bin}}
159                                            : () ],
160                              defs => [ defined $obj
161                                            ? @{$unified_info{defines}->{$obj}}
162                                            : (),
163                                        defined $bin
164                                            ? @{$unified_info{defines}->{$bin}}
165                                            : () ],
166                              %opts);
167          foreach (@{$unified_info{depends}->{$src}}) {
168              dogenerate($_, $obj, $bin, %opts);
169          }
170      }
171      $cache{$src} = 1;
172  }
173
174  # doobj is responsible for producing all the recipes that build
175  # object files as well as dependency files.
176  sub doobj {
177      my $obj = shift;
178      return "" if $cache{$obj};
179      my $bin = shift;
180      my %opts = @_;
181      if (@{$unified_info{sources}->{$obj}}) {
182          my @srcs = @{$unified_info{sources}->{$obj}};
183          my @deps = @{$unified_info{depends}->{$obj}};
184          my @incs = ( @{$unified_info{includes}->{$obj}},
185                       @{$unified_info{includes}->{$bin}} );
186          my @defs = ( @{$unified_info{defines}->{$obj}},
187                       @{$unified_info{defines}->{$bin}} );
188          print STDERR "DEBUG[doobj] \@srcs for $obj ($bin) : ",
189              join(",", map { "\n    $_" } @srcs), "\n"
190              if $debug_rules;
191          print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
192              join(",", map { "\n    $_" } @deps), "\n"
193              if $debug_rules;
194          print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
195              join(",", map { "\n    $_" } @incs), "\n"
196              if $debug_rules;
197          print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
198              join(",", map { "\n    $_" } @defs), "\n"
199              if $debug_rules;
200          print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
201              join(",", map { "\n    $_ = $opts{$_}" } sort keys %opts), "\n"
202              if $debug_rules;
203          $OUT .= src2obj(obj => $obj, product => $bin,
204                          srcs => [ @srcs ], deps => [ @deps ],
205                          incs => [ @incs ], defs => [ @defs ],
206                          %opts);
207          foreach ((@{$unified_info{sources}->{$obj}},
208                    @{$unified_info{depends}->{$obj}})) {
209              dogenerate($_, $obj, $bin, %opts);
210          }
211      }
212      $cache{$obj} = 1;
213  }
214
215  # Helper functions to grab all applicable intermediary files.
216  # This is particularly useful when a library is given as source
217  # rather than a dependency.  In that case, we consider it to be a
218  # container with object file references, or possibly references
219  # to further libraries to pilfer in the same way.
220  sub getsrclibs {
221      my $section = shift;
222
223      # For all input, see if it sources static libraries.  If it does,
224      # return them together with the result of a recursive call.
225      map { ( $_, getsrclibs($section, $_) ) }
226      grep { $_ =~ m|\.a$| }
227      map { @{$unified_info{$section}->{$_} // []} }
228      @_;
229  }
230
231  sub getlibobjs {
232      my $section = shift;
233
234      # For all input, see if it's an intermediary file (library or object).
235      # If it is, collect the result of a recursive call, or if that returns
236      # an empty list, the element itself.  Return the result.
237      map {
238          my @x = getlibobjs($section, @{$unified_info{$section}->{$_}});
239          @x ? @x : ( $_ );
240      }
241      grep { defined $unified_info{$section}->{$_} }
242      @_;
243  }
244
245  # dolib is responsible for building libraries.  It will call
246  # obj2shlib if shared libraries are produced, and obj2lib in all
247  # cases.  It also makes sure all object files for the library are
248  # built.
249  sub dolib {
250      my $lib = shift;
251      return "" if $cache{$lib};
252
253      my %attrs = %{$unified_info{attributes}->{libraries}->{$lib}};
254
255      my @deps = ( resolvedepends(getsrclibs('sources', $lib)) );
256
257      # We support two types of objs, those who are specific to this library
258      # (they end up in @objs) and those that we get indirectly, via other
259      # libraries (they end up in @foreign_objs).  We get the latter any time
260      # someone has done something like this in build.info:
261      #     SOURCE[libfoo.a]=libbar.a
262      # The indirect object files must be kept in a separate array so they
263      # don't get rebuilt unnecessarily (and with incorrect auxiliary
264      # information).
265      #
266      # Object files can't be collected commonly for shared and static
267      # libraries, because we contain their respective object files in
268      # {shared_sources} and {sources}, and because the implications are
269      # slightly different for each library form.
270      #
271      # We grab all these "foreign" object files recursively with getlibobjs().
272
273      unless ($disabled{shared} || $lib =~ /\.a$/) {
274          my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
275          # If this library sources other static libraries and those
276          # libraries are marked {noinst}, there's no need to include
277          # all of their object files.  Instead, we treat those static
278          # libraries as dependents alongside any other library this
279          # one depends on, and let symbol resolution do its job.
280          my @sourced_libs = ();
281          my @objs = ();
282          my @foreign_objs = ();
283          my @deps = ();
284          foreach (@{$unified_info{shared_sources}->{$lib}}) {
285              if ($_ !~ m|\.a$|) {
286                  push @objs, $_;
287              } elsif ($unified_info{attributes}->{libraries}->{$_}->{noinst}) {
288                  push @deps, $_;
289              } else {
290                  push @deps, getsrclibs('sources', $_);
291                  push @foreign_objs, getlibobjs('sources', $_);
292              }
293          }
294          @deps = ( grep { $_ ne $lib } resolvedepends($lib, @deps) );
295          print STDERR "DEBUG[dolib:shlib] \%attrs for $lib : ", ,
296              join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
297              if %attrs && $debug_rules;
298          print STDERR "DEBUG[dolib:shlib] \@deps for $lib : ",
299              join(",", map { "\n    $_" } @deps), "\n"
300              if @deps && $debug_rules;
301          print STDERR "DEBUG[dolib:shlib] \@objs for $lib : ",
302              join(",", map { "\n    $_" } @objs), "\n"
303              if @objs && $debug_rules;
304          print STDERR "DEBUG[dolib:shlib] \@foreign_objs for $lib : ",
305              join(",", map { "\n    $_" } @foreign_objs), "\n"
306              if @foreign_objs && $debug_rules;
307          $OUT .= $obj2shlib->(lib => $lib,
308                               attrs => { %attrs },
309                               objs => [ @objs, @foreign_objs ],
310                               deps => [ @deps ]);
311          foreach (@objs) {
312              # If this is somehow a compiled object, take care of it that way
313              # Otherwise, it might simply be generated
314              if (defined $unified_info{sources}->{$_}) {
315                  if($_ =~ /\.a$/) {
316                      dolib($_);
317                  } else {
318                      doobj($_, $lib, intent => "shlib", attrs => { %attrs });
319                  }
320              } else {
321                  dogenerate($_, undef, undef, intent => "lib");
322              }
323          }
324      }
325      {
326          # When putting static libraries together, we cannot rely on any
327          # symbol resolution, so for all static libraries used as source for
328          # this one, as well as other libraries they depend on, we simply
329          # grab all their object files unconditionally,
330          # Symbol resolution will happen when any program, module or shared
331          # library is linked with this one.
332          my @objs = ();
333          my @sourcedeps = ();
334          my @foreign_objs = ();
335          foreach (@{$unified_info{sources}->{$lib}}) {
336              if ($_ !~ m|\.a$|) {
337                  push @objs, $_;
338              } else {
339                  push @sourcedeps, $_;
340              }
341          }
342          @sourcedeps = ( grep { $_ ne $lib } resolvedepends(@sourcedeps) );
343          print STDERR "DEBUG[dolib:lib] : \@sourcedeps for $_ : ",
344              join(",", map { "\n    $_" } @sourcedeps), "\n"
345              if @sourcedeps && $debug_rules;
346          @foreign_objs = getlibobjs('sources', @sourcedeps);
347          print STDERR "DEBUG[dolib:lib] \%attrs for $lib : ", ,
348              join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
349              if %attrs && $debug_rules;
350          print STDERR "DEBUG[dolib:lib] \@objs for $lib : ",
351              join(",", map { "\n    $_" } @objs), "\n"
352              if @objs && $debug_rules;
353          print STDERR "DEBUG[dolib:lib] \@foreign_objs for $lib : ",
354              join(",", map { "\n    $_" } @foreign_objs), "\n"
355              if @foreign_objs && $debug_rules;
356          $OUT .= obj2lib(lib => $lib, attrs => { %attrs },
357                          objs => [ @objs, @foreign_objs ]);
358          foreach (@objs) {
359              doobj($_, $lib, intent => "lib", attrs => { %attrs });
360          }
361      }
362      $cache{$lib} = 1;
363  }
364
365  # domodule is responsible for building modules.  It will call
366  # obj2dso, and also makes sure all object files for the library
367  # are built.
368  sub domodule {
369      my $module = shift;
370      return "" if $cache{$module};
371      my %attrs = %{$unified_info{attributes}->{modules}->{$module}};
372      my @objs = @{$unified_info{sources}->{$module}};
373      my @deps = ( grep { $_ ne $module }
374                   resolvedepends($module) );
375      print STDERR "DEBUG[domodule] \%attrs for $module :",
376          join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
377          if $debug_rules;
378      print STDERR "DEBUG[domodule] \@objs for $module : ",
379          join(",", map { "\n    $_" } @objs), "\n"
380          if $debug_rules;
381      print STDERR "DEBUG[domodule] \@deps for $module : ",
382          join(",", map { "\n    $_" } @deps), "\n"
383          if $debug_rules;
384      $OUT .= obj2dso(module => $module,
385                      attrs => { %attrs },
386                      objs => [ @objs ],
387                      deps => [ @deps ]);
388      foreach (@{$unified_info{sources}->{$module}}) {
389          # If this is somehow a compiled object, take care of it that way
390          # Otherwise, it might simply be generated
391          if (defined $unified_info{sources}->{$_}) {
392              doobj($_, $module, intent => "dso", attrs => { %attrs });
393          } else {
394              dogenerate($_, undef, $module, intent => "dso");
395          }
396      }
397      $cache{$module} = 1;
398  }
399
400  # dobin is responsible for building programs.  It will call obj2bin,
401  # and also makes sure all object files for the library are built.
402  sub dobin {
403      my $bin = shift;
404      return "" if $cache{$bin};
405      my %attrs = %{$unified_info{attributes}->{programs}->{$bin}};
406      my @objs = @{$unified_info{sources}->{$bin}};
407      my @deps = ( grep { $_ ne $bin } resolvedepends($bin) );
408      print STDERR "DEBUG[dobin] \%attrs for $bin : ",
409          join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
410          if %attrs && $debug_rules;
411      print STDERR "DEBUG[dobin] \@objs for $bin : ",
412          join(",", map { "\n    $_" } @objs), "\n"
413          if @objs && $debug_rules;
414      print STDERR "DEBUG[dobin] \@deps for $bin : ",
415          join(",", map { "\n    $_" } @deps), "\n"
416          if @deps && $debug_rules;
417      $OUT .= obj2bin(bin => $bin,
418                      attrs => { %attrs },
419                      objs => [ @objs ],
420                      deps => [ @deps ]);
421      foreach (@objs) {
422          doobj($_, $bin, intent => "bin", attrs => { %attrs });
423      }
424      $cache{$bin} = 1;
425  }
426
427  # dobin is responsible for building scripts from templates.  It will
428  # call in2script.
429  sub doscript {
430      my $script = shift;
431      return "" if $cache{$script};
432      $OUT .= in2script(script => $script,
433                        attrs => $unified_info{attributes}->{$script},
434                        sources => $unified_info{sources}->{$script});
435      $cache{$script} = 1;
436  }
437
438  sub dodir {
439      my $dir = shift;
440      return "" if !exists(&generatedir) or $cache{$dir};
441      $OUT .= generatedir(dir => $dir,
442                          deps => $unified_info{dirinfo}->{$dir}->{deps},
443                          %{$unified_info{dirinfo}->{$_}->{products}});
444      $cache{$dir} = 1;
445  }
446
447  # dodocs is responsible for building documentation from .pods.
448  # It will call generatesrc.
449  sub dodocs {
450      my $type = shift;
451      my $section = shift;
452      foreach my $doc (@{$unified_info{"${type}docs"}->{$section}}) {
453          next if $cache{$doc};
454          $OUT .= generatesrc(src => $doc,
455                              generator => $unified_info{generate}->{$doc});
456          foreach ((@{$unified_info{depends}->{$doc}})) {
457              dogenerate($_, undef, undef, %opts);
458          }
459          $cache{$doc} = 1;
460      }
461  }
462
463  # Start with populating the cache with all the overrides
464  %cache = map { $_ => 1 } @{$unified_info{overrides}};
465
466  # Build mandatory generated headers
467  foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
468
469  # Build all known libraries, modules, programs and scripts.
470  # Everything else will be handled as a consequence.
471  foreach (@{$unified_info{libraries}}) { dolib($_);    }
472  foreach (@{$unified_info{modules}})   { domodule($_); }
473  foreach (@{$unified_info{programs}})  { dobin($_);    }
474  foreach (@{$unified_info{scripts}})   { doscript($_); }
475  foreach (sort keys %{$unified_info{htmldocs}}) { dodocs('html', $_); }
476  foreach (sort keys %{$unified_info{mandocs}})  { dodocs('man', $_); }
477  foreach (sort keys %{$unified_info{dirinfo}})  { dodir($_); }
478 -}