blib/lib/TinyMake.pm

filter: Statementstmt 17.8% 21 / 118 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 15.3% 2 / 13 Podpod 55.5% 5 / 9 totaltotal 14.8% 28 / 188 CC 29 SCAR 37.4
CC
29
Cov
13%
file
72.0
make
66.9
show
56.0
CRAP 42.0
↑ Summary
1 package TinyMake;
2 1 our $VERSION = '0.06';
3
4 =head1 NAME
5
6 TinyMake - A minimalist build language, similar in purpose to make and ant.
7
8 =head1 SYNOPSIS
9
10 use TinyMake ':all';
11
12 # a file statement without a rule is like a symbolic target
13 file all => ["codeGen", "compile", "dataLoad", "test"];
14
15 file codeGen => ["database.spec"], sub {
16 # generate code here
17 sh "touch $target";
18 } ;
19
20 file compile => ["codeGen"], sub {
21 # compile code here
22 sh "touch $target";
23 } ;
24
25 file dataLoad => ["codeGen"], sub {
26 # load data here
27 sh "touch $target"
28 } ;
29
30 file test => ["compile", "dataLoad"], sub {
31 # test code here
32 sh "touch $target";
33 } ;
34 # a file statement without prerequisites will be executed
35 # if the target doesn't exist.
36 file clean => sub {
37 # perform cleanup here
38 sh "rm compile codeGen dataLoad test"
39 } ;
40
41 make @ARGV
42
43 =cut
44
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use File::Find ;
Subroutine BEGIN: called CC=1 SCAR=0.0
47
48 1 require Exporter;
49 1 our @ISA = ("Exporter");
50 1 our @EXPORT_OK = qw(file make show group $target @changed @sources sh filetree);
51 1 our %EXPORT_TAGS = (all => \@EXPORT_OK,);
52 #---------------------------------------------------------------------------
53 #
54 # Exported variables (for use in rules)
55 #
56 #===========================================================================
57 1 our @changed = ();
58 1 our @sources = ();
59 1 our $target = undef;
60
61 #---------------------------------------------------------------------------
62 #
63 # Private variables
64 #
65 #===========================================================================
66
67 # prerequisites grouped by target (target => [prerequisites])
68 #
69 1 my %prerequisites_for_target = ();
70
71
72 # (target => $coderef)
73 #
74 1 my %command_for_target = ();
75
76
77 # (target => "A descriptive comment for how target gets built")
78 #
79 1 my %comment_for_target = ();
80
81
82 # The default target is always the first target declared using either
83 # file or group
84 #
85 1 my $first = undef;
86
87 #---------------------------------------------------------------------------
88 #
89 # walk a tree in post-order return a list of walked nodes
90 #
91 #===========================================================================
92 0 ▶sub postorderwalk {
Subroutine postorderwalk: not called CC=3 SCAR=24.8
Pod undocumented
93
94 0 my ($node,$children,$store,$visited) = @_;
95
96 0 ▶ return if (grep { $_ eq $node} @$visited);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $node;} @$visited 0 0 0
97
98 0 push @$visited, $node;
99
100 0 my @kids = $children->($node);
101
102 0 foreach (@kids){
103
104 0 postorderwalk($_,$children,$store,$visited) ;
105
106 }
107
108 0 push @$store, $node;
109 0 @$store;
110 }
111 #---------------------------------------------------------------------------
112 #
113 # traverse the dependency tree post-orderly and return the list of targets
114 #
115 #===========================================================================
116 0 ▶sub depends {
Subroutine depends: not called CC=1 SCAR=6.9
Pod undocumented
117
118 0 my ($t,$store,$visited) = @_;
119
120 0 ▶ my $children = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
121
122 0 my ($node) = @_;
123
124 0 ▶ if (exists $prerequisites_for_target{$node}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prerequisites_for_target{$node}) 0 0 0
125
126 0 return @{$prerequisites_for_target{$node}} ;
127
128 }
129 0 return ();
130
131 0 };
132
133 0 postorderwalk ($t, $children, $store,$visited);
134 }
135 #---------------------------------------------------------------------------
136 #
137 # Show Dependency tree: Prints out a nested list of target and dependencies
138 #
139 #===========================================================================
140 0 ▶sub show (@_){
Subroutine show: not called CC=7 SCAR=40.3
Pod undocumented
141
142 0 my ($node,$lvl) = @_;
143
144 0 ▶ if (!(defined $node)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 0 0 0
145 0 $node = $first;
146 }
147
148 0 ▶ if (!(defined $lvl)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lvl) 0 0 0
149 0 $lvl = 0;
150 }
151
152 0 ▶ if (exists $command_for_target{$node}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command_for_target{$node}) { } 0 0 0
153 0 print " " x $lvl . "*$node";
154
155 }else{
156 0 print " " x $lvl . "$node";
157
158 }
159
160 0 ▶ if (exists $comment_for_target{$node}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $comment_for_target{$node}) 0 0 0
161 0 print " - $comment_for_target{$node}";
162 }
163
164 0 print "\n";
165
166 0 $lvl+=1; # increase indentation
167
168 0 ▶ if (exists $prerequisites_for_target{$node}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prerequisites_for_target{$node}) 0 0 0
169
170 0 my @children = @{$prerequisites_for_target{$node}};
171
172 0 foreach (@children){
173 0 show ($_, $lvl);
174 }
175
176 }
177 0 $lvl-=1;
178 }
179 #---------------------------------------------------------------------------
180 #
181 # Execute shell command: prints the shell command, the executes it
182 #
183 #===========================================================================
184 0 ▶sub sh (@){
Subroutine sh: not called CC=1 SCAR=6.9
Pod documented
185
186 0 print "@_\n";
187
188 0 return qx(@_);
189
190 }
191 #---------------------------------------------------------------------------
192 #
193 # Add a new explicit target :
194 #
195 #===========================================================================
196 0 ▶sub file {
Subroutine file: not called CC=8 SCAR=42.8
Pod documented
197
198 0 my ($t,@params) = @_;
199 0 ▶ $first = $t unless defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 0 0 0
200
201 0 ▶ if (@params){
Branch: 1 branch
BranchTrueFalseTotal
if (@params) { } 0 0 0
202
203 0 foreach (0..$#params){
204
205 0 ▶ if (ref $params[$_] eq "CODE"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[$_] eq "CODE") 0 0 0
206 0 $command_for_target{$t} = $params[$_];
207 0 next;
208 }
209
210 0 ▶ if (ref $params[$_] eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[$_] eq "ARRAY") 0 0 0
211 0 $prerequisites_for_target{$t} = $params[$_];
212 0 next;
213 }
214
215 0 ▶ if (ref $params[$_] eq "" and $params[$_] ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[$_] eq "" and $params[$_] ne "") 0 0 0
Condition0%0/3
ref $params[$_] eq "" and $params[$_] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
ref $params[$_] eq "" and $params[$_] ne ""
ref $params[$_] eq "" $params[$_] ne ""
Truth table0%0/3
A and B
A= ref $params[$_] eq ""B= $params[$_] ne ""
ABresult
0X0
100
111
216 0 $comment_for_target{$t} = $params[$_];
217 0 next;
218 }
219
220 }
221
222 }else{
223
224 # TinyMake cannot read minds yet.
225 0 $prerequisites_for_target{$t} = [];
226
227 }
228
229 }
230 #---------------------------------------------------------------------------
231 #
232 # Add a new group of explicit targets
233 #
234 #===========================================================================
235 0 ▶sub group {
Subroutine group: not called CC=2 SCAR=17.9
Pod documented
236
237 0 my ($t,$href,$coderef,@rest) = @_;
238
239 0 foreach (keys %$href) {
240
241 0 file $_ => $href->{$_}, $coderef, @rest;
242
243 }
244
245 0 file $t => [keys %$href];
246
247 }
248 #---------------------------------------------------------------------------
249 #
250 # Return the lastmodified time for each source file
251 #
252 #===========================================================================
253 {
254 1 my %cachedsourcetimes = ();
255
256 0 ▶ sub sourcetimes {
Subroutine sourcetimes: not called CC=5 SCAR=34.0
Pod undocumented
257 # map {$_ => -M $_} @_;
258 0 my %result = ();
259
260 0 foreach my $source (@_){
261
262 0 my $value = undef;
263 #
264 # if the source is not itself a target then
265 # cache its modification time. THis assumes
266 # that such sources will not be modified as a side effect
267 # of any production rule during execution!!!!
268 #
269 0 ▶ if (exists $command_for_target{$source} or
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command_for_target{$source} or exists $prerequisites_for_target{$source}) { } 0 0 0
Condition0%0/3
exists $command_for_target{$source} or exists $prerequisites_for_target{$source}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $command_for_target{$source} or exists $prerequisites_for_target{$source}
exists $command_for_target{$source} exists $prerequisites_for_target{$source}
Truth table0%0/3
A or B
A= exists $command_for_target{$source}B= exists $prerequisites_for_target{$source}
ABresult
1X1
011
000
270 exists $prerequisites_for_target{$source} ){
271
272 # the source file is also mentioned as a target
273 #
274 0 my @filestats = stat $source;
275 0 $value = $filestats[9];
276
277 }else{
278
279 # the sourcefile is not mentioned as a target
280 # (it is pure)
281 #
282 0 ▶ if (!exists $cachedsourcetimes{$source}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cachedsourcetimes{$source}) 0 0 0
283
284 0 my @filestats = stat $source;
285 0 $cachedsourcetimes{$source} = $filestats[9];
286
287 }
288 0 $value = $cachedsourcetimes{$source};
289
290 }
291 0 $result{$source} = $value;
292
293 }
294 0 %result;
295
296 }
297
298 }
299 #---------------------------------------------------------------------------
300 #
301 # build the production script.
302 #
303 #===========================================================================
304 1 ▶sub make {
Subroutine make: not called CC=8 SCAR=42.0
Pod documented
305
306 0 my @result = ();
307 0 ▶ @_ = ($first) unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
308 0 my $exec = 0;
309
310 0 foreach (@_){
311
312 0 my @files = depends $_,[],[];
313 0 my @files_i_can_build = grep {exists $command_for_target{$_} } @files;
314
315 0 foreach (@files_i_can_build){
316
317 0 $target = $_;
318 0 @changed = @sources = ();
319
320 0 ▶ if (exists $prerequisites_for_target{$target}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prerequisites_for_target{$TinyMake::target}) 0 0 0
321
322 0 @sources = @{$prerequisites_for_target{$target}} ;
323 0 @changed = @sources;
324
325 }
326
327 0 $exec = 1;
328
329 0 ▶ if (-e $target){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $TinyMake::target) 0 0 0
330
331 0 my @filestats = stat $target;
332 0 my $targettime = $filestats[9];
333 0 my %sourcetimes = sourcetimes @sources;
334 0 $exec = @changed = grep { $sourcetimes{$_} > $targettime } @sources;
335
336 }
337
338 0 ▶ if ($exec){
Branch: 1 branch
BranchTrueFalseTotal
if ($exec) 0 0 0
339
340 0 $command_for_target{$target}->() ;
341 0 push @result, $target;
342
343 }
344
345 }
346 0 ▶ print "'$_' is up to date\n" unless ($exec);
Branch: 1 branch
BranchTrueFalseTotal
unless $exec 0 0 0
347 }
348 0 @result;
349 }
350
351 #---------------------------------------------------------------------------
352 #
353 # Get a recursive listing of files in a given directory
354 #
355 #===========================================================================
356 0 ▶sub filetree {
Subroutine filetree: not called CC=1 SCAR=6.9
Pod documented
357
358 0 my @found = ();
359 0 ▶ File::Find::find sub{ push @found, $File::Find::name }, @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
360 0 return @found;
361
362 }
363
364 1 1;
365
366 __END__
↑ Summary