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