ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/staticperl.pod
Revision: 1.17
Committed: Thu Dec 9 08:55:52 2010 UTC (13 years, 6 months ago) by root
Branch: MAIN
Changes since 1.16: +104 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.8 staticperl - perl, libc, 100 modules, all in one 500kb file
4 root 1.1
5     =head1 SYNOPSIS
6    
7     staticperl help # print the embedded documentation
8     staticperl fetch # fetch and unpack perl sources
9     staticperl configure # fetch and then configure perl
10     staticperl build # configure and then build perl
11     staticperl install # build and then install perl
12     staticperl clean # clean most intermediate files (restart at configure)
13     staticperl distclean # delete everything installed by this script
14     staticperl cpan # invoke CPAN shell
15     staticperl instmod path... # install unpacked modules
16     staticperl instcpan modulename... # install modules from CPAN
17     staticperl mkbundle <bundle-args...> # see documentation
18     staticperl mkperl <bundle-args...> # see documentation
19 root 1.14 staticperl mkapp appname <bundle-args...> # see documentation
20 root 1.1
21     Typical Examples:
22    
23     staticperl install # fetch, configure, build and install perl
24     staticperl cpan # run interactive cpan shell
25     staticperl mkperl -M '"Config_heavy.pl"' # build a perl that supports -V
26     staticperl mkperl -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI -MURI::http
27     # build a perl with the above modules linked in
28 root 1.14 staticperl mkapp myapp --boot mainprog mymodules
29     # build a binary "myapp" from mainprog and mymodules
30 root 1.1
31     =head1 DESCRIPTION
32    
33 root 1.16 This script helps you to create single-file perl interpreters
34     or applications, or embedding a perl interpreter in your
35     applications. Single-file means that it is fully self-contained - no
36     separate shared objects, no autoload fragments, no .pm or .pl files are
37     needed. And when linking statically, you can create (or embed) a single
38     file that contains perl interpreter, libc, all the modules you need, all
39     the libraries you need and of course your actual program.
40 root 1.1
41 root 1.8 With F<uClibc> and F<upx> on x86, you can create a single 500kb binary
42     that contains perl and 100 modules such as POSIX, AnyEvent, EV, IO::AIO,
43     Coro and so on. Or any other choice of modules.
44 root 1.1
45 root 1.3 The created files do not need write access to the file system (like PAR
46 root 1.1 does). In fact, since this script is in many ways similar to PAR::Packer,
47     here are the differences:
48    
49     =over 4
50    
51     =item * The generated executables are much smaller than PAR created ones.
52    
53     Shared objects and the perl binary contain a lot of extra info, while
54     the static nature of F<staticperl> allows the linker to remove all
55     functionality and meta-info not required by the final executable. Even
56     extensions statically compiled into perl at build time will only be
57     present in the final executable when needed.
58    
59     In addition, F<staticperl> can strip perl sources much more effectively
60     than PAR.
61    
62     =item * The generated executables start much faster.
63    
64     There is no need to unpack files, or even to parse Zip archives (which is
65     slow and memory-consuming business).
66    
67     =item * The generated executables don't need a writable filesystem.
68    
69     F<staticperl> loads all required files directly from memory. There is no
70     need to unpack files into a temporary directory.
71    
72 root 1.17 =item * More control over included files, more burden.
73 root 1.1
74 root 1.3 PAR tries to be maintenance and hassle-free - it tries to include more
75 root 1.17 files than necessary to make sure everything works out of the box. It
76     mostly succeeds at this, but he extra files (such as the unicode database)
77     can take substantial amounts of memory and file size.
78 root 1.1
79     With F<staticperl>, the burden is mostly with the developer - only direct
80     compile-time dependencies and L<AutoLoader> are handled automatically.
81     This means the modules to include often need to be tweaked manually.
82    
83 root 1.17 All this does not preclude more permissive modes to be implemented in
84     the future, but right now, you have to resolve state hidden dependencies
85     manually.
86    
87 root 1.1 =item * PAR works out of the box, F<staticperl> does not.
88    
89     Maintaining your own custom perl build can be a pain in the ass, and while
90     F<staticperl> tries to make this easy, it still requires a custom perl
91     build and possibly fiddling with some modules. PAR is likely to produce
92     results faster.
93    
94 root 1.13 Ok, PAR never has worked for me out of the box, and for some people,
95     F<staticperl> does work out of the box, as they don't count "fiddling with
96     module use lists" against it, but nevertheless, F<staticperl> is certainly
97     a bit more difficult to use.
98    
99 root 1.1 =back
100    
101     =head1 HOW DOES IT WORK?
102    
103     Simple: F<staticperl> downloads, compile and installs a perl version of
104     your choice in F<~/.staticperl>. You can add extra modules either by
105     letting F<staticperl> install them for you automatically, or by using CPAN
106     and doing it interactively. This usually takes 5-10 minutes, depending on
107 root 1.3 the speed of your computer and your internet connection.
108 root 1.1
109     It is possible to do program development at this stage, too.
110    
111     Afterwards, you create a list of files and modules you want to include,
112 root 1.3 and then either build a new perl binary (that acts just like a normal perl
113 root 1.1 except everything is compiled in), or you create bundle files (basically C
114     sources you can use to embed all files into your project).
115    
116     This step is very fast (a few seconds if PPI is not used for stripping,
117     more seconds otherwise, as PPI is very slow), and can be tweaked and
118     repeated as often as necessary.
119    
120     =head1 THE F<STATICPERL> SCRIPT
121    
122     This module installs a script called F<staticperl> into your perl
123     binary directory. The script is fully self-contained, and can be used
124     without perl (for example, in an uClibc chroot environment). In fact,
125     it can be extracted from the C<App::Staticperl> distribution tarball as
126     F<bin/staticperl>, without any installation.
127    
128     F<staticperl> interprets the first argument as a command to execute,
129     optionally followed by any parameters.
130    
131     There are two command categories: the "phase 1" commands which deal with
132     installing perl and perl modules, and the "phase 2" commands, which deal
133     with creating binaries and bundle files.
134    
135     =head2 PHASE 1 COMMANDS: INSTALLING PERL
136    
137     The most important command is F<install>, which does basically
138     everything. The default is to download and install perl 5.12.2 and a few
139     modules required by F<staticperl> itself, but all this can (and should) be
140     changed - see L<CONFIGURATION>, below.
141    
142     The command
143    
144     staticperl install
145    
146     Is normally all you need: It installs the perl interpreter in
147     F<~/.staticperl/perl>. It downloads, configures, builds and installs the
148     perl interpreter if required.
149    
150     Most of the following commands simply run one or more steps of this
151     sequence.
152    
153 root 1.3 To force recompilation or reinstallation, you need to run F<staticperl
154 root 1.1 distclean> first.
155    
156     =over 4
157    
158     =item F<staticperl fetch>
159    
160     Runs only the download and unpack phase, unless this has already happened.
161    
162     =item F<staticperl configure>
163    
164     Configures the unpacked perl sources, potentially after downloading them first.
165    
166     =item F<staticperl build>
167    
168     Builds the configured perl sources, potentially after automatically
169     configuring them.
170    
171     =item F<staticperl install>
172    
173 root 1.3 Wipes the perl installation directory (usually F<~/.staticperl/perl>) and
174     installs the perl distribution, potentially after building it first.
175 root 1.1
176     =item F<staticperl cpan> [args...]
177    
178 root 1.3 Starts an interactive CPAN shell that you can use to install further
179     modules. Installs the perl first if necessary, but apart from that,
180 root 1.1 no magic is involved: you could just as well run it manually via
181     F<~/.staticperl/perl/bin/cpan>.
182    
183     Any additional arguments are simply passed to the F<cpan> command.
184    
185     =item F<staticperl instcpan> module...
186    
187     Tries to install all the modules given and their dependencies, using CPAN.
188    
189     Example:
190    
191     staticperl instcpan EV AnyEvent::HTTPD Coro
192    
193     =item F<staticperl instsrc> directory...
194    
195     In the unlikely case that you have unpacked perl modules around and want
196 root 1.3 to install from these instead of from CPAN, you can do this using this
197 root 1.1 command by specifying all the directories with modules in them that you
198     want to have built.
199    
200     =item F<staticperl clean>
201    
202 root 1.12 Deletes the perl source directory (and potentially cleans up other
203     intermediate files). This can be used to clean up files only needed for
204     building perl, without removing the installed perl interpreter, or to
205     force a re-build from scratch.
206    
207     At the moment, it doesn't delete downloaded tarballs.
208 root 1.1
209     =item F<staticperl distclean>
210    
211     This wipes your complete F<~/.staticperl> directory. Be careful with this,
212     it nukes your perl download, perl sources, perl distribution and any
213     installed modules. It is useful if you wish to start over "from scratch"
214     or when you want to uninstall F<staticperl>.
215    
216     =back
217    
218     =head2 PHASE 2 COMMANDS: BUILDING PERL BUNDLES
219    
220     Building (linking) a new F<perl> binary is handled by a separate
221     script. To make it easy to use F<staticperl> from a F<chroot>, the script
222     is embedded into F<staticperl>, which will write it out and call for you
223     with any arguments you pass:
224    
225     staticperl mkbundle mkbundle-args...
226    
227     In the oh so unlikely case of something not working here, you
228 root 1.2 can run the script manually as well (by default it is written to
229 root 1.1 F<~/.staticperl/mkbundle>).
230    
231     F<mkbundle> is a more conventional command and expect the argument
232 root 1.3 syntax commonly used on UNIX clones. For example, this command builds
233 root 1.1 a new F<perl> binary and includes F<Config.pm> (for F<perl -V>),
234     F<AnyEvent::HTTPD>, F<URI> and a custom F<httpd> script (from F<eg/httpd>
235     in this distribution):
236    
237     # first make sure we have perl and the required modules
238     staticperl instcpan AnyEvent::HTTPD
239    
240     # now build the perl
241     staticperl mkperl -M'"Config_heavy.pl"' -MAnyEvent::Impl::Perl \
242     -MAnyEvent::HTTPD -MURI::http \
243     --add 'eg/httpd httpd.pm'
244    
245     # finally, invoke it
246     ./perl -Mhttpd
247    
248     As you can see, things are not quite as trivial: the L<Config> module has
249     a hidden dependency which is not even a perl module (F<Config_heavy.pl>),
250     L<AnyEvent> needs at least one event loop backend that we have to
251 root 1.3 specify manually (here L<AnyEvent::Impl::Perl>), and the F<URI> module
252 root 1.1 (required by L<AnyEvent::HTTPD>) implements various URI schemes as extra
253     modules - since L<AnyEvent::HTTPD> only needs C<http> URIs, we only need
254 root 1.3 to include that module. I found out about these dependencies by carefully
255     watching any error messages about missing modules...
256 root 1.1
257 root 1.14 Instead of building a new perl binary, you can also build a standalone
258     application:
259    
260     # build the app
261     staticperl mkapp app --boot eg/httpd \
262     -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI::http
263    
264     # run it
265     ./app
266    
267 root 1.1 =head3 OPTION PROCESSING
268    
269 root 1.3 All options can be given as arguments on the command line (typically
270     using long (e.g. C<--verbose>) or short option (e.g. C<-v>) style). Since
271     specifying a lot of modules can make the command line very cumbersome,
272 root 1.1 you can put all long options into a "bundle specification file" (with or
273     without C<--> prefix) and specify this bundle file instead.
274    
275     For example, the command given earlier could also look like this:
276    
277     staticperl mkperl httpd.bundle
278    
279     And all options could be in F<httpd.bundle>:
280    
281     use "Config_heavy.pl"
282     use AnyEvent::Impl::Perl
283     use AnyEvent::HTTPD
284     use URI::http
285     add eg/httpd httpd.pm
286    
287 root 1.2 All options that specify modules or files to be added are processed in the
288 root 1.3 order given on the command line (that affects the C<--use> and C<--eval>
289 root 1.2 options at the moment).
290    
291 root 1.1 =head3 MKBUNDLE OPTIONS
292    
293     =over 4
294    
295 root 1.2 =item --verbose | -v
296    
297     Increases the verbosity level by one (the default is C<1>).
298    
299     =item --quiet | -q
300    
301     Decreases the verbosity level by one.
302    
303     =item --strip none|pod|ppi
304    
305     Specify the stripping method applied to reduce the file of the perl
306     sources included.
307    
308     The default is C<pod>, which uses the L<Pod::Strip> module to remove all
309 root 1.3 pod documentation, which is very fast and reduces file size a lot.
310 root 1.2
311     The C<ppi> method uses L<PPI> to parse and condense the perl sources. This
312 root 1.3 saves a lot more than just L<Pod::Strip>, and is generally safer, but
313     is also a lot slower, so is best used for production builds. Note that
314     this method doesn't optimise for raw file size, but for best compression
315     (that means that the uncompressed file size is a bit larger, but the files
316     compress better, e.g. with F<upx>).
317 root 1.2
318 root 1.9 Last not least, if you need accurate line numbers in error messages,
319     or in the unlikely case where C<pod> is too slow, or some module gets
320     mistreated, you can specify C<none> to not mangle included perl sources in
321     any way.
322 root 1.2
323     =item --perl
324    
325     After writing out the bundle files, try to link a new perl interpreter. It
326     will be called F<perl> and will be left in the current working
327     directory. The bundle files will be removed.
328    
329 root 1.3 This switch is automatically used when F<staticperl> is invoked with the
330 root 1.2 C<mkperl> command (instead of C<mkbundle>):
331    
332     # build a new ./perl with only common::sense in it - very small :)
333     staticperl mkperl -Mcommon::sense
334    
335 root 1.14 =item --app name
336    
337     After writing out the bundle files, try to link a new standalone
338     program. It will be called C<name>, and the bundle files get removed after
339     linking it.
340    
341     The difference to the (mutually exclusive) C<--perl> option is that the
342     binary created by this option will not try to act as a perl interpreter -
343     instead it will simply initialise the perl interpreter, clean it up and
344     exit.
345    
346     This switch is automatically used when F<staticperl> is invoked with the
347     C<mkapp> command (instead of C<mkbundle>):
348    
349     To let it do something useful you I<must> add some boot code, e.g. with
350     the C<--boot> option.
351    
352     Example: create a standalone perl binary that will execute F<appfile> when
353     it is started.
354    
355     staticperl mkbundle --app myexe --boot appfile
356    
357 root 1.2 =item --use module | -Mmodule
358    
359     Include the named module and all direct dependencies. This is done by
360     C<require>'ing the module in a subprocess and tracing which other modules
361     and files it actually loads. If the module uses L<AutoLoader>, then all
362     splitfiles will be included as well.
363    
364     Example: include AnyEvent and AnyEvent::Impl::Perl.
365    
366     staticperl mkbundle --use AnyEvent --use AnyEvent::Impl::Perl
367    
368     Sometimes you want to load old-style "perl libraries" (F<.pl> files), or
369     maybe other weirdly named files. To do that, you need to quote the name in
370 root 1.3 single or double quotes. When given on the command line, you probably need
371 root 1.2 to quote once more to avoid your shell interpreting it. Common cases that
372     need this are F<Config_heavy.pl> and F<utf8_heavy.pl>.
373    
374     Example: include the required files for F<perl -V> to work in all its
375     glory (F<Config.pm> is included automatically by this).
376    
377     # bourne shell
378     staticperl mkbundle --use '"Config_heavy.pl"'
379    
380     # bundle specification file
381     use "Config_heavy.pl"
382    
383     The C<-Mmodule> syntax is included as an alias that might be easier to
384     remember than C<use>. Or maybe it confuses people. Time will tell. Or
385     maybe not. Argh.
386    
387     =item --eval "perl code" | -e "perl code"
388    
389     Sometimes it is easier (or necessary) to specify dependencies using perl
390     code, or maybe one of the modules you use need a special use statement. In
391     that case, you can use C<eval> to execute some perl snippet or set some
392     variables or whatever you need. All files C<require>'d or C<use>'d in the
393     script are included in the final bundle.
394    
395     Keep in mind that F<mkbundle> will only C<require> the modules named
396     by the C<--use> option, so do not expect the symbols from modules you
397 root 1.3 C<--use>'d earlier on the command line to be available.
398 root 1.2
399     Example: force L<AnyEvent> to detect a backend and therefore include it
400     in the final bundle.
401    
402     staticperl mkbundle --eval 'use AnyEvent; AnyEvent::detect'
403    
404     # or like this
405     staticperl mkbundle -MAnyEvent --eval 'use AnyEvent; AnyEvent::detect'
406    
407     Example: use a separate "bootstrap" script that C<use>'s lots of modules
408     and include this in the final bundle, to be executed automatically.
409    
410     staticperl mkbundle --eval 'do "bootstrap"' --boot bootstrap
411    
412     =item --boot filename
413    
414     Include the given file in the bundle and arrange for it to be executed
415     (using a C<require>) before anything else when the new perl is
416     initialised. This can be used to modify C<@INC> or anything else before
417 root 1.3 the perl interpreter executes scripts given on the command line (or via
418 root 1.2 C<-e>). This works even in an embedded interpreter.
419    
420     =item --add "file" | --add "file alias"
421    
422     Adds the given (perl) file into the bundle (and optionally call it
423     "alias"). This is useful to include any custom files into the bundle.
424    
425     Example: embed the file F<httpd> as F<httpd.pm> when creating the bundle.
426    
427     staticperl mkperl --add "httpd httpd.pm"
428    
429     It is also a great way to add any custom modules:
430    
431     # specification file
432     add file1 myfiles/file1
433     add file2 myfiles/file2
434     add file3 myfiles/file3
435    
436 root 1.10 =item --binadd "file" | --add "file alias"
437    
438     Just like C<--add>, except that it treats the file as binary and adds it
439     without any processing.
440    
441     You should probably add a C</> prefix to avoid clashing with embedded
442     perl files (whose paths do not start with C</>), and/or use a special
443     directory, such as C</res/name>.
444    
445     You can later get a copy of these files by calling C<staticperl::find
446     "alias">.
447    
448 root 1.2 =item --static
449    
450     When C<--perl> is also given, link statically instead of dynamically. The
451     default is to link the new perl interpreter fully dynamic (that means all
452     perl modules are linked statically, but all external libraries are still
453     referenced dynamically).
454    
455     Keep in mind that Solaris doesn't support static linking at all, and
456     systems based on GNU libc don't really support it in a usable fashion
457     either. Try uClibc if you want to create fully statically linked
458     executables, or try the C<--staticlibs> option to link only some libraries
459     statically.
460    
461     =item any other argument
462    
463     Any other argument is interpreted as a bundle specification file, which
464     supports most long options (without extra quoting), one option per line.
465 root 1.1
466     =back
467    
468 root 1.15 =head2 F<STATICPERL> CONFIGURATION AND HOOKS
469 root 1.1
470 root 1.2 During (each) startup, F<staticperl> tries to source the following shell
471     files in order:
472    
473     /etc/staticperlrc
474     ~/.staticperlrc
475     $STATICPERL/rc
476    
477     They can be used to override shell variables, or define functions to be
478     called at specific phases.
479    
480     Note that the last file is erased during F<staticperl distclean>, so
481     generally should not be used.
482    
483     =head3 CONFIGURATION VARIABLES
484    
485     =head4 Variables you I<should> override
486    
487     =over 4
488    
489     =item C<EMAIL>
490    
491     The e-mail address of the person who built this binary. Has no good
492     default, so should be specified by you.
493    
494     =item C<CPAN>
495    
496     The URL of the CPAN mirror to use (e.g. L<http://mirror.netcologne.de/cpan/>).
497    
498 root 1.6 =item C<EXTRA_MODULES>
499 root 1.2
500 root 1.6 Additional modules installed during F<staticperl install>. Here you can
501     set which modules you want have to installed from CPAN.
502 root 1.2
503 root 1.11 Example: I really really need EV, AnyEvent, Coro and AnyEvent::AIO.
504 root 1.2
505 root 1.11 EXTRA_MODULES="EV AnyEvent Coro AnyEvent::AIO"
506 root 1.2
507 root 1.6 Note that you can also use a C<postinstall> hook to achieve this, and
508     more.
509 root 1.2
510 root 1.11 =back
511    
512     =head4 Variables you might I<want> to override
513    
514     =over 4
515    
516     =item C<STATICPERL>
517    
518     The directory where staticperl stores all its files
519     (default: F<~/.staticperl>).
520    
521 root 1.6 =item C<PERL_MM_USE_DEFAULT>, C<EV_EXTRA_DEFS>, ...
522 root 1.2
523     Usually set to C<1> to make modules "less inquisitive" during their
524     installation, you can set any environment variable you want - some modules
525     (such as L<Coro> or L<EV>) use environment variables for further tweaking.
526    
527 root 1.11 =item C<PERL_VERSION>
528 root 1.6
529 root 1.11 The perl version to install - default is currently C<5.12.2>, but C<5.8.9>
530     is also a good choice (5.8.9 is much smaller than 5.12.2, while 5.10.1 is
531     about as big as 5.12.2).
532 root 1.2
533 root 1.11 =item C<PERL_PREFIX>
534 root 1.2
535 root 1.6 The prefix where perl gets installed (default: F<$STATICPERL/perl>),
536     i.e. where the F<bin> and F<lib> subdirectories will end up.
537 root 1.2
538 root 1.10 =item C<PERL_CONFIGURE>
539    
540     Additional Configure options - these are simply passed to the perl
541     Configure script. For example, if you wanted to enable dynamic loading,
542     you could pass C<-Dusedl>. To enable ithreads (Why would you want that
543     insanity? Don't! Use L<forks> instead!) you would pass C<-Duseithreads>
544     and so on.
545    
546     More commonly, you would either activate 64 bit integer support
547     (C<-Duse64bitint>), or disable large files support (-Uuselargefiles), to
548     reduce filesize further.
549    
550 root 1.6 =item C<PERL_CPPFLAGS>, C<PERL_OPTIMIZE>, C<PERL_LDFLAGS>, C<PERL_LIBS>
551 root 1.2
552 root 1.6 These flags are passed to perl's F<Configure> script, and are generally
553     optimised for small size (at the cost of performance). Since they also
554     contain subtle workarounds around various build issues, changing these
555     usually requires understanding their default values - best look at the top
556     of the F<staticperl> script for more info on these.
557 root 1.2
558     =back
559    
560 root 1.5 =head4 Variables you probably I<do not want> to override
561 root 1.2
562     =over 4
563    
564     =item C<MKBUNDLE>
565    
566     Where F<staticperl> writes the C<mkbundle> command to
567     (default: F<$STATICPERL/mkbundle>).
568    
569     =item C<STATICPERL_MODULES>
570    
571     Additional modules needed by C<mkbundle> - should therefore not be changed
572     unless you know what you are doing.
573    
574     =back
575    
576     =head3 OVERRIDABLE HOOKS
577    
578     In addition to environment variables, it is possible to provide some
579     shell functions that are called at specific times. To provide your own
580 root 1.3 commands, just define the corresponding function.
581 root 1.2
582     Example: install extra modules from CPAN and from some directories
583     at F<staticperl install> time.
584    
585     postinstall() {
586 root 1.4 rm -rf lib/threads* # weg mit Schaden
587 root 1.2 instcpan IO::AIO EV
588     instsrc ~/src/AnyEvent
589     instsrc ~/src/XML-Sablotron-1.0100001
590 root 1.4 instcpan Anyevent::AIO AnyEvent::HTTPD
591 root 1.2 }
592    
593     =over 4
594    
595 root 1.12 =item preconfigure
596    
597     Called just before running F<./Configur> in the perl source
598     directory. Current working directory is the perl source directory.
599    
600     This can be used to set any C<PERL_xxx> variables, which might be costly
601     to compute.
602    
603 root 1.2 =item postconfigure
604    
605     Called after configuring, but before building perl. Current working
606     directory is the perl source directory.
607    
608 root 1.12 Could be used to tailor/patch config.sh (followed by F<sh Configure -S>)
609     or do any other modifications.
610 root 1.2
611     =item postbuild
612    
613     Called after building, but before installing perl. Current working
614     directory is the perl source directory.
615    
616     I have no clue what this could be used for - tell me.
617    
618     =item postinstall
619    
620     Called after perl and any extra modules have been installed in C<$PREFIX>,
621     but before setting the "installation O.K." flag.
622    
623     The current working directory is C<$PREFIX>, but maybe you should not rely
624     on that.
625    
626     This hook is most useful to customise the installation, by deleting files,
627     or installing extra modules using the C<instcpan> or C<instsrc> functions.
628    
629     The script must return with a zero exit status, or the installation will
630     fail.
631    
632     =back
633 root 1.1
634 root 1.9 =head1 ANATOMY OF A BUNDLE
635    
636     When not building a new perl binary, C<mkbundle> will leave a number of
637     files in the current working directory, which can be used to embed a perl
638     interpreter in your program.
639    
640     Intimate knowledge of L<perlembed> and preferably some experience with
641     embedding perl is highly recommended.
642    
643     C<mkperl> (or the C<--perl> option) basically does this to link the new
644     interpreter (it also adds a main program to F<bundle.>):
645    
646     $Config{cc} $(cat bundle.ccopts) -o perl bundle.c $(cat bundle.ldopts)
647    
648     =over 4
649    
650     =item bundle.h
651    
652     A header file that contains the prototypes of the few symbols "exported"
653     by bundle.c, and also exposes the perl headers to the application.
654    
655     =over 4
656    
657     =item staticperl_init ()
658    
659     Initialises the perl interpreter. You can use the normal perl functions
660     after calling this function, for example, to define extra functions or
661     to load a .pm file that contains some initialisation code, or the main
662     program function:
663    
664     XS (xsfunction)
665     {
666     dXSARGS;
667    
668     // now we have items, ST(i) etc.
669     }
670    
671     static void
672     run_myapp(void)
673     {
674     staticperl_init ();
675     newXSproto ("myapp::xsfunction", xsfunction, __FILE__, "$$;$");
676     eval_pv ("require myapp::main", 1); // executes "myapp/main.pm"
677     }
678    
679     =item staticperl_xs_init (pTHX)
680    
681     Sometimes you need direct control over C<perl_parse> and C<perl_run>, in
682     which case you do not want to use C<staticperl_init> but call them on your
683     own.
684    
685     Then you need this function - either pass it directly as the C<xs_init>
686     function to C<perl_parse>, or call it from your own C<xs_init> function.
687    
688     =item staticperl_cleanup ()
689    
690     In the unlikely case that you want to destroy the perl interpreter, here
691     is the corresponding function.
692    
693     =item PerlInterpreter *staticperl
694    
695     The perl interpreter pointer used by staticperl. Not normally so useful,
696     but there it is.
697    
698     =back
699    
700     =item bundle.ccopts
701    
702     Contains the compiler options required to compile at least F<bundle.c> and
703     any file that includes F<bundle.h> - you should probably use it in your
704     C<CFLAGS>.
705    
706     =item bundle.ldopts
707    
708     The linker options needed to link the final program.
709    
710     =back
711    
712     =head1 RUNTIME FUNCTIONALITY
713    
714     Binaries created with C<mkbundle>/C<mkperl> contain extra functions, which
715     are required to access the bundled perl sources, but might be useful for
716     other purposes.
717    
718     In addition, for the embedded loading of perl files to work, F<staticperl>
719     overrides the C<@INC> array.
720    
721     =over 4
722    
723     =item $file = staticperl::find $path
724    
725     Returns the data associated with the given C<$path>
726     (e.g. C<Digest/MD5.pm>, C<auto/POSIX/autosplit.ix>), which is basically
727     the UNIX path relative to the perl library directory.
728    
729     Returns C<undef> if the file isn't embedded.
730    
731     =item @paths = staticperl::list
732    
733     Returns the list of all paths embedded in this binary.
734    
735     =back
736    
737     =head1 FULLY STATIC BINARIES - BUILDROOT
738    
739 root 1.10 To make truly static (Linux-) libraries, you might want to have a look at
740 root 1.9 buildroot (L<http://buildroot.uclibc.org/>).
741    
742     Buildroot is primarily meant to set up a cross-compile environment (which
743     is not so useful as perl doesn't quite like cross compiles), but it can also compile
744     a chroot environment where you can use F<staticperl>.
745    
746     To do so, download buildroot, and enable "Build options => development
747     files in target filesystem" and optionally "Build options => gcc
748     optimization level (optimize for size)". At the time of writing, I had
749     good experiences with GCC 4.4.x but not GCC 4.5.
750    
751     To minimise code size, I used C<-pipe -ffunction-sections -fdata-sections
752     -finline-limit=8 -fno-builtin-strlen -mtune=i386>. The C<-mtune=i386>
753     doesn't decrease codesize much, but it makes the file much more
754     compressible.
755    
756     If you don't need Coro or threads, you can go with "linuxthreads.old" (or
757     no thread support). For Coro, it is highly recommended to switch to a
758     uClibc newer than 0.9.31 (at the time of this writing, I used the 20101201
759     snapshot) and enable NPTL, otherwise Coro needs to be configured with the
760     ultra-slow pthreads backend to work around linuxthreads bugs (it also uses
761     twice the address space needed for stacks).
762    
763 root 1.10 If you use C<linuxthreads.old>, then you should also be aware that
764     uClibc shares C<errno> between all threads when statically linking. See
765     L<http://lists.uclibc.org/pipermail/uclibc/2010-June/044157.html> for a
766     workaround (And L<https://bugs.uclibc.org/2089> for discussion).
767    
768 root 1.11 C<ccache> support is also recommended, especially if you want
769     to play around with buildroot options. Enabling the C<miniperl>
770     package will probably enable all options required for a successful
771     perl build. F<staticperl> itself additionally needs either C<wget>
772     (recommended, for CPAN) or C<curl>.
773 root 1.9
774     As for shells, busybox should provide all that is needed, but the default
775     busybox configuration doesn't include F<comm> which is needed by perl -
776     either make a custom busybox config, or compile coreutils.
777    
778     For the latter route, you might find that bash has some bugs that keep
779     it from working properly in a chroot - either use dash (and link it to
780     F</bin/sh> inside the chroot) or link busybox to F</bin/sh>, using it's
781     built-in ash shell.
782    
783     Finally, you need F</dev/null> inside the chroot for many scripts to work
784     - F<cp /dev/null output/target/dev> or bind-mounting your F</dev> will
785     both provide this.
786    
787     After you have compiled and set up your buildroot target, you can copy
788     F<staticperl> from the C<App::Staticperl> distribution or from your
789     perl f<bin> directory (if you installed it) into the F<output/target>
790     filesystem, chroot inside and run it.
791    
792 root 1.17 =head1 RECIPES / SPECIFIC MODULES
793    
794     This section contains some common(?) recipes and information about
795     problems with some common modules or perl constructs that require extra
796     files to be included.
797    
798     =head2 MODULES
799    
800     =over 4
801    
802     =item utf8
803    
804     Some functionality in the utf8 module, such as swash handling (used
805     for unicode character ranges in regexes) is implemented in the
806     C<"utf8_heavy.pl"> library.
807    
808     Many Unicode properties in turn are defined in separate modules,
809     such as C<"unicore/Heavy.pl"> and more specific data tables such as
810     C<"unicore/To/Digit.pl"> or C<"unicore/lib/Perl/Word.pl">. These
811     tables are big (7MB uncompressed), so including them on demand by your
812     applciation only might pay off.
813    
814     =item Carp
815    
816     Carp had (in older versions of perl) a dependency on L<Carp::Heavy>. As of
817     perl 5.12.2 (maybe earlier), this dependency no longer exists.
818    
819     =item Config
820    
821     The F<perl -V> switch (as well as many modules) needs L<Config>, which in
822     turn might need L<"Config_heavy.pl">. Including the latter gives you
823     both.
824    
825     =item AnyEvent
826    
827     AnyEvent needs a backend implementation that it will load in a delayed
828     fashion. The L<AnyEvent::Impl::Perl> backend is the default choice
829     for AnyEvent if it can't find anything else, and is usually a safe
830     fallback. If you plan to use e.g. L<EV> (L<POE>...), then you need to
831     include the L<AnyEvent::Impl::EV> (L<AnyEvent::Impl::POE>...) backend as
832     well.
833    
834     If you want to handle IRIs or IDNs (L<AnyEvent::Util> punycode and idn
835     functions), you also need to include C<"AnyEvent/Util/idna.pl"> and
836     C<"AnyEvent/Util/uts46data.pl">.
837    
838     =item URI
839    
840     URI implements schemes as separate modules - the generic URL scheme is
841     implemented in L<URI::_generic>, HTTP is implemented in L<URI::http>. If
842     you need to use any of these schemes, you should include these manually.
843    
844     =back
845    
846     =head2 RECIPES
847    
848     =over 4
849    
850     =item Getting rid of netdb function
851    
852     The perl core has lots of netdb functions (C<getnetbyname>, C<getgrent>
853     and so on) that few applications use. You can avoid compiling them in by
854     putting the following fragment into a C<preconfigure> hook:
855    
856     preconfigure() {
857     for sym in \
858     d_getgrnam_r d_endgrent d_endgrent_r d_endhent \
859     d_endhostent_r d_endnent d_endnetent_r d_endpent \
860     d_endprotoent_r d_endpwent d_endpwent_r d_endsent \
861     d_endservent_r d_getgrent d_getgrent_r d_getgrgid_r \
862     d_getgrnam_r d_gethbyaddr d_gethent d_getsbyport \
863     d_gethostbyaddr_r d_gethostbyname_r d_gethostent_r \
864     d_getlogin_r d_getnbyaddr d_getnbyname d_getnent \
865     d_getnetbyaddr_r d_getnetbyname_r d_getnetent_r \
866     d_getpent d_getpbyname d_getpbynumber d_getprotobyname_r \
867     d_getprotobynumber_r d_getprotoent_r d_getpwent \
868     d_getpwent_r d_getpwnam_r d_getpwuid_r d_getsent \
869     d_getservbyname_r d_getservbyport_r d_getservent_r \
870     d_getspnam_r d_getsbyname
871     # d_gethbyname
872     do
873     PERL_CONFIGURE="$PERL_CONFIGURE -U$sym"
874     done
875     }
876    
877     This mostly gains space when linking staticaly, as the functions will
878     liekly not be linked in. The gain for dynamically-linked binaries is
879     smaller.
880    
881     Also, this leaves C<gethostbyname> in - not only is it actually used
882     often, the L<Socket> module also exposes it, so leaving it out usually
883     gains little. Why Socket exposes a C function that is in the core already
884     is anybody's guess.
885    
886     =back
887    
888 root 1.1 =head1 AUTHOR
889    
890     Marc Lehmann <schmorp@schmorp.de>
891     http://software.schmorp.de/pkg/staticperl.html