Add testing for non-default library context into evp_extra_test
[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      # Safety measure
141      return "" unless defined $unified_info{generate}->{$_};
142      return "" if $cache{$src};
143      my $obj = shift;
144      my $bin = shift;
145      my %opts = @_;
146      if ($unified_info{generate}->{$src}) {
147          die "$src is generated by Configure, should not appear in build file\n"
148              if ref $unified_info{generate}->{$src} eq "";
149          my $script = $unified_info{generate}->{$src}->[0];
150          $OUT .= generatesrc(src => $src,
151                              product => $bin,
152                              generator => $unified_info{generate}->{$src},
153                              generator_incs => $unified_info{includes}->{$script},
154                              generator_deps => $unified_info{depends}->{$script},
155                              deps => $unified_info{depends}->{$src},
156                              incs => [ defined $obj
157                                            ? @{$unified_info{includes}->{$obj}}
158                                            : (),
159                                        defined $bin
160                                            ? @{$unified_info{includes}->{$bin}}
161                                            : () ],
162                              defs => [ defined $obj
163                                            ? @{$unified_info{defines}->{$obj}}
164                                            : (),
165                                        defined $bin
166                                            ? @{$unified_info{defines}->{$bin}}
167                                            : () ],
168                              %opts);
169          foreach (@{$unified_info{depends}->{$src}}) {
170              dogenerate($_, $obj, $bin, %opts);
171          }
172      }
173      $cache{$src} = 1;
174  }
175
176  sub dotarget {
177      my $target = shift;
178      return "" if $cache{$target};
179      $OUT .= generatetarget(target => $target,
180                             deps => $unified_info{depends}->{$target});
181      foreach (@{$unified_info{depends}->{$target}}) {
182          dogenerate($_);
183      }
184      $cache{$target} = 1;
185  }
186
187  # doobj is responsible for producing all the recipes that build
188  # object files as well as dependency files.
189  sub doobj {
190      my $obj = shift;
191      return "" if $cache{$obj};
192      my $bin = shift;
193      my %opts = @_;
194      if (@{$unified_info{sources}->{$obj}}) {
195          my @srcs = @{$unified_info{sources}->{$obj}};
196          my @deps = @{$unified_info{depends}->{$obj}};
197          my @incs = ( @{$unified_info{includes}->{$obj}},
198                       @{$unified_info{includes}->{$bin}} );
199          my @defs = ( @{$unified_info{defines}->{$obj}},
200                       @{$unified_info{defines}->{$bin}} );
201          print STDERR "DEBUG[doobj] \@srcs for $obj ($bin) : ",
202              join(",", map { "\n    $_" } @srcs), "\n"
203              if $debug_rules;
204          print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
205              join(",", map { "\n    $_" } @deps), "\n"
206              if $debug_rules;
207          print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
208              join(",", map { "\n    $_" } @incs), "\n"
209              if $debug_rules;
210          print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
211              join(",", map { "\n    $_" } @defs), "\n"
212              if $debug_rules;
213          print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
214              join(",", map { "\n    $_ = $opts{$_}" } sort keys %opts), "\n"
215              if $debug_rules;
216          $OUT .= src2obj(obj => $obj, product => $bin,
217                          srcs => [ @srcs ], deps => [ @deps ],
218                          incs => [ @incs ], defs => [ @defs ],
219                          %opts);
220          foreach ((@{$unified_info{sources}->{$obj}},
221                    @{$unified_info{depends}->{$obj}})) {
222              dogenerate($_, $obj, $bin, %opts);
223          }
224      }
225      $cache{$obj} = 1;
226  }
227
228  # Helper functions to grab all applicable intermediary files.
229  # This is particularly useful when a library is given as source
230  # rather than a dependency.  In that case, we consider it to be a
231  # container with object file references, or possibly references
232  # to further libraries to pilfer in the same way.
233  sub getsrclibs {
234      my $section = shift;
235
236      # For all input, see if it sources static libraries.  If it does,
237      # return them together with the result of a recursive call.
238      map { ( $_, getsrclibs($section, $_) ) }
239      grep { $_ =~ m|\.a$| }
240      map { @{$unified_info{$section}->{$_} // []} }
241      @_;
242  }
243
244  sub getlibobjs {
245      my $section = shift;
246
247      # For all input, see if it's an intermediary file (library or object).
248      # If it is, collect the result of a recursive call, or if that returns
249      # an empty list, the element itself.  Return the result.
250      map {
251          my @x = getlibobjs($section, @{$unified_info{$section}->{$_}});
252          @x ? @x : ( $_ );
253      }
254      grep { defined $unified_info{$section}->{$_} }
255      @_;
256  }
257
258  # dolib is responsible for building libraries.  It will call
259  # obj2shlib if shared libraries are produced, and obj2lib in all
260  # cases.  It also makes sure all object files for the library are
261  # built.
262  sub dolib {
263      my $lib = shift;
264      return "" if $cache{$lib};
265
266      my %attrs = %{$unified_info{attributes}->{libraries}->{$lib}};
267
268      my @deps = ( resolvedepends(getsrclibs('sources', $lib)) );
269
270      # We support two types of objs, those who are specific to this library
271      # (they end up in @objs) and those that we get indirectly, via other
272      # libraries (they end up in @foreign_objs).  We get the latter any time
273      # someone has done something like this in build.info:
274      #     SOURCE[libfoo.a]=libbar.a
275      # The indirect object files must be kept in a separate array so they
276      # don't get rebuilt unnecessarily (and with incorrect auxiliary
277      # information).
278      #
279      # Object files can't be collected commonly for shared and static
280      # libraries, because we contain their respective object files in
281      # {shared_sources} and {sources}, and because the implications are
282      # slightly different for each library form.
283      #
284      # We grab all these "foreign" object files recursively with getlibobjs().
285
286      unless ($disabled{shared} || $lib =~ /\.a$/) {
287          my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
288          # If this library sources other static libraries and those
289          # libraries are marked {noinst}, there's no need to include
290          # all of their object files.  Instead, we treat those static
291          # libraries as dependents alongside any other library this
292          # one depends on, and let symbol resolution do its job.
293          my @sourced_libs = ();
294          my @objs = ();
295          my @foreign_objs = ();
296          my @deps = ();
297          foreach (@{$unified_info{shared_sources}->{$lib}}) {
298              if ($_ !~ m|\.a$|) {
299                  push @objs, $_;
300              } elsif ($unified_info{attributes}->{libraries}->{$_}->{noinst}) {
301                  push @deps, $_;
302              } else {
303                  push @deps, getsrclibs('sources', $_);
304                  push @foreign_objs, getlibobjs('sources', $_);
305              }
306          }
307          @deps = ( grep { $_ ne $lib } resolvedepends($lib, @deps) );
308          print STDERR "DEBUG[dolib:shlib] \%attrs for $lib : ", ,
309              join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
310              if %attrs && $debug_rules;
311          print STDERR "DEBUG[dolib:shlib] \@deps for $lib : ",
312              join(",", map { "\n    $_" } @deps), "\n"
313              if @deps && $debug_rules;
314          print STDERR "DEBUG[dolib:shlib] \@objs for $lib : ",
315              join(",", map { "\n    $_" } @objs), "\n"
316              if @objs && $debug_rules;
317          print STDERR "DEBUG[dolib:shlib] \@foreign_objs for $lib : ",
318              join(",", map { "\n    $_" } @foreign_objs), "\n"
319              if @foreign_objs && $debug_rules;
320          $OUT .= $obj2shlib->(lib => $lib,
321                               attrs => { %attrs },
322                               objs => [ @objs, @foreign_objs ],
323                               deps => [ @deps ]);
324          foreach (@objs) {
325              # If this is somehow a compiled object, take care of it that way
326              # Otherwise, it might simply be generated
327              if (defined $unified_info{sources}->{$_}) {
328                  if($_ =~ /\.a$/) {
329                      dolib($_);
330                  } else {
331                      doobj($_, $lib, intent => "shlib", attrs => { %attrs });
332                  }
333              } else {
334                  dogenerate($_, undef, undef, intent => "lib");
335              }
336          }
337      }
338      {
339          # When putting static libraries together, we cannot rely on any
340          # symbol resolution, so for all static libraries used as source for
341          # this one, as well as other libraries they depend on, we simply
342          # grab all their object files unconditionally,
343          # Symbol resolution will happen when any program, module or shared
344          # library is linked with this one.
345          my @objs = ();
346          my @sourcedeps = ();
347          my @foreign_objs = ();
348          foreach (@{$unified_info{sources}->{$lib}}) {
349              if ($_ !~ m|\.a$|) {
350                  push @objs, $_;
351              } else {
352                  push @sourcedeps, $_;
353              }
354          }
355          @sourcedeps = ( grep { $_ ne $lib } resolvedepends(@sourcedeps) );
356          print STDERR "DEBUG[dolib:lib] : \@sourcedeps for $_ : ",
357              join(",", map { "\n    $_" } @sourcedeps), "\n"
358              if @sourcedeps && $debug_rules;
359          @foreign_objs = getlibobjs('sources', @sourcedeps);
360          print STDERR "DEBUG[dolib:lib] \%attrs for $lib : ", ,
361              join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
362              if %attrs && $debug_rules;
363          print STDERR "DEBUG[dolib:lib] \@objs for $lib : ",
364              join(",", map { "\n    $_" } @objs), "\n"
365              if @objs && $debug_rules;
366          print STDERR "DEBUG[dolib:lib] \@foreign_objs for $lib : ",
367              join(",", map { "\n    $_" } @foreign_objs), "\n"
368              if @foreign_objs && $debug_rules;
369          $OUT .= obj2lib(lib => $lib, attrs => { %attrs },
370                          objs => [ @objs, @foreign_objs ]);
371          foreach (@objs) {
372              doobj($_, $lib, intent => "lib", attrs => { %attrs });
373          }
374      }
375      $cache{$lib} = 1;
376  }
377
378  # domodule is responsible for building modules.  It will call
379  # obj2dso, and also makes sure all object files for the library
380  # are built.
381  sub domodule {
382      my $module = shift;
383      return "" if $cache{$module};
384      my %attrs = %{$unified_info{attributes}->{modules}->{$module}};
385      my @objs = @{$unified_info{sources}->{$module}};
386      my @deps = ( grep { $_ ne $module }
387                   resolvedepends($module) );
388      print STDERR "DEBUG[domodule] \%attrs for $module :",
389          join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
390          if $debug_rules;
391      print STDERR "DEBUG[domodule] \@objs for $module : ",
392          join(",", map { "\n    $_" } @objs), "\n"
393          if $debug_rules;
394      print STDERR "DEBUG[domodule] \@deps for $module : ",
395          join(",", map { "\n    $_" } @deps), "\n"
396          if $debug_rules;
397      $OUT .= obj2dso(module => $module,
398                      attrs => { %attrs },
399                      objs => [ @objs ],
400                      deps => [ @deps ]);
401      foreach (@{$unified_info{sources}->{$module}}) {
402          # If this is somehow a compiled object, take care of it that way
403          # Otherwise, it might simply be generated
404          if (defined $unified_info{sources}->{$_}) {
405              doobj($_, $module, intent => "dso", attrs => { %attrs });
406          } else {
407              dogenerate($_, undef, $module, intent => "dso");
408          }
409      }
410      $cache{$module} = 1;
411  }
412
413  # dobin is responsible for building programs.  It will call obj2bin,
414  # and also makes sure all object files for the library are built.
415  sub dobin {
416      my $bin = shift;
417      return "" if $cache{$bin};
418      my %attrs = %{$unified_info{attributes}->{programs}->{$bin}};
419      my @objs = @{$unified_info{sources}->{$bin}};
420      my @deps = ( grep { $_ ne $bin } resolvedepends($bin) );
421      print STDERR "DEBUG[dobin] \%attrs for $bin : ",
422          join(",", map { "\n    $_ = $attrs{$_}" } sort keys %attrs), "\n"
423          if %attrs && $debug_rules;
424      print STDERR "DEBUG[dobin] \@objs for $bin : ",
425          join(",", map { "\n    $_" } @objs), "\n"
426          if @objs && $debug_rules;
427      print STDERR "DEBUG[dobin] \@deps for $bin : ",
428          join(",", map { "\n    $_" } @deps), "\n"
429          if @deps && $debug_rules;
430      $OUT .= obj2bin(bin => $bin,
431                      attrs => { %attrs },
432                      objs => [ @objs ],
433                      deps => [ @deps ]);
434      foreach (@objs) {
435          doobj($_, $bin, intent => "bin", attrs => { %attrs });
436      }
437      $cache{$bin} = 1;
438  }
439
440  # dobin is responsible for building scripts from templates.  It will
441  # call in2script.
442  sub doscript {
443      my $script = shift;
444      return "" if $cache{$script};
445      $OUT .= in2script(script => $script,
446                        attrs => $unified_info{attributes}->{$script},
447                        sources => $unified_info{sources}->{$script});
448      $cache{$script} = 1;
449  }
450
451  sub dodir {
452      my $dir = shift;
453      return "" if !exists(&generatedir) or $cache{$dir};
454      $OUT .= generatedir(dir => $dir,
455                          deps => $unified_info{dirinfo}->{$dir}->{deps},
456                          %{$unified_info{dirinfo}->{$_}->{products}});
457      $cache{$dir} = 1;
458  }
459
460  # dodocs is responsible for building documentation from .pods.
461  # It will call generatesrc.
462  sub dodocs {
463      my $type = shift;
464      my $section = shift;
465      foreach my $doc (@{$unified_info{"${type}docs"}->{$section}}) {
466          next if $cache{$doc};
467          $OUT .= generatesrc(src => $doc,
468                              generator => $unified_info{generate}->{$doc});
469          foreach ((@{$unified_info{depends}->{$doc}})) {
470              dogenerate($_, undef, undef, %opts);
471          }
472          $cache{$doc} = 1;
473      }
474  }
475
476  # Start with populating the cache with all the overrides
477  %cache = map { $_ => 1 } @{$unified_info{overrides}};
478
479  # Build mandatory header file generators
480  foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
481
482  # Build all known targets, libraries, modules, programs and scripts.
483  # Everything else will be handled as a consequence.
484  foreach (@{$unified_info{targets}})   { dotarget($_); }
485  foreach (@{$unified_info{libraries}}) { dolib($_);    }
486  foreach (@{$unified_info{modules}})   { domodule($_); }
487  foreach (@{$unified_info{programs}})  { dobin($_);    }
488  foreach (@{$unified_info{scripts}})   { doscript($_); }
489  foreach (sort keys %{$unified_info{htmldocs}}) { dodocs('html', $_); }
490  foreach (sort keys %{$unified_info{mandocs}})  { dodocs('man', $_); }
491  foreach (sort keys %{$unified_info{dirinfo}})  { dodir($_); }
492 -}