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