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 $OUT .= src2obj(obj => $obj,
176 srcs => $unified_info{sources}->{$obj},
177 deps => $unified_info{depends}->{$obj},
178 incs => [ @{$unified_info{includes}->{$obj}},
179 @{$unified_info{includes}->{$bin}} ],
180 defs => [ @{$unified_info{defines}->{$obj}},
181 @{$unified_info{defines}->{$bin}} ],
183 foreach ((@{$unified_info{sources}->{$obj}},
184 @{$unified_info{depends}->{$obj}})) {
185 dogenerate($_, $obj, $bin, %opts);
191 # dolib is responsible for building libraries. It will call
192 # obj2shlib is shared libraries are produced, and obj2lib in all
193 # cases. It also makes sure all object files for the library are
197 return "" if $cache{$lib};
198 unless ($disabled{shared} || $lib =~ /\.a$/) {
199 my $obj2shlib = defined &obj2shlib ? \&obj2shlib : \&libobj2shlib;
200 $OUT .= $obj2shlib->(lib => $lib,
201 attrs => $unified_info{attributes}->{libraries}->{$lib},
202 objs => $unified_info{shared_sources}->{$lib},
203 deps => [ grep { $_ ne $lib } resolvedepends($lib) ]);
204 foreach ((@{$unified_info{shared_sources}->{$lib}},
205 @{$unified_info{sources}->{$lib}})) {
206 # If this is somehow a compiled object, take care of it that way
207 # Otherwise, it might simply be generated
208 if (defined $unified_info{sources}->{$_}) {
209 doobj($_, $lib, intent => "shlib",
210 attrs => $unified_info{attributes}->{libraries}->{$lib});
212 dogenerate($_, undef, undef, intent => "lib");
216 $OUT .= obj2lib(lib => $lib,
217 attrs => $unified_info{attributes}->{libraries}->{$lib},
218 objs => [ @{$unified_info{sources}->{$lib}} ]);
219 foreach (@{$unified_info{sources}->{$lib}}) {
220 doobj($_, $lib, intent => "lib",
221 attrs => $unified_info{attributes}->{libraries}->{$lib});
226 # domodule is responsible for building modules. It will call
227 # obj2dso, and also makes sure all object files for the library
231 return "" if $cache{$module};
232 $OUT .= obj2dso(module => $module,
233 attrs => $unified_info{attributes}->{modules}->{$module},
234 objs => $unified_info{sources}->{$module},
235 deps => [ grep { $_ ne $module }
236 resolvedepends($module) ]);
237 foreach (@{$unified_info{sources}->{$module}}) {
238 # If this is somehow a compiled object, take care of it that way
239 # Otherwise, it might simply be generated
240 if (defined $unified_info{sources}->{$_}) {
241 doobj($_, $module, intent => "dso",
242 attrs => $unified_info{attributes}->{modules}->{$module});
244 dogenerate($_, undef, $module, intent => "dso");
250 # dobin is responsible for building programs. It will call obj2bin,
251 # and also makes sure all object files for the library are built.
254 return "" if $cache{$bin};
255 $OUT .= obj2bin(bin => $bin,
256 attrs => $unified_info{attributes}->{programs}->{$bin},
257 objs => [ @{$unified_info{sources}->{$bin}} ],
258 deps => [ grep { $_ ne $bin } resolvedepends($bin) ]);
259 foreach (@{$unified_info{sources}->{$bin}}) {
260 doobj($_, $bin, intent => "bin",
261 attrs => $unified_info{attributes}->{$bin});
266 # dobin is responsible for building scripts from templates. It will
270 return "" if $cache{$script};
271 $OUT .= in2script(script => $script,
272 attrs => $unified_info{attributes}->{$script},
273 sources => $unified_info{sources}->{$script});
279 return "" if !exists(&generatedir) or $cache{$dir};
280 $OUT .= generatedir(dir => $dir,
281 deps => $unified_info{dirinfo}->{$dir}->{deps},
282 %{$unified_info{dirinfo}->{$_}->{products}});
286 # Start with populating the cache with all the overrides
287 %cache = map { $_ => 1 } @{$unified_info{overrides}};
289 # Build mandatory generated headers
290 foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
292 # Build all known libraries, modules, programs and scripts.
293 # Everything else will be handled as a consequence.
294 foreach (@{$unified_info{libraries}}) { dolib($_); }
295 foreach (@{$unified_info{modules}}) { domodule($_); }
296 foreach (@{$unified_info{programs}}) { dobin($_); }
297 foreach (@{$unified_info{scripts}}) { doscript($_); }
299 foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }