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