xref: /openbsd-src/gnu/usr.bin/perl/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm (revision dd7183d711827caf3ffa441a8d52da6bdd7ae879)
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.08';
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.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%s
82static void xs_init (pTHX);
83static PerlInterpreter *my_perl;
84
85#if defined(PERL_GLOBAL_STRUCT_PRIVATE)
86/* The static struct perl_vars* may seem counterproductive since the
87 * whole idea PERL_GLOBAL_STRUCT_PRIVATE was to avoid statics, but note
88 * that this static is not in the shared perl library, the globals PL_Vars
89 * and PL_VarsPtr will stay away. */
90static struct perl_vars* my_plvarsp;
91struct perl_vars* Perl_GetVarsPrivate(void) { return my_plvarsp; }
92#endif
93
94#ifdef NO_ENV_ARRAY_IN_MAIN
95extern char **environ;
96int
97main(int argc, char **argv)
98#else
99int
100main(int argc, char **argv, char **env)
101#endif
102{
103    int exitstatus, i;
104#ifdef PERL_GLOBAL_STRUCT
105    struct perl_vars *my_vars = init_global_struct();
106#  ifdef PERL_GLOBAL_STRUCT_PRIVATE
107    int veto;
108
109    my_plvarsp = my_vars;
110#  endif
111#endif /* PERL_GLOBAL_STRUCT */
112#ifndef NO_ENV_ARRAY_IN_MAIN
113    PERL_UNUSED_ARG(env);
114#endif
115#ifndef PERL_USE_SAFE_PUTENV
116    PL_use_safe_putenv = FALSE;
117#endif /* PERL_USE_SAFE_PUTENV */
118
119    /* if user wants control of gprof profiling off by default */
120    /* noop unless Configure is given -Accflags=-DPERL_GPROF_CONTROL */
121    PERL_GPROF_MONCONTROL(0);
122
123#ifdef NO_ENV_ARRAY_IN_MAIN
124    PERL_SYS_INIT3(&argc,&argv,&environ);
125#else
126    PERL_SYS_INIT3(&argc,&argv,&env);
127#endif
128
129#if defined(USE_ITHREADS)
130    /* XXX Ideally, this should really be happening in perl_alloc() or
131     * perl_construct() to keep libperl.a transparently fork()-safe.
132     * It is currently done here only because Apache/mod_perl have
133     * problems due to lack of a call to cancel pthread_atfork()
134     * handlers when shared objects that contain the handlers may
135     * be dlclose()d.  This forces applications that embed perl to
136     * call PTHREAD_ATFORK() explicitly, but if and only if it hasn't
137     * been called at least once before in the current process.
138     * --GSAR 2001-07-20 */
139    PTHREAD_ATFORK(Perl_atfork_lock,
140                   Perl_atfork_unlock,
141                   Perl_atfork_unlock);
142#endif
143
144    PERL_SYS_FPU_INIT;
145
146    if (!PL_do_undump) {
147	my_perl = perl_alloc();
148	if (!my_perl)
149	    exit(1);
150	perl_construct(my_perl);
151	PL_perl_destruct_level = 0;
152    }
153    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
154    if (!perl_parse(my_perl, xs_init, argc, argv, (char **)NULL))
155        perl_run(my_perl);
156
157#ifndef PERL_MICRO
158    /* Unregister our signal handler before destroying my_perl */
159    for (i = 1; PL_sig_name[i]; i++) {
160	if (rsignal_state(PL_sig_num[i]) == (Sighandler_t) PL_csighandlerp) {
161	    rsignal(PL_sig_num[i], (Sighandler_t) SIG_DFL);
162	}
163    }
164#endif
165
166    exitstatus = perl_destruct(my_perl);
167
168    perl_free(my_perl);
169
170#if defined(USE_ENVIRON_ARRAY) && defined(PERL_TRACK_MEMPOOL) && !defined(NO_ENV_ARRAY_IN_MAIN)
171    /*
172     * The old environment may have been freed by perl_free()
173     * when PERL_TRACK_MEMPOOL is defined, but without having
174     * been restored by perl_destruct() before (this is only
175     * done if destruct_level > 0).
176     *
177     * It is important to have a valid environment for atexit()
178     * routines that are eventually called.
179     */
180    environ = env;
181#endif
182
183    PERL_SYS_TERM();
184
185#ifdef PERL_GLOBAL_STRUCT
186#  ifdef PERL_GLOBAL_STRUCT_PRIVATE
187    veto = my_plvarsp->Gveto_cleanup;
188#  endif
189    free_global_struct(my_vars);
190#  ifdef PERL_GLOBAL_STRUCT_PRIVATE
191    if (!veto)
192        my_plvarsp = NULL;
193    /* Remember, functions registered with atexit() can run after this point,
194       and may access "global" variables, and hence end up calling
195       Perl_GetVarsPrivate()  */
196#endif
197#endif /* PERL_GLOBAL_STRUCT */
198
199    exit(exitstatus);
200}
201
202/* Register any extra external extensions */
203
204EOF!HEAD
205
206    print $fh xsi_protos(@exts), <<'EOT', xsi_body(@exts), "}\n";
207
208static void
209xs_init(pTHX)
210{
211EOT
212
213    if ($real) {
214        close $fh or die "Can't close '$temp': $!";
215        rename $temp, $real or die "Can't rename '$temp' to '$real': $!";
216    }
217}
218
2191;
220__END__
221
222=head1 NAME
223
224ExtUtils::Miniperl - write the C code for miniperlmain.c and perlmain.c
225
226=head1 SYNOPSIS
227
228    use ExtUtils::Miniperl;
229    writemain(@directories);
230    # or
231    writemain($fh, @directories);
232    # or
233    writemain(\$filename, @directories);
234
235=head1 DESCRIPTION
236
237C<writemain()> takes an argument list of zero or more directories
238containing archive
239libraries that relate to perl modules and should be linked into a new
240perl binary. It writes a corresponding F<miniperlmain.c> or F<perlmain.c>
241file that
242is a plain C file containing all the bootstrap code to make the
243modules associated with the libraries available from within perl.
244If the first argument to C<writemain()> is a reference to a scalar it is
245used as the filename to open for output. Any other reference is used as
246the filehandle to write to. Otherwise output defaults to C<STDOUT>.
247
248The typical usage is from within perl's own Makefile (to build
249F<perlmain.c>) or from F<regen/miniperlmain.pl> (to build miniperlmain.c).
250So under normal circumstances you won't have to deal with this module
251directly.
252
253=head1 SEE ALSO
254
255L<ExtUtils::MakeMaker>
256
257=cut
258
259# ex: set ts=8 sts=4 sw=4 et:
260