forked from manwar/Term-ProgressBar
/
make-pm
538 lines (402 loc) · 15 KB
/
make-pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# (X)Emacs mode: -*- cperl -*-
use 5.005;
use strict;
=head1 NAME
make - tools for making makefiles with.
=head1 SYNOPSIS
use constant MOD_REQS =>
[
{ name => 'Pod::Usage',
version => '1.12', },
{ name => 'IPC::Run',
package => 'IPC-Run',
version => '0.44', },
{ name => 'DBI::Wrap',
package => 'DBI-Wrap',
version => '1.00',
optional => 1, },
];
use constant EXEC_REQS =>
[
{ name => 'blastpgp',
version => '1.50',
vopt => '--version', },
{ name => 'mkprofile', },
{ name => 'mp3id',
version => '0.4',
vopt => '--help',
vexpect => 255, },
];
use constant NAME => 'Module-Name';
use constant VERSION_FROM => catfile (qw( lib Module Name.pm ));
use constant AUTHOR => 'Martyn J. Pearce fluffy@cpan.org';
use constant ABSTRACT => 'This module makes chocolate biscuits';
use make.pm
=head1 DESCRIPTION
This package provides methods and initialization to build standard perl
modules.
The plan is, you define the requirements, and let the module take care of the
rest.
The requirements you must define are:
=over 4
=item MOD_REQS
An arrayref of hashrefs. Each hashref represents a required Perl module, and
has the following keys:
=over 4
=item name
B<Mandatory> Name of the module used. The presence of this module is checked,
and an exception is raised if it does not exist.
=item package
B<Optional> Name of the package in which the module is to be found. If not
defined, the package is assumed to be present in core Perl.
Modules that have been in core Perl since 5.005 need not be listed; the "core
perl" default is for modules such as C<Pod::Usage> which have been added to
the core since 5.005.
=item version
B<Optional> If supplied, the version of the module is checked against this
number, and an exception raised if the version found is lower than that
requested.
=item optional
B<Optional> If true, then failure to locate the package (or a suitable
version) is not an error, but will generate a warning message.
=item message
If supplied, then this message will be given to the user in case of failure.
=back
=item EXEC_REQS
=over 4
=item name
Name of the executable used. The presence of this executable is checked, and
an exception is raised if it does not exist (in the PATH).
=item package
B<Optional> Name of the package in which the executable is to be found.
=item version
B<Optional> If supplied, the version of the module is checked against this
number, and an exception raised if the version found is lower than that
requested.
If supplied, the L<vopt> key must also be supplied.
=item vopt
B<Optional> This is used only if the C<version> key is also used. This is the
option that is passed to the executable to ask for its version number. It may
be the empty string if no option is used (but must be defined if C<version> is
defined).
=item vexpect
B<Optional> This is used only if the C<version> key is also used. This is the
exit code to expect from the program when polling for its version number.
Defaults to 0. This is the exit code (value of C<$?> in the shell) to use,
I<not> the value of the C<wait> call.
=item optional
B<Optional> If true, then failure to locate the package (or a suitable
version) is not an error, but will generate a warning message.
=item message
If supplied, then this message will be given to the user in case of failure.
=back
=item NAME
The module name. It must conform to the established standard; in particular,
it must B<not> contain colon characters. The usual process, when providing a
single-package module (e.g., to provide C<MIME::Base64>), is to replace the
C<::> occurences with hyphens (hence, C<MIME-Base64>).
=item VERSION_FROM
The module from which to establish the version number. This module must have
a line of the form C<$VERSION = '0.01';>. Declarative prefixes (.e.g, C<our>)
are fine; C<our> is the usual one, since C<$VERSION> is almost always a
package variable.
=item AUTHOR
The name of the module author(s), along with an email address. This is
normally the person primarily responsible for the upkeep of the module.
=item ABSTRACT
A single (concise!) sentence describing the rough purpose of the module. It
is not expected to be mightily accurate, but is for quick browsing of modules.
=item DEPENDS
I<Optional>
If defined, this must be an arrayref of additional targets to insert into
F<Makefile>. Each element must be a hashref, with the following keys:
=over 4
=item target
Name of the rule target
=item reqs
Arrayref of rule requisites
=item rules
Arrayref of rule lines. Do not precede these with a tab character; this will
be inserted for you. Likewise, do not break the lines up.
=back
E.g.,
use constant DEPENDS => [
{ target => 'lib/Class/MethodMaker.pm',
reqs => [qw/ cmmg.pl /],
rules => [ '$(PERL) $< > $@' ],
},
];
=item DERIVED_PM
I<Optional>. If defined, this is expected to be an arrayref of file names
(relative to the dist base), that are pm files to be installed.
By default, F<make.pm> finds the pms to install by a conducting a C<find> over
the F<lib> directory when C<perl Makefile.PL> is run. However, for pm files
that are created, that will be insufficient. By specifying extras with this
constant, such files may be named (and therefore made), and also cleaned when
a C<make clean> is issued. This might well be used in conjunction with the
L<DEPENDS|"DEPENDS"> constant to auto-make pm files.
E.g.,
use constant DERIVED_PM => [qw( lib/Class/MethodMaker.pm )];
=cut
use Config qw( %Config );
use ExtUtils::MakeMaker qw( WriteMakefile );
use File::Find qw( find );
use File::Spec qw( );
sub catfile { File::Spec->catfile(@_) }
# Constants ---------------------------
use constant TYPE_EXEC => 'executable';
use constant TYPE_MOD => 'module';
use constant TYPES => [ TYPE_EXEC, TYPE_MOD ];
use constant CONFIG =>
{
TYPE_MOD , { defaults => { package => 'core perl',
},
find => sub { eval "require $_[0]"; $@ eq '' },
vers => sub {
no strict 'refs';
# Fool emacs indenter
my $_x = q={=; my $pv = ${"$_[0]::VERSION"};
return defined $pv ? $pv : -1;
},
},
TYPE_EXEC , { defaults => { vexpect => 0, },
find => sub {
my ($name) = @_;
my $exec;
PATH_COMPONENT:
for my $path (split /:/, $ENV{PATH}) {
my $try = catfile $path, $name;
if ( -x $try ) {
$exec = $try;
last PATH_COMPONENT;
}
}
defined $exec;
},
vers => sub {
my ($name, $vopt, $expect) = @_;
die "Cannot test version of $name without vopt\n"
unless defined $vopt;
my $cmd = join ' ', $name, $vopt;
my $vstr = qx($cmd 2>&1);
my $rv = $? >> 8;
die sprintf "Command $cmd exited with value: $rv\n"
if $rv != $expect;
if ( $vstr =~ /(?:^|\D)v?(\d+(?:[._]\d+)+)(?![\d_.])/ ) {
(my $version = $1) =~ tr/_/./;
return $version;
} else {
return -1;
}
},
},
};
# Subrs ----------------------------------------------------------------------
sub warn_missing {
my ($missing) = @_;
my ($type_max) = sort { $b <=> $a } map length $_->{type}, @$missing;
my ($name_max) = sort { $b <=> $a } map length $_->{name}, @$missing;
for (@$missing) {
my ($type, $name, $pkg, $vers, $pv, $optional, $message) =
@{$_}{qw( type name package vers_req vers_fnd optional message )};
if ( defined $pv ) {
print STDERR sprintf("%-${type_max}s %${name_max}s requires version " .
"$vers (found $pv)",
$type, $name)
} else {
print STDERR sprintf("Couldn't find %${type_max}s %${name_max}s",
$type, $name);
}
print STDERR " (from $pkg)"
if defined $pkg;
print STDERR "\n";
print STDERR " ...but this isn't fatal\n"
if $optional;
if ( defined $message ) {
$message =~ s/(^|\n)/$1 /g;
$message =~ s/([^\n])$/$1\n/;
print STDERR "\n";
print STDERR $message;
print STDERR "\n";
}
}
}
# -------------------------------------
sub check {
my ($items, $verbose) = @_;
my ($type_max) = sort { $b <=> $a } map length, @{TYPES()};
my ($name_max) = sort { $b <=> $a } map length($_->{name}), @$items;
my @missing;
foreach my $item (@$items) {
my $type = $item->{type};
my $defaults = CONFIG->{$type}->{defaults};
$item->{$_} = $defaults->{$_}
for grep ! exists $item->{$_}, keys %$defaults;
my ($name, $pkg, $vers, $vopt, $vexpect) =
@{$item}{qw( name package version vopt vexpect)};
printf STDERR "Checking for %-${type_max}s %-${name_max}s...", $type, $name
if $verbose;
if ( CONFIG->{$type}->{find}->($name) ) {
print STDERR " found\n"
if $verbose;
if ( defined $vers ) {
my $vfound = CONFIG->{$type}->{vers}->($name, $vopt, $vexpect);
my $str_v_reqd = join '_', map sprintf('%09d',$_), split /\./,$vers;
my $str_v_found = join '_', map sprintf('%09d',$_), split /\./,$vfound;
push @missing, { type => $type,
name => $name,
package => $pkg,
vers_req => $vers,
vers_fnd => $vfound,
optional => $item->{optional},
message => $item->{message},
}
if $str_v_reqd gt $str_v_found;
}
} else {
print STDERR " failed\n"
if $verbose;
push @missing, { type => $type,
name => $name,
package => $pkg,
vers_req => $vers,
optional => $item->{optional},
message => $item->{message},
};
}
}
return @missing;
}
# Main -----------------------------------------------------------------------
# Self Test
if ( $ENV{MAKE_SELF_TEST} ) {
# Find Module (no version)
check([{ name => 'integer' , type => TYPE_MOD, }])
and die "Internal Check (1) failed\n";
# Fail module (no version)
check([{ name => 'flubble' , type => TYPE_MOD, }])
or die "Internal Check (2) failed\n";
# Find module, wrong version
check([{ name => 'IO' , type => TYPE_MOD, version => '100.0', }])
or die "Internal Check (3) failed\n";
# Find module, right version
check([{ name => 'IO' , type => TYPE_MOD, version => '1.00', }])
and die "Internal Check (4) failed\n";
# Find exec (no version)
# Use more (common to dog/windoze too!) (mac?)
check([{ name => 'more' , type => TYPE_EXEC, }])
and die "Internal Check (5) failed\n";
# Fail exec (no version)
check([{ name => ' wibwib' , type => TYPE_EXEC, }])
or die "Internal Check (6) failed\n";
# Could do with one that works on dog/windoze/mac...
if ( $Config{osname} eq 'linux' ) {
# Find exec, wrong version
check([{ name => 'cut' , type => TYPE_EXEC,
version => '100.0', vopt => '--version', }])
or die "Internal Check (7) failed\n";
# Find exec, right version
check([{ name => 'cut' , type => TYPE_EXEC,
version => '1.0', vopt => '--version', }])
and die "Internal Check (8) failed\n";
}
}
# -------------------------------------
my @missing;
{
no strict 'refs';
die "$_ not defined\n"
for grep ! defined *$_{CODE}, qw( MOD_REQS EXEC_REQS
NAME VERSION_FROM AUTHOR ABSTRACT );
}
die sprintf(<<'END', NAME) unless NAME =~ /^[A-Za-z0-9-]+$/;
The module name:%s: is illegal (letters, numbers & hyphens only, please)
END
$_->{type} = TYPE_MOD
for @{MOD_REQS()};
$_->{type} = TYPE_EXEC
for @{EXEC_REQS()};
push @missing, check(MOD_REQS, 1), check(EXEC_REQS, 1);
warn_missing(\@missing);
exit 2
for grep ! $_->{optional}, @missing;
my %pm;
find (sub {
$File::Find::prune = 1, return
if -d $_ and $_ eq 'CVS';
return unless /\.pm$/;
(my $target = $File::Find::name) =~
s/^$File::Find::topdir/\$(INST_LIBDIR)/;
$pm{$File::Find::name} = $target;
},
'lib');
sub MY::postamble {
<<EOF;
check: test
EOF
}
my %Config =
(NAME => NAME,
VERSION_FROM => VERSION_FROM,
AUTHOR => AUTHOR,
ABSTRACT => ABSTRACT,
PREREQ_PM => { map (($_->{name} => $_->{version} || 0 ),
grep ! $_->{optional}, @{MOD_REQS()})},
PM => \%pm,
# Need this to stop Makefile treating Build.PL as a producer of Build as a
# target for 'all'.
PL_FILES => +{},
EXE_FILES => [ grep !/(?:CVS|~)$/, glob catfile (qw( bin * )) ],
clean => +{ FILES => [qw( Build _build )] },
realclean => +{ FILES => [qw( Build.PL META.yml
INSTALL
SIGNATURE
make-pm )] },
);
$Config{PREFIX} = *PREFIX{CODE}->()
if defined *PREFIX{CODE};
push @{$Config{clean}->{FILES}}, @{*EXTRA_CLEAN{CODE}->()}
if defined *EXTRA_CLEAN{CODE};
push @{$Config{realclean}->{FILES}}, qw( Makefile.PL configure README )
if -e 'INFO.yaml';
if ( defined *DEPENDS{CODE} ) {
my $depends = *DEPENDS{CODE}->();
my %depends;
for (@$depends) {
my ($target) = $_->{target};
my ($reqs) = $_->{reqs};
my ($rules) = $_->{rules};
$depends{$target} = join("\n\t", join(' ', @$reqs), @$rules) . "\n";
}
$Config{depend} = \%depends;
}
if ( defined *DERIVED_PM{CODE} ) {
my $extra = *DERIVED_PM{CODE}->();
die sprintf "Don't know how to handle type: %s\n", ref $extra
unless UNIVERSAL::isa($extra, 'ARRAY');
for (@$extra) {
$Config{PM}->{catfile('lib', $_)} = catfile '$(INST_LIBDIR)', $_;
push @{$Config{clean}->{FILES}}, $_;
}
}
$Config{clean}->{FILES} = join ' ', @{$Config{clean}->{FILES}};
$Config{realclean}->{FILES} = join ' ', @{$Config{realclean}->{FILES}};
WriteMakefile (%Config);
# ----------------------------------------------------------------------------
=head1 EXAMPLES
Z<>
=head1 BUGS
Z<>
=head1 REPORTING BUGS
Email the author.
=head1 AUTHOR
Martyn J. Pearce C<fluffy@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2001, 2002, 2003 Martyn J. Pearce. This program is free
software; you can redistribute it and/or modify it under the same terms as
Perl itself.
=head1 SEE ALSO
Z<>
=cut
1; # keep require happy
__END__