1 {- # -*- Mode: perl -*-
5 # A cache of objects for which a recipe has already been generated
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;
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}};
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);
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;
52 map { $replace{$_} // $_; } @newlist;
55 # is_installed checks if a given file will be installed (i.e. they are
56 # not defined _NO_INST in build.info)
59 if (grep { $product eq $_ }
60 map { (@{$unified_info{install}->{$_}}) }
61 keys %{$unified_info{install}}) {
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.
72 return "" if $cache{$src};
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 generator => $unified_info{generate}->{$src},
82 generator_incs => $unified_info{includes}->{$script},
83 generator_deps => $unified_info{depends}->{$script},
84 deps => $unified_info{depends}->{$src},
85 incs => [ @{$unified_info{includes}->{$bin}},
86 @{$unified_info{includes}->{$obj}} ],
88 foreach (@{$unified_info{depends}->{$src}}) {
89 dogenerate($_, $obj, $bin, %opts);
95 # doobj is responsible for producing all the recipes that build
96 # object files as well as dependency files.
99 return "" if $cache{$obj};
102 if (@{$unified_info{sources}->{$obj}}) {
103 $OUT .= src2obj(obj => $obj,
105 srcs => $unified_info{sources}->{$obj},
106 deps => $unified_info{depends}->{$obj},
107 incs => [ @{$unified_info{includes}->{$bin}},
108 @{$unified_info{includes}->{$obj}} ],
110 foreach ((@{$unified_info{sources}->{$obj}},
111 @{$unified_info{depends}->{$obj}})) {
112 dogenerate($_, $obj, $bin, %opts);
118 # dolib is responsible for building libraries. It will call
119 # libobj2shlib is shared libraries are produced, and obj2lib in all
120 # cases. It also makes sure all object files for the library are
124 return "" if $cache{$lib};
125 unless ($disabled{shared} || $lib =~ /\.a$/) {
127 $unified_info{ordinals}->{$lib}
128 ? (ordinals => $unified_info{ordinals}->{$lib}) : ();
129 $OUT .= libobj2shlib(shlib => $unified_info{sharednames}->{$lib},
131 objs => [ @{$unified_info{shared_sources}->{$lib}},
132 @{$unified_info{sources}->{$lib}} ],
133 deps => [ reducedepends(resolvedepends($lib)) ],
134 installed => is_installed($lib),
136 foreach ((@{$unified_info{shared_sources}->{$lib}},
137 @{$unified_info{sources}->{$lib}})) {
138 # If this is somehow a compiled object, take care of it that way
139 # Otherwise, it might simply be generated
140 if (defined $unified_info{sources}->{$_}) {
141 doobj($_, $lib, intent => "lib", installed => is_installed($lib));
143 dogenerate($_, undef, undef, intent => "lib");
147 $OUT .= obj2lib(lib => $lib,
148 objs => [ @{$unified_info{sources}->{$lib}} ]);
149 foreach (@{$unified_info{sources}->{$lib}}) {
150 doobj($_, $lib, intent => "lib", installed => is_installed($lib));
155 # doengine is responsible for building engines. It will call
156 # obj2dso, and also makes sure all object files for the library
160 return "" if $cache{$lib};
161 $OUT .= obj2dso(lib => $lib,
162 objs => [ @{$unified_info{sources}->{$lib}},
163 @{$unified_info{shared_sources}->{$lib}} ],
164 deps => [ resolvedepends($lib) ],
165 installed => is_installed($lib));
166 foreach ((@{$unified_info{sources}->{$lib}},
167 @{$unified_info{shared_sources}->{$lib}})) {
168 doobj($_, $lib, intent => "dso", installed => is_installed($lib));
173 # dobin is responsible for building programs. It will call obj2bin,
174 # and also makes sure all object files for the library are built.
177 return "" if $cache{$bin};
178 my $deps = [ reducedepends(resolvedepends($bin)) ];
179 $OUT .= obj2bin(bin => $bin,
180 objs => [ @{$unified_info{sources}->{$bin}} ],
182 installed => is_installed($bin));
183 foreach (@{$unified_info{sources}->{$bin}}) {
184 doobj($_, $bin, intent => "bin", installed => is_installed($bin));
189 # dobin is responsible for building scripts from templates. It will
193 return "" if $cache{$script};
194 $OUT .= in2script(script => $script,
195 sources => $unified_info{sources}->{$script},
196 installed => is_installed($script));
202 return "" if !exists(&generatedir) or $cache{$dir};
203 $OUT .= generatedir(dir => $dir,
204 deps => $unified_info{dirinfo}->{$dir}->{deps},
205 %{$unified_info{dirinfo}->{$_}->{products}});
209 # Start with populating the cache with all the overrides
210 %cache = map { $_ => 1 } @{$unified_info{overrides}};
212 # For convenience collect information regarding directories where
213 # files are generated, those generated files and the end product
214 # they end up in where applicable. Then, add build rules for those
216 if (exists &generatedir) {
217 my %loopinfo = ( "dso" => [ @{$unified_info{engines}} ],
218 "lib" => [ @{$unified_info{libraries}} ],
219 "bin" => [ @{$unified_info{programs}} ],
220 "script" => [ @{$unified_info{scripts}} ] );
221 foreach my $type (keys %loopinfo) {
222 foreach my $product (@{$loopinfo{$type}}) {
224 my $pd = dirname($product);
226 # We already have a "test" target, and the current directory
227 # is just silly to make a target for
228 $dirs{$pd} = 1 unless $pd eq "test" || $pd eq ".";
230 foreach (@{$unified_info{sources}->{$product}}) {
233 # We don't want to create targets for source directories
234 # when building out of source
235 next if ($config{sourcedir} ne $config{builddir}
236 && $d =~ m|^\Q$config{sourcedir}\E|);
237 # We already have a "test" target, and the current directory
238 # is just silly to make a target for
239 next if $d eq "test" || $d eq ".";
242 push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
245 foreach (keys %dirs) {
246 push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
253 # Build mandatory generated headers
254 foreach (@{$unified_info{depends}->{""}}) { dogenerate($_); }
256 # Build all known libraries, engines, programs and scripts.
257 # Everything else will be handled as a consequence.
258 foreach (@{$unified_info{libraries}}) { dolib($_); }
259 foreach (@{$unified_info{engines}}) { doengine($_); }
260 foreach (@{$unified_info{programs}}) { dobin($_); }
261 foreach (@{$unified_info{scripts}}) { doscript($_); }
263 foreach (sort keys %{$unified_info{dirinfo}}) { dodir($_); }
265 # Finally, should there be any applicable BEGINRAW/ENDRAW sections,
266 # they are added here.
267 $OUT .= $_."\n" foreach @{$unified_info{rawlines}};