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