-sub dump_hash()
-{
- print "\n\e[1;32mPre-build configuration is complete!\e[0m\n\n";
- print "\e[0mBase install path:\e[1;32m\t\t$main::config{BASE_DIR}\e[0m\n";
- print "\e[0mConfig path:\e[1;32m\t\t\t$main::config{CONFIG_DIR}\e[0m\n";
- print "\e[0mData path:\e[1;32m\t\t\t$main::config{DATA_DIR}\e[0m\n";
- print "\e[0mLog path:\e[1;32m\t\t\t$main::config{LOG_DIR}\e[0m\n";
- print "\e[0mModule path:\e[1;32m\t\t\t$main::config{MODULE_DIR}\e[0m\n";
- print "\e[0mCompiler:\e[1;32m\t\t\t$main::cxx{NAME} $main::cxx{VERSION}\e[0m\n";
- print "\e[0mSocket engine:\e[1;32m\t\t\t$main::config{SOCKETENGINE}\e[0m\n";
- print "\e[0mGnuTLS support:\e[1;32m\t\t\t$main::config{USE_GNUTLS}\e[0m\n";
- print "\e[0mOpenSSL support:\e[1;32m\t\t$main::config{USE_OPENSSL}\e[0m\n";
+sub parse_templates($$$) {
+
+ # These are actually hash references
+ my ($config, $compiler, $version) = @_;
+
+ # Collect settings to be used when generating files
+ my %settings = __get_template_settings($config, $compiler, $version);
+
+ # Iterate through files in make/template.
+ foreach (<make/template/*>) {
+ print_format "Parsing <|GREEN $_|> ...\n";
+ open(my $fh, $_) or print_error "unable to read $_: $!";
+ my (@lines, $mode, @platforms, @targets);
+
+ # First pass: parse template variables and directives.
+ while (my $line = <$fh>) {
+ chomp $line;
+
+ # Does this line match a variable?
+ while ($line =~ /(@(\w+?)(?:\|(\w*))?@)/) {
+ my ($variable, $name, $default) = ($1, $2, $3);
+ if (defined $settings{$name}) {
+ $line =~ s/\Q$variable\E/$settings{$name}/;
+ } elsif (defined $default) {
+ $line =~ s/\Q$variable\E/$default/;
+ } else {
+ print_warning "unknown template variable '$name' in $_!";
+ last;
+ }
+ }
+
+ # Does this line match a directive?
+ if ($line =~ /^(\s*)%(\w+)\s+(.+)$/) {
+ if ($2 eq 'define') {
+ if ($settings{$3}) {
+ push @lines, "#$1define $3";
+ } else {
+ push @lines, "#$1undef $3";
+ }
+ } elsif ($2 eq 'mode') {
+ $mode = oct $3;
+ } elsif ($2 eq 'platform') {
+ push @platforms, $3;
+ } elsif ($2 eq 'target') {
+ push @targets, $3
+ } else {
+ print_warning "unknown template command '$2' in $_!";
+ push @lines, $line;
+ }
+ next;
+ }
+ push @lines, $line;
+ }
+ close $fh;
+
+ # Only proceed if this file should be templated on this platform.
+ if ($#platforms < 0 || grep { $_ eq $^O } @platforms) {
+
+ # Add a default target if the template has not defined one.
+ unless (@targets) {
+ push @targets, catfile(CONFIGURE_DIRECTORY, basename $_);
+ }
+
+ # Write the templated files to disk.
+ for my $target (@targets) {
+
+ # Create the directory if it doesn't already exist.
+ my $directory = dirname $target;
+ unless (-e $directory) {
+ print_format "Creating <|GREEN $directory|> ...\n";
+ create_directory $directory, 0750 or print_error "unable to create $directory: $!";
+ };
+
+ # Write the template file.
+ print_format "Writing <|GREEN $target|> ...\n";
+ open(my $fh, '>', $target) or print_error "unable to write $target: $!";
+ foreach (@lines) {
+ say $fh $_;
+ }
+ close $fh;
+
+ # Set file permissions.
+ if (defined $mode) {
+ chmod $mode, $target;
+ }
+ }
+ }
+ }