#!/usr/bin/perl -w # # subdirmk - &-filter (makefile generation program) # Copyright 2019 Ian Jackson # SPDX-License-Identifier: LGPL-2.0-or-later # # $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR... # # generates in each subdirectory from in each subdirectory # Subdir.mk.tmp Subdir.sd.mk # Makefile and included files # and in toplevel and in toplevel # main.mk.tmp Perdir.sd.mk use strict; use POSIX; print "$0 @ARGV\n" or die $!; our $srcdir='.'; while (@ARGV && $ARGV[0] =~ m/^-/) { $_ = shift @ARGV; last if $_ eq '--'; if (s/^--srcdir=//) { $srcdir=$'; } else { die "$0: unknown option \`$_'\n"; } } our @subdirs = @ARGV; s{/+$}{} foreach @subdirs; our $root = [ '.', [ ], 1 ]; # each node is [ 'relative subdir name', \@children, $mentioned ] sub build_tree () { foreach my $subdir (@subdirs) { my @path = $subdir eq '.' ? () : split m{/+}, $subdir; my $node = $root; foreach my $d (@path) { my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] }; if (!$c) { $c = [ $d, [ ] ]; push @{ $node->[1] }, $c; } $node = $c; } $node->[2] = 1; } } sub target_varname ($$) { my ($var_prefix, $target) = @_; return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target"); } our $writing_output; our $buffering_output; our %output_files; our %input_files; our @output_makefiles; sub close_any_output_file() { return unless defined $writing_output; O->error and die "error writing $writing_output.tmp: $! (?)\n"; close O or die "error closing $writing_output.tmp: $!\n"; $writing_output = undef; } sub o { if (defined $buffering_output) { $buffering_output .= $_ foreach @_; return; } die unless defined $writing_output; print O @_ or die "error writing $writing_output.tmp: $!\n"; } sub start_output_file ($) { close_any_output_file(); ($writing_output) = @_; die "$writing_output ?" if $output_files{$writing_output}++; my $tmp = "$writing_output.tmp"; open O, ">", $tmp or die "create $tmp: $!\n"; o "# autogenerated - do not edit\n"; } sub install_output_files () { close_any_output_file(); foreach my $f (sort keys %output_files) { rename "$f.tmp", $f or die "install new $f: $!\n"; } } sub write_makefile ($$) { my ($dir_prefix,$depth) = @_; #print STDERR "write_makefile @_\n"; start_output_file("${dir_prefix}Makefile"); my $cd = $depth ? join('/', ('..',) x $depth) : '.'; my $suppress_templates= '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'. ' MAKEFILE_TEMPLATES=)'; o <(); my $input = new IO::File $f, '<'; if (!$input) { die "open $f: $!\n" unless $!==ENOENT && $enoent_ok; return; } $input_files{$f}++; my %srcdirmap = ( '^' => "\$(top_srcdir)${dir_suffix}", '~' => "\$(top_srcdir)", ); my %pfxmap = ( '' => $dir_prefix, ); $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap; while (<$input>) { if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) { $$esclitr = $1; $set_esc->(); next; } elsif (s#^\s*$esc\:(?=(-?)include)##) { $buffering_output=''; } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) { die "unknown directive $1"; } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) { my $t = $1 // 'all'; o target_varname($var_prefix, $t); $targets->{$t} //= [ ]; } for (;;) { unless (s{^(.*?)$esc}{}) { o $_; last; } o $1; if (s{^\\$esc}{}) { o "$$esclitr" } elsif (s{^\\\$}{}) { o '$' } elsif (s{^\\\s+$}{}) { } elsif (s{^$esc}{}) { o "$$esclitr$$esclitr" } elsif (m{^(?=$caps_re)}) { o $var_prefix } elsif (s{^\$([A-Za-z]\w+)}{}) { o "\$(${var_prefix}$1)" } elsif (s{^([~^]?)(?=$lc_re)}{}) { o $pfxmap{$1} } elsif (s{^_}{}) { o $var_prefix } elsif (s{^=}{}) { o $var_prefix_name } elsif (s{^([~^]?)/}{}) { o $pfxmap{$1} } elsif (s{^\.}{}) { o $dir_name } elsif (s{^([~^])\.}{}) { o $srcdirmap{$1} } elsif (s{^([~^]?)(?=[ \t])}{}) { my $prefix = $pfxmap{$1} // die; my $after=''; if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); } s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g; o $_; $_ = $after; } elsif (s{^\#}{}) { $_ = ''; } elsif (s{^![ \t]+}{}) { o $_; $_ = ''; } else { die "bad escape $$esclitr$_ "; } } if (defined $buffering_output) { $_=$buffering_output; $buffering_output=undef; if (m#^(-?)include\s+(\S+)\s+$#) { my $subf = "$srcdir/$2"; process_input_mk($dir_prefix, $dir_suffix, $dir_name, $var_prefix, $var_prefix_name, $targets, $subf, $esclitr, $1); o "\n"; } else { die "internal error buffering directive $_ "; } } } $input->error and die "read $f: $!\n"; close $input or die "close $f: $!\n"; } sub filter_subdir_mk ($$$$$$) { my ($dir_prefix, $dir_suffix, $dir_name, $var_prefix, $var_prefix_name, $targets) = @_; #use Data::Dumper; #print STDERR "filter @_\n"; my $esclit = '&'; my $pi = sub { my ($f, $enoentok) = @_; process_input_mk($dir_prefix, $dir_suffix, $dir_name, $var_prefix, $var_prefix_name, $targets, "${srcdir}/$f", \$esclit, $enoentok); }; $pi->("${dir_prefix}Subdir.sd.mk", 0); $pi->("Perdir.sd.mk", 1); } sub process_subtree ($$); sub process_subtree ($$) { # => list of descendants (in form SUBDIR/) # recursive, children first my ($node, $path) = @_; #use Data::Dumper; #print STDERR Dumper(\@_); my $dir_prefix = join '', map { "$_/" } @$path; my $dir_suffix = join '', map { "/$_" } @$path; my $dir_name = join '/', @$path ? @$path : '.'; my $var_prefix_name = join '_', @$path ? @$path : qw(TOP); my $var_prefix = "${var_prefix_name}_"; push @output_makefiles, "${dir_prefix}Subdir.mk"; write_makefile($dir_prefix, scalar @$path); my %targets = (all => []); foreach my $child (@{ $node->[1] }) { my @childpath = (@$path, $child->[0]); my $child_subdir = join '/', @childpath; mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!"; push @{ $targets{$_} }, $child_subdir foreach process_subtree($child, \@childpath); } start_output_file("${dir_prefix}Subdir.mk.tmp"); if ($node->[2]) { filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name, $var_prefix, $var_prefix_name, \%targets); } else { my $sdmk = "${dir_prefix}Subdir.sd.mk"; if (stat $sdmk) { die "$sdmk unexpectedly exists (${dir_prefix} not mentioned)"; } elsif ($!==ENOENT) { } else { die "stat $sdmk: $!"; } } o "\n"; my @targets = sort keys %targets; foreach my $target (@targets) { my $target_varname = target_varname($var_prefix, $target); print O "${dir_prefix}${target}:: \$($target_varname)"; foreach my $child_subdir (@{ $targets{$target} }) { print O " $child_subdir/$target"; } print O "\n"; } if (@targets) { print O ".PHONY:"; print O " ${dir_prefix}${_}" foreach @targets; print O "\n"; } return @targets; } sub process_tree() { process_subtree($root, [ ]); start_output_file("main.mk.tmp"); foreach my $v (qw(top_srcdir abs_top_srcdir)) { o "$v=\@$v@\n"; } o "SUBDIRMK_MAKEFILES :=\n"; o "MAKEFILE_TEMPLATES :=\n"; foreach my $mf (@output_makefiles) { o "SUBDIRMK_MAKEFILES += $mf\n"; } foreach my $input (sort keys %input_files) { o "MAKEFILE_TEMPLATES += $input\n"; } o "include \$(SUBDIRMK_MAKEFILES)\n"; } build_tree(); process_tree(); install_output_files();