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 => [ defined $obj
155 ? @{$unified_info{includes}->{$obj}}
158 ? @{$unified_info{includes}->{$bin}}
160 defs => [ defined $obj
161 ? @{$unified_info{defines}->{$obj}}
164 ? @{$unified_info{defines}->{$bin}}
167 foreach (@{$unified_info{depends}->{$src}}) {
168 dogenerate($_, $obj, $bin, %opts);
174 # doobj is responsible for producing all the recipes that build
175 # object files as well as dependency files.
178 return "" if $cache{$obj};
181 if (@{$unified_info{sources}->{$obj}}) {
182 my @srcs = @{$unified_info{sources}->{$obj}};
183 my @deps = @{$unified_info{depends}->{$obj}};
184 my @incs = ( @{$unified_info{includes}->{$obj}},
185 @{$unified_info{includes}->{$bin}} );
186 my @defs = ( @{$unified_info{defines}->{$obj}},
187 @{$unified_info{defines}->{$bin}} );
188 print STDERR "DEBUG[doobj] \@srcs for $obj ($bin) : ",
189 join(",", map { "\n $_" } @srcs), "\n"
191 print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
192 join(",", map { "\n $_" } @deps), "\n"
194 print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
195 join(",", map { "\n $_" } @incs), "\n"
197 print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
198 join(",", map { "\n $_" } @defs), "\n"
200 print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
201 join(",", map { "\n $_ = $opts{$_}" } sort keys %opts), "\n"
203 $OUT .= src2obj(obj => $obj, product => $bin,
204 srcs => [ @srcs ], deps => [ @deps ],
205 incs => [ @incs ], defs => [ @defs ],
207 foreach ((@{$unified_info{sources}->{$obj}},
208 @{$unified_info{depends}->{$obj}})) {
209 dogenerate($_, $obj, $bin, %opts);
215 # Helper functions to grab all applicable intermediary files.
216 # This is particularly useful when a library is given as source
217 # rather than a dependency. In that case, we consider it to be a
218 # container with object file references, or possibly references
219 # to further libraries to pilfer in the same way.
223 # For all input, see if it sources static libraries. If it does,
224 # return them together with the result of a recursive call.
225 map { ( $_, getsrclibs($section, $_) ) }
226 grep { $_ =~ m|\.a$| }
227 map { @{$unified_info{$section}->{$_} // []} }
234 # For all input, see if it's an intermediary file (library or object).
235 # If it is, collect the result of a recursive call, or if that returns
236 # an empty list, the element itself. Return the result.
238 my @x = getlibobjs($section, @{$unified_info{$section}->{$_}});
241 grep { defined $unified_info{$section}->{$_} }
245 # dolib is responsible for building libraries. It will call
246 # obj2shlib if shared libraries are produced, and obj2lib in all
247 # cases. It also makes sure all object files for the library are
251 return "" if $cache{$lib};
253 my %attrs = %{$unified_info{attributes}->{libraries}->{$lib}};
255 my @deps = ( resolvedepends(getsrclibs('sources', $lib)) );
257 # We support two types of objs, those who are specific to this library
258 # (they end up in @objs) and those that we get indirectly, via other
259 # libraries (they end up in @foreign_objs). We get the latter any time
260 # someone has done something like this in build.info:
261 # SOURCE[libfoo.a]=libbar.a
262 # The indirect object files must be kept in a separate array so they
263 # don't get rebuilt unnecessarily (and with incorrect auxiliary
266 # Object files can't be collected commonly for shared and static
267 # libraries, because we contain their respective object files in
268 # {shared_sources} and {sources}, and because the implications are
269 # slightly different for each library form.
271 # We grab all these "foreign" object files recursively with getlibobjs().
273 unless ($disabled{shared} || $lib =~ /\.a$/) {
274 my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
275 # If this library sources other static libraries and those
276 # libraries are marked {noinst}, there's no need to include
277 # all of their object files. Instead, we treat those static
278 # libraries as dependents alongside any other library this
279 # one depends on, and let symbol resolution do its job.
280 my @sourced_libs = ();
282 my @foreign_objs = ();
284 foreach (@{$unified_info{shared_sources}->{$lib}}) {
287 } elsif ($unified_info{attributes}->{libraries}->{$_}->{noinst}) {
290 push @deps, getsrclibs('sources', $_);
291 push @foreign_objs, getlibobjs('sources', $_);
294 @deps = ( grep { $_ ne $lib } resolvedepends($lib, @deps) );
295 print STDERR "DEBUG[dolib:shlib] \%attrs for $lib : ", ,
296 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
297 if %attrs && $debug_rules;
298 print STDERR "DEBUG[dolib:shlib] \@deps for $lib : ",
299 join(",", map { "\n $_" } @deps), "\n"
300 if @deps && $debug_rules;
301 print STDERR "DEBUG[dolib:shlib] \@objs for $lib : ",
302 join(",", map { "\n $_" } @objs), "\n"
303 if @objs && $debug_rules;
304 print STDERR "DEBUG[dolib:shlib] \@foreign_objs for $lib : ",
305 join(",", map { "\n $_" } @foreign_objs), "\n"
306 if @foreign_objs && $debug_rules;
307 $OUT .= $obj2shlib->(lib => $lib,
309 objs => [ @objs, @foreign_objs ],
312 # If this is somehow a compiled object, take care of it that way
313 # Otherwise, it might simply be generated
314 if (defined $unified_info{sources}->{$_}) {
318 doobj($_, $lib, intent => "shlib", attrs => { %attrs });
321 dogenerate($_, undef, undef, intent => "lib");
326 # When putting static libraries together, we cannot rely on any
327 # symbol resolution, so for all static libraries used as source for
328 # this one, as well as other libraries they depend on, we simply
329 # grab all their object files unconditionally,
330 # Symbol resolution will happen when any program, module or shared
331 # library is linked with this one.
334 my @foreign_objs = ();
335 foreach (@{$unified_info{sources}->{$lib}}) {
339 push @sourcedeps, $_;
342 @sourcedeps = ( grep { $_ ne $lib } resolvedepends(@sourcedeps) );
343 print STDERR "DEBUG[dolib:lib] : \@sourcedeps for $_ : ",
344 join(",", map { "\n $_" } @sourcedeps), "\n"
345 if @sourcedeps && $debug_rules;
346 @foreign_objs = getlibobjs('sources', @sourcedeps);
347 print STDERR "DEBUG[dolib:lib] \%attrs for $lib : ", ,
348 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
349 if %attrs && $debug_rules;
350 print STDERR "DEBUG[dolib:lib] \@objs for $lib : ",
351 join(",", map { "\n $_" } @objs), "\n"
352 if @objs && $debug_rules;
353 print STDERR "DEBUG[dolib:lib] \@foreign_objs for $lib : ",
354 join(",", map { "\n $_" } @foreign_objs), "\n"
355 if @foreign_objs && $debug_rules;
356 $OUT .= obj2lib(lib => $lib, attrs => { %attrs },
357 objs => [ @objs, @foreign_objs ]);
359 doobj($_, $lib, intent => "lib", attrs => { %attrs });
365 # domodule is responsible for building modules. It will call
366 # obj2dso, and also makes sure all object files for the library
370 return "" if $cache{$module};
371 my %attrs = %{$unified_info{attributes}->{modules}->{$module}};
372 my @objs = @{$unified_info{sources}->{$module}};
373 my @deps = ( grep { $_ ne $module }
374 resolvedepends($module) );
375 print STDERR "DEBUG[domodule] \%attrs for $module :",
376 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
378 print STDERR "DEBUG[domodule] \@objs for $module : ",
379 join(",", map { "\n $_" } @objs), "\n"
381 print STDERR "DEBUG[domodule] \@deps for $module : ",
382 join(",", map { "\n $_" } @deps), "\n"
384 $OUT .= obj2dso(module => $module,
388 foreach (@{$unified_info{sources}->{$module}}) {
389 # If this is somehow a compiled object, take care of it that way
390 # Otherwise, it might simply be generated
391 if (defined $unified_info{sources}->{$_}) {
392 doobj($_, $module, intent => "dso", attrs => { %attrs });
394 dogenerate($_, undef, $module, intent => "dso");
400 # dobin is responsible for building programs. It will call obj2bin,
401 # and also makes sure all object files for the library are built.
404 return "" if $cache{$bin};
405 my %attrs = %{$unified_info{attributes}->{programs}->{$bin}};
406 my @objs = @{$unified_info{sources}->{$bin}};
407 my @deps = ( grep { $_ ne $bin } resolvedepends($bin) );
408 print STDERR "DEBUG[dobin] \%attrs for $bin : ",
409 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
410 if %attrs && $debug_rules;
411 print STDERR "DEBUG[dobin] \@objs for $bin : ",
412 join(",", map { "\n $_" } @objs), "\n"
413 if @objs && $debug_rules;
414 print STDERR "DEBUG[dobin] \@deps for $bin : ",
415 join(",", map { "\n $_" } @deps), "\n"
416 if @deps && $debug_rules;
417 $OUT .= obj2bin(bin => $bin,
422 doobj($_, $bin, intent => "bin", attrs => { %attrs });
427 # dobin is responsible for building scripts from templates. It will
431 return "" if $cache{$script};
432 $OUT .= in2script(script => $script,
433 attrs => $unified_info{attributes}->{$script},
434 sources => $unified_info{sources}->{$script});
440 return "" if !exists(&generatedir) or $cache{$dir};
441 $OUT .= generatedir(dir => $dir,
442 deps => $unified_info{dirinfo}->{$dir}->{deps},
443 %{$unified_info{dirinfo}->{$_}->{products}});
447 # dodocs is responsible for building documentation from .pods.
448 # It will call generatesrc.
452 foreach my $doc (@{$unified_info{"${type}docs"}->{$section}}) {
453 next if $cache{$doc};
454 $OUT .= generatesrc(src => $doc,
455 generator => $unified_info{generate}->{$doc});
456 foreach ((@{$unified_info{depends}->{$doc}})) {
457 dogenerate($_, undef, undef, %opts);
463 # Start with populating the cache with all the overrides
464 %cache = map { $_ => 1 } @{$unified_info{overrides}};
466 # Build mandatory generated headers
467 foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
469 # Build all known libraries, modules, programs and scripts.
470 # Everything else will be handled as a consequence.
471 foreach (@{$unified_info{libraries}}) { dolib($_); }
472 foreach (@{$unified_info{modules}}) { domodule($_); }
473 foreach (@{$unified_info{programs}}) { dobin($_); }
474 foreach (@{$unified_info{scripts}}) { doscript($_); }
475 foreach (sort keys %{$unified_info{htmldocs}}) { dodocs('html', $_); }
476 foreach (sort keys %{$unified_info{mandocs}}) { dodocs('man', $_); }
477 foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }