xref: /openbsd-src/gnu/usr.bin/perl/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
16fb12b70Safresh1#!./perl -w
26fb12b70Safresh1package ExtUtils::Miniperl;
36fb12b70Safresh1use strict;
4*eac174f2Safresh1use Exporter 'import';
56fb12b70Safresh1use ExtUtils::Embed 1.31, qw(xsi_header xsi_protos xsi_body);
66fb12b70Safresh1
79f11ffb7Safresh1our @EXPORT = qw(writemain);
8*eac174f2Safresh1our $VERSION = '1.11';
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#ifndef PERL_USE_SAFE_PUTENV
1036fb12b70Safresh1    PL_use_safe_putenv = FALSE;
1046fb12b70Safresh1#endif /* PERL_USE_SAFE_PUTENV */
1056fb12b70Safresh1
1066fb12b70Safresh1    /* if user wants control of gprof profiling off by default */
1076fb12b70Safresh1    /* noop unless Configure is given -Accflags=-DPERL_GPROF_CONTROL */
1086fb12b70Safresh1    PERL_GPROF_MONCONTROL(0);
1096fb12b70Safresh1
1106fb12b70Safresh1#ifdef NO_ENV_ARRAY_IN_MAIN
1116fb12b70Safresh1    PERL_SYS_INIT3(&argc,&argv,&environ);
1126fb12b70Safresh1#else
1136fb12b70Safresh1    PERL_SYS_INIT3(&argc,&argv,&env);
1146fb12b70Safresh1#endif
1156fb12b70Safresh1
1166fb12b70Safresh1#if defined(USE_ITHREADS)
1176fb12b70Safresh1    /* XXX Ideally, this should really be happening in perl_alloc() or
1186fb12b70Safresh1     * perl_construct() to keep libperl.a transparently fork()-safe.
1196fb12b70Safresh1     * It is currently done here only because Apache/mod_perl have
1206fb12b70Safresh1     * problems due to lack of a call to cancel pthread_atfork()
1216fb12b70Safresh1     * handlers when shared objects that contain the handlers may
1226fb12b70Safresh1     * be dlclose()d.  This forces applications that embed perl to
1236fb12b70Safresh1     * call PTHREAD_ATFORK() explicitly, but if and only if it hasn't
1246fb12b70Safresh1     * been called at least once before in the current process.
1256fb12b70Safresh1     * --GSAR 2001-07-20 */
1266fb12b70Safresh1    PTHREAD_ATFORK(Perl_atfork_lock,
1276fb12b70Safresh1                   Perl_atfork_unlock,
1286fb12b70Safresh1                   Perl_atfork_unlock);
1296fb12b70Safresh1#endif
1306fb12b70Safresh1
131b8851fccSafresh1    PERL_SYS_FPU_INIT;
132b8851fccSafresh1
1336fb12b70Safresh1    if (!PL_do_undump) {
1346fb12b70Safresh1	my_perl = perl_alloc();
1356fb12b70Safresh1	if (!my_perl)
1366fb12b70Safresh1	    exit(1);
1376fb12b70Safresh1	perl_construct(my_perl);
1386fb12b70Safresh1	PL_perl_destruct_level = 0;
1396fb12b70Safresh1    }
1406fb12b70Safresh1    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1419f11ffb7Safresh1    if (!perl_parse(my_perl, xs_init, argc, argv, (char **)NULL))
1426fb12b70Safresh1        perl_run(my_perl);
1436fb12b70Safresh1
1446fb12b70Safresh1#ifndef PERL_MICRO
1456fb12b70Safresh1    /* Unregister our signal handler before destroying my_perl */
1466fb12b70Safresh1    for (i = 1; PL_sig_name[i]; i++) {
1476fb12b70Safresh1	if (rsignal_state(PL_sig_num[i]) == (Sighandler_t) PL_csighandlerp) {
1486fb12b70Safresh1	    rsignal(PL_sig_num[i], (Sighandler_t) SIG_DFL);
1496fb12b70Safresh1	}
1506fb12b70Safresh1    }
1516fb12b70Safresh1#endif
1526fb12b70Safresh1
1536fb12b70Safresh1    exitstatus = perl_destruct(my_perl);
1546fb12b70Safresh1
1556fb12b70Safresh1    perl_free(my_perl);
1566fb12b70Safresh1
1576fb12b70Safresh1#if defined(USE_ENVIRON_ARRAY) && defined(PERL_TRACK_MEMPOOL) && !defined(NO_ENV_ARRAY_IN_MAIN)
1586fb12b70Safresh1    /*
1596fb12b70Safresh1     * The old environment may have been freed by perl_free()
1606fb12b70Safresh1     * when PERL_TRACK_MEMPOOL is defined, but without having
1616fb12b70Safresh1     * been restored by perl_destruct() before (this is only
1626fb12b70Safresh1     * done if destruct_level > 0).
1636fb12b70Safresh1     *
1646fb12b70Safresh1     * It is important to have a valid environment for atexit()
1656fb12b70Safresh1     * routines that are eventually called.
1666fb12b70Safresh1     */
1676fb12b70Safresh1    environ = env;
1686fb12b70Safresh1#endif
1696fb12b70Safresh1
1706fb12b70Safresh1    PERL_SYS_TERM();
1716fb12b70Safresh1
1726fb12b70Safresh1    exit(exitstatus);
1736fb12b70Safresh1}
1746fb12b70Safresh1
1756fb12b70Safresh1/* Register any extra external extensions */
1766fb12b70Safresh1
1776fb12b70Safresh1EOF!HEAD
1786fb12b70Safresh1
1796fb12b70Safresh1    print $fh xsi_protos(@exts), <<'EOT', xsi_body(@exts), "}\n";
1806fb12b70Safresh1
1816fb12b70Safresh1static void
1826fb12b70Safresh1xs_init(pTHX)
1836fb12b70Safresh1{
1846fb12b70Safresh1EOT
1856fb12b70Safresh1
1866fb12b70Safresh1    if ($real) {
1876fb12b70Safresh1        close $fh or die "Can't close '$temp': $!";
1886fb12b70Safresh1        rename $temp, $real or die "Can't rename '$temp' to '$real': $!";
1896fb12b70Safresh1    }
1906fb12b70Safresh1}
1916fb12b70Safresh1
1926fb12b70Safresh11;
1936fb12b70Safresh1__END__
1946fb12b70Safresh1
1956fb12b70Safresh1=head1 NAME
1966fb12b70Safresh1
1979f11ffb7Safresh1ExtUtils::Miniperl - write the C code for miniperlmain.c and perlmain.c
1986fb12b70Safresh1
1996fb12b70Safresh1=head1 SYNOPSIS
2006fb12b70Safresh1
2016fb12b70Safresh1    use ExtUtils::Miniperl;
2026fb12b70Safresh1    writemain(@directories);
2036fb12b70Safresh1    # or
2046fb12b70Safresh1    writemain($fh, @directories);
2056fb12b70Safresh1    # or
2066fb12b70Safresh1    writemain(\$filename, @directories);
2076fb12b70Safresh1
2086fb12b70Safresh1=head1 DESCRIPTION
2096fb12b70Safresh1
2109f11ffb7Safresh1C<writemain()> takes an argument list of zero or more directories
2119f11ffb7Safresh1containing archive
2126fb12b70Safresh1libraries that relate to perl modules and should be linked into a new
2139f11ffb7Safresh1perl binary. It writes a corresponding F<miniperlmain.c> or F<perlmain.c>
2149f11ffb7Safresh1file that
2156fb12b70Safresh1is a plain C file containing all the bootstrap code to make the
216b8851fccSafresh1modules associated with the libraries available from within perl.
2176fb12b70Safresh1If the first argument to C<writemain()> is a reference to a scalar it is
218b8851fccSafresh1used as the filename to open for output. Any other reference is used as
2196fb12b70Safresh1the filehandle to write to. Otherwise output defaults to C<STDOUT>.
2206fb12b70Safresh1
2219f11ffb7Safresh1The typical usage is from within perl's own Makefile (to build
2229f11ffb7Safresh1F<perlmain.c>) or from F<regen/miniperlmain.pl> (to build miniperlmain.c).
2239f11ffb7Safresh1So under normal circumstances you won't have to deal with this module
2249f11ffb7Safresh1directly.
2256fb12b70Safresh1
2266fb12b70Safresh1=head1 SEE ALSO
2276fb12b70Safresh1
2286fb12b70Safresh1L<ExtUtils::MakeMaker>
2296fb12b70Safresh1
2306fb12b70Safresh1=cut
2316fb12b70Safresh1
2326fb12b70Safresh1# ex: set ts=8 sts=4 sw=4 et:
233