xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-MakeMaker/lib/ExtUtils/Command/MM.pm (revision e068048151d29f2562a32185e21a8ba885482260)
1b39c5158Smillertpackage ExtUtils::Command::MM;
2b39c5158Smillert
3b39c5158Smillertrequire 5.006;
4b39c5158Smillert
5b39c5158Smillertuse strict;
6b39c5158Smillertuse warnings;
7b39c5158Smillert
8b39c5158Smillertrequire Exporter;
9b39c5158Smillertour @ISA = qw(Exporter);
10b39c5158Smillert
11b39c5158Smillertour @EXPORT  = qw(test_harness pod2man perllocal_install uninstall
126fb12b70Safresh1                  warn_if_old_packlist test_s cp_nonempty);
13*e0680481Safresh1our $VERSION = '7.70';
1456d68f1eSafresh1$VERSION =~ tr/_//d;
15b39c5158Smillert
16b39c5158Smillertmy $Is_VMS = $^O eq 'VMS';
17b39c5158Smillert
189f11ffb7Safresh1sub mtime {
199f11ffb7Safresh1  no warnings 'redefine';
209f11ffb7Safresh1  local $@;
219f11ffb7Safresh1  *mtime = (eval { require Time::HiRes } && defined &Time::HiRes::stat)
229f11ffb7Safresh1    ? sub { (Time::HiRes::stat($_[0]))[9] }
239f11ffb7Safresh1    : sub { (             stat($_[0]))[9] }
249f11ffb7Safresh1  ;
259f11ffb7Safresh1  goto &mtime;
269f11ffb7Safresh1}
27b39c5158Smillert
28b39c5158Smillert=head1 NAME
29b39c5158Smillert
30b39c5158SmillertExtUtils::Command::MM - Commands for the MM's to use in Makefiles
31b39c5158Smillert
32b39c5158Smillert=head1 SYNOPSIS
33b39c5158Smillert
34b39c5158Smillert  perl "-MExtUtils::Command::MM" -e "function" "--" arguments...
35b39c5158Smillert
36b39c5158Smillert
37b39c5158Smillert=head1 DESCRIPTION
38b39c5158Smillert
39b39c5158SmillertB<FOR INTERNAL USE ONLY!>  The interface is not stable.
40b39c5158Smillert
41b39c5158SmillertExtUtils::Command::MM encapsulates code which would otherwise have to
42b39c5158Smillertbe done with large "one" liners.
43b39c5158Smillert
44b39c5158SmillertAny $(FOO) used in the examples are make variables, not Perl.
45b39c5158Smillert
46b39c5158Smillert=over 4
47b39c5158Smillert
48b39c5158Smillert=item B<test_harness>
49b39c5158Smillert
50b39c5158Smillert  test_harness($verbose, @test_libs);
51b39c5158Smillert
52b39c5158SmillertRuns the tests on @ARGV via Test::Harness passing through the $verbose
53b39c5158Smillertflag.  Any @test_libs will be unshifted onto the test's @INC.
54b39c5158Smillert
55b39c5158Smillert@test_libs are run in alphabetical order.
56b39c5158Smillert
57b39c5158Smillert=cut
58b39c5158Smillert
59b39c5158Smillertsub test_harness {
60b39c5158Smillert    require Test::Harness;
61b39c5158Smillert    require File::Spec;
62b39c5158Smillert
63b39c5158Smillert    $Test::Harness::verbose = shift;
64b39c5158Smillert
65b39c5158Smillert    # Because Windows doesn't do this for us and listing all the *.t files
66b39c5158Smillert    # out on the command line can blow over its exec limit.
67b39c5158Smillert    require ExtUtils::Command;
68b39c5158Smillert    my @argv = ExtUtils::Command::expand_wildcards(@ARGV);
69b39c5158Smillert
70b39c5158Smillert    local @INC = @INC;
71b39c5158Smillert    unshift @INC, map { File::Spec->rel2abs($_) } @_;
72b39c5158Smillert    Test::Harness::runtests(sort { lc $a cmp lc $b } @argv);
73b39c5158Smillert}
74b39c5158Smillert
75b39c5158Smillert
76b39c5158Smillert
77b39c5158Smillert=item B<pod2man>
78b39c5158Smillert
79b39c5158Smillert  pod2man( '--option=value',
80b39c5158Smillert           $podfile1 => $manpage1,
81b39c5158Smillert           $podfile2 => $manpage2,
82b39c5158Smillert           ...
83b39c5158Smillert         );
84b39c5158Smillert
85b39c5158Smillert  # or args on @ARGV
86b39c5158Smillert
87b39c5158Smillertpod2man() is a function performing most of the duties of the pod2man
88b39c5158Smillertprogram.  Its arguments are exactly the same as pod2man as of 5.8.0
89b39c5158Smillertwith the addition of:
90b39c5158Smillert
91b39c5158Smillert    --perm_rw   octal permission to set the resulting manpage to
92b39c5158Smillert
93b39c5158SmillertAnd the removal of:
94b39c5158Smillert
95b39c5158Smillert    --verbose/-v
96b39c5158Smillert    --help/-h
97b39c5158Smillert
98b39c5158SmillertIf no arguments are given to pod2man it will read from @ARGV.
99b39c5158Smillert
100b39c5158SmillertIf Pod::Man is unavailable, this function will warn and return undef.
101b39c5158Smillert
102b39c5158Smillert=cut
103b39c5158Smillert
104b39c5158Smillertsub pod2man {
105b39c5158Smillert    local @ARGV = @_ ? @_ : @ARGV;
106b39c5158Smillert
107b39c5158Smillert    {
108b39c5158Smillert        local $@;
109b39c5158Smillert        if( !eval { require Pod::Man } ) {
110b39c5158Smillert            warn "Pod::Man is not available: $@".
111b39c5158Smillert                 "Man pages will not be generated during this install.\n";
11291f110e0Safresh1            return 0;
113b39c5158Smillert        }
114b39c5158Smillert    }
115b39c5158Smillert    require Getopt::Long;
116b39c5158Smillert
117b39c5158Smillert    # We will cheat and just use Getopt::Long.  We fool it by putting
118b39c5158Smillert    # our arguments into @ARGV.  Should be safe.
119b39c5158Smillert    my %options = ();
120b39c5158Smillert    Getopt::Long::config ('bundling_override');
121b39c5158Smillert    Getopt::Long::GetOptions (\%options,
122b39c5158Smillert                'section|s=s', 'release|r=s', 'center|c=s',
123b39c5158Smillert                'date|d=s', 'fixed=s', 'fixedbold=s', 'fixeditalic=s',
124b39c5158Smillert                'fixedbolditalic=s', 'official|o', 'quotes|q=s', 'lax|l',
125b8851fccSafresh1                'name|n=s', 'perm_rw=i', 'utf8|u'
126b39c5158Smillert    );
127b8851fccSafresh1    delete $options{utf8} unless $Pod::Man::VERSION >= 2.17;
128b39c5158Smillert
129b39c5158Smillert    # If there's no files, don't bother going further.
130b39c5158Smillert    return 0 unless @ARGV;
131b39c5158Smillert
132b39c5158Smillert    # Official sets --center, but don't override things explicitly set.
133b39c5158Smillert    if ($options{official} && !defined $options{center}) {
134b39c5158Smillert        $options{center} = q[Perl Programmer's Reference Guide];
135b39c5158Smillert    }
136b39c5158Smillert
137b39c5158Smillert    # This isn't a valid Pod::Man option and is only accepted for backwards
138b39c5158Smillert    # compatibility.
139b39c5158Smillert    delete $options{lax};
140b8851fccSafresh1    my $count = scalar @ARGV / 2;
141b8851fccSafresh1    my $plural = $count == 1 ? 'document' : 'documents';
142b8851fccSafresh1    print "Manifying $count pod $plural\n";
143b39c5158Smillert
144b39c5158Smillert    do {{  # so 'next' works
145b39c5158Smillert        my ($pod, $man) = splice(@ARGV, 0, 2);
146b39c5158Smillert
147b39c5158Smillert        next if ((-e $man) &&
1486fb12b70Safresh1                 (mtime($man) > mtime($pod)) &&
1496fb12b70Safresh1                 (mtime($man) > mtime("Makefile")));
150b39c5158Smillert
151b39c5158Smillert        my $parser = Pod::Man->new(%options);
152b39c5158Smillert        $parser->parse_from_file($pod, $man)
153b39c5158Smillert          or do { warn("Could not install $man\n");  next };
154b39c5158Smillert
155b39c5158Smillert        if (exists $options{perm_rw}) {
156b39c5158Smillert            chmod(oct($options{perm_rw}), $man)
157b39c5158Smillert              or do { warn("chmod $options{perm_rw} $man: $!\n"); next };
158b39c5158Smillert        }
159b39c5158Smillert    }} while @ARGV;
160b39c5158Smillert
161b39c5158Smillert    return 1;
162b39c5158Smillert}
163b39c5158Smillert
164b39c5158Smillert
165b39c5158Smillert=item B<warn_if_old_packlist>
166b39c5158Smillert
167b39c5158Smillert  perl "-MExtUtils::Command::MM" -e warn_if_old_packlist <somefile>
168b39c5158Smillert
169b39c5158SmillertDisplays a warning that an old packlist file was found.  Reads the
170b39c5158Smillertfilename from @ARGV.
171b39c5158Smillert
172b39c5158Smillert=cut
173b39c5158Smillert
174b39c5158Smillertsub warn_if_old_packlist {
175b39c5158Smillert    my $packlist = $ARGV[0];
176b39c5158Smillert
177b39c5158Smillert    return unless -f $packlist;
178b39c5158Smillert    print <<"PACKLIST_WARNING";
179b39c5158SmillertWARNING: I have found an old package in
180b39c5158Smillert    $packlist.
181b39c5158SmillertPlease make sure the two installations are not conflicting
182b39c5158SmillertPACKLIST_WARNING
183b39c5158Smillert
184b39c5158Smillert}
185b39c5158Smillert
186b39c5158Smillert
187b39c5158Smillert=item B<perllocal_install>
188b39c5158Smillert
189b39c5158Smillert    perl "-MExtUtils::Command::MM" -e perllocal_install
190b39c5158Smillert        <type> <module name> <key> <value> ...
191b39c5158Smillert
192b39c5158Smillert    # VMS only, key|value pairs come on STDIN
193b39c5158Smillert    perl "-MExtUtils::Command::MM" -e perllocal_install
194b39c5158Smillert        <type> <module name> < <key>|<value> ...
195b39c5158Smillert
196b39c5158SmillertPrints a fragment of POD suitable for appending to perllocal.pod.
197b39c5158SmillertArguments are read from @ARGV.
198b39c5158Smillert
199b39c5158Smillert'type' is the type of what you're installing.  Usually 'Module'.
200b39c5158Smillert
201b39c5158Smillert'module name' is simply the name of your module.  (Foo::Bar)
202b39c5158Smillert
203b39c5158SmillertKey/value pairs are extra information about the module.  Fields include:
204b39c5158Smillert
205b39c5158Smillert    installed into      which directory your module was out into
206b39c5158Smillert    LINKTYPE            dynamic or static linking
207b39c5158Smillert    VERSION             module version number
2089f11ffb7Safresh1    EXE_FILES           any executables installed in a space separated
209b39c5158Smillert                        list
210b39c5158Smillert
211b39c5158Smillert=cut
212b39c5158Smillert
213b39c5158Smillertsub perllocal_install {
214b39c5158Smillert    my($type, $name) = splice(@ARGV, 0, 2);
215b39c5158Smillert
216b39c5158Smillert    # VMS feeds args as a piped file on STDIN since it usually can't
217b39c5158Smillert    # fit all the args on a single command line.
218b39c5158Smillert    my @mod_info = $Is_VMS ? split /\|/, <STDIN>
219b39c5158Smillert                           : @ARGV;
220b39c5158Smillert
221b39c5158Smillert    my $pod;
2229f11ffb7Safresh1    my $time = gmtime($ENV{SOURCE_DATE_EPOCH} || time);
2239f11ffb7Safresh1    $pod = sprintf <<'POD', scalar($time), $type, $name, $name;
2249f11ffb7Safresh1 =head2 %s: C<%s> L<%s|%s>
225b39c5158Smillert
226b39c5158Smillert =over 4
227b39c5158Smillert
228b39c5158SmillertPOD
229b39c5158Smillert
230b39c5158Smillert    do {
231b39c5158Smillert        my($key, $val) = splice(@mod_info, 0, 2);
232b39c5158Smillert
233b39c5158Smillert        $pod .= <<POD
234b39c5158Smillert =item *
235b39c5158Smillert
236b39c5158Smillert C<$key: $val>
237b39c5158Smillert
238b39c5158SmillertPOD
239b39c5158Smillert
240b39c5158Smillert    } while(@mod_info);
241b39c5158Smillert
242b39c5158Smillert    $pod .= "=back\n\n";
243b39c5158Smillert    $pod =~ s/^ //mg;
244b39c5158Smillert    print $pod;
245b39c5158Smillert
246b39c5158Smillert    return 1;
247b39c5158Smillert}
248b39c5158Smillert
249b39c5158Smillert=item B<uninstall>
250b39c5158Smillert
251b39c5158Smillert    perl "-MExtUtils::Command::MM" -e uninstall <packlist>
252b39c5158Smillert
253b39c5158SmillertA wrapper around ExtUtils::Install::uninstall().  Warns that
254b39c5158Smillertuninstallation is deprecated and doesn't actually perform the
255b39c5158Smillertuninstallation.
256b39c5158Smillert
257b39c5158Smillert=cut
258b39c5158Smillert
259b39c5158Smillertsub uninstall {
260b39c5158Smillert    my($packlist) = shift @ARGV;
261b39c5158Smillert
262b39c5158Smillert    require ExtUtils::Install;
263b39c5158Smillert
264b39c5158Smillert    print <<'WARNING';
265b39c5158Smillert
266b39c5158SmillertUninstall is unsafe and deprecated, the uninstallation was not performed.
267b39c5158SmillertWe will show what would have been done.
268b39c5158Smillert
269b39c5158SmillertWARNING
270b39c5158Smillert
271b39c5158Smillert    ExtUtils::Install::uninstall($packlist, 1, 1);
272b39c5158Smillert
273b39c5158Smillert    print <<'WARNING';
274b39c5158Smillert
275b39c5158SmillertUninstall is unsafe and deprecated, the uninstallation was not performed.
276b39c5158SmillertPlease check the list above carefully, there may be errors.
277b39c5158SmillertRemove the appropriate files manually.
278b39c5158SmillertSorry for the inconvenience.
279b39c5158Smillert
280b39c5158SmillertWARNING
281b39c5158Smillert
282b39c5158Smillert}
283b39c5158Smillert
2846fb12b70Safresh1=item B<test_s>
2856fb12b70Safresh1
2866fb12b70Safresh1   perl "-MExtUtils::Command::MM" -e test_s <file>
2876fb12b70Safresh1
2886fb12b70Safresh1Tests if a file exists and is not empty (size > 0).
2896fb12b70Safresh1I<Exits> with 0 if it does, 1 if it does not.
2906fb12b70Safresh1
2916fb12b70Safresh1=cut
2926fb12b70Safresh1
2936fb12b70Safresh1sub test_s {
2946fb12b70Safresh1  exit(-s $ARGV[0] ? 0 : 1);
2956fb12b70Safresh1}
2966fb12b70Safresh1
2976fb12b70Safresh1=item B<cp_nonempty>
2986fb12b70Safresh1
2996fb12b70Safresh1  perl "-MExtUtils::Command::MM" -e cp_nonempty <srcfile> <dstfile> <perm>
3006fb12b70Safresh1
3016fb12b70Safresh1Tests if the source file exists and is not empty (size > 0). If it is not empty
3026fb12b70Safresh1it copies it to the given destination with the given permissions.
3036fb12b70Safresh1
304b39c5158Smillert=back
305b39c5158Smillert
306b39c5158Smillert=cut
307b39c5158Smillert
3086fb12b70Safresh1sub cp_nonempty {
3096fb12b70Safresh1  my @args = @ARGV;
3106fb12b70Safresh1  return 0 unless -s $args[0];
3116fb12b70Safresh1  require ExtUtils::Command;
3126fb12b70Safresh1  {
3136fb12b70Safresh1    local @ARGV = @args[0,1];
3146fb12b70Safresh1    ExtUtils::Command::cp(@ARGV);
3156fb12b70Safresh1  }
3166fb12b70Safresh1  {
3176fb12b70Safresh1    local @ARGV = @args[2,1];
3186fb12b70Safresh1    ExtUtils::Command::chmod(@ARGV);
3196fb12b70Safresh1  }
3206fb12b70Safresh1}
3216fb12b70Safresh1
3226fb12b70Safresh1
323b39c5158Smillert1;
324