Configure: DON'T trickle down includes from products to sources
[openssl.git] / Configurations / common.tmpl
1 {- # -*- Mode: perl -*-
2
3  use File::Basename;
4
5  # A cache of objects for which a recipe has already been generated
6  my %cache;
7
8  # resolvedepends and reducedepends work in tandem to make sure
9  # there are no duplicate dependencies and that they are in the
10  # right order.  This is especially used to sort the list of
11  # libraries that a build depends on.
12  sub extensionlesslib {
13      my @result = map { $_ =~ /(\.a)?$/; $` } @_;
14      return @result if wantarray;
15      return $result[0];
16  }
17  sub resolvedepends {
18      my $thing = shift;
19      my $extensionlessthing = extensionlesslib($thing);
20      my @listsofar = @_;    # to check if we're looping
21      my @list = @{$unified_info{depends}->{$thing} //
22                       $unified_info{depends}->{$extensionlessthing}};
23      my @newlist = ();
24      if (scalar @list) {
25          foreach my $item (@list) {
26              my $extensionlessitem = extensionlesslib($item);
27              # It's time to break off when the dependency list starts looping
28              next if grep { extensionlesslib($_) eq $extensionlessitem } @listsofar;
29              push @newlist, $item, resolvedepends($item, @listsofar, $item);
30          }
31      }
32      @newlist;
33  }
34  sub reducedepends {
35      my @list = @_;
36      my @newlist = ();
37      my %replace = ();
38      while (@list) {
39          my $item = shift @list;
40          my $extensionlessitem = extensionlesslib($item);
41          if (grep { $extensionlessitem eq extensionlesslib($_) } @list) {
42              if ($item ne $extensionlessitem) {
43                  # If this instance of the library is explicitly static, we
44                  # prefer that to any shared library name, since it must have
45                  # been done on purpose.
46                  $replace{$extensionlessitem} = $item;
47              }
48          } else {
49              push @newlist, $item;
50          }
51      }
52      map { $replace{$_} // $_; } @newlist;
53  }
54
55  # is_installed checks if a given file will be installed (i.e. they are
56  # not defined _NO_INST in build.info)
57  sub is_installed {
58      my $product = shift;
59      if (grep { $product eq $_ }
60          map { (@{$unified_info{install}->{$_}}) }
61          keys %{$unified_info{install}}) {
62          return 1;
63      }
64      return 0;
65  }
66
67  # dogenerate is responsible for producing all the recipes that build
68  # generated source files.  It recurses in case a dependency is also a
69  # generated source file.
70  sub dogenerate {
71      my $src = shift;
72      return "" if $cache{$src};
73      my $obj = shift;
74      my $bin = shift;
75      my %opts = @_;
76      if ($unified_info{generate}->{$src}) {
77          die "$src is generated by Configure, should not appear in build file\n"
78              if ref $unified_info{generate}->{$src} eq "";
79          my $script = $unified_info{generate}->{$src}->[0];
80          $OUT .= generatesrc(src => $src,
81                              generator => $unified_info{generate}->{$src},
82                              generator_incs => $unified_info{includes}->{$script},
83                              generator_deps => $unified_info{depends}->{$script},
84                              deps => $unified_info{depends}->{$src},
85                              incs => [ @{$unified_info{includes}->{$obj}},
86                                        @{$unified_info{includes}->{$bin}} ],
87                              %opts);
88          foreach (@{$unified_info{depends}->{$src}}) {
89              dogenerate($_, $obj, $bin, %opts);
90          }
91      }
92      $cache{$src} = 1;
93  }
94
95  # doobj is responsible for producing all the recipes that build
96  # object files as well as dependency files.
97  sub doobj {
98      my $obj = shift;
99      return "" if $cache{$obj};
100      my $bin = shift;
101      my %opts = @_;
102      if (@{$unified_info{sources}->{$obj}}) {
103          $OUT .= src2obj(obj => $obj,
104                          product => $bin,
105                          srcs => $unified_info{sources}->{$obj},
106                          deps => $unified_info{depends}->{$obj},
107                          incs => [ @{$unified_info{includes}->{$obj}},
108                                    @{$unified_info{includes}->{$bin}} ],
109                          %opts);
110          foreach ((@{$unified_info{sources}->{$obj}},
111                    @{$unified_info{depends}->{$obj}})) {
112              dogenerate($_, $obj, $bin, %opts);
113          }
114      }
115      $cache{$obj} = 1;
116  }
117
118  # dolib is responsible for building libraries.  It will call
119  # libobj2shlib is shared libraries are produced, and obj2lib in all
120  # cases.  It also makes sure all object files for the library are
121  # built.
122  sub dolib {
123      my $lib = shift;
124      return "" if $cache{$lib};
125      unless ($disabled{shared} || $lib =~ /\.a$/) {
126          $OUT .= libobj2shlib(shlib => $unified_info{sharednames}->{$lib},
127                               lib => $lib,
128                               objs => [ @{$unified_info{shared_sources}->{$lib}},
129                                         @{$unified_info{sources}->{$lib}} ],
130                               deps => [ reducedepends(resolvedepends($lib)) ],
131                               installed => is_installed($lib));
132          foreach ((@{$unified_info{shared_sources}->{$lib}},
133                    @{$unified_info{sources}->{$lib}})) {
134              # If this is somehow a compiled object, take care of it that way
135              # Otherwise, it might simply be generated
136              if (defined $unified_info{sources}->{$_}) {
137                  doobj($_, $lib, intent => "lib", installed => is_installed($lib));
138              } else {
139                  dogenerate($_, undef, undef, intent => "lib");
140              }
141          }
142      }
143      $OUT .= obj2lib(lib => $lib,
144                      objs => [ @{$unified_info{sources}->{$lib}} ]);
145      foreach (@{$unified_info{sources}->{$lib}}) {
146          doobj($_, $lib, intent => "lib", installed => is_installed($lib));
147      }
148      $cache{$lib} = 1;
149  }
150
151  # doengine is responsible for building engines.  It will call
152  # obj2dso, and also makes sure all object files for the library
153  # are built.
154  sub doengine {
155      my $lib = shift;
156      return "" if $cache{$lib};
157      $OUT .= obj2dso(lib => $lib,
158                      objs => [ @{$unified_info{sources}->{$lib}},
159                                @{$unified_info{shared_sources}->{$lib}} ],
160                      deps => [ resolvedepends($lib) ],
161                      installed => is_installed($lib));
162      foreach ((@{$unified_info{sources}->{$lib}},
163                @{$unified_info{shared_sources}->{$lib}})) {
164          doobj($_, $lib, intent => "dso", installed => is_installed($lib));
165      }
166      $cache{$lib} = 1;
167  }
168
169  # dobin is responsible for building programs.  It will call obj2bin,
170  # and also makes sure all object files for the library are built.
171  sub dobin {
172      my $bin = shift;
173      return "" if $cache{$bin};
174      my $deps = [ reducedepends(resolvedepends($bin)) ];
175      $OUT .= obj2bin(bin => $bin,
176                      objs => [ @{$unified_info{sources}->{$bin}} ],
177                      deps => $deps,
178                      installed => is_installed($bin));
179      foreach (@{$unified_info{sources}->{$bin}}) {
180          doobj($_, $bin, intent => "bin", installed => is_installed($bin));
181      }
182      $cache{$bin} = 1;
183  }
184
185  # dobin is responsible for building scripts from templates.  It will
186  # call in2script.
187  sub doscript {
188      my $script = shift;
189      return "" if $cache{$script};
190      $OUT .= in2script(script => $script,
191                        sources => $unified_info{sources}->{$script},
192                        installed => is_installed($script));
193      $cache{$script} = 1;
194  }
195
196  sub dodir {
197      my $dir = shift;
198      return "" if !exists(&generatedir) or $cache{$dir};
199      $OUT .= generatedir(dir => $dir,
200                          deps => $unified_info{dirinfo}->{$dir}->{deps},
201                          %{$unified_info{dirinfo}->{$_}->{products}});
202      $cache{$dir} = 1;
203  }
204
205  # Start with populating the cache with all the overrides
206  %cache = map { $_ => 1 } @{$unified_info{overrides}};
207
208  # For convenience collect information regarding directories where
209  # files are generated, those generated files and the end product
210  # they end up in where applicable.  Then, add build rules for those
211  # directories
212  if (exists &generatedir) {
213      my %loopinfo = ( "dso" => [ @{$unified_info{engines}} ],
214                       "lib" => [ @{$unified_info{libraries}} ],
215                       "bin" => [ @{$unified_info{programs}} ],
216                       "script" => [ @{$unified_info{scripts}} ] );
217      foreach my $type (keys %loopinfo) {
218          foreach my $product (@{$loopinfo{$type}}) {
219              my %dirs = ();
220              my $pd = dirname($product);
221
222              # We already have a "test" target, and the current directory
223              # is just silly to make a target for
224              $dirs{$pd} = 1 unless $pd eq "test" || $pd eq ".";
225
226              foreach (@{$unified_info{sources}->{$product}}) {
227                  my $d = dirname($_);
228
229                  # We don't want to create targets for source directories
230                  # when building out of source
231                  next if ($config{sourcedir} ne $config{builddir}
232                           && $d =~ m|^\Q$config{sourcedir}\E|);
233                  # We already have a "test" target, and the current directory
234                  # is just silly to make a target for
235                  next if $d eq "test" || $d eq ".";
236
237                  $dirs{$d} = 1;
238                  push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
239                      if $d ne $pd;
240              }
241              foreach (keys %dirs) {
242                  push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
243                  $product;
244              }
245          }
246      }
247  }
248
249  # Build mandatory generated headers
250  foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
251
252  # Build all known libraries, engines, programs and scripts.
253  # Everything else will be handled as a consequence.
254  foreach (@{$unified_info{libraries}}) { dolib($_);    }
255  foreach (@{$unified_info{engines}})   { doengine($_); }
256  foreach (@{$unified_info{programs}})  { dobin($_);    }
257  foreach (@{$unified_info{scripts}})   { doscript($_); }
258
259  foreach (sort keys %{$unified_info{dirinfo}})  { dodir($_); }
260
261  # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
262  # they are added here.
263  $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
264 -}