This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Finalize perldelta
[perl5.git] / write_buildcustomize.pl
1 #!./miniperl -w
2
3 use strict;
4
5 my $osname = $^O;
6 my $file = 'lib/buildcustomize.pl';
7
8 if ( @ARGV % 2 ) {
9     my $dir = shift;
10     chdir $dir or die "Can't chdir '$dir': $!";
11     unshift @INC, 'lib';
12 }
13
14 if ( @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 # Term::ReadLine is not here for building but for allowing the debugger to
26 # run under miniperl when nothing but miniperl will build :-(.
27
28 my @toolchain = qw(cpan/AutoLoader/lib
29                    dist/Carp/lib
30                    dist/PathTools dist/PathTools/lib
31                    cpan/ExtUtils-Install/lib
32                    cpan/ExtUtils-MakeMaker/lib
33                    cpan/ExtUtils-Manifest/lib
34                    cpan/File-Path/lib
35                    ext/re
36                    dist/Term-ReadLine/lib
37                    dist/Exporter/lib
38                    ext/File-Find/lib
39                    cpan/Text-Tabs/lib
40                    dist/constant/lib
41                    );
42
43 # Text-ParseWords used only in ExtUtils::Liblist::Kid::_win32_ext()
44 # the rest are for XS building on Win32, since nonxs and xs build simultaneously
45 # on Win32 if parallel building
46 push @toolchain, qw(
47         cpan/Text-ParseWords/lib
48         dist/ExtUtils-ParseXS/lib
49         cpan/Getopt-Long/lib
50         cpan/parent/lib
51         cpan/ExtUtils-Constant/lib
52 ) if $^O eq 'MSWin32';
53 push @toolchain, 'ext/VMS-Filespec/lib' if $^O eq 'VMS';
54
55 unshift @INC, @toolchain;
56 require File::Spec::Functions;
57 require Cwd;
58
59 my $cwd  = Cwd::getcwd();
60
61 # lib must be last, as the toolchain modules write themselves into it
62 # as they build, and it's important that @INC order ensures that the partially
63 # written files are always masked by the complete versions.
64
65 my $inc = join ",\n        ",
66     map { "q\0$_\0" }
67     (map {File::Spec::Functions::rel2abs($_, $cwd)} (
68 # faster build on the non-parallel Win32 build process
69         $^O eq 'MSWin32' ? ('lib', @toolchain ) : (@toolchain, 'lib')
70     ));
71
72 open my $fh, '>', $file
73     or die "Can't open $file: $!";
74
75 my $error;
76
77 # If any of the system's build tools are written in Perl, then this module
78 # may well be loaded by a much older version than we are building. So keep it
79 # as backwards compatible as is easy.
80 print $fh <<"EOT" or $error = "Can't print to $file: $!";
81 #!perl
82
83 #   !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
84 #   This file is generated by write_buildcustomize.pl.
85 #   Any changes made here will be lost!
86
87 # We are miniperl, building extensions
88 # Replace the first entry of \@INC ("lib") with the list of
89 # directories we need.
90 splice(\@INC, 0, 1, $inc);
91 \$^O = '$osname';
92 __END__
93 EOT
94
95 if ($error) {
96     close $fh
97         or warn "Can't unlink $file after error: $!";
98 } else {
99     if (close $fh) {
100         do $file and exit;
101         $error = "Can't load generated $file: $@";
102     } else {
103         $error = "Can't close $file: $!";
104     }
105 }
106
107 # It's going very wrong, so try to remove the botched file.
108
109 unlink $file
110     or warn "Can't unlink $file after error: $!";
111 die $error;
112
113 # ex: set ts=8 sts=4 sw=4 et: