From c2d2f671bb27ba469e04610717bbb055bf5d6bd6 Mon Sep 17 00:00:00 2001 From: Ralph Lange Date: Fri, 9 Oct 2015 14:15:26 +0200 Subject: [PATCH] tools: add -M option to assembleSnippets.pl (creates dependency file) --- src/tools/assembleSnippets.pl | 20 +++++++++++++++++--- src/tools/test/Snippets.plt | 15 ++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/tools/assembleSnippets.pl b/src/tools/assembleSnippets.pl index 30f8d2db0..4a62e67ec 100644 --- a/src/tools/assembleSnippets.pl +++ b/src/tools/assembleSnippets.pl @@ -14,12 +14,13 @@ use Getopt::Std; use File::Basename; use Data::Dumper; -our ($opt_o, $opt_d, $opt_m, $opt_i); +our ($opt_o, $opt_d, $opt_m, $opt_i, $opt_M); $Getopt::Std::OUTPUT_HELP_VERSION = 1; -&HELP_MESSAGE if !getopts('i:m:o:d') || @ARGV == 0; +&HELP_MESSAGE if !getopts('M:i:m:o:d') || @ARGV == 0; my $out; +my $dep; my %snippets; my $ipattern; @@ -46,6 +47,13 @@ if ($opt_m) { } } +if ($opt_M) { + open $dep, '>', $opt_M or + die "Can't create $opt_M: $!\n"; + print STDERR "opened dependency file $opt_M for output\n" if $opt_d; + print $dep basename($opt_o), ":"; +} + if ($opt_i) { $ipattern = qr($opt_i); } @@ -54,7 +62,7 @@ if ($opt_i) { # of hashes {name-after-rank} # of arrays[] [files...] # of arrays[2] [filename, command] -print STDERR "reading input files\n" if $opt_d;; +print STDERR "reading input files\n" if $opt_d; foreach (@ARGV) { my $name = basename($_); if ($opt_i and not $name =~ /$ipattern/) { @@ -105,6 +113,7 @@ foreach my $r (sort {$a<=>$b} keys %snippets) { my $f = $s->[0]; print STDERR " snippet $n from file $f\n" if $opt_d; open $in, '<', $f or die "Can't open $f: $!\n"; + print $dep " \\\n $f" if $opt_M; while (<$in>) { chomp; foreach my $k (keys %replacements) { @@ -118,6 +127,10 @@ foreach my $r (sort {$a<=>$b} keys %snippets) { } print STDERR "finished creating output, closing\n" if $opt_d; +if ($opt_M) { + print $dep "\n"; + close $dep; +} close $out; sub HELP_MESSAGE { @@ -127,5 +140,6 @@ sub HELP_MESSAGE { print STDERR " -d debug mode [no]\n"; print STDERR " -m macros list of macro replacements as \"key=val,key=val\"\n"; print STDERR " -i pattern pattern for input files to match\n"; + print STDERR " -M file write file with dependency rule suitable for make\n"; exit 2; } diff --git a/src/tools/test/Snippets.plt b/src/tools/test/Snippets.plt index 792ba3a17..3f30f6593 100644 --- a/src/tools/test/Snippets.plt +++ b/src/tools/test/Snippets.plt @@ -2,7 +2,7 @@ use File::Path; -use Test::More tests => 23; +use Test::More tests => 26; (my $user, my @rest) = getpwuid($<); @@ -88,6 +88,15 @@ is assemble("-m", "_M2_=REP2", "b$$/35_c"), "Line REP2 with REP2", "single user # Input pattern mksnip('a', '10_a.cmd', '10cmd'); -is assemble("-i", "\.cmd", "a$$/10_a", "b$$/10_c", "a$$/R10_b", "a$$/10_a.cmd"), '10cmd', "'input pattern"; +is assemble("-i", "\.cmd", "a$$/10_a", "b$$/10_c", "a$$/R10_b", "a$$/10_a.cmd"), '10cmd', "input pattern"; -rmtree([ "a$$", "b$$", "out$$" ]); +# Dependency file generation +assemble("-M", "./dep$$", "a$$/10_a", "b$$/10_c"); +open(my $fh, '<', "dep$$") or die "can't open dep$$ : $!"; +chomp(my @result = <$fh>); +close $fh; +is "$result[0]", "out$$: \\", "dependency file (line 1)"; +is "$result[1]", " a$$/10_a \\", "dependency file (line 2)"; +is "$result[2]", " b$$/10_c", "dependency file (line 3)"; + +rmtree([ "a$$", "b$$", "out$$", "dep$$" ]);