xref: /openbsd-src/gnu/usr.bin/perl/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
16fb12b70Safresh1#!./perl -w
26fb12b70Safresh1package ExtUtils::Miniperl;
36fb12b70Safresh1use strict;
46fb12b70Safresh1require Exporter;
56fb12b70Safresh1use ExtUtils::Embed 1.31, qw(xsi_header xsi_protos xsi_body);
66fb12b70Safresh1
7*9f11ffb7Safresh1our @ISA = qw(Exporter);
8*9f11ffb7Safresh1our @EXPORT = qw(writemain);
9*9f11ffb7Safresh1our $VERSION = '1.08';
106fb12b70Safresh1
116fb12b70Safresh1# blead will run this with miniperl, hence we can't use autodie or File::Temp
126fb12b70Safresh1my $temp;
136fb12b70Safresh1
146fb12b70Safresh1END {
156fb12b70Safresh1    return if !defined $temp || !-e $temp;
166fb12b70Safresh1    unlink $temp or warn "Can't unlink '$temp': $!";
176fb12b70Safresh1}
186fb12b70Safresh1
196fb12b70Safresh1sub writemain{
206fb12b70Safresh1    my ($fh, $real);
216fb12b70Safresh1
226fb12b70Safresh1    if (ref $_[0] eq 'SCALAR') {
236fb12b70Safresh1        $real = ${+shift};
246fb12b70Safresh1        $temp = $real;
256fb12b70Safresh1        $temp =~ s/(?:.c)?\z/.new/;
266fb12b70Safresh1        open $fh, '>', $temp
276fb12b70Safresh1            or die "Can't open '$temp' for writing: $!";
286fb12b70Safresh1    } elsif (ref $_[0]) {
296fb12b70Safresh1        $fh = shift;
306fb12b70Safresh1    } else {
316fb12b70Safresh1        $fh = \*STDOUT;
326fb12b70Safresh1    }
336fb12b70Safresh1
346fb12b70Safresh1    my(@exts) = @_;
356fb12b70Safresh1
366fb12b70Safresh1    printf $fh <<'EOF!HEAD', xsi_header();
37*9f11ffb7Safresh1/*    miniperlmain.c or perlmain.c - a generated file
386fb12b70Safresh1 *
396fb12b70Safresh1 *    Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
40*9f11ffb7Safresh1 *    2004, 2005, 2006, 2007, 2016 by Larry Wall and others
416fb12b70Safresh1 *
426fb12b70Safresh1 *    You may distribute under the terms of either the GNU General Public
436fb12b70Safresh1 *    License or the Artistic License, as specified in the README file.
446fb12b70Safresh1 *
456fb12b70Safresh1 */
466fb12b70Safresh1
476fb12b70Safresh1/*
486fb12b70Safresh1 *      The Road goes ever on and on
496fb12b70Safresh1 *          Down from the door where it began.
506fb12b70Safresh1 *
516fb12b70Safresh1 *     [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
526fb12b70Safresh1 *     [Frodo on p.73 of _The Lord of the Rings_, I/iii: "Three Is Company"]
536fb12b70Safresh1 */
546fb12b70Safresh1
556fb12b70Safresh1/* This file contains the main() function for the perl interpreter.
566fb12b70Safresh1 * Note that miniperlmain.c contains main() for the 'miniperl' binary,
57*9f11ffb7Safresh1 * while perlmain.c contains main() for the 'perl' binary. The typical
58*9f11ffb7Safresh1 * difference being that the latter includes Dynaloader.
596fb12b70Safresh1 *
606fb12b70Safresh1 * Miniperl is like perl except that it does not support dynamic loading,
616fb12b70Safresh1 * and in fact is used to build the dynamic modules needed for the 'real'
626fb12b70Safresh1 * perl executable.
63*9f11ffb7Safresh1 *
64*9f11ffb7Safresh1 * The content of the body of this generated file is mostly contained
65*9f11ffb7Safresh1 * in Miniperl.pm - edit that file if you want to change anything.
66*9f11ffb7Safresh1 * miniperlmain.c is generated by running regen/miniperlmain.pl.pl, while
67*9f11ffb7Safresh1 * perlmain.c is built automatically by Makefile (so the former is
68*9f11ffb7Safresh1 * included in the tarball while the latter isn't).
696fb12b70Safresh1 */
706fb12b70Safresh1
716fb12b70Safresh1#ifdef OEMVS
726fb12b70Safresh1#ifdef MYMALLOC
736fb12b70Safresh1/* sbrk is limited to first heap segment so make it big */
746fb12b70Safresh1#pragma runopts(HEAP(8M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON))
756fb12b70Safresh1#else
766fb12b70Safresh1#pragma runopts(HEAP(2M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON))
776fb12b70Safresh1#endif
786fb12b70Safresh1#endif
796fb12b70Safresh1
806fb12b70Safresh1#define PERL_IN_MINIPERLMAIN_C
816fb12b70Safresh1%s
826fb12b70Safresh1static void xs_init (pTHX);
836fb12b70Safresh1static PerlInterpreter *my_perl;
846fb12b70Safresh1
856fb12b70Safresh1#if defined(PERL_GLOBAL_STRUCT_PRIVATE)
866fb12b70Safresh1/* The static struct perl_vars* may seem counterproductive since the
876fb12b70Safresh1 * whole idea PERL_GLOBAL_STRUCT_PRIVATE was to avoid statics, but note
886fb12b70Safresh1 * that this static is not in the shared perl library, the globals PL_Vars
896fb12b70Safresh1 * and PL_VarsPtr will stay away. */
906fb12b70Safresh1static struct perl_vars* my_plvarsp;
916fb12b70Safresh1struct perl_vars* Perl_GetVarsPrivate(void) { return my_plvarsp; }
926fb12b70Safresh1#endif
936fb12b70Safresh1
946fb12b70Safresh1#ifdef NO_ENV_ARRAY_IN_MAIN
956fb12b70Safresh1extern char **environ;
966fb12b70Safresh1int
976fb12b70Safresh1main(int argc, char **argv)
986fb12b70Safresh1#else
996fb12b70Safresh1int
1006fb12b70Safresh1main(int argc, char **argv, char **env)
1016fb12b70Safresh1#endif
1026fb12b70Safresh1{
1036fb12b70Safresh1    int exitstatus, i;
1046fb12b70Safresh1#ifdef PERL_GLOBAL_STRUCT
1056fb12b70Safresh1    struct perl_vars *my_vars = init_global_struct();
1066fb12b70Safresh1#  ifdef PERL_GLOBAL_STRUCT_PRIVATE
1076fb12b70Safresh1    int veto;
1086fb12b70Safresh1
1096fb12b70Safresh1    my_plvarsp = my_vars;
1106fb12b70Safresh1#  endif
1116fb12b70Safresh1#endif /* PERL_GLOBAL_STRUCT */
1126fb12b70Safresh1#ifndef NO_ENV_ARRAY_IN_MAIN
1136fb12b70Safresh1    PERL_UNUSED_ARG(env);
1146fb12b70Safresh1#endif
1156fb12b70Safresh1#ifndef PERL_USE_SAFE_PUTENV
1166fb12b70Safresh1    PL_use_safe_putenv = FALSE;
1176fb12b70Safresh1#endif /* PERL_USE_SAFE_PUTENV */
1186fb12b70Safresh1
1196fb12b70Safresh1    /* if user wants control of gprof profiling off by default */
1206fb12b70Safresh1    /* noop unless Configure is given -Accflags=-DPERL_GPROF_CONTROL */
1216fb12b70Safresh1    PERL_GPROF_MONCONTROL(0);
1226fb12b70Safresh1
1236fb12b70Safresh1#ifdef NO_ENV_ARRAY_IN_MAIN
1246fb12b70Safresh1    PERL_SYS_INIT3(&argc,&argv,&environ);
1256fb12b70Safresh1#else
1266fb12b70Safresh1    PERL_SYS_INIT3(&argc,&argv,&env);
1276fb12b70Safresh1#endif
1286fb12b70Safresh1
1296fb12b70Safresh1#if defined(USE_ITHREADS)
1306fb12b70Safresh1    /* XXX Ideally, this should really be happening in perl_alloc() or
1316fb12b70Safresh1     * perl_construct() to keep libperl.a transparently fork()-safe.
1326fb12b70Safresh1     * It is currently done here only because Apache/mod_perl have
1336fb12b70Safresh1     * problems due to lack of a call to cancel pthread_atfork()
1346fb12b70Safresh1     * handlers when shared objects that contain the handlers may
1356fb12b70Safresh1     * be dlclose()d.  This forces applications that embed perl to
1366fb12b70Safresh1     * call PTHREAD_ATFORK() explicitly, but if and only if it hasn't
1376fb12b70Safresh1     * been called at least once before in the current process.
1386fb12b70Safresh1     * --GSAR 2001-07-20 */
1396fb12b70Safresh1    PTHREAD_ATFORK(Perl_atfork_lock,
1406fb12b70Safresh1                   Perl_atfork_unlock,
1416fb12b70Safresh1                   Perl_atfork_unlock);
1426fb12b70Safresh1#endif
1436fb12b70Safresh1
144b8851fccSafresh1    PERL_SYS_FPU_INIT;
145b8851fccSafresh1
1466fb12b70Safresh1    if (!PL_do_undump) {
1476fb12b70Safresh1	my_perl = perl_alloc();
1486fb12b70Safresh1	if (!my_perl)
1496fb12b70Safresh1	    exit(1);
1506fb12b70Safresh1	perl_construct(my_perl);
1516fb12b70Safresh1	PL_perl_destruct_level = 0;
1526fb12b70Safresh1    }
1536fb12b70Safresh1    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
154*9f11ffb7Safresh1    if (!perl_parse(my_perl, xs_init, argc, argv, (char **)NULL))
1556fb12b70Safresh1        perl_run(my_perl);
1566fb12b70Safresh1
1576fb12b70Safresh1#ifndef PERL_MICRO
1586fb12b70Safresh1    /* Unregister our signal handler before destroying my_perl */
1596fb12b70Safresh1    for (i = 1; PL_sig_name[i]; i++) {
1606fb12b70Safresh1	if (rsignal_state(PL_sig_num[i]) == (Sighandler_t) PL_csighandlerp) {
1616fb12b70Safresh1	    rsignal(PL_sig_num[i], (Sighandler_t) SIG_DFL);
1626fb12b70Safresh1	}
1636fb12b70Safresh1    }
1646fb12b70Safresh1#endif
1656fb12b70Safresh1
1666fb12b70Safresh1    exitstatus = perl_destruct(my_perl);
1676fb12b70Safresh1
1686fb12b70Safresh1    perl_free(my_perl);
1696fb12b70Safresh1
1706fb12b70Safresh1#if defined(USE_ENVIRON_ARRAY) && defined(PERL_TRACK_MEMPOOL) && !defined(NO_ENV_ARRAY_IN_MAIN)
1716fb12b70Safresh1    /*
1726fb12b70Safresh1     * The old environment may have been freed by perl_free()
1736fb12b70Safresh1     * when PERL_TRACK_MEMPOOL is defined, but without having
1746fb12b70Safresh1     * been restored by perl_destruct() before (this is only
1756fb12b70Safresh1     * done if destruct_level > 0).
1766fb12b70Safresh1     *
1776fb12b70Safresh1     * It is important to have a valid environment for atexit()
1786fb12b70Safresh1     * routines that are eventually called.
1796fb12b70Safresh1     */
1806fb12b70Safresh1    environ = env;
1816fb12b70Safresh1#endif
1826fb12b70Safresh1
1836fb12b70Safresh1    PERL_SYS_TERM();
1846fb12b70Safresh1
1856fb12b70Safresh1#ifdef PERL_GLOBAL_STRUCT
1866fb12b70Safresh1#  ifdef PERL_GLOBAL_STRUCT_PRIVATE
1876fb12b70Safresh1    veto = my_plvarsp->Gveto_cleanup;
1886fb12b70Safresh1#  endif
1896fb12b70Safresh1    free_global_struct(my_vars);
1906fb12b70Safresh1#  ifdef PERL_GLOBAL_STRUCT_PRIVATE
1916fb12b70Safresh1    if (!veto)
1926fb12b70Safresh1        my_plvarsp = NULL;
1936fb12b70Safresh1    /* Remember, functions registered with atexit() can run after this point,
1946fb12b70Safresh1       and may access "global" variables, and hence end up calling
1956fb12b70Safresh1       Perl_GetVarsPrivate()  */
1966fb12b70Safresh1#endif
1976fb12b70Safresh1#endif /* PERL_GLOBAL_STRUCT */
1986fb12b70Safresh1
1996fb12b70Safresh1    exit(exitstatus);
2006fb12b70Safresh1}
2016fb12b70Safresh1
2026fb12b70Safresh1/* Register any extra external extensions */
2036fb12b70Safresh1
2046fb12b70Safresh1EOF!HEAD
2056fb12b70Safresh1
2066fb12b70Safresh1    print $fh xsi_protos(@exts), <<'EOT', xsi_body(@exts), "}\n";
2076fb12b70Safresh1
2086fb12b70Safresh1static void
2096fb12b70Safresh1xs_init(pTHX)
2106fb12b70Safresh1{
2116fb12b70Safresh1EOT
2126fb12b70Safresh1
2136fb12b70Safresh1    if ($real) {
2146fb12b70Safresh1        close $fh or die "Can't close '$temp': $!";
2156fb12b70Safresh1        rename $temp, $real or die "Can't rename '$temp' to '$real': $!";
2166fb12b70Safresh1    }
2176fb12b70Safresh1}
2186fb12b70Safresh1
2196fb12b70Safresh11;
2206fb12b70Safresh1__END__
2216fb12b70Safresh1
2226fb12b70Safresh1=head1 NAME
2236fb12b70Safresh1
224*9f11ffb7Safresh1ExtUtils::Miniperl - write the C code for miniperlmain.c and perlmain.c
2256fb12b70Safresh1
2266fb12b70Safresh1=head1 SYNOPSIS
2276fb12b70Safresh1
2286fb12b70Safresh1    use ExtUtils::Miniperl;
2296fb12b70Safresh1    writemain(@directories);
2306fb12b70Safresh1    # or
2316fb12b70Safresh1    writemain($fh, @directories);
2326fb12b70Safresh1    # or
2336fb12b70Safresh1    writemain(\$filename, @directories);
2346fb12b70Safresh1
2356fb12b70Safresh1=head1 DESCRIPTION
2366fb12b70Safresh1
237*9f11ffb7Safresh1C<writemain()> takes an argument list of zero or more directories
238*9f11ffb7Safresh1containing archive
2396fb12b70Safresh1libraries that relate to perl modules and should be linked into a new
240*9f11ffb7Safresh1perl binary. It writes a corresponding F<miniperlmain.c> or F<perlmain.c>
241*9f11ffb7Safresh1file that
2426fb12b70Safresh1is a plain C file containing all the bootstrap code to make the
243b8851fccSafresh1modules associated with the libraries available from within perl.
2446fb12b70Safresh1If the first argument to C<writemain()> is a reference to a scalar it is
245b8851fccSafresh1used as the filename to open for output. Any other reference is used as
2466fb12b70Safresh1the filehandle to write to. Otherwise output defaults to C<STDOUT>.
2476fb12b70Safresh1
248*9f11ffb7Safresh1The typical usage is from within perl's own Makefile (to build
249*9f11ffb7Safresh1F<perlmain.c>) or from F<regen/miniperlmain.pl> (to build miniperlmain.c).
250*9f11ffb7Safresh1So under normal circumstances you won't have to deal with this module
251*9f11ffb7Safresh1directly.
2526fb12b70Safresh1
2536fb12b70Safresh1=head1 SEE ALSO
2546fb12b70Safresh1
2556fb12b70Safresh1L<ExtUtils::MakeMaker>
2566fb12b70Safresh1
2576fb12b70Safresh1=cut
2586fb12b70Safresh1
2596fb12b70Safresh1# ex: set ts=8 sts=4 sw=4 et:
260