1package gentemplate;
2
3use strict;
4use warnings;
5use Carp;
6
7use Exporter;
8use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
9@ISA = qw(Exporter);
10@EXPORT = qw(gentemplate);
11
12use File::Basename;
13
14sub gentemplate {
15    my %opts = @_;
16
17    my $generator = OpenSSL::GenTemplate->new(%opts);
18
19    # Build mandatory header file generators
20    foreach (@{$generator->{info}->{depends}->{""}}) { $generator->dogenerate($_); }
21
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($_); }
32}
33
34package OpenSSL::GenTemplate;
35
36use OpenSSL::Util;
37
38sub new {
39    my $class = shift;
40    my %opts = @_;
41
42    my $data = {
43        output   => $opts{output},
44        config   => $opts{config} // {},
45        disabled => $opts{disabled} // {},
46        info     => $opts{unified_info} // {},
47    };
48
49    return bless $data, $class;
50};
51
52sub emit {
53    my $self = shift;
54    my $name = shift;
55    my %opts = @_;
56    my $fh = $self->{output};
57
58    die "No name?" unless $name;
59    print $fh "{-\n ", $name, '(', dump_data(\%opts), ');', " \n-}";
60}
61
62my $debug_resolvedepends = $ENV{BUILDFILE_DEBUG_DEPENDS};
63my $debug_rules = $ENV{BUILDFILE_DEBUG_RULES};
64
65# A cache of objects for which a recipe has already been generated
66our %cache;
67
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
71# build depends on.
72sub extensionlesslib {
73    my @result = map { $_ =~ /(\.a)?$/; $` } @_;
74    return @result if wantarray;
75    return $result[0];
76}
77
78# collectdepends dives into the tree of dependencies and returns
79# a list of all the non-weak ones.
80sub collectdepends {
81    my $self = shift;
82    return () unless @_;
83
84    my $thing = shift;
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}
89                  // [] };
90    my @newlist = ();
91
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;
102    }
103    print STDERR "DEBUG[collectdepends] $thing < ", join(' ', @newlist), "\n"
104        if $debug_resolvedepends;
105    @newlist;
106}
107
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.
111sub expanddepends {
112    my $self = shift;
113    my @after = ( @_ );
114    print STDERR "DEBUG[expanddepends]> ", join(' ', @after), "\n"
115        if $debug_resolvedepends;
116    my @before = ();
117    while (@after) {
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;
123        my @middle = (
124            $item,
125            map {
126                my $x = $_;
127                my $extlessx = extensionlesslib($x);
128                if (grep { $extlessx eq extensionlesslib($_) } @before
129                    and
130                    !grep { $extlessx eq extensionlesslib($_) } @after) {
131                    print STDERR "DEBUG[expanddepends] + ", $x, "\n"
132                        if $debug_resolvedepends;
133                    ( $x )
134                } else {
135                    print STDERR "DEBUG[expanddepends] ! ", $x, "\n"
136                        if $debug_resolvedepends;
137                    ()
138                }
139            } @{$self->{info}->{depends}->{$item} // []}
140            );
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;
146    }
147    print STDERR "DEBUG[expanddepends]< ", join(' ', @before), "\n"
148        if $debug_resolvedepends;
149    @before;
150}
151
152# reducedepends looks through a list, and checks if each item is
153# repeated later on.  If it is, the earlier copy is dropped.
154sub reducedepends {
155    my @list = @_;
156    print STDERR "DEBUG[reducedepends]> ", join(' ', @list), "\n"
157        if $debug_resolvedepends;
158    my @newlist = ();
159    my %replace = ();
160    while (@list) {
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;
169            }
170        } else {
171            push @newlist, $item;
172        }
173    }
174    @newlist = map { $replace{$_} // $_; } @newlist;
175    print STDERR "DEBUG[reducedepends]< ", join(' ', @newlist), "\n"
176        if $debug_resolvedepends;
177    @newlist;
178}
179
180# Do it all
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.
184sub resolvedepends {
185    my $self = shift;
186    print STDERR "DEBUG[resolvedepends] START (", join(', ', @_), ")\n"
187        if $debug_resolvedepends;
188    my @all =
189        reducedepends($self->expanddepends(map { ( $_, $self->collectdepends($_) ) } @_));
190    print STDERR "DEBUG[resolvedepends] END (", join(', ', @_), ") : ",
191        join(',', map { "\n    $_" } @all), "\n"
192        if $debug_resolvedepends;
193    @all;
194}
195
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.
199sub dogenerate {
200    my $self = shift;
201    my $src = shift;
202    # Safety measure
203    return "" unless defined $self->{info}->{generate}->{$_};
204    return "" if $cache{$src};
205    my $obj = shift;
206    my $bin = shift;
207    my %opts = @_;
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',
213             src => $src,
214             product => $bin,
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} // []} : () ],
223             %opts);
224        foreach (@{$self->{info}->{depends}->{$src} // []}) {
225            $self->dogenerate($_, $obj, $bin, %opts);
226        }
227    }
228    $cache{$src} = 1;
229}
230
231sub dotarget {
232    my $self = shift;
233    my $target = shift;
234    return "" if $cache{$target};
235    $self->emit('generatetarget',
236         target => $target,
237         deps => $self->{info}->{depends}->{$target} // []);
238    foreach (@{$self->{info}->{depends}->{$target} // []}) {
239        $self->dogenerate($_);
240    }
241    $cache{$target} = 1;
242}
243
244# doobj is responsible for producing all the recipes that build
245# object files as well as dependency files.
246sub doobj {
247    my $self = shift;
248    my $obj = shift;
249    return "" if $cache{$obj};
250    my $bin = shift;
251    my %opts = @_;
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"
261            if $debug_rules;
262        print STDERR "DEBUG[doobj] \@deps for $obj ($bin) : ",
263            join(",", map { "\n    $_" } @deps), "\n"
264            if $debug_rules;
265        print STDERR "DEBUG[doobj] \@incs for $obj ($bin) : ",
266            join(",", map { "\n    $_" } @incs), "\n"
267            if $debug_rules;
268        print STDERR "DEBUG[doobj] \@defs for $obj ($bin) : ",
269            join(",", map { "\n    $_" } @defs), "\n"
270            if $debug_rules;
271        print STDERR "DEBUG[doobj] \%opts for $obj ($bin) : ", ,
272            join(",", map { "\n    $_ = $opts{$_}" } sort keys %opts), "\n"
273            if $debug_rules;
274        $self->emit('src2obj',
275             obj => $obj, product => $bin,
276             srcs => [ @srcs ], deps => [ @deps ],
277             incs => [ @incs ], defs => [ @defs ],
278             %opts);
279        foreach ((@{$self->{info}->{sources}->{$obj}},
280                  @{$self->{info}->{depends}->{$obj} // []})) {
281            $self->dogenerate($_, $obj, $bin, %opts);
282        }
283    }
284    $cache{$obj} = 1;
285}
286
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.
292sub getsrclibs {
293    my $self = shift;
294    my $section = shift;
295
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}->{$_} // []} }
301    @_;
302}
303
304sub getlibobjs {
305    my $self = shift;
306    my $section = shift;
307
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.
311    map {
312        my @x = $self->getlibobjs($section, @{$self->{info}->{$section}->{$_}});
313        @x ? @x : ( $_ );
314    }
315    grep { defined $self->{info}->{$section}->{$_} }
316    @_;
317}
318
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
322# built.
323sub dolib {
324    my $self = shift;
325    my $lib = shift;
326    return "" if $cache{$lib};
327
328    my %attrs = %{$self->{info}->{attributes}->{libraries}->{$lib} // {}};
329
330    my @deps = ( $self->resolvedepends(getsrclibs('sources', $lib)) );
331
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
339    # information).
340    #
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.
345    #
346    # We grab all these "foreign" object files recursively with getlibobjs().
347
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 = ();
355        my @objs = ();
356        my @foreign_objs = ();
357        my @deps = ();
358        foreach (@{$self->{info}->{shared_sources}->{$lib} // []}) {
359            if ($_ !~ m|\.a$|) {
360                push @objs, $_;
361            } elsif ($self->{info}->{attributes}->{libraries}->{$_}->{noinst}) {
362                push @deps, $_;
363            } else {
364                push @deps, $self->getsrclibs('sources', $_);
365                push @foreign_objs, $self->getlibobjs('sources', $_);
366            }
367        }
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',
382             lib => $lib,
383             attrs => { %attrs },
384             objs => [ @objs, @foreign_objs ],
385             deps => [ @deps ]);
386        foreach (@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}->{$_}) {
390                if($_ =~ /\.a$/) {
391                    $self->dolib($_);
392                } else {
393                    $self->doobj($_, $lib, intent => "shlib", attrs => { %attrs });
394                }
395            } else {
396                $self->dogenerate($_, undef, undef, intent => "lib");
397            }
398        }
399    }
400    {
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.
407        my @objs = ();
408        my @sourcedeps = ();
409        my @foreign_objs = ();
410        foreach (@{$self->{info}->{sources}->{$lib}}) {
411            if ($_ !~ m|\.a$|) {
412                push @objs, $_;
413            } else {
414                push @sourcedeps, $_;
415            }
416        }
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 ]);
434        foreach (@objs) {
435            $self->doobj($_, $lib, intent => "lib", attrs => { %attrs });
436        }
437    }
438    $cache{$lib} = 1;
439}
440
441# domodule is responsible for building modules.  It will call
442# obj2dso, and also makes sure all object files for the library
443# are built.
444sub domodule {
445    my $self = shift;
446    my $module = shift;
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"
454        if $debug_rules;
455    print STDERR "DEBUG[domodule] \@objs for $module : ",
456        join(",", map { "\n    $_" } @objs), "\n"
457        if $debug_rules;
458    print STDERR "DEBUG[domodule] \@deps for $module : ",
459        join(",", map { "\n    $_" } @deps), "\n"
460        if $debug_rules;
461    $self->emit('obj2dso',
462         module => $module,
463         attrs => { %attrs },
464         objs => [ @objs ],
465         deps => [ @deps ]);
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 });
471        } else {
472            $self->dogenerate($_, undef, $module, intent => "dso");
473        }
474    }
475    $cache{$module} = 1;
476}
477
478# dobin is responsible for building programs.  It will call obj2bin,
479# and also makes sure all object files for the library are built.
480sub dobin {
481    my $self = shift;
482    my $bin = shift;
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',
497         bin => $bin,
498         attrs => { %attrs },
499         objs => [ @objs ],
500         deps => [ @deps ]);
501    foreach (@objs) {
502        $self->doobj($_, $bin, intent => "bin", attrs => { %attrs });
503    }
504    $cache{$bin} = 1;
505}
506
507# doscript is responsible for building scripts from templates.  It will
508# call in2script.
509sub doscript {
510    my $self = shift;
511    my $script = shift;
512    return "" if $cache{$script};
513    $self->emit('in2script',
514         script => $script,
515         attrs => $self->{info}->{attributes}->{scripts}->{$script} // {},
516         sources => $self->{info}->{sources}->{$script});
517    $cache{$script} = 1;
518}
519
520sub dodir {
521    my $self = shift;
522    my $dir = shift;
523    return "" if !exists(&generatedir) or $cache{$dir};
524    $self->emit('generatedir',
525         dir => $dir,
526         deps => $self->{info}->{dirinfo}->{$dir}->{deps} // [],
527         %{$self->{info}->{dirinfo}->{$_}->{products}});
528    $cache{$dir} = 1;
529}
530
531# dodocs is responsible for building documentation from .pods.
532# It will call generatesrc.
533sub dodocs {
534    my $self = shift;
535    my $type = shift;
536    my $section = shift;
537    foreach my $doc (@{$self->{info}->{"${type}docs"}->{$section}}) {
538        next if $cache{$doc};
539        $self->emit('generatesrc',
540             src => $doc,
541             generator => $self->{info}->{generate}->{$doc});
542        foreach ((@{$self->{info}->{depends}->{$doc} // []})) {
543            $self->dogenerate($_, undef, undef);
544        }
545        $cache{$doc} = 1;
546    }
547}
548
5491;
550