This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / mkppport
CommitLineData
42e07562
MHM
1use strict;
2use warnings;
3
4use Getopt::Long;
42e07562 5use File::Spec;
16a92355 6use File::Compare qw( compare );
42e07562
MHM
7use File::Copy qw( copy );
8use File::Basename qw( dirname );
9
aa4b50cc 10use feature 'signatures';
42e07562
MHM
11
12my $rootdir = dirname($0);
13
9dafbe2f 14unshift @INC, File::Spec->catdir($rootdir, qw(cpan ExtUtils-MakeMaker t lib));
42e07562
MHM
15
16eval q{ use MakeMaker::Test::Utils qw( which_perl ) };
17$@ and die $@;
18
19my %opt = (
20 list => File::Spec->catfile($rootdir, 'mkppport.lst'),
21 clean => 0,
22);
23
463da0ac
CBW
24unless ( GetOptions(\%opt, qw( clean list=s )) ) {
25 require Pod::Usage;
26 Pod::Usage::pod2usage(2);
27}
42e07562
MHM
28
29my $absroot = File::Spec->rel2abs($rootdir);
30my @destdirs = readlist($opt{list});
31
32# Nothing to do...
33unless (@destdirs) {
34 print "no destination directories found in $opt{list}\n";
35 exit 0;
36}
37
38# Remove all installed ppport.h files
39if ($opt{clean}) {
aa4b50cc 40 iterdirs( sub ($dir, $fulldir) {
42e07562
MHM
41 my $dest = File::Spec->catfile($fulldir, 'ppport.h');
42 if (-f $dest) {
43 print "removing ppport.h for $dir\n";
44 unlink $dest or warn "WARNING: could not remove $dest: $!\n";
45 1 while unlink $dest; # remove any remaining versions
46 }
aa4b50cc 47 } );
42e07562
MHM
48 exit 0;
49}
50
51# Determine full perl location
52my $perl = which_perl();
53
54# We're now changing the directory, which confuses the deferred
55# loading in Config.pm, so we better use an absolute @INC path
56unshift @INC, File::Spec->catdir($absroot, 'lib');
57
58# Change to Devel::PPPort directory, as it needs the stuff
59# from the parts/ directory
7baf245a 60chdir File::Spec->catdir($rootdir, 'dist', 'Devel-PPPort');
42e07562
MHM
61
62# Capture and remove temporary files
63my @unlink;
64
65END {
66 for my $file (@unlink) {
67 print "removing temporary file $file\n";
68 unlink $file or warn "WARNING: could not remove $file: $!\n";
69 1 while unlink $file; # remove any remaining versions
70 }
71}
72
73# Try to create a ppport.h if it doesn't exist yet, and
74# remember all files that need to be removed later.
75unless (-e 'ppport.h') {
76 unless (-e 'PPPort.pm') {
77 run('PPPort_pm.PL');
78 push @unlink, 'PPPort.pm';
79 }
80 run('ppport_h.PL');
81 push @unlink, 'ppport.h';
82}
83
84# Now install the created ppport.h into extension directories
aa4b50cc 85iterdirs( sub ($dir, $fulldir) {
42e07562 86 my $dest = File::Spec->catfile($fulldir, 'ppport.h');
16a92355
MHM
87 if (compare('ppport.h', $dest)) {
88 print "installing ppport.h for $dir\n";
89 copy('ppport.h', $dest) or die "copying ppport.h to $dest failed: $!\n";
90 }
91 else {
92 print "ppport.h in $dir is up-to-date\n";
93 }
aa4b50cc 94} );
42e07562
MHM
95
96exit 0;
97
98#---------------------------------------
99# Iterate through extension directories
100#---------------------------------------
aa4b50cc 101sub iterdirs($code)
42e07562 102{
42e07562
MHM
103 for my $dir (@destdirs) {
104 my $fulldir = File::Spec->catdir($absroot, $dir);
105 if (-d $fulldir) {
106 $code->($dir, $fulldir);
107 }
108 else {
109 warn "WARNING: no such directory: $fulldir\n";
110 }
111 }
112}
113
114#----------------------------------------
115# Read the list of extension directories
116#----------------------------------------
aa4b50cc 117sub readlist($list)
42e07562 118{
42e07562
MHM
119 my @dirs;
120 open LIST, $list or die "$list: $!\n";
121 while (<LIST>) {
122 chomp;
123 /^\s*(?:$|#)/ or push @dirs, $_;
124 }
125 close LIST;
126 return @dirs;
127}
128
129#----------------------------------------------
130# Runs a script in the Devel::PPPort directory
131#----------------------------------------------
132sub run
133{
0ff33da8 134 my @args = ("-I" . File::Spec->catdir((File::Spec->updir) x 2, 'lib'), @_);
42e07562
MHM
135 my $run = $perl =~ m/\s/ ? qq("$perl") : $perl;
136 for (@args) {
137 $_ = qq("$_") if $^O eq 'VMS' && /^[^"]/;
138 $run .= " $_";
139 }
140 print "running $run\n";
141 system $run and die "$run failed: $?\n";
142}
143
144__END__
145
146=head1 NAME
147
148mkppport - distribute ppport.h among extensions
149
150=head1 SYNOPSIS
151
152mkppport [B<--list>=I<file>] [B<--clean>]
153
154=head1 DESCRIPTION
155
156B<mkppport> generates a I<ppport.h> file using Devel::PPPort
157and distributes it to the various extension directories that
3bdc51af
DD
158need it to build. On certain Win32 builds, this script is not
159used and an alternative mechanism is used to create I<ppport.h>.
42e07562
MHM
160
161=head1 OPTIONS
162
163=over 4
164
165=item B<--list>=I<file>
166
167Name of the file that holds the list of extension directories
168that I<ppport.h> should be distributed to.
169This defaults to I<mkppport.lst> in the same directory as this
170script.
171
172=item B<--clean>
173
174Run with this option to clean out all distributed I<ppport.h> files.
175
176=back
177
178=head1 COPYRIGHT
179
180Copyright 2006 by Marcus Holland-Moritz <mhx@cpan.org>.
181
182This program is free software; you may redistribute it
183and/or modify it under the same terms as Perl itself.
184
185=cut