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