xref: /openbsd-src/gnu/usr.bin/perl/write_buildcustomize.pl (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1#!./miniperl -w
2
3use strict;
4
5my $osname = $^O;
6my $file = 'lib/buildcustomize.pl';
7
8if ( @ARGV % 2 ) {
9    my $dir = shift;
10    chdir $dir or die "Can't chdir '$dir': $!";
11    unshift @INC, 'lib';
12}
13
14if ( @ARGV ) {
15    # Used during cross-compilation.
16    $osname = $ARGV[1];
17}
18
19# To clarify, this isn't the entire suite of modules considered "toolchain"
20# It's not even all modules needed to build ext/
21# It's just the source paths of the (minimum complete set of) modules in ext/
22# needed to build the nonxs modules
23# After which, all nonxs modules are in lib, which was always sufficient to
24# allow miniperl to build everything else.
25# Getopt::Long is here because it's used by podlators, which is one of the
26# nonxs modules.
27# Term::ReadLine is not here for building but for allowing the debugger to
28# run under miniperl when nothing but miniperl will build :-(.
29# Text::ParseWords is required in ExtUtils::Liblist::Kid
30
31my @toolchain = qw(cpan/AutoLoader/lib
32		   dist/Carp/lib
33		   dist/PathTools dist/PathTools/lib
34		   cpan/ExtUtils-Install/lib
35		   cpan/ExtUtils-MakeMaker/lib
36		   cpan/ExtUtils-Manifest/lib
37		   cpan/File-Path/lib
38		   ext/re
39		   dist/Term-ReadLine/lib
40                   dist/Exporter/lib
41                   ext/File-Find/lib
42                   cpan/Text-Tabs/lib
43		   dist/constant/lib
44		   cpan/version/lib
45		   cpan/Getopt-Long/lib
46		   cpan/Text-ParseWords/lib
47                   cpan/ExtUtils-PL2Bat/lib
48		   );
49
50# These are for XS building on Win32, since nonxs and xs build simultaneously
51# on Win32 if parallel building
52push @toolchain, qw(
53	dist/ExtUtils-ParseXS/lib
54	cpan/parent/lib
55	cpan/ExtUtils-Constant/lib
56    dist/base/lib
57) if $^O eq 'MSWin32';
58push @toolchain, 'ext/VMS-Filespec/lib' if $^O eq 'VMS';
59
60unshift @INC, @toolchain;
61require File::Spec::Functions;
62require Cwd;
63
64my $cwd  = Cwd::getcwd();
65
66defined $cwd
67  or die "$0: Can't determine current working directory\n";
68
69# lib must be last, as the toolchain modules write themselves into it
70# as they build, and it's important that @INC order ensures that the partially
71# written files are always masked by the complete versions.
72
73my $inc = join ",\n        ",
74    map { "q\0$_\0" }
75    (map {File::Spec::Functions::rel2abs($_, $cwd)} (
76# faster build on the non-parallel Win32 build process
77        $^O eq 'MSWin32' ? ('lib', @toolchain ) : (@toolchain, 'lib')
78    ));
79
80open my $fh, '>', $file
81    or die "Can't open $file: $!";
82
83my $error;
84
85# If any of the system's build tools are written in Perl, then this module
86# may well be loaded by a much older version than we are building. So keep it
87# as backwards compatible as is easy.
88print $fh <<"EOT" or $error = "Can't print to $file: $!";
89#!perl
90
91#   !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
92#   This file is generated by write_buildcustomize.pl.
93#   Any changes made here will be lost!
94
95# We are miniperl, building extensions
96# Replace the first entry of \@INC ("lib") with the list of
97# directories we need.
98splice(\@INC, 0, 1, $inc);
99\$^O = '$osname';
100__END__
101EOT
102
103if ($error) {
104    close $fh
105        or warn "Can't unlink $file after error: $!";
106} else {
107    if (close $fh) {
108        do $file and exit;
109        $error = "Can't load generated $file: $@";
110    } else {
111        $error = "Can't close $file: $!";
112    }
113}
114
115# It's going very wrong, so try to remove the botched file.
116
117unlink $file
118    or warn "Can't unlink $file after error: $!";
119die $error;
120
121# ex: set ts=8 sts=4 sw=4 et:
122