94e49319a94d8310c9424e112e23004e7ca1976f
[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                               deps => [ reducedepends(resolvedepends($lib)) ],
130                               installed => is_installed($lib));
131          foreach ((@{$unified_info{shared_sources}->{$lib}},
132                    @{$unified_info{sources}->{$lib}})) {
133              # If this is somehow a compiled object, take care of it that way
134              # Otherwise, it might simply be generated
135              if (defined $unified_info{sources}->{$_}) {
136                  doobj($_, $lib, intent => "shlib", installed => is_installed($lib));
137              } else {
138                  dogenerate($_, undef, undef, intent => "lib");
139              }
140          }
141      }
142      $OUT .= obj2lib(lib => $lib,
143                      objs => [ @{$unified_info{sources}->{$lib}} ]);
144      foreach (@{$unified_info{sources}->{$lib}}) {
145          doobj($_, $lib, intent => "lib", installed => is_installed($lib));
146      }
147      $cache{$lib} = 1;
148  }
149
150  # doengine is responsible for building engines.  It will call
151  # obj2dso, and also makes sure all object files for the library
152  # are built.
153  sub doengine {
154      my $lib = shift;
155      return "" if $cache{$lib};
156      $OUT .= obj2dso(lib => $lib,
157                      objs => $unified_info{shared_sources}->{$lib},
158                      deps => [ resolvedepends($lib) ],
159                      installed => is_installed($lib));
160      foreach (@{$unified_info{shared_sources}->{$lib}}) {
161          doobj($_, $lib, intent => "dso", installed => is_installed($lib));
162      }
163      $cache{$lib} = 1;
164  }
165
166  # dobin is responsible for building programs.  It will call obj2bin,
167  # and also makes sure all object files for the library are built.
168  sub dobin {
169      my $bin = shift;
170      return "" if $cache{$bin};
171      my $deps = [ reducedepends(resolvedepends($bin)) ];
172      $OUT .= obj2bin(bin => $bin,
173                      objs => [ @{$unified_info{sources}->{$bin}} ],
174                      deps => $deps,
175                      installed => is_installed($bin));
176      foreach (@{$unified_info{sources}->{$bin}}) {
177          doobj($_, $bin, intent => "bin", installed => is_installed($bin));
178      }
179      $cache{$bin} = 1;
180  }
181
182  # dobin is responsible for building scripts from templates.  It will
183  # call in2script.
184  sub doscript {
185      my $script = shift;
186      return "" if $cache{$script};
187      $OUT .= in2script(script => $script,
188                        sources => $unified_info{sources}->{$script},
189                        installed => is_installed($script));
190      $cache{$script} = 1;
191  }
192
193  sub dodir {
194      my $dir = shift;
195      return "" if !exists(&generatedir) or $cache{$dir};
196      $OUT .= generatedir(dir => $dir,
197                          deps => $unified_info{dirinfo}->{$dir}->{deps},
198                          %{$unified_info{dirinfo}->{$_}->{products}});
199      $cache{$dir} = 1;
200  }
201
202  # Start with populating the cache with all the overrides
203  %cache = map { $_ => 1 } @{$unified_info{overrides}};
204
205  # For convenience collect information regarding directories where
206  # files are generated, those generated files and the end product
207  # they end up in where applicable.  Then, add build rules for those
208  # directories
209  if (exists &generatedir) {
210      my %loopinfo = ( "dso" => [ @{$unified_info{engines}} ],
211                       "lib" => [ @{$unified_info{libraries}} ],
212                       "bin" => [ @{$unified_info{programs}} ],
213                       "script" => [ @{$unified_info{scripts}} ] );
214      foreach my $type (keys %loopinfo) {
215          foreach my $product (@{$loopinfo{$type}}) {
216              my %dirs = ();
217              my $pd = dirname($product);
218
219              # We already have a "test" target, and the current directory
220              # is just silly to make a target for
221              $dirs{$pd} = 1 unless $pd eq "test" || $pd eq ".";
222
223              foreach (@{$unified_info{sources}->{$product}}) {
224                  my $d = dirname($_);
225
226                  # We don't want to create targets for source directories
227                  # when building out of source
228                  next if ($config{sourcedir} ne $config{builddir}
229                           && $d =~ m|^\Q$config{sourcedir}\E|);
230                  # We already have a "test" target, and the current directory
231                  # is just silly to make a target for
232                  next if $d eq "test" || $d eq ".";
233
234                  $dirs{$d} = 1;
235                  push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
236                      if $d ne $pd;
237              }
238              foreach (keys %dirs) {
239                  push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
240                  $product;
241              }
242          }
243      }
244  }
245
246  # Build mandatory generated headers
247  foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
248
249  # Build all known libraries, engines, programs and scripts.
250  # Everything else will be handled as a consequence.
251  foreach (@{$unified_info{libraries}}) { dolib($_);    }
252  foreach (@{$unified_info{engines}})   { doengine($_); }
253  foreach (@{$unified_info{programs}})  { dobin($_);    }
254  foreach (@{$unified_info{scripts}})   { doscript($_); }
255
256  foreach (sort keys %{$unified_info{dirinfo}})  { dodir($_); }
257
258  # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
259  # they are added here.
260  $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
261 -}