]> git.ipfire.org Git - thirdparty/openssl.git/blob - Configurations/common.tmpl
Add build file support for generic symbol exports with DSOs
[thirdparty/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 product => $bin,
82 generator => $unified_info{generate}->{$src},
83 generator_incs => $unified_info{includes}->{$script},
84 generator_deps => $unified_info{depends}->{$script},
85 deps => $unified_info{depends}->{$src},
86 incs => [ @{$unified_info{includes}->{$obj}},
87 @{$unified_info{includes}->{$bin}} ],
88 %opts);
89 foreach (@{$unified_info{depends}->{$src}}) {
90 dogenerate($_, $obj, $bin, %opts);
91 }
92 }
93 $cache{$src} = 1;
94 }
95
96 # doobj is responsible for producing all the recipes that build
97 # object files as well as dependency files.
98 sub doobj {
99 my $obj = shift;
100 return "" if $cache{$obj};
101 my $bin = shift;
102 my %opts = @_;
103 if (@{$unified_info{sources}->{$obj}}) {
104 $OUT .= src2obj(obj => $obj,
105 product => $bin,
106 srcs => $unified_info{sources}->{$obj},
107 deps => $unified_info{depends}->{$obj},
108 incs => [ @{$unified_info{includes}->{$obj}},
109 @{$unified_info{includes}->{$bin}} ],
110 %opts);
111 foreach ((@{$unified_info{sources}->{$obj}},
112 @{$unified_info{depends}->{$obj}})) {
113 dogenerate($_, $obj, $bin, %opts);
114 }
115 }
116 $cache{$obj} = 1;
117 }
118
119 # dolib is responsible for building libraries. It will call
120 # obj2shlib is shared libraries are produced, and obj2lib in all
121 # cases. It also makes sure all object files for the library are
122 # built.
123 sub dolib {
124 my $lib = shift;
125 return "" if $cache{$lib};
126 unless ($disabled{shared} || $lib =~ /\.a$/) {
127 my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
128 $OUT .= $obj2shlib->(shlib => $unified_info{sharednames}->{$lib},
129 lib => $lib,
130 objs => $unified_info{shared_sources}->{$lib},
131 deps => [ reducedepends(resolvedepends($lib)) ],
132 installed => is_installed($lib));
133 foreach ((@{$unified_info{shared_sources}->{$lib}},
134 @{$unified_info{sources}->{$lib}})) {
135 # If this is somehow a compiled object, take care of it that way
136 # Otherwise, it might simply be generated
137 if (defined $unified_info{sources}->{$_}) {
138 doobj($_, $lib, intent => "shlib", installed => is_installed($lib));
139 } else {
140 dogenerate($_, undef, undef, intent => "lib");
141 }
142 }
143 }
144 $OUT .= obj2lib(lib => $lib,
145 objs => [ @{$unified_info{sources}->{$lib}} ]);
146 foreach (@{$unified_info{sources}->{$lib}}) {
147 doobj($_, $lib, intent => "lib", installed => is_installed($lib));
148 }
149 $cache{$lib} = 1;
150 }
151
152 # doengine is responsible for building engines. It will call
153 # obj2dso, and also makes sure all object files for the library
154 # are built.
155 sub doengine {
156 my $lib = shift;
157 return "" if $cache{$lib};
158 $OUT .= obj2dso(lib => $lib,
159 objs => $unified_info{shared_sources}->{$lib},
160 deps => [ resolvedepends($lib) ],
161 installed => is_installed($lib));
162 foreach (@{$unified_info{shared_sources}->{$lib}}) {
163 # If this is somehow a compiled object, take care of it that way
164 # Otherwise, it might simply be generated
165 if (defined $unified_info{sources}->{$_}) {
166 doobj($_, $lib, intent => "dso", installed => is_installed($lib));
167 } else {
168 dogenerate($_, undef, $lib, intent => "dso");
169 }
170 }
171 $cache{$lib} = 1;
172 }
173
174 # dobin is responsible for building programs. It will call obj2bin,
175 # and also makes sure all object files for the library are built.
176 sub dobin {
177 my $bin = shift;
178 return "" if $cache{$bin};
179 my $deps = [ reducedepends(resolvedepends($bin)) ];
180 $OUT .= obj2bin(bin => $bin,
181 objs => [ @{$unified_info{sources}->{$bin}} ],
182 deps => $deps,
183 installed => is_installed($bin));
184 foreach (@{$unified_info{sources}->{$bin}}) {
185 doobj($_, $bin, intent => "bin", installed => is_installed($bin));
186 }
187 $cache{$bin} = 1;
188 }
189
190 # dobin is responsible for building scripts from templates. It will
191 # call in2script.
192 sub doscript {
193 my $script = shift;
194 return "" if $cache{$script};
195 $OUT .= in2script(script => $script,
196 sources => $unified_info{sources}->{$script},
197 installed => is_installed($script));
198 $cache{$script} = 1;
199 }
200
201 sub dodir {
202 my $dir = shift;
203 return "" if !exists(&generatedir) or $cache{$dir};
204 $OUT .= generatedir(dir => $dir,
205 deps => $unified_info{dirinfo}->{$dir}->{deps},
206 %{$unified_info{dirinfo}->{$_}->{products}});
207 $cache{$dir} = 1;
208 }
209
210 # Start with populating the cache with all the overrides
211 %cache = map { $_ => 1 } @{$unified_info{overrides}};
212
213 # For convenience collect information regarding directories where
214 # files are generated, those generated files and the end product
215 # they end up in where applicable. Then, add build rules for those
216 # directories
217 if (exists &generatedir) {
218 my %loopinfo = ( "dso" => [ @{$unified_info{engines}} ],
219 "lib" => [ @{$unified_info{libraries}} ],
220 "bin" => [ @{$unified_info{programs}} ],
221 "script" => [ @{$unified_info{scripts}} ] );
222 foreach my $type (keys %loopinfo) {
223 foreach my $product (@{$loopinfo{$type}}) {
224 my %dirs = ();
225 my $pd = dirname($product);
226
227 # We already have a "test" target, and the current directory
228 # is just silly to make a target for
229 $dirs{$pd} = 1 unless $pd eq "test" || $pd eq ".";
230
231 foreach (@{$unified_info{sources}->{$product}}) {
232 my $d = dirname($_);
233
234 # We don't want to create targets for source directories
235 # when building out of source
236 next if ($config{sourcedir} ne $config{builddir}
237 && $d =~ m|^\Q$config{sourcedir}\E|);
238 # We already have a "test" target, and the current directory
239 # is just silly to make a target for
240 next if $d eq "test" || $d eq ".";
241
242 $dirs{$d} = 1;
243 push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
244 if $d ne $pd;
245 }
246 foreach (keys %dirs) {
247 push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
248 $product;
249 }
250 }
251 }
252 }
253
254 # Build mandatory generated headers
255 foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
256
257 # Build all known libraries, engines, programs and scripts.
258 # Everything else will be handled as a consequence.
259 foreach (@{$unified_info{libraries}}) { dolib($_); }
260 foreach (@{$unified_info{engines}}) { doengine($_); }
261 foreach (@{$unified_info{programs}}) { dobin($_); }
262 foreach (@{$unified_info{scripts}}) { doscript($_); }
263
264 foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
265
266 # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
267 # they are added here.
268 $OUT .= $_."\n" foreach @{$unified_info{rawlines}};
269 -}