Perl cleanup: don't create lists unnecessarily
[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          $OUT .= generatesrc(src => $src,
46                              generator => $unified_info{generate}->{$src},
47                              deps => $unified_info{depends}->{$src},
48                              incs => [ @{$unified_info{includes}->{$bin}},
49                                        @{$unified_info{includes}->{$obj}} ],
50                              %opts);
51          foreach (@{$unified_info{depends}->{$src}}) {
52              dogenerate($_, $obj, $bin, %opts);
53          }
54      }
55      $cache{$src} = 1;
56  }
57
58  # doobj is responsible for producing all the recipes that build
59  # object files as well as dependency files.
60  sub doobj {
61      my $obj = shift;
62      return "" if $cache{$obj};
63      (my $obj_no_o = $obj) =~ s|\.o$||;
64      my $bin = shift;
65      my %opts = @_;
66      if (@{$unified_info{sources}->{$obj}}) {
67          $OUT .= src2obj(obj => $obj_no_o,
68                          srcs => $unified_info{sources}->{$obj},
69                          deps => $unified_info{depends}->{$obj},
70                          incs => [ @{$unified_info{includes}->{$bin}},
71                                    @{$unified_info{includes}->{$obj}} ],
72                          %opts);
73          foreach ((@{$unified_info{sources}->{$obj}},
74                    @{$unified_info{depends}->{$obj}})) {
75              dogenerate($_, $obj, $bin, %opts);
76          }
77      }
78      $cache{$obj} = 1;
79  }
80
81  # dolib is responsible for building libraries.  It will call
82  # libobj2shlib is shared libraries are produced, and obj2lib in all
83  # cases.  It also makes sure all object files for the library are
84  # built.
85  sub dolib {
86      my $lib = shift;
87      return "" if $cache{$lib};
88      unless ($disabled{shared}) {
89          my %ordinals =
90              $unified_info{ordinals}->{$lib}
91              ? (ordinals => $unified_info{ordinals}->{$lib}) : ();
92          $OUT .= libobj2shlib(shlib => $unified_info{sharednames}->{$lib},
93                               lib => $lib,
94                               objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
95                                         (@{$unified_info{sources}->{$lib}},
96                                          @{$unified_info{shared_sources}->{$lib}}) ],
97                               deps => [ reducedepends(resolvedepends($lib)) ],
98                               %ordinals);
99          foreach (@{$unified_info{shared_sources}->{$lib}}) {
100              doobj($_, $lib, intent => "lib");
101          }
102      }
103      $OUT .= obj2lib(lib => $lib,
104                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
105                                @{$unified_info{sources}->{$lib}} ]);
106      foreach (@{$unified_info{sources}->{$lib}}) {
107          doobj($_, $lib, intent => "lib");
108      }
109      $cache{$lib} = 1;
110  }
111
112  # doengine is responsible for building engines.  It will call
113  # obj2dso, and also makes sure all object files for the library
114  # are built.
115  sub doengine {
116      my $lib = shift;
117      return "" if $cache{$lib};
118      $OUT .= obj2dso(lib => $lib,
119                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
120                                (@{$unified_info{sources}->{$lib}},
121                                 @{$unified_info{shared_sources}->{$lib}}) ],
122                      deps => [ resolvedepends($lib) ]);
123      foreach ((@{$unified_info{sources}->{$lib}},
124                @{$unified_info{shared_sources}->{$lib}})) {
125          doobj($_, $lib, intent => "dso");
126      }
127      $cache{$lib} = 1;
128  }
129
130  # dobin is responsible for building programs.  It will call obj2bin,
131  # and also makes sure all object files for the library are built.
132  sub dobin {
133      my $bin = shift;
134      return "" if $cache{$bin};
135      my $deps = [ reducedepends(resolvedepends($bin)) ];
136      $OUT .= obj2bin(bin => $bin,
137                      objs => [ map { (my $x = $_) =~ s|\.o$||; $x }
138                                @{$unified_info{sources}->{$bin}} ],
139                      deps => $deps);
140      foreach (@{$unified_info{sources}->{$bin}}) {
141          doobj($_, $bin, intent => "bin");
142      }
143      $cache{$bin} = 1;
144  }
145
146  # dobin is responsible for building scripts from templates.  It will
147  # call in2script.
148  sub doscript {
149      my $script = shift;
150      return "" if $cache{$script};
151      $OUT .= in2script(script => $script,
152                        sources => $unified_info{sources}->{$script});
153      $cache{$script} = 1;
154  }
155
156  # Start with populating the cache with all the overrides
157  %cache = map { $_ => 1 } @{$unified_info{overrides}};
158
159  # Build all known libraries, engines, programs and scripts.
160  # Everything else will be handled as a consequence.
161  dolib($_)     foreach @{$unified_info{libraries}};
162  doengine($_)  foreach @{$unified_info{engines}};
163  dobin($_)     foreach @{$unified_info{programs}};
164  doscript($_)  foreach @{$unified_info{scripts}};
165
166  # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
167  # they are added here.
168  $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
169 -}