| 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 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 3 | ▶ | use Panotools::Makefile::Utils qw/quotetarget quoteprerequisite quoteshell/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 62 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 36 | 62 | my $class = shift; | |||||||||||||||||||
| 37 | 62 | ▶ | $class = ref $class || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 38 | 62 | my $self = bless {targets => [@_], prerequisites => [], command => []}, $class; | |||||||||||||||||||
| 39 | 62 | 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 | 62 | ▶ | { | ||||||||||||||||||
|
Subroutine
Targets: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 60 | 62 | my $self = shift; | |||||||||||||||||||
| 61 | 62 | 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 | 62 | ▶ | { | ||||||||||||||||||
|
Subroutine
Prerequisites: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 82 | 62 | my $self = shift; | |||||||||||||||||||
| 83 | 62 | 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 | 65 | ▶ | { | ||||||||||||||||||
|
Subroutine
Command: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 99 | 65 | my $self = shift; | |||||||||||||||||||
| 100 | 65 | 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 | 72 | ▶ | { | ||||||||||||||||||
|
Subroutine
Assemble: called CC=8 SCAR=22.6
Pod
undocumented
| |||||||||||||||||||||
| 113 | 72 | my $self = shift; | |||||||||||||||||||
| 114 | 72 | ▶ | my $opts = shift || {}; | ||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 115 | 72 | ▶ | return '' unless scalar @{$self->{targets}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 72 | my $text; | |||||||||||||||||||
| 118 | 72 | $text .= join ' ', (map { quotetarget ($_)} @{$self->{targets}}); | |||||||||||||||||||
| 119 | 72 | $text .= ' : '; | |||||||||||||||||||
| 120 | 72 | $text .= join ' ', (map { quoteprerequisite ($_)} @{$self->{prerequisites}}); | |||||||||||||||||||
| 121 | 72 | for my $command (@{$self->{command}}) | |||||||||||||||||||
| 122 | { | ||||||||||||||||||||
| 123 | 86 | $text .= "\n\t"; | |||||||||||||||||||
| 124 | 86 | $text .= join ' ', (map { quoteshell ($_)} @{$command}); | |||||||||||||||||||
| 125 | 86 | ▶ | next unless defined $opts->{warnings}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | for my $token (@{$command}) | |||||||||||||||||||
| 127 | { | ||||||||||||||||||||
| 128 | 0 | my @vars = $token =~ /\$\([[:alnum:]_]+\)/g; | |||||||||||||||||||
| 129 | 0 | for my $var (@vars) | |||||||||||||||||||
| 130 | { | ||||||||||||||||||||
| 131 | 0 | ▶ | warn "underquoted \"$token\"" unless $var =~ /\$\([[:alnum:]_]+_SHELL\)/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | 72 | $text .= "\n\n"; | |||||||||||||||||||
| 136 | 72 | return $text; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 3 | 1; | |||||||||||||||||||