1 {- # -*- Mode: perl -*-
5 my $debug_resolvedepends = $ENV{BUILDFILE_DEBUG_DEPENDS};
6 my $debug_rules = $ENV{BUILDFILE_DEBUG_RULES};
8 # A cache of objects for which a recipe has already been generated
11 # collectdepends, expanddepends and reducedepends work together to make
12 # sure there are no duplicate or weak dependencies and that they are in
13 # the right order. This is used to sort the list of libraries that a
15 sub extensionlesslib {
16 my @result = map { $_ =~ /(\.a)?$/; $` } @_;
17 return @result if wantarray;
21 # collectdepends dives into the tree of dependencies and returns
22 # a list of all the non-weak ones.
27 my $extensionlessthing = extensionlesslib($thing);
28 my @listsofar = @_; # to check if we're looping
29 my @list = @{$unified_info{depends}->{$thing} //
30 $unified_info{depends}->{$extensionlessthing}};
33 print STDERR "DEBUG[collectdepends] $thing > ", join(' ', @listsofar), "\n"
34 if $debug_resolvedepends;
35 foreach my $item (@list) {
36 my $extensionlessitem = extensionlesslib($item);
37 # It's time to break off when the dependency list starts looping
38 next if grep { extensionlesslib($_) eq $extensionlessitem } @listsofar;
39 # Don't add anything here if the dependency is weak
40 next if defined $unified_info{attributes}->{depends}->{$thing}->{$item}->{'weak'};
41 my @resolved = collectdepends($item, @listsofar, $item);
42 push @newlist, $item, @resolved;
44 print STDERR "DEBUG[collectdepends] $thing < ", join(' ', @newlist), "\n"
45 if $debug_resolvedepends;
49 # expanddepends goes through a list of stuff, checks if they have any
50 # dependencies, and adds them at the end of the current position if
51 # they aren't already present later on.
54 print STDERR "DEBUG[expanddepends]> ", join(' ', @after), "\n"
55 if $debug_resolvedepends;
58 my $item = shift @after;
59 print STDERR "DEBUG[expanddepends]\\ ", join(' ', @before), "\n"
60 if $debug_resolvedepends;
61 print STDERR "DEBUG[expanddepends] - ", $item, "\n"
62 if $debug_resolvedepends;
67 my $extlessx = extensionlesslib($x);
68 if (grep { $extlessx eq extensionlesslib($_) } @before
70 !grep { $extlessx eq extensionlesslib($_) } @after) {
71 print STDERR "DEBUG[expanddepends] + ", $x, "\n"
72 if $debug_resolvedepends;
75 print STDERR "DEBUG[expanddepends] ! ", $x, "\n"
76 if $debug_resolvedepends;
79 } @{$unified_info{depends}->{$item} // []}
81 print STDERR "DEBUG[expanddepends] = ", join(' ', @middle), "\n"
82 if $debug_resolvedepends;
83 print STDERR "DEBUG[expanddepends]/ ", join(' ', @after), "\n"
84 if $debug_resolvedepends;
85 push @before, @middle;
87 print STDERR "DEBUG[expanddepends]< ", join(' ', @before), "\n"
88 if $debug_resolvedepends;
92 # reducedepends looks through a list, and checks if each item is
93 # repeated later on. If it is, the earlier copy is dropped.
96 print STDERR "DEBUG[reducedepends]> ", join(' ', @list), "\n"
97 if $debug_resolvedepends;
101 my $item = shift @list;
102 my $extensionlessitem = extensionlesslib($item);
103 if (grep { $extensionlessitem eq extensionlesslib($_) } @list) {
104 if ($item ne $extensionlessitem) {
105 # If this instance of the library is explicitly static, we
106 # prefer that to any shared library name, since it must have
107 # been done on purpose.
108 $replace{$extensionlessitem} = $item;
111 push @newlist, $item;
114 @newlist = map { $replace{$_} // $_; } @newlist;
115 print STDERR "DEBUG[reducedepends]< ", join(' ', @newlist), "\n"
116 if $debug_resolvedepends;
121 # This takes multiple inputs and combine them into a single list of
122 # interdependent things. The returned value will include all the input.
123 # Callers are responsible for taking away the things they are building.
125 print STDERR "DEBUG[resolvedepends] START (", join(', ', @_), ")\n"
126 if $debug_resolvedepends;
128 reducedepends(expanddepends(map { ( $_, collectdepends($_) ) } @_));
129 print STDERR "DEBUG[resolvedepends] END (", join(', ', @_), ") : ",
130 join(',', map { "\n $_" } @all), "\n"
131 if $debug_resolvedepends;
135 # dogenerate is responsible for producing all the recipes that build
136 # generated source files. It recurses in case a dependency is also a
137 # generated source file.
140 return "" if $cache{$src};
144 if ($unified_info{generate}->{$src}) {
145 die "$src is generated by Configure, should not appear in build file\n"
146 if ref $unified_info{generate}->{$src} eq "";
147 my $script = $unified_info{generate}->{$src}->[0];
148 $OUT .= generatesrc(src => $src,
150 generator => $unified_info{generate}->{$src},
151 generator_incs => $unified_info{includes}->{$script},
152 generator_deps => $unified_info{depends}->{$script},
153 deps => $unified_info{depends}->{$src},
154 incs => [ @{$unified_info{includes}->{$obj}},
155 @{$unified_info{includes}->{$bin}} ],
156 defs => [ @{$unified_info{defines}->{$obj}},
157 @{$unified_info{defines}->{$bin}} ],
159 foreach (@{$unified_info{depends}->{$src}}) {
160 dogenerate($_, $obj, $bin, %opts);
166 # doobj is responsible for producing all the recipes that build
167 # object files as well as dependency files.
170 return "" if $cache{$obj};
173 if (@{$unified_info{sources}->{$obj}}) {
174 my @srcs = @{$unified_info{sources}->{$obj}};
175 my @deps = @{$unified_info{depends}->{$obj}};
176 my @incs = ( @{$unified_info{includes}->{$obj}},
177 @{$unified_info{includes}->{$bin}} );
178 my @defs = ( @{$unified_info{defines}->{$obj}},
179 @{$unified_info{defines}->{$bin}} );
180 print STDERR "DEBUG[doobj] \@srcs for $obj ($bin) : ",
181 join(",", map { "\n $_" } @srcs), "\n"
183 print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
184 join(",", map { "\n $_" } @deps), "\n"
186 print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
187 join(",", map { "\n $_" } @incs), "\n"
189 print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
190 join(",", map { "\n $_" } @defs), "\n"
192 print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
193 join(",", map { "\n $_ = $opts{$_}" } sort keys %opts), "\n"
195 $OUT .= src2obj(obj => $obj, product => $bin,
196 srcs => [ @srcs ], deps => [ @deps ],
197 incs => [ @incs ], defs => [ @defs ],
199 foreach ((@{$unified_info{sources}->{$obj}},
200 @{$unified_info{depends}->{$obj}})) {
201 dogenerate($_, $obj, $bin, %opts);
207 # Helper functions to grab all applicable intermediary files.
208 # This is particularly useful when a library is given as source
209 # rather than a dependency. In that case, we consider it to be a
210 # container with object file references, or possibly references
211 # to further libraries to pilfer in the same way.
215 # For all input, see if it sources static libraries. If it does,
216 # return them together with the result of a recursive call.
217 map { ( $_, getsrclibs($section, $_) ) }
218 grep { $_ =~ m|\.a$| }
219 map { @{$unified_info{$section}->{$_} // []} }
226 # For all input, see if it's an intermediary file (library or object).
227 # If it is, collect the result of a recursive call, or if that returns
228 # an empty list, the element itself. Return the result.
230 my @x = getlibobjs($section, @{$unified_info{$section}->{$_}});
233 grep { defined $unified_info{$section}->{$_} }
237 # dolib is responsible for building libraries. It will call
238 # obj2shlib if shared libraries are produced, and obj2lib in all
239 # cases. It also makes sure all object files for the library are
243 return "" if $cache{$lib};
245 my %attrs = %{$unified_info{attributes}->{libraries}->{$lib}};
247 my @deps = ( resolvedepends(getsrclibs('sources', $lib)) );
249 # We support two types of objs, those who are specific to this library
250 # (they end up in @objs) and those that we get indirectly, via other
251 # libraries (they end up in @foreign_objs). We get the latter any time
252 # someone has done something like this in build.info:
253 # SOURCE[libfoo.a]=libbar.a
254 # The indirect object files must be kept in a separate array so they
255 # don't get rebuilt unnecessarily (and with incorrect auxiliary
258 # Object files can't be collected commonly for shared and static
259 # libraries, because we contain their respective object files in
260 # {shared_sources} and {sources}, and because the implications are
261 # slightly different for each library form.
263 # We grab all these "foreign" object files recursively with getlibobjs().
265 unless ($disabled{shared} || $lib =~ /\.a$/) {
266 my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
267 # If this library sources other static libraries and those
268 # libraries are marked {noinst}, there's no need to include
269 # all of their object files. Instead, we treat those static
270 # libraries as dependents alongside any other library this
271 # one depends on, and let symbol resolution do its job.
272 my @sourced_libs = ();
274 my @foreign_objs = ();
276 foreach (@{$unified_info{shared_sources}->{$lib}}) {
279 } elsif ($unified_info{attributes}->{libraries}->{$_}->{noinst}) {
282 push @deps, getsrclibs('sources', $_);
283 push @foreign_objs, getlibobjs('sources', $_);
286 @deps = ( grep { $_ ne $lib } resolvedepends($lib, @deps) );
287 print STDERR "DEBUG[dolib:shlib] \%attrs for $lib : ", ,
288 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
289 if %attrs && $debug_rules;
290 print STDERR "DEBUG[dolib:shlib] \@deps for $lib : ",
291 join(",", map { "\n $_" } @deps), "\n"
292 if @deps && $debug_rules;
293 print STDERR "DEBUG[dolib:shlib] \@objs for $lib : ",
294 join(",", map { "\n $_" } @objs), "\n"
295 if @objs && $debug_rules;
296 print STDERR "DEBUG[dolib:shlib] \@foreign_objs for $lib : ",
297 join(",", map { "\n $_" } @foreign_objs), "\n"
298 if @foreign_objs && $debug_rules;
299 $OUT .= $obj2shlib->(lib => $lib,
301 objs => [ @objs, @foreign_objs ],
304 # If this is somehow a compiled object, take care of it that way
305 # Otherwise, it might simply be generated
306 if (defined $unified_info{sources}->{$_}) {
310 doobj($_, $lib, intent => "shlib", attrs => { %attrs });
313 dogenerate($_, undef, undef, intent => "lib");
318 # When putting static libraries together, we cannot rely on any
319 # symbol resolution, so for all static libraries used as source for
320 # this one, as well as other libraries they depend on, we simply
321 # grab all their object files unconditionally,
322 # Symbol resolution will happen when any program, module or shared
323 # library is linked with this one.
326 my @foreign_objs = ();
327 foreach (@{$unified_info{sources}->{$lib}}) {
331 push @sourcedeps, $_;
334 @sourcedeps = ( grep { $_ ne $lib } resolvedepends(@sourcedeps) );
335 print STDERR "DEBUG[dolib:lib] : \@sourcedeps for $_ : ",
336 join(",", map { "\n $_" } @sourcedeps), "\n"
337 if @sourcedeps && $debug_rules;
338 @foreign_objs = getlibobjs('sources', @sourcedeps);
339 print STDERR "DEBUG[dolib:lib] \%attrs for $lib : ", ,
340 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
341 if %attrs && $debug_rules;
342 print STDERR "DEBUG[dolib:lib] \@objs for $lib : ",
343 join(",", map { "\n $_" } @objs), "\n"
344 if @objs && $debug_rules;
345 print STDERR "DEBUG[dolib:lib] \@foreign_objs for $lib : ",
346 join(",", map { "\n $_" } @foreign_objs), "\n"
347 if @foreign_objs && $debug_rules;
348 $OUT .= obj2lib(lib => $lib, attrs => { %attrs },
349 objs => [ @objs, @foreign_objs ]);
351 doobj($_, $lib, intent => "lib", attrs => { %attrs });
357 # domodule is responsible for building modules. It will call
358 # obj2dso, and also makes sure all object files for the library
362 return "" if $cache{$module};
363 my %attrs = %{$unified_info{attributes}->{modules}->{$module}};
364 my @objs = @{$unified_info{sources}->{$module}};
365 my @deps = ( grep { $_ ne $module }
366 resolvedepends($module) );
367 print STDERR "DEBUG[domodule] \%attrs for $module :",
368 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
370 print STDERR "DEBUG[domodule] \@objs for $module : ",
371 join(",", map { "\n $_" } @objs), "\n"
373 print STDERR "DEBUG[domodule] \@deps for $module : ",
374 join(",", map { "\n $_" } @deps), "\n"
376 $OUT .= obj2dso(module => $module,
380 foreach (@{$unified_info{sources}->{$module}}) {
381 # If this is somehow a compiled object, take care of it that way
382 # Otherwise, it might simply be generated
383 if (defined $unified_info{sources}->{$_}) {
384 doobj($_, $module, intent => "dso", attrs => { %attrs });
386 dogenerate($_, undef, $module, intent => "dso");
392 # dobin is responsible for building programs. It will call obj2bin,
393 # and also makes sure all object files for the library are built.
396 return "" if $cache{$bin};
397 my %attrs = %{$unified_info{attributes}->{programs}->{$bin}};
398 my @objs = @{$unified_info{sources}->{$bin}};
399 my @deps = ( grep { $_ ne $bin } resolvedepends($bin) );
400 print STDERR "DEBUG[dobin] \%attrs for $bin : ",
401 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
402 if %attrs && $debug_rules;
403 print STDERR "DEBUG[dobin] \@objs for $bin : ",
404 join(",", map { "\n $_" } @objs), "\n"
405 if @objs && $debug_rules;
406 print STDERR "DEBUG[dobin] \@deps for $bin : ",
407 join(",", map { "\n $_" } @deps), "\n"
408 if @deps && $debug_rules;
409 $OUT .= obj2bin(bin => $bin,
414 doobj($_, $bin, intent => "bin", attrs => { %attrs });
419 # dobin is responsible for building scripts from templates. It will
423 return "" if $cache{$script};
424 $OUT .= in2script(script => $script,
425 attrs => $unified_info{attributes}->{$script},
426 sources => $unified_info{sources}->{$script});
432 return "" if !exists(&generatedir) or $cache{$dir};
433 $OUT .= generatedir(dir => $dir,
434 deps => $unified_info{dirinfo}->{$dir}->{deps},
435 %{$unified_info{dirinfo}->{$_}->{products}});
439 # Start with populating the cache with all the overrides
440 %cache = map { $_ => 1 } @{$unified_info{overrides}};
442 # Build mandatory generated headers
443 foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
445 # Build all known libraries, modules, programs and scripts.
446 # Everything else will be handled as a consequence.
447 foreach (@{$unified_info{libraries}}) { dolib($_); }
448 foreach (@{$unified_info{modules}}) { domodule($_); }
449 foreach (@{$unified_info{programs}}) { dobin($_); }
450 foreach (@{$unified_info{scripts}}) { doscript($_); }
452 foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }