1#!./perl -w 2package ExtUtils::Miniperl; 3use strict; 4use Exporter 'import'; 5use ExtUtils::Embed 1.31, qw(xsi_header xsi_protos xsi_body); 6 7our @EXPORT = qw(writemain); 8our $VERSION = '1.14'; 9 10# blead will run this with miniperl, hence we can't use autodie or File::Temp 11my $temp; 12 13END { 14 return if !defined $temp || !-e $temp; 15 unlink $temp or warn "Can't unlink '$temp': $!"; 16} 17 18sub writemain{ 19 my ($fh, $real); 20 21 if (ref $_[0] eq 'SCALAR') { 22 $real = ${+shift}; 23 $temp = $real; 24 $temp =~ s/(?:.c)?\z/.new/; 25 open $fh, '>', $temp 26 or die "Can't open '$temp' for writing: $!"; 27 } elsif (ref $_[0]) { 28 $fh = shift; 29 } else { 30 $fh = \*STDOUT; 31 } 32 33 my(@exts) = @_; 34 35 printf $fh <<'EOF!HEAD', xsi_header(); 36/* miniperlmain.c or perlmain.c - a generated file 37 * 38 * Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, 39 * 2004, 2005, 2006, 2007, 2016 by Larry Wall and others 40 * 41 * You may distribute under the terms of either the GNU General Public 42 * License or the Artistic License, as specified in the README file. 43 * 44 */ 45 46/* 47 * The Road goes ever on and on 48 * Down from the door where it began. 49 * 50 * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] 51 * [Frodo on p.73 of _The Lord of the Rings_, I/iii: "Three Is Company"] 52 */ 53 54/* This file contains the main() function for the perl interpreter. 55 * Note that miniperlmain.c contains main() for the 'miniperl' binary, 56 * while perlmain.c contains main() for the 'perl' binary. The typical 57 * difference being that the latter includes Dynaloader. 58 * 59 * Miniperl is like perl except that it does not support dynamic loading, 60 * and in fact is used to build the dynamic modules needed for the 'real' 61 * perl executable. 62 * 63 * The content of the body of this generated file is mostly contained 64 * in Miniperl.pm - edit that file if you want to change anything. 65 * miniperlmain.c is generated by running regen/miniperlmain.pl, while 66 * perlmain.c is built automatically by Makefile (so the former is 67 * included in the tarball while the latter isn't). 68 */ 69 70#ifdef OEMVS 71#ifdef MYMALLOC 72/* sbrk is limited to first heap segment so make it big */ 73#pragma runopts(HEAP(8M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON)) 74#else 75#pragma runopts(HEAP(2M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON)) 76#endif 77#endif 78 79#define PERL_IN_MINIPERLMAIN_C 80 81/* work round bug in MakeMaker which doesn't currently (2019) supply this 82 * flag when making a statically linked perl */ 83#define PERL_CORE 1 84 85%s 86static void xs_init (pTHX); 87static PerlInterpreter *my_perl; 88 89#ifdef NO_ENV_ARRAY_IN_MAIN 90extern char **environ; 91int 92main(int argc, char **argv) 93#else 94int 95main(int argc, char **argv, char **env) 96#endif 97{ 98 int exitstatus, i; 99#ifndef NO_ENV_ARRAY_IN_MAIN 100 PERL_UNUSED_ARG(env); 101#endif 102 103 /* if user wants control of gprof profiling off by default */ 104 /* noop unless Configure is given -Accflags=-DPERL_GPROF_CONTROL */ 105 PERL_GPROF_MONCONTROL(0); 106 107#ifdef NO_ENV_ARRAY_IN_MAIN 108 PERL_SYS_INIT3(&argc,&argv,&environ); 109#else 110 PERL_SYS_INIT3(&argc,&argv,&env); 111#endif 112 113#if defined(USE_ITHREADS) 114 /* XXX Ideally, this should really be happening in perl_alloc() or 115 * perl_construct() to keep libperl.a transparently fork()-safe. 116 * It is currently done here only because Apache/mod_perl have 117 * problems due to lack of a call to cancel pthread_atfork() 118 * handlers when shared objects that contain the handlers may 119 * be dlclose()d. This forces applications that embed perl to 120 * call PTHREAD_ATFORK() explicitly, but if and only if it hasn't 121 * been called at least once before in the current process. 122 * --GSAR 2001-07-20 */ 123 PTHREAD_ATFORK(Perl_atfork_lock, 124 Perl_atfork_unlock, 125 Perl_atfork_unlock); 126#endif 127 128 PERL_SYS_FPU_INIT; 129 130 if (!PL_do_undump) { 131 my_perl = perl_alloc(); 132 if (!my_perl) 133 exit(1); 134 perl_construct(my_perl); 135 PL_perl_destruct_level = 0; 136 } 137 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 138 if (!perl_parse(my_perl, xs_init, argc, argv, (char **)NULL)) { 139 140 /* perl_parse() may end up starting its own run loops, which 141 * might end up "leaking" PL_restartop from the parse phase into 142 * the run phase which then ends up confusing run_body(). This 143 * leakage shouldn't happen and if it does its a bug. 144 * 145 * Note we do not do this assert in perl_run() or perl_parse() 146 * as there are modules out there which explicitly set 147 * PL_restartop before calling perl_run() directly from XS code 148 * (Coro), and it is conceivable PL_restartop could be set prior 149 * to calling perl_parse() by XS code as well. 150 * 151 * What we want to check is that the top level perl_parse(), 152 * perl_run() pairing does not allow a leaking PL_restartop, as 153 * that indicates a bug in perl. By putting the assert here we 154 * can validate that Perl itself is operating correctly without 155 * risking breakage to XS code under DEBUGGING. - Yves 156 */ 157 assert(!PL_restartop); 158 159 perl_run(my_perl); 160 } 161 162 /* Unregister our signal handler before destroying my_perl */ 163 for (i = 1; PL_sig_name[i]; i++) { 164 if (rsignal_state(PL_sig_num[i]) == (Sighandler_t) PL_csighandlerp) { 165 rsignal(PL_sig_num[i], (Sighandler_t) SIG_DFL); 166 } 167 } 168 169 exitstatus = perl_destruct(my_perl); 170 171 perl_free(my_perl); 172 173 PERL_SYS_TERM(); 174 175 exit(exitstatus); 176} 177 178/* Register any extra external extensions */ 179 180EOF!HEAD 181 182 print $fh xsi_protos(@exts), <<'EOT', xsi_body(@exts), "}\n"; 183 184static void 185xs_init(pTHX) 186{ 187EOT 188 189 if ($real) { 190 close $fh or die "Can't close '$temp': $!"; 191 rename $temp, $real or die "Can't rename '$temp' to '$real': $!"; 192 } 193} 194 1951; 196__END__ 197 198=head1 NAME 199 200ExtUtils::Miniperl - write the C code for miniperlmain.c and perlmain.c 201 202=head1 SYNOPSIS 203 204 use ExtUtils::Miniperl; 205 writemain(@directories); 206 # or 207 writemain($fh, @directories); 208 # or 209 writemain(\$filename, @directories); 210 211=head1 DESCRIPTION 212 213C<writemain()> takes an argument list of zero or more directories 214containing archive 215libraries that relate to perl modules and should be linked into a new 216perl binary. It writes a corresponding F<miniperlmain.c> or F<perlmain.c> 217file that 218is a plain C file containing all the bootstrap code to make the 219modules associated with the libraries available from within perl. 220If the first argument to C<writemain()> is a reference to a scalar it is 221used as the filename to open for output. Any other reference is used as 222the filehandle to write to. Otherwise output defaults to C<STDOUT>. 223 224The typical usage is from within perl's own Makefile (to build 225F<perlmain.c>) or from F<regen/miniperlmain.pl> (to build miniperlmain.c). 226So under normal circumstances you won't have to deal with this module 227directly. 228 229=head1 SEE ALSO 230 231L<ExtUtils::MakeMaker> 232 233=cut 234 235# ex: set ts=8 sts=4 sw=4 et: 236