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