Build system: add include directories and dependencies for generators
[openssl.git] / Configurations / common.tmpl
1 {- # -*- Mode: perl -*-
2
3      # A cache of objects for which a recipe has already been generated
4      my %cache;
5
6  # resolvedepends and reducedepends work in tandem to make sure
7  # there are no duplicate dependencies and that they are in the
8  # right order.  This is especially used to sort the list of
9  # libraries that a build depends on.
10  sub resolvedepends {
11      my $thing = shift;
12      my @listsofar = @_;    # to check if we're looping
13      my @list = @{$unified_info{depends}->{$thing}};
14      my @newlist = ();
15      if (scalar @list) {
16          foreach my $item (@list) {
17              # It's time to break off when the dependency list starts looping
18              next if grep { $_ eq $item } @listsofar;
19              push @newlist, $item, resolvedepends($item, @listsofar, $item);
20          }
21      }
22      @newlist;
23  }
24  sub reducedepends {
25      my @list = @_;
26      my @newlist = ();
27      while (@list) {
28          my $item = shift @list;
29          push @newlist, $item
30              unless grep { $item eq $_ } @list;
31      }
32      @newlist;
33  }
34
35  # dogenerate is responsible for producing all the recipes that build
36  # generated source files.  It recurses in case a dependency is also a
37  # generated source file.
38  sub dogenerate {
39      my $src = shift;
40      return "" if $cache{$src};
41      my $obj = shift;
42      my $bin = shift;
43      my %opts = @_;
44      if ($unified_info{generate}->{$src}) {
45          my $script = $unified_info{generate}->{$src}->[0];
46          $OUT .= generatesrc(src => $src,
47                              generator => $unified_info{generate}->{$src},
48                              generator_incs => $unified_info{includes}->{$script},
49                              generator_deps => $unified_info{depends}->{$script},
50                              deps => $unified_info{depends}->{$src},
51                              incs => [ @{$unified_info{includes}->{$bin}},
52                                        @{$unified_info{includes}->{$obj}} ],
53                              %opts);
54          foreach (@{$unified_info{depends}->{$src}}) {
55              dogenerate($_, $obj, $bin, %opts);
56          }
57      }
58      $cache{$src} = 1;
59  }
60
61  # doobj is responsible for producing all the recipes that build
62  # object files as well as dependency files.
63  sub doobj {
64      my $obj = shift;
65      return "" if $cache{$obj};
66      (my $obj_no_o = $obj) =~ s|\.o$||;
67      my $bin = shift;
68      my %opts = @_;
69      if (@{$unified_info{sources}->{$obj}}) {
70          $OUT .= src2obj(obj => $obj_no_o,
71                          srcs => $unified_info{sources}->{$obj},
72                          deps => $unified_info{depends}->{$obj},
73                          incs => [ @{$unified_info{includes}->{$bin}},
74                                    @{$unified_info{includes}->{$obj}} ],
75                          %opts);
76          foreach ((@{$unified_info{sources}->{$obj}},
77                    @{$unified_info{depends}->{$obj}})) {
78              dogenerate($_, $obj, $bin, %opts);
79          }
80      }
81      $cache{$obj} = 1;
82  }
83
84  # dolib is responsible for building libraries.  It will call
85  # libobj2shlib is shared libraries are produced, and obj2lib in all
86  # cases.  It also makes sure all object files for the library are
87  # built.
88  sub dolib {
89      my $lib = shift;
90      return "" if $cache{$lib};
91      unless ($disabled{shared}) {
92          my %ordinals =
93              $unified_info{ordinals}->{$lib}
94              ? (ordinals => $unified_info{ordinals}->{$lib}) : ();
95          $OUT .= libobj2shlib(shlib => $unified_info{sharednames}->{$lib},
96                               lib => $lib,
97                               objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
98                                         (@{$unified_info{sources}->{$lib}},
99                                          @{$unified_info{shared_sources}->{$lib}}) ],
100                               deps => [ reducedepends(resolvedepends($lib)) ],
101                               %ordinals);
102          foreach (@{$unified_info{shared_sources}->{$lib}}) {
103              doobj($_, $lib, intent => "lib");
104          }
105      }
106      $OUT .= obj2lib(lib => $lib,
107                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
108                                @{$unified_info{sources}->{$lib}} ]);
109      foreach (@{$unified_info{sources}->{$lib}}) {
110          doobj($_, $lib, intent => "lib");
111      }
112      $cache{$lib} = 1;
113  }
114
115  # doengine is responsible for building engines.  It will call
116  # obj2dso, and also makes sure all object files for the library
117  # are built.
118  sub doengine {
119      my $lib = shift;
120      return "" if $cache{$lib};
121      $OUT .= obj2dso(lib => $lib,
122                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
123                                (@{$unified_info{sources}->{$lib}},
124                                 @{$unified_info{shared_sources}->{$lib}}) ],
125                      deps => [ resolvedepends($lib) ]);
126      foreach ((@{$unified_info{sources}->{$lib}},
127                @{$unified_info{shared_sources}->{$lib}})) {
128          doobj($_, $lib, intent => "dso");
129      }
130      $cache{$lib} = 1;
131  }
132
133  # dobin is responsible for building programs.  It will call obj2bin,
134  # and also makes sure all object files for the library are built.
135  sub dobin {
136      my $bin = shift;
137      return "" if $cache{$bin};
138      my $deps = [ reducedepends(resolvedepends($bin)) ];
139      $OUT .= obj2bin(bin => $bin,
140                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
141                                @{$unified_info{sources}->{$bin}} ],
142                      deps => $deps);
143      foreach (@{$unified_info{sources}->{$bin}}) {
144          doobj($_, $bin, intent => "bin");
145      }
146      $cache{$bin} = 1;
147  }
148
149  # dobin is responsible for building scripts from templates.  It will
150  # call in2script.
151  sub doscript {
152      my $script = shift;
153      return "" if $cache{$script};
154      $OUT .= in2script(script => $script,
155                        sources => $unified_info{sources}->{$script});
156      $cache{$script} = 1;
157  }
158
159  # Start with populating the cache with all the overrides
160  %cache = map { $_ => 1 } @{$unified_info{overrides}};
161
162  # Build all known libraries, engines, programs and scripts.
163  # Everything else will be handled as a consequence.
164  foreach (@{$unified_info{libraries}}) { dolib($_);    }
165  foreach (@{$unified_info{engines}})   { doengine($_); }
166  foreach (@{$unified_info{programs}})  { dobin($_);    }
167  foreach (@{$unified_info{scripts}})   { doscript($_); }
168
169  # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
170  # they are added here.
171  $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
172 -}