Fix a possible crash in BN_from_montgomery_word
[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 resolvedepends {
13      my $thing = shift;
14      my @listsofar = @_;    # to check if we're looping
15      my @list = @{$unified_info{depends}->{$thing}};
16      my @newlist = ();
17      if (scalar @list) {
18          foreach my $item (@list) {
19              # It's time to break off when the dependency list starts looping
20              next if grep { $_ eq $item } @listsofar;
21              push @newlist, $item, resolvedepends($item, @listsofar, $item);
22          }
23      }
24      @newlist;
25  }
26  sub reducedepends {
27      my @list = @_;
28      my @newlist = ();
29      while (@list) {
30          my $item = shift @list;
31          push @newlist, $item
32              unless grep { $item eq $_ } @list;
33      }
34      @newlist;
35  }
36
37  # dogenerate is responsible for producing all the recipes that build
38  # generated source files.  It recurses in case a dependency is also a
39  # generated source file.
40  sub dogenerate {
41      my $src = shift;
42      return "" if $cache{$src};
43      my $obj = shift;
44      my $bin = shift;
45      my %opts = @_;
46      if ($unified_info{generate}->{$src}) {
47          die "$src is generated by Configure, should not appear in build file\n"
48              if ref $unified_info{generate}->{$src} eq "";
49          my $script = $unified_info{generate}->{$src}->[0];
50          $OUT .= generatesrc(src => $src,
51                              generator => $unified_info{generate}->{$src},
52                              generator_incs => $unified_info{includes}->{$script},
53                              generator_deps => $unified_info{depends}->{$script},
54                              deps => $unified_info{depends}->{$src},
55                              incs => $unified_info{includes}->{$obj},
56                              %opts);
57          foreach (@{$unified_info{depends}->{$src}}) {
58              dogenerate($_, $obj, $bin, %opts);
59          }
60      }
61      $cache{$src} = 1;
62  }
63
64  # doobj is responsible for producing all the recipes that build
65  # object files as well as dependency files.
66  sub doobj {
67      my $obj = shift;
68      return "" if $cache{$obj};
69      (my $obj_no_o = $obj) =~ s|\.o$||;
70      my $bin = shift;
71      my %opts = @_;
72      if (@{$unified_info{sources}->{$obj}}) {
73          $OUT .= src2obj(obj => $obj_no_o,
74                          srcs => $unified_info{sources}->{$obj},
75                          deps => $unified_info{depends}->{$obj},
76                          incs => $unified_info{includes}->{$obj},
77                          %opts);
78          foreach ((@{$unified_info{sources}->{$obj}},
79                    @{$unified_info{depends}->{$obj}})) {
80              dogenerate($_, $obj, $bin, %opts);
81          }
82      }
83      $cache{$obj} = 1;
84  }
85
86  # dolib is responsible for building libraries.  It will call
87  # libobj2shlib is shared libraries are produced, and obj2lib in all
88  # cases.  It also makes sure all object files for the library are
89  # built.
90  sub dolib {
91      my $lib = shift;
92      return "" if $cache{$lib};
93      unless ($disabled{shared}) {
94          my %ordinals =
95              $unified_info{ordinals}->{$lib}
96              ? (ordinals => $unified_info{ordinals}->{$lib}) : ();
97          $OUT .= libobj2shlib(shlib => $unified_info{sharednames}->{$lib},
98                               lib => $lib,
99                               objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
100                                         (@{$unified_info{sources}->{$lib}},
101                                          @{$unified_info{shared_sources}->{$lib}}) ],
102                               deps => [ reducedepends(resolvedepends($lib)) ],
103                               %ordinals);
104          foreach (@{$unified_info{shared_sources}->{$lib}}) {
105              doobj($_, $lib, intent => "lib");
106          }
107      }
108      $OUT .= obj2lib(lib => $lib,
109                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
110                                @{$unified_info{sources}->{$lib}} ]);
111      foreach (@{$unified_info{sources}->{$lib}}) {
112          doobj($_, $lib, intent => "lib");
113      }
114      $cache{$lib} = 1;
115  }
116
117  # doengine is responsible for building engines.  It will call
118  # obj2dso, and also makes sure all object files for the library
119  # are built.
120  sub doengine {
121      my $lib = shift;
122      return "" if $cache{$lib};
123      $OUT .= obj2dso(lib => $lib,
124                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
125                                (@{$unified_info{sources}->{$lib}},
126                                 @{$unified_info{shared_sources}->{$lib}}) ],
127                      deps => [ resolvedepends($lib) ]);
128      foreach ((@{$unified_info{sources}->{$lib}},
129                @{$unified_info{shared_sources}->{$lib}})) {
130          doobj($_, $lib, intent => "dso");
131      }
132      $cache{$lib} = 1;
133  }
134
135  # dobin is responsible for building programs.  It will call obj2bin,
136  # and also makes sure all object files for the library are built.
137  sub dobin {
138      my $bin = shift;
139      return "" if $cache{$bin};
140      my $deps = [ reducedepends(resolvedepends($bin)) ];
141      $OUT .= obj2bin(bin => $bin,
142                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
143                                @{$unified_info{sources}->{$bin}} ],
144                      deps => $deps);
145      foreach (@{$unified_info{sources}->{$bin}}) {
146          doobj($_, $bin, intent => "bin");
147      }
148      $cache{$bin} = 1;
149  }
150
151  # dobin is responsible for building scripts from templates.  It will
152  # call in2script.
153  sub doscript {
154      my $script = shift;
155      return "" if $cache{$script};
156      $OUT .= in2script(script => $script,
157                        sources => $unified_info{sources}->{$script});
158      $cache{$script} = 1;
159  }
160
161  sub dodir {
162      my $dir = shift;
163      return "" if !exists(&generatedir) or $cache{$dir};
164      $OUT .= generatedir(dir => $dir,
165                          deps => $unified_info{dirinfo}->{$dir}->{deps},
166                          %{$unified_info{dirinfo}->{$_}->{products}});
167      $cache{$dir} = 1;
168  }
169
170  # Start with populating the cache with all the overrides
171  %cache = map { $_ => 1 } @{$unified_info{overrides}};
172
173  # For convenience collect information regarding directories where
174  # files are generated, those generated files and the end product
175  # they end up in where applicable.  Then, add build rules for those
176  # directories
177  if (exists &generatedir) {
178      my %loopinfo = ( "dso" => [ @{$unified_info{engines}} ],
179                       "lib" => [ @{$unified_info{libraries}} ],
180                       "bin" => [ @{$unified_info{programs}} ],
181                       "script" => [ @{$unified_info{scripts}} ] );
182      foreach my $type (keys %loopinfo) {
183          foreach my $product (@{$loopinfo{$type}}) {
184              my %dirs = ();
185              my $pd = dirname($product);
186
187              # We already have a "test" target, and the current directory
188              # is just silly to make a target for
189              $dirs{$pd} = 1 unless $pd eq "test" || $pd eq ".";
190
191              foreach (@{$unified_info{sources}->{$product}}) {
192                  my $d = dirname($_);
193
194                  # We don't want to create targets for source directories
195                  # when building out of source
196                  next if ($config{sourcedir} ne $config{builddir}
197                           && $d =~ m|^\Q$config{sourcedir}\E|);
198                  # We already have a "test" target, and the current directory
199                  # is just silly to make a target for
200                  next if $d eq "test" || $d eq ".";
201
202                  $dirs{$d} = 1;
203                  push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
204                      if $d ne $pd;
205              }
206              foreach (keys %dirs) {
207                  push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
208                  $product;
209              }
210          }
211      }
212  }
213
214  # Build mandatory generated headers
215  foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
216
217  # Build all known libraries, engines, programs and scripts.
218  # Everything else will be handled as a consequence.
219  foreach (@{$unified_info{libraries}}) { dolib($_);    }
220  foreach (@{$unified_info{engines}})   { doengine($_); }
221  foreach (@{$unified_info{programs}})  { dobin($_);    }
222  foreach (@{$unified_info{scripts}})   { doscript($_); }
223
224  foreach (sort keys %{$unified_info{dirinfo}})  { dodir($_); }
225
226  # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
227  # they are added here.
228  $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
229 -}