blib/lib/Panotools/Makefile/Rule.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 74 SCAR 30.0
CC
4
Cov
0%
Assemble
20.0
new
2.0
Targets
2.0
CRAP 20.0
1 package Panotools::Makefile::Rule;
2  
3 =head1 NAME
4  
5 Panotools::Makefile::Rule - Assemble Makefile rules
6  
7 =head1 SYNOPSIS
8  
9 Simple interface for generating Makefile syntax
10  
11 =head1 DESCRIPTION
12  
13 Writing Makefiles directly from perl scripts with print and "\t" etc... is
14 prone to error, this library provides a simple perl interface for assembling
15 Makefile rules.
16  
17 =cut
18  
19 use strict;
20 use warnings;
21  
22 use Panotools::Makefile::Utils qw/quotetarget quoteprerequisite quoteshell/;
23  
24 =head1 USAGE
25  
26 my $rule = new Panotools::Makefile::Rule;
27  
28 ..or additionally specify targets at creation time
29  
30 my $rule = new Panotools::Makefile::Rule ('all');
31  
32 =cut
33  
34 sub new
35 {
36 my $class = shift;
37 $class = ref $class || $class;
38 my $self = bless {targets => [@_], prerequisites => [], command => []}, $class;
39 return $self;
40 }
41  
42 =pod
43  
44 A Makefile rule always has one or more 'targets', these are typically
45 filenames, but can be 'phony' non-files.
46  
47 (phony targets should be listed as perequisites of the special .PHONY target)
48  
49 $rule->Targets ('output1.txt', 'output2.txt');
50  
51 ..or equivalently:
52  
53 $rule->Targets ('output1.txt');
54 $rule->Targets ('output2.txt');
55  
56 =cut
57  
58 sub Targets
59 {
60 my $self = shift;
61 push @{$self->{targets}}, @_;
62 #warn 'Error: unescapable =;:% in targets: '. join (' ', @_) if grep /[=;:%]/, @_;
63 #warn 'Warning: non-portable target name: '. join (' ', @_) if grep /[?<>:*|"^]/, @_;
64 }
65  
66 =pod
67  
68 Rules can have zero or more 'prerequisites', again these are typically
69 filenames, but can be 'phony' non-files.
70  
71 $rule->Prerequisites ('input1.txt', 'input2.txt');
72  
73 ..or equivalently:
74  
75 $rule->Prerequisites ('input1.txt');
76 $rule->Prerequisites ('input2.txt');
77  
78 =cut
79  
80 sub Prerequisites
81 {
82 my $self = shift;
83 push @{$self->{prerequisites}}, @_;
84 #warn 'Error: unescapable =;:% in prerequisites: '. join (' ', @_) if grep /[=;:%]/, @_;
85 #warn 'Warning: non-portable target name: '. join (' ', @_) if grep /[?<>:*|"^]/, @_;
86 }
87  
88 =pod
89  
90 Rules zero or more 'commands':
91  
92 $rule->Command ('cp', 'input1.txt', 'output1.txt');
93 $rule->Command ('cp', 'input2.txt', 'output2.txt');
94  
95 =cut
96  
97 sub Command
98 {
99 my $self = shift;
100 push @{$self->{command}}, [@_];
101 }
102  
103 =pod
104  
105 Assemble all this into string that can be written to a Makefile:
106  
107 my $string = $rule->Assemble;
108  
109 =cut
110  
111 sub Assemble
112 {
113 my $self = shift;
114 my $opts = shift || {};
115 return '' unless scalar @{$self->{targets}};
116  
117 my $text;
118 $text .= join ' ', (map { quotetarget ($_)} @{$self->{targets}});
119 $text .= ' : ';
120 $text .= join ' ', (map { quoteprerequisite ($_)} @{$self->{prerequisites}});
121 for my $command (@{$self->{command}})
122 {
123 $text .= "\n\t";
124 $text .= join ' ', (map { quoteshell ($_)} @{$command});
125 next unless defined $opts->{warnings};
126 for my $token (@{$command})
127 {
128 my @vars = $token =~ /\$\([[:alnum:]_]+\)/g;
129 for my $var (@vars)
130 {
131 warn "underquoted \"$token\"" unless $var =~ /\$\([[:alnum:]_]+_SHELL\)/;
132 }
133 }
134 }
135 $text .= "\n\n";
136 return $text;
137 }
138  
139 1;