xref: /openbsd-src/gnu/usr.bin/perl/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
16fb12b70Safresh1#!./perl -w
26fb12b70Safresh1package ExtUtils::Miniperl;
36fb12b70Safresh1use strict;
4eac174f2Safresh1use Exporter 'import';
56fb12b70Safresh1use ExtUtils::Embed 1.31, qw(xsi_header xsi_protos xsi_body);
66fb12b70Safresh1
79f11ffb7Safresh1our @EXPORT = qw(writemain);
8*3d61058aSafresh1our $VERSION = '1.14';
96fb12b70Safresh1
106fb12b70Safresh1# blead will run this with miniperl, hence we can't use autodie or File::Temp
116fb12b70Safresh1my $temp;
126fb12b70Safresh1
136fb12b70Safresh1END {
146fb12b70Safresh1    return if !defined $temp || !-e $temp;
156fb12b70Safresh1    unlink $temp or warn "Can't unlink '$temp': $!";
166fb12b70Safresh1}
176fb12b70Safresh1
186fb12b70Safresh1sub writemain{
196fb12b70Safresh1    my ($fh, $real);
206fb12b70Safresh1
216fb12b70Safresh1    if (ref $_[0] eq 'SCALAR') {
226fb12b70Safresh1        $real = ${+shift};
236fb12b70Safresh1        $temp = $real;
246fb12b70Safresh1        $temp =~ s/(?:.c)?\z/.new/;
256fb12b70Safresh1        open $fh, '>', $temp
266fb12b70Safresh1            or die "Can't open '$temp' for writing: $!";
276fb12b70Safresh1    } elsif (ref $_[0]) {
286fb12b70Safresh1        $fh = shift;
296fb12b70Safresh1    } else {
306fb12b70Safresh1        $fh = \*STDOUT;
316fb12b70Safresh1    }
326fb12b70Safresh1
336fb12b70Safresh1    my(@exts) = @_;
346fb12b70Safresh1
356fb12b70Safresh1    printf $fh <<'EOF!HEAD', xsi_header();
369f11ffb7Safresh1/*    miniperlmain.c or perlmain.c - a generated file
376fb12b70Safresh1 *
386fb12b70Safresh1 *    Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
399f11ffb7Safresh1 *    2004, 2005, 2006, 2007, 2016 by Larry Wall and others
406fb12b70Safresh1 *
416fb12b70Safresh1 *    You may distribute under the terms of either the GNU General Public
426fb12b70Safresh1 *    License or the Artistic License, as specified in the README file.
436fb12b70Safresh1 *
446fb12b70Safresh1 */
456fb12b70Safresh1
466fb12b70Safresh1/*
476fb12b70Safresh1 *      The Road goes ever on and on
486fb12b70Safresh1 *          Down from the door where it began.
496fb12b70Safresh1 *
506fb12b70Safresh1 *     [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
516fb12b70Safresh1 *     [Frodo on p.73 of _The Lord of the Rings_, I/iii: "Three Is Company"]
526fb12b70Safresh1 */
536fb12b70Safresh1
546fb12b70Safresh1/* This file contains the main() function for the perl interpreter.
556fb12b70Safresh1 * Note that miniperlmain.c contains main() for the 'miniperl' binary,
569f11ffb7Safresh1 * while perlmain.c contains main() for the 'perl' binary. The typical
579f11ffb7Safresh1 * difference being that the latter includes Dynaloader.
586fb12b70Safresh1 *
596fb12b70Safresh1 * Miniperl is like perl except that it does not support dynamic loading,
606fb12b70Safresh1 * and in fact is used to build the dynamic modules needed for the 'real'
616fb12b70Safresh1 * perl executable.
629f11ffb7Safresh1 *
639f11ffb7Safresh1 * The content of the body of this generated file is mostly contained
649f11ffb7Safresh1 * in Miniperl.pm - edit that file if you want to change anything.
65b46d8ef2Safresh1 * miniperlmain.c is generated by running regen/miniperlmain.pl, while
669f11ffb7Safresh1 * perlmain.c is built automatically by Makefile (so the former is
679f11ffb7Safresh1 * included in the tarball while the latter isn't).
686fb12b70Safresh1 */
696fb12b70Safresh1
706fb12b70Safresh1#ifdef OEMVS
716fb12b70Safresh1#ifdef MYMALLOC
726fb12b70Safresh1/* sbrk is limited to first heap segment so make it big */
736fb12b70Safresh1#pragma runopts(HEAP(8M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON))
746fb12b70Safresh1#else
756fb12b70Safresh1#pragma runopts(HEAP(2M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON))
766fb12b70Safresh1#endif
776fb12b70Safresh1#endif
786fb12b70Safresh1
796fb12b70Safresh1#define PERL_IN_MINIPERLMAIN_C
80b46d8ef2Safresh1
81b46d8ef2Safresh1/* work round bug in MakeMaker which doesn't currently (2019) supply this
82b46d8ef2Safresh1 * flag when making a statically linked perl */
83b46d8ef2Safresh1#define PERL_CORE 1
84b46d8ef2Safresh1
856fb12b70Safresh1%s
866fb12b70Safresh1static void xs_init (pTHX);
876fb12b70Safresh1static PerlInterpreter *my_perl;
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#ifndef NO_ENV_ARRAY_IN_MAIN
1006fb12b70Safresh1    PERL_UNUSED_ARG(env);
1016fb12b70Safresh1#endif
1026fb12b70Safresh1
1036fb12b70Safresh1    /* if user wants control of gprof profiling off by default */
1046fb12b70Safresh1    /* noop unless Configure is given -Accflags=-DPERL_GPROF_CONTROL */
1056fb12b70Safresh1    PERL_GPROF_MONCONTROL(0);
1066fb12b70Safresh1
1076fb12b70Safresh1#ifdef NO_ENV_ARRAY_IN_MAIN
1086fb12b70Safresh1    PERL_SYS_INIT3(&argc,&argv,&environ);
1096fb12b70Safresh1#else
1106fb12b70Safresh1    PERL_SYS_INIT3(&argc,&argv,&env);
1116fb12b70Safresh1#endif
1126fb12b70Safresh1
1136fb12b70Safresh1#if defined(USE_ITHREADS)
1146fb12b70Safresh1    /* XXX Ideally, this should really be happening in perl_alloc() or
1156fb12b70Safresh1     * perl_construct() to keep libperl.a transparently fork()-safe.
1166fb12b70Safresh1     * It is currently done here only because Apache/mod_perl have
1176fb12b70Safresh1     * problems due to lack of a call to cancel pthread_atfork()
1186fb12b70Safresh1     * handlers when shared objects that contain the handlers may
1196fb12b70Safresh1     * be dlclose()d.  This forces applications that embed perl to
1206fb12b70Safresh1     * call PTHREAD_ATFORK() explicitly, but if and only if it hasn't
1216fb12b70Safresh1     * been called at least once before in the current process.
1226fb12b70Safresh1     * --GSAR 2001-07-20 */
1236fb12b70Safresh1    PTHREAD_ATFORK(Perl_atfork_lock,
1246fb12b70Safresh1                   Perl_atfork_unlock,
1256fb12b70Safresh1                   Perl_atfork_unlock);
1266fb12b70Safresh1#endif
1276fb12b70Safresh1
128b8851fccSafresh1    PERL_SYS_FPU_INIT;
129b8851fccSafresh1
1306fb12b70Safresh1    if (!PL_do_undump) {
1316fb12b70Safresh1	my_perl = perl_alloc();
1326fb12b70Safresh1	if (!my_perl)
1336fb12b70Safresh1	    exit(1);
1346fb12b70Safresh1	perl_construct(my_perl);
1356fb12b70Safresh1	PL_perl_destruct_level = 0;
1366fb12b70Safresh1    }
1376fb12b70Safresh1    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
138e0680481Safresh1    if (!perl_parse(my_perl, xs_init, argc, argv, (char **)NULL)) {
139e0680481Safresh1
140e0680481Safresh1        /* perl_parse() may end up starting its own run loops, which
141e0680481Safresh1         * might end up "leaking" PL_restartop from the parse phase into
142e0680481Safresh1         * the run phase which then ends up confusing run_body(). This
143e0680481Safresh1         * leakage shouldn't happen and if it does its a bug.
144e0680481Safresh1         *
145e0680481Safresh1         * Note we do not do this assert in perl_run() or perl_parse()
146e0680481Safresh1         * as there are modules out there which explicitly set
147e0680481Safresh1         * PL_restartop before calling perl_run() directly from XS code
148e0680481Safresh1         * (Coro), and it is conceivable PL_restartop could be set prior
149e0680481Safresh1         * to calling perl_parse() by XS code as well.
150e0680481Safresh1         *
151e0680481Safresh1         * What we want to check is that the top level perl_parse(),
152e0680481Safresh1         * perl_run() pairing does not allow a leaking PL_restartop, as
153e0680481Safresh1         * that indicates a bug in perl. By putting the assert here we
154e0680481Safresh1         * can validate that Perl itself is operating correctly without
155e0680481Safresh1         * risking breakage to XS code under DEBUGGING. - Yves
156e0680481Safresh1         */
157e0680481Safresh1        assert(!PL_restartop);
158e0680481Safresh1
1596fb12b70Safresh1        perl_run(my_perl);
160e0680481Safresh1    }
1616fb12b70Safresh1
1626fb12b70Safresh1    /* Unregister our signal handler before destroying my_perl */
1636fb12b70Safresh1    for (i = 1; PL_sig_name[i]; i++) {
1646fb12b70Safresh1	if (rsignal_state(PL_sig_num[i]) == (Sighandler_t) PL_csighandlerp) {
1656fb12b70Safresh1	    rsignal(PL_sig_num[i], (Sighandler_t) SIG_DFL);
1666fb12b70Safresh1	}
1676fb12b70Safresh1    }
1686fb12b70Safresh1
1696fb12b70Safresh1    exitstatus = perl_destruct(my_perl);
1706fb12b70Safresh1
1716fb12b70Safresh1    perl_free(my_perl);
1726fb12b70Safresh1
1736fb12b70Safresh1    PERL_SYS_TERM();
1746fb12b70Safresh1
1756fb12b70Safresh1    exit(exitstatus);
1766fb12b70Safresh1}
1776fb12b70Safresh1
1786fb12b70Safresh1/* Register any extra external extensions */
1796fb12b70Safresh1
1806fb12b70Safresh1EOF!HEAD
1816fb12b70Safresh1
1826fb12b70Safresh1    print $fh xsi_protos(@exts), <<'EOT', xsi_body(@exts), "}\n";
1836fb12b70Safresh1
1846fb12b70Safresh1static void
1856fb12b70Safresh1xs_init(pTHX)
1866fb12b70Safresh1{
1876fb12b70Safresh1EOT
1886fb12b70Safresh1
1896fb12b70Safresh1    if ($real) {
1906fb12b70Safresh1        close $fh or die "Can't close '$temp': $!";
1916fb12b70Safresh1        rename $temp, $real or die "Can't rename '$temp' to '$real': $!";
1926fb12b70Safresh1    }
1936fb12b70Safresh1}
1946fb12b70Safresh1
1956fb12b70Safresh11;
1966fb12b70Safresh1__END__
1976fb12b70Safresh1
1986fb12b70Safresh1=head1 NAME
1996fb12b70Safresh1
2009f11ffb7Safresh1ExtUtils::Miniperl - write the C code for miniperlmain.c and perlmain.c
2016fb12b70Safresh1
2026fb12b70Safresh1=head1 SYNOPSIS
2036fb12b70Safresh1
2046fb12b70Safresh1    use ExtUtils::Miniperl;
2056fb12b70Safresh1    writemain(@directories);
2066fb12b70Safresh1    # or
2076fb12b70Safresh1    writemain($fh, @directories);
2086fb12b70Safresh1    # or
2096fb12b70Safresh1    writemain(\$filename, @directories);
2106fb12b70Safresh1
2116fb12b70Safresh1=head1 DESCRIPTION
2126fb12b70Safresh1
2139f11ffb7Safresh1C<writemain()> takes an argument list of zero or more directories
2149f11ffb7Safresh1containing archive
2156fb12b70Safresh1libraries that relate to perl modules and should be linked into a new
2169f11ffb7Safresh1perl binary. It writes a corresponding F<miniperlmain.c> or F<perlmain.c>
2179f11ffb7Safresh1file that
2186fb12b70Safresh1is a plain C file containing all the bootstrap code to make the
219b8851fccSafresh1modules associated with the libraries available from within perl.
2206fb12b70Safresh1If the first argument to C<writemain()> is a reference to a scalar it is
221b8851fccSafresh1used as the filename to open for output. Any other reference is used as
2226fb12b70Safresh1the filehandle to write to. Otherwise output defaults to C<STDOUT>.
2236fb12b70Safresh1
2249f11ffb7Safresh1The typical usage is from within perl's own Makefile (to build
2259f11ffb7Safresh1F<perlmain.c>) or from F<regen/miniperlmain.pl> (to build miniperlmain.c).
2269f11ffb7Safresh1So under normal circumstances you won't have to deal with this module
2279f11ffb7Safresh1directly.
2286fb12b70Safresh1
2296fb12b70Safresh1=head1 SEE ALSO
2306fb12b70Safresh1
2316fb12b70Safresh1L<ExtUtils::MakeMaker>
2326fb12b70Safresh1
2336fb12b70Safresh1=cut
2346fb12b70Safresh1
2356fb12b70Safresh1# ex: set ts=8 sts=4 sw=4 et:
236