| 1 | package Panotools::Makefile; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Panotools::Makefile - Makefile creation | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Simple object interface for generating Makefiles | ||
| 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 | Makefiles. | ||
| 16 | |||
| 17 | Note GNU make syntax is assumed, i.e. on BSD systems where pmake is the default | ||
| 18 | you will have to switch to gmake if you want to work with weirdly named targets | ||
| 19 | containing special characters such as spaces or parentheses. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | |||
| 26 | use Panotools::Script; | ||
| 27 | use Panotools::Makefile::Rule; | ||
| 28 | use Panotools::Makefile::Variable; | ||
| 29 | use Panotools::Makefile::Comment; | ||
| 30 | use File::Temp qw/tempdir/; | ||
| 31 | use File::Spec; | ||
| 32 | |||
| 33 | =head1 USAGE | ||
| 34 | |||
| 35 | use Panotools::Makefile; | ||
| 36 | |||
| 37 | Create a new Makefile object: | ||
| 38 | |||
| 39 | my $makefile = new Panotools::Makefile; | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub new | ||
| 44 | { | ||
| 45 | my $class = shift; | ||
| 46 | $class = ref $class || $class; | ||
| 47 | my $self = bless {items => []}, $class; | ||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | =pod | ||
| 52 | |||
| 53 | Start adding items to the Makefile: | ||
| 54 | |||
| 55 | Rule() returns a new L<Panotools::Makefile::Rule> object, Variable() returns a | ||
| 56 | new L<Panotools::Makefile::Variable> object and Comment() returns a new | ||
| 57 | L<Panotools::Makefile::Comment> object: | ||
| 58 | |||
| 59 | my $var_user = $makefile->Variable ('USER'); | ||
| 60 | $var_user->Values ("Dr. Largio d'Apalansius (MB)"); | ||
| 61 | |||
| 62 | my $rule_all = $makefile->Rule ('all'); | ||
| 63 | $rule_all->Command ('echo', '$(USER_SHELL)', '>', 'My File.txt'); | ||
| 64 | |||
| 65 | $makefile->Comment ('.PHONY target isn't strictly necessary in this case'); | ||
| 66 | my $rule_phony = $makefile->Rule; | ||
| 67 | $rule_phony->Targets ('.PHONY'); | ||
| 68 | $rule_phony->Prerequisites ('all'); | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub Rule | ||
| 73 | { | ||
| 74 | my $self = shift; | ||
| 75 | my $rule = new Panotools::Makefile::Rule (@_); | ||
| 76 | push @{$self->{items}}, $rule; | ||
| 77 | return $rule; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub Variable | ||
| 81 | { | ||
| 82 | my $self = shift; | ||
| 83 | my $variable = new Panotools::Makefile::Variable (@_); | ||
| 84 | push @{$self->{items}}, $variable; | ||
| 85 | return $variable; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub Comment | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | my $comment = new Panotools::Makefile::Comment (@_); | ||
| 92 | push @{$self->{items}}, $comment; | ||
| 93 | return $comment; | ||
| 94 | } | ||
| 95 | |||
| 96 | =pod | ||
| 97 | |||
| 98 | Assemble all this into string that can be written to a Makefile: | ||
| 99 | |||
| 100 | my $string = $makefile->Assemble; | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub Assemble | ||
| 105 | { | ||
| 106 | my $self = shift; | ||
| 107 | "# Created by Panotools::Script $Panotools::Script::VERSION\n\n" | ||
| 108 | . join '', map {$_->Assemble} @{$self->{items}}; | ||
| 109 | } | ||
| 110 | |||
| 111 | =pod | ||
| 112 | |||
| 113 | ..or write the Makefile: | ||
| 114 | |||
| 115 | $makefile->Write ('/path/to/Makefile'); | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub Write | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | my $path_makefile = shift; | ||
| 123 | open MAKE, ">", $path_makefile or warn "cannot write-open $path_makefile"; | ||
| 124 | print MAKE $self->Assemble; | ||
| 125 | close MAKE; | ||
| 126 | } | ||
| 127 | |||
| 128 | =pod | ||
| 129 | |||
| 130 | ..or let the module execute rules with 'make' directly: | ||
| 131 | |||
| 132 | $makefile->DoIt ('all') || warn "Didn't work :-("; | ||
| 133 | |||
| 134 | The following command will be executed, something that isn't possible with perl | ||
| 135 | system() or exec(), and would otherwise require careful assembly with backticks: | ||
| 136 | |||
| 137 | echo Dr.\ Largio\ d\'Apalansius\ \(MB\) > My\ File.txt | ||
| 138 | |||
| 139 | On the Windows platform you get appropriate quoting: | ||
| 140 | |||
| 141 | echo "Dr. Largio d'Apalansius (MB)" > "My File.txt" | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub DoIt | ||
| 146 | { | ||
| 147 | my $self = shift; | ||
| 148 | my $tempdir = tempdir (CLEANUP => 1); | ||
| 149 | my $path_makefile = File::Spec->catfile ($tempdir, 'Makefile'); | ||
| 150 | $self->Write ($path_makefile); | ||
| 151 | my $make_exe = 'make'; | ||
| 152 | $make_exe = 'gmake' if ($^O =~ /^(.*bsd|dragonfly|irix|solaris|sunos)$/); | ||
| 153 | system ($make_exe, '-f', $path_makefile, @_); | ||
| 154 | return 1 if ($? == 0); | ||
| 155 | return 0; | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; | ||
| 159 |