summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/mkppport
blob: 07aa8b22e50b7ad4ec064f7c95750f20ce3267cf (plain)
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
use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;
use File::Spec;
use File::Compare qw( compare );
use File::Copy qw( copy );
use File::Basename qw( dirname );

sub iterdirs(&);

my $rootdir = dirname($0);

unshift @INC, File::Spec->catfile($rootdir, 't', 'lib');

eval q{ use MakeMaker::Test::Utils qw( which_perl ) };
$@ and die $@;

my %opt = (
  list   => File::Spec->catfile($rootdir, 'mkppport.lst'),
  clean  => 0,
);

GetOptions(\%opt, qw( clean list=s )) or pod2usage(2);

my $absroot = File::Spec->rel2abs($rootdir);
my @destdirs = readlist($opt{list});

# Nothing to do...
unless (@destdirs) {
  print "no destination directories found in $opt{list}\n";
  exit 0;
}

# Remove all installed ppport.h files
if ($opt{clean}) {
  iterdirs {
    my($dir, $fulldir) = @_;
    my $dest = File::Spec->catfile($fulldir, 'ppport.h');
    if (-f $dest) {
      print "removing ppport.h for $dir\n";
      unlink $dest or warn "WARNING: could not remove $dest: $!\n";
      1 while unlink $dest;  # remove any remaining versions
    }
  };
  exit 0;
}

# Determine full perl location
my $perl = which_perl();

# We're now changing the directory, which confuses the deferred
# loading in Config.pm, so we better use an absolute @INC path
unshift @INC, File::Spec->catdir($absroot, 'lib');

# Change to Devel::PPPort directory, as it needs the stuff
# from the parts/ directory
chdir File::Spec->catdir($rootdir, 'ext', 'Devel', 'PPPort');

# Capture and remove temporary files
my @unlink;

END {
  for my $file (@unlink) {
    print "removing temporary file $file\n";
    unlink $file or warn "WARNING: could not remove $file: $!\n";
    1 while unlink $file;  # remove any remaining versions
  }
}

# Try to create a ppport.h if it doesn't exist yet, and
# remember all files that need to be removed later.
unless (-e 'ppport.h') {
  unless (-e 'PPPort.pm') {
    run('PPPort_pm.PL');
    push @unlink, 'PPPort.pm';
  }
  run('ppport_h.PL');
  push @unlink, 'ppport.h';
}

# Now install the created ppport.h into extension directories
iterdirs {
  my($dir, $fulldir) = @_;
  my $dest = File::Spec->catfile($fulldir, 'ppport.h');
  if (compare('ppport.h', $dest)) {
    print "installing ppport.h for $dir\n";
    copy('ppport.h', $dest) or die "copying ppport.h to $dest failed: $!\n";
  }
  else {
    print "ppport.h in $dir is up-to-date\n";
  }
};

exit 0;

#---------------------------------------
# Iterate through extension directories
#---------------------------------------
sub iterdirs(&)
{
  my $code = shift;
  
  for my $dir (@destdirs) {
    my $fulldir = File::Spec->catdir($absroot, $dir);
    if (-d $fulldir) {
      $code->($dir, $fulldir);
    }
    else {
      warn "WARNING: no such directory: $fulldir\n";
    }
  }
}

#----------------------------------------
# Read the list of extension directories
#----------------------------------------
sub readlist
{
  my $list = shift;
  my @dirs;
  open LIST, $list or die "$list: $!\n";
  while (<LIST>) {
    chomp;
    /^\s*(?:$|#)/ or push @dirs, $_;
  }
  close LIST;
  return @dirs;
}

#----------------------------------------------
# Runs a script in the Devel::PPPort directory
#----------------------------------------------
sub run
{
  my @args = ("-I" . File::Spec->catdir((File::Spec->updir) x 3, 'lib'), @_);
  my $run = $perl =~ m/\s/ ? qq("$perl") : $perl;
  for (@args) {
    $_ = qq("$_") if $^O eq 'VMS' && /^[^"]/;
    $run .= " $_";
  }
  print "running $run\n";
  system $run and die "$run failed: $?\n";
}

__END__

=head1 NAME

mkppport - distribute ppport.h among extensions

=head1 SYNOPSIS

mkppport [B<--list>=I<file>] [B<--clean>]

=head1 DESCRIPTION

B<mkppport> generates a I<ppport.h> file using Devel::PPPort
and distributes it to the various extension directories that
need it to build.

=head1 OPTIONS

=over 4

=item B<--list>=I<file>

Name of the file that holds the list of extension directories
that I<ppport.h> should be distributed to.
This defaults to I<mkppport.lst> in the same directory as this
script.

=item B<--clean>

Run with this option to clean out all distributed I<ppport.h> files.

=back

=head1 COPYRIGHT

Copyright 2006 by Marcus Holland-Moritz <mhx@cpan.org>.

This program is free software; you may redistribute it
and/or modify it under the same terms as Perl itself.

=cut