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.
141 return "" unless defined $unified_info{generate}->{$_};
142 return "" if $cache{$src};
146 if ($unified_info{generate}->{$src}) {
147 die "$src is generated by Configure, should not appear in build file\n"
148 if ref $unified_info{generate}->{$src} eq "";
149 my $script = $unified_info{generate}->{$src}->[0];
150 $OUT .= generatesrc(src => $src,
152 generator => $unified_info{generate}->{$src},
153 generator_incs => $unified_info{includes}->{$script},
154 generator_deps => $unified_info{depends}->{$script},
155 deps => $unified_info{depends}->{$src},
156 incs => [ defined $obj
157 ? @{$unified_info{includes}->{$obj}}
160 ? @{$unified_info{includes}->{$bin}}
162 defs => [ defined $obj
163 ? @{$unified_info{defines}->{$obj}}
166 ? @{$unified_info{defines}->{$bin}}
169 foreach (@{$unified_info{depends}->{$src}}) {
170 dogenerate($_, $obj, $bin, %opts);
178 return "" if $cache{$target};
179 $OUT .= generatetarget(target => $target,
180 deps => $unified_info{depends}->{$target});
181 foreach (@{$unified_info{depends}->{$target}}) {
187 # doobj is responsible for producing all the recipes that build
188 # object files as well as dependency files.
191 return "" if $cache{$obj};
194 if (@{$unified_info{sources}->{$obj}}) {
195 my @srcs = @{$unified_info{sources}->{$obj}};
196 my @deps = @{$unified_info{depends}->{$obj}};
197 my @incs = ( @{$unified_info{includes}->{$obj}},
198 @{$unified_info{includes}->{$bin}} );
199 my @defs = ( @{$unified_info{defines}->{$obj}},
200 @{$unified_info{defines}->{$bin}} );
201 print STDERR "DEBUG[doobj] \@srcs for $obj ($bin) : ",
202 join(",", map { "\n $_" } @srcs), "\n"
204 print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
205 join(",", map { "\n $_" } @deps), "\n"
207 print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
208 join(",", map { "\n $_" } @incs), "\n"
210 print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
211 join(",", map { "\n $_" } @defs), "\n"
213 print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
214 join(",", map { "\n $_ = $opts{$_}" } sort keys %opts), "\n"
216 $OUT .= src2obj(obj => $obj, product => $bin,
217 srcs => [ @srcs ], deps => [ @deps ],
218 incs => [ @incs ], defs => [ @defs ],
220 foreach ((@{$unified_info{sources}->{$obj}},
221 @{$unified_info{depends}->{$obj}})) {
222 dogenerate($_, $obj, $bin, %opts);
228 # Helper functions to grab all applicable intermediary files.
229 # This is particularly useful when a library is given as source
230 # rather than a dependency. In that case, we consider it to be a
231 # container with object file references, or possibly references
232 # to further libraries to pilfer in the same way.
236 # For all input, see if it sources static libraries. If it does,
237 # return them together with the result of a recursive call.
238 map { ( $_, getsrclibs($section, $_) ) }
239 grep { $_ =~ m|\.a$| }
240 map { @{$unified_info{$section}->{$_} // []} }
247 # For all input, see if it's an intermediary file (library or object).
248 # If it is, collect the result of a recursive call, or if that returns
249 # an empty list, the element itself. Return the result.
251 my @x = getlibobjs($section, @{$unified_info{$section}->{$_}});
254 grep { defined $unified_info{$section}->{$_} }
258 # dolib is responsible for building libraries. It will call
259 # obj2shlib if shared libraries are produced, and obj2lib in all
260 # cases. It also makes sure all object files for the library are
264 return "" if $cache{$lib};
266 my %attrs = %{$unified_info{attributes}->{libraries}->{$lib}};
268 my @deps = ( resolvedepends(getsrclibs('sources', $lib)) );
270 # We support two types of objs, those who are specific to this library
271 # (they end up in @objs) and those that we get indirectly, via other
272 # libraries (they end up in @foreign_objs). We get the latter any time
273 # someone has done something like this in build.info:
274 # SOURCE[libfoo.a]=libbar.a
275 # The indirect object files must be kept in a separate array so they
276 # don't get rebuilt unnecessarily (and with incorrect auxiliary
279 # Object files can't be collected commonly for shared and static
280 # libraries, because we contain their respective object files in
281 # {shared_sources} and {sources}, and because the implications are
282 # slightly different for each library form.
284 # We grab all these "foreign" object files recursively with getlibobjs().
286 unless ($disabled{shared} || $lib =~ /\.a$/) {
287 my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
288 # If this library sources other static libraries and those
289 # libraries are marked {noinst}, there's no need to include
290 # all of their object files. Instead, we treat those static
291 # libraries as dependents alongside any other library this
292 # one depends on, and let symbol resolution do its job.
293 my @sourced_libs = ();
295 my @foreign_objs = ();
297 foreach (@{$unified_info{shared_sources}->{$lib}}) {
300 } elsif ($unified_info{attributes}->{libraries}->{$_}->{noinst}) {
303 push @deps, getsrclibs('sources', $_);
304 push @foreign_objs, getlibobjs('sources', $_);
307 @deps = ( grep { $_ ne $lib } resolvedepends($lib, @deps) );
308 print STDERR "DEBUG[dolib:shlib] \%attrs for $lib : ", ,
309 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
310 if %attrs && $debug_rules;
311 print STDERR "DEBUG[dolib:shlib] \@deps for $lib : ",
312 join(",", map { "\n $_" } @deps), "\n"
313 if @deps && $debug_rules;
314 print STDERR "DEBUG[dolib:shlib] \@objs for $lib : ",
315 join(",", map { "\n $_" } @objs), "\n"
316 if @objs && $debug_rules;
317 print STDERR "DEBUG[dolib:shlib] \@foreign_objs for $lib : ",
318 join(",", map { "\n $_" } @foreign_objs), "\n"
319 if @foreign_objs && $debug_rules;
320 $OUT .= $obj2shlib->(lib => $lib,
322 objs => [ @objs, @foreign_objs ],
325 # If this is somehow a compiled object, take care of it that way
326 # Otherwise, it might simply be generated
327 if (defined $unified_info{sources}->{$_}) {
331 doobj($_, $lib, intent => "shlib", attrs => { %attrs });
334 dogenerate($_, undef, undef, intent => "lib");
339 # When putting static libraries together, we cannot rely on any
340 # symbol resolution, so for all static libraries used as source for
341 # this one, as well as other libraries they depend on, we simply
342 # grab all their object files unconditionally,
343 # Symbol resolution will happen when any program, module or shared
344 # library is linked with this one.
347 my @foreign_objs = ();
348 foreach (@{$unified_info{sources}->{$lib}}) {
352 push @sourcedeps, $_;
355 @sourcedeps = ( grep { $_ ne $lib } resolvedepends(@sourcedeps) );
356 print STDERR "DEBUG[dolib:lib] : \@sourcedeps for $_ : ",
357 join(",", map { "\n $_" } @sourcedeps), "\n"
358 if @sourcedeps && $debug_rules;
359 @foreign_objs = getlibobjs('sources', @sourcedeps);
360 print STDERR "DEBUG[dolib:lib] \%attrs for $lib : ", ,
361 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
362 if %attrs && $debug_rules;
363 print STDERR "DEBUG[dolib:lib] \@objs for $lib : ",
364 join(",", map { "\n $_" } @objs), "\n"
365 if @objs && $debug_rules;
366 print STDERR "DEBUG[dolib:lib] \@foreign_objs for $lib : ",
367 join(",", map { "\n $_" } @foreign_objs), "\n"
368 if @foreign_objs && $debug_rules;
369 $OUT .= obj2lib(lib => $lib, attrs => { %attrs },
370 objs => [ @objs, @foreign_objs ]);
372 doobj($_, $lib, intent => "lib", attrs => { %attrs });
378 # domodule is responsible for building modules. It will call
379 # obj2dso, and also makes sure all object files for the library
383 return "" if $cache{$module};
384 my %attrs = %{$unified_info{attributes}->{modules}->{$module}};
385 my @objs = @{$unified_info{sources}->{$module}};
386 my @deps = ( grep { $_ ne $module }
387 resolvedepends($module) );
388 print STDERR "DEBUG[domodule] \%attrs for $module :",
389 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
391 print STDERR "DEBUG[domodule] \@objs for $module : ",
392 join(",", map { "\n $_" } @objs), "\n"
394 print STDERR "DEBUG[domodule] \@deps for $module : ",
395 join(",", map { "\n $_" } @deps), "\n"
397 $OUT .= obj2dso(module => $module,
401 foreach (@{$unified_info{sources}->{$module}}) {
402 # If this is somehow a compiled object, take care of it that way
403 # Otherwise, it might simply be generated
404 if (defined $unified_info{sources}->{$_}) {
405 doobj($_, $module, intent => "dso", attrs => { %attrs });
407 dogenerate($_, undef, $module, intent => "dso");
413 # dobin is responsible for building programs. It will call obj2bin,
414 # and also makes sure all object files for the library are built.
417 return "" if $cache{$bin};
418 my %attrs = %{$unified_info{attributes}->{programs}->{$bin}};
419 my @objs = @{$unified_info{sources}->{$bin}};
420 my @deps = ( grep { $_ ne $bin } resolvedepends($bin) );
421 print STDERR "DEBUG[dobin] \%attrs for $bin : ",
422 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
423 if %attrs && $debug_rules;
424 print STDERR "DEBUG[dobin] \@objs for $bin : ",
425 join(",", map { "\n $_" } @objs), "\n"
426 if @objs && $debug_rules;
427 print STDERR "DEBUG[dobin] \@deps for $bin : ",
428 join(",", map { "\n $_" } @deps), "\n"
429 if @deps && $debug_rules;
430 $OUT .= obj2bin(bin => $bin,
435 doobj($_, $bin, intent => "bin", attrs => { %attrs });
440 # dobin is responsible for building scripts from templates. It will
444 return "" if $cache{$script};
445 $OUT .= in2script(script => $script,
446 attrs => $unified_info{attributes}->{$script},
447 sources => $unified_info{sources}->{$script});
453 return "" if !exists(&generatedir) or $cache{$dir};
454 $OUT .= generatedir(dir => $dir,
455 deps => $unified_info{dirinfo}->{$dir}->{deps},
456 %{$unified_info{dirinfo}->{$_}->{products}});
460 # dodocs is responsible for building documentation from .pods.
461 # It will call generatesrc.
465 foreach my $doc (@{$unified_info{"${type}docs"}->{$section}}) {
466 next if $cache{$doc};
467 $OUT .= generatesrc(src => $doc,
468 generator => $unified_info{generate}->{$doc});
469 foreach ((@{$unified_info{depends}->{$doc}})) {
470 dogenerate($_, undef, undef, %opts);
476 # Start with populating the cache with all the overrides
477 %cache = map { $_ => 1 } @{$unified_info{overrides}};
479 # Build mandatory header file generators
480 foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
482 # Build all known targets, libraries, modules, programs and scripts.
483 # Everything else will be handled as a consequence.
484 foreach (@{$unified_info{targets}}) { dotarget($_); }
485 foreach (@{$unified_info{libraries}}) { dolib($_); }
486 foreach (@{$unified_info{modules}}) { domodule($_); }
487 foreach (@{$unified_info{programs}}) { dobin($_); }
488 foreach (@{$unified_info{scripts}}) { doscript($_); }
489 foreach (sort keys %{$unified_info{htmldocs}}) { dodocs('html', $_); }
490 foreach (sort keys %{$unified_info{mandocs}}) { dodocs('man', $_); }
491 foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }