8 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
10 @EXPORT = qw(gentemplate);
17 my $generator = OpenSSL::GenTemplate->new(%opts);
19 # Build mandatory header file generators
20 foreach (@{$generator->{info}->{depends}->{""}}) { $generator->dogenerate($_); }
22 # Build all known targets, libraries, modules, programs and scripts.
23 # Everything else will be handled as a consequence.
24 foreach (@{$generator->{info}->{targets}}) { $generator->dotarget($_); }
25 foreach (@{$generator->{info}->{libraries}}) { $generator->dolib($_); }
26 foreach (@{$generator->{info}->{modules}}) { $generator->domodule($_); }
27 foreach (@{$generator->{info}->{programs}}) { $generator->dobin($_); }
28 foreach (@{$generator->{info}->{scripts}}) { $generator->doscript($_); }
29 foreach (sort keys %{$generator->{info}->{htmldocs}}) { $generator->dodocs('html', $_); }
30 foreach (sort keys %{$generator->{info}->{mandocs}}) { $generator->dodocs('man', $_); }
31 foreach (sort keys %{$generator->{info}->{dirinfo}}) { $generator->dodir($_); }
34 package OpenSSL::GenTemplate;
43 output => $opts{output},
44 config => $opts{config} // {},
45 disabled => $opts{disabled} // {},
46 info => $opts{unified_info} // {},
49 return bless $data, $class;
56 my $fh = $self->{output};
58 die "No name?" unless $name;
59 print $fh "{-\n ", $name, '(', dump_data(\%opts), ');', " \n-}";
62 my $debug_resolvedepends = $ENV{BUILDFILE_DEBUG_DEPENDS};
63 my $debug_rules = $ENV{BUILDFILE_DEBUG_RULES};
65 # A cache of objects for which a recipe has already been generated
68 # collectdepends, expanddepends and reducedepends work together to make
69 # sure there are no duplicate or weak dependencies and that they are in
70 # the right order. This is used to sort the list of libraries that a
72 sub extensionlesslib {
73 my @result = map { $_ =~ /(\.a)?$/; $` } @_;
74 return @result if wantarray;
78 # collectdepends dives into the tree of dependencies and returns
79 # a list of all the non-weak ones.
85 my $extensionlessthing = extensionlesslib($thing);
86 my @listsofar = @_; # to check if we're looping
87 my @list = @{ $self->{info}->{depends}->{$thing} //
88 $self->{info}->{depends}->{$extensionlessthing}
92 print STDERR "DEBUG[collectdepends] $thing > ", join(' ', @listsofar), "\n"
93 if $debug_resolvedepends;
94 foreach my $item (@list) {
95 my $extensionlessitem = extensionlesslib($item);
96 # It's time to break off when the dependency list starts looping
97 next if grep { extensionlesslib($_) eq $extensionlessitem } @listsofar;
98 # Don't add anything here if the dependency is weak
99 next if defined $self->{info}->{attributes}->{depends}->{$thing}->{$item}->{'weak'};
100 my @resolved = $self->collectdepends($item, @listsofar, $item);
101 push @newlist, $item, @resolved;
103 print STDERR "DEBUG[collectdepends] $thing < ", join(' ', @newlist), "\n"
104 if $debug_resolvedepends;
108 # expanddepends goes through a list of stuff, checks if they have any
109 # dependencies, and adds them at the end of the current position if
110 # they aren't already present later on.
114 print STDERR "DEBUG[expanddepends]> ", join(' ', @after), "\n"
115 if $debug_resolvedepends;
118 my $item = shift @after;
119 print STDERR "DEBUG[expanddepends]\\ ", join(' ', @before), "\n"
120 if $debug_resolvedepends;
121 print STDERR "DEBUG[expanddepends] - ", $item, "\n"
122 if $debug_resolvedepends;
127 my $extlessx = extensionlesslib($x);
128 if (grep { $extlessx eq extensionlesslib($_) } @before
130 !grep { $extlessx eq extensionlesslib($_) } @after) {
131 print STDERR "DEBUG[expanddepends] + ", $x, "\n"
132 if $debug_resolvedepends;
135 print STDERR "DEBUG[expanddepends] ! ", $x, "\n"
136 if $debug_resolvedepends;
139 } @{$self->{info}->{depends}->{$item} // []}
141 print STDERR "DEBUG[expanddepends] = ", join(' ', @middle), "\n"
142 if $debug_resolvedepends;
143 print STDERR "DEBUG[expanddepends]/ ", join(' ', @after), "\n"
144 if $debug_resolvedepends;
145 push @before, @middle;
147 print STDERR "DEBUG[expanddepends]< ", join(' ', @before), "\n"
148 if $debug_resolvedepends;
152 # reducedepends looks through a list, and checks if each item is
153 # repeated later on. If it is, the earlier copy is dropped.
156 print STDERR "DEBUG[reducedepends]> ", join(' ', @list), "\n"
157 if $debug_resolvedepends;
161 my $item = shift @list;
162 my $extensionlessitem = extensionlesslib($item);
163 if (grep { $extensionlessitem eq extensionlesslib($_) } @list) {
164 if ($item ne $extensionlessitem) {
165 # If this instance of the library is explicitly static, we
166 # prefer that to any shared library name, since it must have
167 # been done on purpose.
168 $replace{$extensionlessitem} = $item;
171 push @newlist, $item;
174 @newlist = map { $replace{$_} // $_; } @newlist;
175 print STDERR "DEBUG[reducedepends]< ", join(' ', @newlist), "\n"
176 if $debug_resolvedepends;
181 # This takes multiple inputs and combine them into a single list of
182 # interdependent things. The returned value will include all the input.
183 # Callers are responsible for taking away the things they are building.
186 print STDERR "DEBUG[resolvedepends] START (", join(', ', @_), ")\n"
187 if $debug_resolvedepends;
189 reducedepends($self->expanddepends(map { ( $_, $self->collectdepends($_) ) } @_));
190 print STDERR "DEBUG[resolvedepends] END (", join(', ', @_), ") : ",
191 join(',', map { "\n $_" } @all), "\n"
192 if $debug_resolvedepends;
196 # dogenerate is responsible for producing all the recipes that build
197 # generated source files. It recurses in case a dependency is also a
198 # generated source file.
203 return "" unless defined $self->{info}->{generate}->{$_};
204 return "" if $cache{$src};
208 if ($self->{info}->{generate}->{$src}) {
209 die "$src is generated by Configure, should not appear in build file\n"
210 if ref $self->{info}->{generate}->{$src} eq "";
211 my $script = $self->{info}->{generate}->{$src}->[0];
212 $self->emit('generatesrc',
215 generator => $self->{info}->{generate}->{$src},
216 generator_incs => $self->{info}->{includes}->{$script} // [],
217 generator_deps => $self->{info}->{depends}->{$script} // [],
218 deps => $self->{info}->{depends}->{$src} // [],
219 incs => [ defined $obj ? @{$self->{info}->{includes}->{$obj} // []} : (),
220 defined $bin ? @{$self->{info}->{includes}->{$bin} // []} : () ],
221 defs => [ defined $obj ? @{$self->{info}->{defines}->{$obj} // []} : (),
222 defined $bin ? @{$self->{info}->{defines}->{$bin} // []} : () ],
224 foreach (@{$self->{info}->{depends}->{$src} // []}) {
225 $self->dogenerate($_, $obj, $bin, %opts);
234 return "" if $cache{$target};
235 $self->emit('generatetarget',
237 deps => $self->{info}->{depends}->{$target} // []);
238 foreach (@{$self->{info}->{depends}->{$target} // []}) {
239 $self->dogenerate($_);
244 # doobj is responsible for producing all the recipes that build
245 # object files as well as dependency files.
249 return "" if $cache{$obj};
252 if (@{$self->{info}->{sources}->{$obj} // []}) {
253 my @srcs = @{$self->{info}->{sources}->{$obj}};
254 my @deps = @{$self->{info}->{depends}->{$obj} // []};
255 my @incs = ( @{$self->{info}->{includes}->{$obj} // []},
256 @{$self->{info}->{includes}->{$bin} // []} );
257 my @defs = ( @{$self->{info}->{defines}->{$obj} // []},
258 @{$self->{info}->{defines}->{$bin} // []} );
259 print STDERR "DEBUG[doobj] \@srcs for $obj ($bin) : ",
260 join(",", map { "\n $_" } @srcs), "\n"
262 print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
263 join(",", map { "\n $_" } @deps), "\n"
265 print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
266 join(",", map { "\n $_" } @incs), "\n"
268 print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
269 join(",", map { "\n $_" } @defs), "\n"
271 print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
272 join(",", map { "\n $_ = $opts{$_}" } sort keys %opts), "\n"
274 $self->emit('src2obj',
275 obj => $obj, product => $bin,
276 srcs => [ @srcs ], deps => [ @deps ],
277 incs => [ @incs ], defs => [ @defs ],
279 foreach ((@{$self->{info}->{sources}->{$obj}},
280 @{$self->{info}->{depends}->{$obj} // []})) {
281 $self->dogenerate($_, $obj, $bin, %opts);
287 # Helper functions to grab all applicable intermediary files.
288 # This is particularly useful when a library is given as source
289 # rather than a dependency. In that case, we consider it to be a
290 # container with object file references, or possibly references
291 # to further libraries to pilfer in the same way.
296 # For all input, see if it sources static libraries. If it does,
297 # return them together with the result of a recursive call.
298 map { ( $_, getsrclibs($section, $_) ) }
299 grep { $_ =~ m|\.a$| }
300 map { @{$self->{info}->{$section}->{$_} // []} }
308 # For all input, see if it's an intermediary file (library or object).
309 # If it is, collect the result of a recursive call, or if that returns
310 # an empty list, the element itself. Return the result.
312 my @x = $self->getlibobjs($section, @{$self->{info}->{$section}->{$_}});
315 grep { defined $self->{info}->{$section}->{$_} }
319 # dolib is responsible for building libraries. It will call
320 # obj2shlib if shared libraries are produced, and obj2lib in all
321 # cases. It also makes sure all object files for the library are
326 return "" if $cache{$lib};
328 my %attrs = %{$self->{info}->{attributes}->{libraries}->{$lib} // {}};
330 my @deps = ( $self->resolvedepends(getsrclibs('sources', $lib)) );
332 # We support two types of objs, those who are specific to this library
333 # (they end up in @objs) and those that we get indirectly, via other
334 # libraries (they end up in @foreign_objs). We get the latter any time
335 # someone has done something like this in build.info:
336 # SOURCE[libfoo.a]=libbar.a
337 # The indirect object files must be kept in a separate array so they
338 # don't get rebuilt unnecessarily (and with incorrect auxiliary
341 # Object files can't be collected commonly for shared and static
342 # libraries, because we contain their respective object files in
343 # {shared_sources} and {sources}, and because the implications are
344 # slightly different for each library form.
346 # We grab all these "foreign" object files recursively with getlibobjs().
348 unless ($self->{disabled}->{shared} || $lib =~ /\.a$/) {
349 # If this library sources other static libraries and those
350 # libraries are marked {noinst}, there's no need to include
351 # all of their object files. Instead, we treat those static
352 # libraries as dependents alongside any other library this
353 # one depends on, and let symbol resolution do its job.
354 my @sourced_libs = ();
356 my @foreign_objs = ();
358 foreach (@{$self->{info}->{shared_sources}->{$lib} // []}) {
361 } elsif ($self->{info}->{attributes}->{libraries}->{$_}->{noinst}) {
364 push @deps, $self->getsrclibs('sources', $_);
365 push @foreign_objs, $self->getlibobjs('sources', $_);
368 @deps = ( grep { $_ ne $lib } $self->resolvedepends($lib, @deps) );
369 print STDERR "DEBUG[dolib:shlib] \%attrs for $lib : ", ,
370 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
371 if %attrs && $debug_rules;
372 print STDERR "DEBUG[dolib:shlib] \@deps for $lib : ",
373 join(",", map { "\n $_" } @deps), "\n"
374 if @deps && $debug_rules;
375 print STDERR "DEBUG[dolib:shlib] \@objs for $lib : ",
376 join(",", map { "\n $_" } @objs), "\n"
377 if @objs && $debug_rules;
378 print STDERR "DEBUG[dolib:shlib] \@foreign_objs for $lib : ",
379 join(",", map { "\n $_" } @foreign_objs), "\n"
380 if @foreign_objs && $debug_rules;
381 $self->emit('obj2shlib',
384 objs => [ @objs, @foreign_objs ],
387 # If this is somehow a compiled object, take care of it that way
388 # Otherwise, it might simply be generated
389 if (defined $self->{info}->{sources}->{$_}) {
393 $self->doobj($_, $lib, intent => "shlib", attrs => { %attrs });
396 $self->dogenerate($_, undef, undef, intent => "lib");
401 # When putting static libraries together, we cannot rely on any
402 # symbol resolution, so for all static libraries used as source for
403 # this one, as well as other libraries they depend on, we simply
404 # grab all their object files unconditionally,
405 # Symbol resolution will happen when any program, module or shared
406 # library is linked with this one.
409 my @foreign_objs = ();
410 foreach (@{$self->{info}->{sources}->{$lib}}) {
414 push @sourcedeps, $_;
417 @sourcedeps = ( grep { $_ ne $lib } $self->resolvedepends(@sourcedeps) );
418 print STDERR "DEBUG[dolib:lib] : \@sourcedeps for $_ : ",
419 join(",", map { "\n $_" } @sourcedeps), "\n"
420 if @sourcedeps && $debug_rules;
421 @foreign_objs = $self->getlibobjs('sources', @sourcedeps);
422 print STDERR "DEBUG[dolib:lib] \%attrs for $lib : ", ,
423 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
424 if %attrs && $debug_rules;
425 print STDERR "DEBUG[dolib:lib] \@objs for $lib : ",
426 join(",", map { "\n $_" } @objs), "\n"
427 if @objs && $debug_rules;
428 print STDERR "DEBUG[dolib:lib] \@foreign_objs for $lib : ",
429 join(",", map { "\n $_" } @foreign_objs), "\n"
430 if @foreign_objs && $debug_rules;
431 $self->emit('obj2lib',
432 lib => $lib, attrs => { %attrs },
433 objs => [ @objs, @foreign_objs ]);
435 $self->doobj($_, $lib, intent => "lib", attrs => { %attrs });
441 # domodule is responsible for building modules. It will call
442 # obj2dso, and also makes sure all object files for the library
447 return "" if $cache{$module};
448 my %attrs = %{$self->{info}->{attributes}->{modules}->{$module} // {}};
449 my @objs = @{$self->{info}->{sources}->{$module}};
450 my @deps = ( grep { $_ ne $module }
451 $self->resolvedepends($module) );
452 print STDERR "DEBUG[domodule] \%attrs for $module :",
453 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
455 print STDERR "DEBUG[domodule] \@objs for $module : ",
456 join(",", map { "\n $_" } @objs), "\n"
458 print STDERR "DEBUG[domodule] \@deps for $module : ",
459 join(",", map { "\n $_" } @deps), "\n"
461 $self->emit('obj2dso',
466 foreach (@{$self->{info}->{sources}->{$module}}) {
467 # If this is somehow a compiled object, take care of it that way
468 # Otherwise, it might simply be generated
469 if (defined $self->{info}->{sources}->{$_}) {
470 $self->doobj($_, $module, intent => "dso", attrs => { %attrs });
472 $self->dogenerate($_, undef, $module, intent => "dso");
478 # dobin is responsible for building programs. It will call obj2bin,
479 # and also makes sure all object files for the library are built.
483 return "" if $cache{$bin};
484 my %attrs = %{$self->{info}->{attributes}->{programs}->{$bin} // {}};
485 my @objs = @{$self->{info}->{sources}->{$bin}};
486 my @deps = ( grep { $_ ne $bin } $self->resolvedepends($bin) );
487 print STDERR "DEBUG[dobin] \%attrs for $bin : ",
488 join(",", map { "\n $_ = $attrs{$_}" } sort keys %attrs), "\n"
489 if %attrs && $debug_rules;
490 print STDERR "DEBUG[dobin] \@objs for $bin : ",
491 join(",", map { "\n $_" } @objs), "\n"
492 if @objs && $debug_rules;
493 print STDERR "DEBUG[dobin] \@deps for $bin : ",
494 join(",", map { "\n $_" } @deps), "\n"
495 if @deps && $debug_rules;
496 $self->emit('obj2bin',
502 $self->doobj($_, $bin, intent => "bin", attrs => { %attrs });
507 # doscript is responsible for building scripts from templates. It will
512 return "" if $cache{$script};
513 $self->emit('in2script',
515 attrs => $self->{info}->{attributes}->{scripts}->{$script} // {},
516 sources => $self->{info}->{sources}->{$script});
523 return "" if !exists(&generatedir) or $cache{$dir};
524 $self->emit('generatedir',
526 deps => $self->{info}->{dirinfo}->{$dir}->{deps} // [],
527 %{$self->{info}->{dirinfo}->{$_}->{products}});
531 # dodocs is responsible for building documentation from .pods.
532 # It will call generatesrc.
537 foreach my $doc (@{$self->{info}->{"${type}docs"}->{$section}}) {
538 next if $cache{$doc};
539 $self->emit('generatesrc',
541 generator => $self->{info}->{generate}->{$doc});
542 foreach ((@{$self->{info}->{depends}->{$doc} // []})) {
543 $self->dogenerate($_, undef, undef);