Configurations/common.tmpl: Rework dependency resolution
[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          $OUT .= src2obj(obj => $obj,
175                          product => $bin,
176                          srcs => $unified_info{sources}->{$obj},
177                          deps => $unified_info{depends}->{$obj},
178                          incs => [ @{$unified_info{includes}->{$obj}},
179                                    @{$unified_info{includes}->{$bin}} ],
180                          defs => [ @{$unified_info{defines}->{$obj}},
181                                    @{$unified_info{defines}->{$bin}} ],
182                          %opts);
183          foreach ((@{$unified_info{sources}->{$obj}},
184                    @{$unified_info{depends}->{$obj}})) {
185              dogenerate($_, $obj, $bin, %opts);
186          }
187      }
188      $cache{$obj} = 1;
189  }
190
191  # dolib is responsible for building libraries.  It will call
192  # obj2shlib is shared libraries are produced, and obj2lib in all
193  # cases.  It also makes sure all object files for the library are
194  # built.
195  sub dolib {
196      my $lib = shift;
197      return "" if $cache{$lib};
198      unless ($disabled{shared} || $lib =~ /\.a$/) {
199          my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
200          $OUT .= $obj2shlib->(lib => $lib,
201                               attrs => $unified_info{attributes}->{libraries}->{$lib},
202                               objs => $unified_info{shared_sources}->{$lib},
203                               deps => [ grep { $_ ne $lib } resolvedepends($lib) ]);
204          foreach ((@{$unified_info{shared_sources}->{$lib}},
205                    @{$unified_info{sources}->{$lib}})) {
206              # If this is somehow a compiled object, take care of it that way
207              # Otherwise, it might simply be generated
208              if (defined $unified_info{sources}->{$_}) {
209                  doobj($_, $lib, intent => "shlib",
210                        attrs => $unified_info{attributes}->{libraries}->{$lib});
211              } else {
212                  dogenerate($_, undef, undef, intent => "lib");
213              }
214          }
215      }
216      $OUT .= obj2lib(lib => $lib,
217                      attrs => $unified_info{attributes}->{libraries}->{$lib},
218                      objs => [ @{$unified_info{sources}->{$lib}} ]);
219      foreach (@{$unified_info{sources}->{$lib}}) {
220          doobj($_, $lib, intent => "lib",
221                attrs => $unified_info{attributes}->{libraries}->{$lib});
222      }
223      $cache{$lib} = 1;
224  }
225
226  # domodule is responsible for building modules.  It will call
227  # obj2dso, and also makes sure all object files for the library
228  # are built.
229  sub domodule {
230      my $module = shift;
231      return "" if $cache{$module};
232      $OUT .= obj2dso(module => $module,
233                      attrs => $unified_info{attributes}->{modules}->{$module},
234                      objs => $unified_info{sources}->{$module},
235                      deps => [ grep { $_ ne $module }
236                                resolvedepends($module) ]);
237      foreach (@{$unified_info{sources}->{$module}}) {
238          # If this is somehow a compiled object, take care of it that way
239          # Otherwise, it might simply be generated
240          if (defined $unified_info{sources}->{$_}) {
241              doobj($_, $module, intent => "dso",
242                    attrs => $unified_info{attributes}->{modules}->{$module});
243          } else {
244              dogenerate($_, undef, $module, intent => "dso");
245          }
246      }
247      $cache{$module} = 1;
248  }
249
250  # dobin is responsible for building programs.  It will call obj2bin,
251  # and also makes sure all object files for the library are built.
252  sub dobin {
253      my $bin = shift;
254      return "" if $cache{$bin};
255      $OUT .= obj2bin(bin => $bin,
256                      attrs => $unified_info{attributes}->{programs}->{$bin},
257                      objs => [ @{$unified_info{sources}->{$bin}} ],
258                      deps => [ grep { $_ ne $bin } resolvedepends($bin) ]);
259      foreach (@{$unified_info{sources}->{$bin}}) {
260          doobj($_, $bin, intent => "bin",
261                attrs => $unified_info{attributes}->{$bin});
262      }
263      $cache{$bin} = 1;
264  }
265
266  # dobin is responsible for building scripts from templates.  It will
267  # call in2script.
268  sub doscript {
269      my $script = shift;
270      return "" if $cache{$script};
271      $OUT .= in2script(script => $script,
272                        attrs => $unified_info{attributes}->{$script},
273                        sources => $unified_info{sources}->{$script});
274      $cache{$script} = 1;
275  }
276
277  sub dodir {
278      my $dir = shift;
279      return "" if !exists(&generatedir) or $cache{$dir};
280      $OUT .= generatedir(dir => $dir,
281                          deps => $unified_info{dirinfo}->{$dir}->{deps},
282                          %{$unified_info{dirinfo}->{$_}->{products}});
283      $cache{$dir} = 1;
284  }
285
286  # Start with populating the cache with all the overrides
287  %cache = map { $_ => 1 } @{$unified_info{overrides}};
288
289  # Build mandatory generated headers
290  foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
291
292  # Build all known libraries, modules, programs and scripts.
293  # Everything else will be handled as a consequence.
294  foreach (@{$unified_info{libraries}}) { dolib($_);    }
295  foreach (@{$unified_info{modules}})   { domodule($_); }
296  foreach (@{$unified_info{programs}})  { dobin($_);    }
297  foreach (@{$unified_info{scripts}})   { doscript($_); }
298
299  foreach (sort keys %{$unified_info{dirinfo}})  { dodir($_); }
300 -}