ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/README
Revision: 1.5
Committed: Tue Dec 7 10:40:39 2010 UTC (15 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-0_1
Changes since 1.4: +178 -18 lines
Log Message:
rel-0_1

File Contents

# Content
1 NAME
2 staticperl - perl, libc, 100 modules, all in one 500kb file
3
4 SYNOPSIS
5 staticperl help # print the embedded documentation
6 staticperl fetch # fetch and unpack perl sources
7 staticperl configure # fetch and then configure perl
8 staticperl build # configure and then build perl
9 staticperl install # build and then install perl
10 staticperl clean # clean most intermediate files (restart at configure)
11 staticperl distclean # delete everything installed by this script
12 staticperl cpan # invoke CPAN shell
13 staticperl instmod path... # install unpacked modules
14 staticperl instcpan modulename... # install modules from CPAN
15 staticperl mkbundle <bundle-args...> # see documentation
16 staticperl mkperl <bundle-args...> # see documentation
17
18 Typical Examples:
19
20 staticperl install # fetch, configure, build and install perl
21 staticperl cpan # run interactive cpan shell
22 staticperl mkperl -M '"Config_heavy.pl"' # build a perl that supports -V
23 staticperl mkperl -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI -MURI::http
24 # build a perl with the above modules linked in
25
26 DESCRIPTION
27 This script helps you creating single-file perl interpreters, or
28 embedding a perl interpreter in your applications. Single-file means
29 that it is fully self-contained - no separate shared objects, no
30 autoload fragments, no .pm or .pl files are needed. And when linking
31 statically, you can create (or embed) a single file that contains perl
32 interpreter, libc, all the modules you need and all the libraries you
33 need.
34
35 With uClibc and upx on x86, you can create a single 500kb binary that
36 contains perl and 100 modules such as POSIX, AnyEvent, EV, IO::AIO, Coro
37 and so on. Or any other choice of modules.
38
39 The created files do not need write access to the file system (like PAR
40 does). In fact, since this script is in many ways similar to
41 PAR::Packer, here are the differences:
42
43 * The generated executables are much smaller than PAR created ones.
44
45 Shared objects and the perl binary contain a lot of extra info,
46 while the static nature of staticperl allows the linker to remove
47 all functionality and meta-info not required by the final
48 executable. Even extensions statically compiled into perl at build
49 time will only be present in the final executable when needed.
50
51 In addition, staticperl can strip perl sources much more effectively
52 than PAR.
53
54 * The generated executables start much faster.
55
56 There is no need to unpack files, or even to parse Zip archives
57 (which is slow and memory-consuming business).
58
59 * The generated executables don't need a writable filesystem.
60
61 staticperl loads all required files directly from memory. There is
62 no need to unpack files into a temporary directory.
63
64 * More control over included files.
65
66 PAR tries to be maintenance and hassle-free - it tries to include
67 more files than necessary to make sure everything works out of the
68 box. The extra files (such as the unicode database) can take
69 substantial amounts of memory and file size.
70
71 With staticperl, the burden is mostly with the developer - only
72 direct compile-time dependencies and AutoLoader are handled
73 automatically. This means the modules to include often need to be
74 tweaked manually.
75
76 * PAR works out of the box, staticperl does not.
77
78 Maintaining your own custom perl build can be a pain in the ass, and
79 while staticperl tries to make this easy, it still requires a custom
80 perl build and possibly fiddling with some modules. PAR is likely to
81 produce results faster.
82
83 HOW DOES IT WORK?
84 Simple: staticperl downloads, compile and installs a perl version of
85 your choice in ~/.staticperl. You can add extra modules either by
86 letting staticperl install them for you automatically, or by using CPAN
87 and doing it interactively. This usually takes 5-10 minutes, depending
88 on the speed of your computer and your internet connection.
89
90 It is possible to do program development at this stage, too.
91
92 Afterwards, you create a list of files and modules you want to include,
93 and then either build a new perl binary (that acts just like a normal
94 perl except everything is compiled in), or you create bundle files
95 (basically C sources you can use to embed all files into your project).
96
97 This step is very fast (a few seconds if PPI is not used for stripping,
98 more seconds otherwise, as PPI is very slow), and can be tweaked and
99 repeated as often as necessary.
100
101 THE STATICPERL SCRIPT
102 This module installs a script called staticperl into your perl binary
103 directory. The script is fully self-contained, and can be used without
104 perl (for example, in an uClibc chroot environment). In fact, it can be
105 extracted from the "App::Staticperl" distribution tarball as
106 bin/staticperl, without any installation.
107
108 staticperl interprets the first argument as a command to execute,
109 optionally followed by any parameters.
110
111 There are two command categories: the "phase 1" commands which deal with
112 installing perl and perl modules, and the "phase 2" commands, which deal
113 with creating binaries and bundle files.
114
115 PHASE 1 COMMANDS: INSTALLING PERL
116 The most important command is install, which does basically everything.
117 The default is to download and install perl 5.12.2 and a few modules
118 required by staticperl itself, but all this can (and should) be changed
119 - see CONFIGURATION, below.
120
121 The command
122
123 staticperl install
124
125 Is normally all you need: It installs the perl interpreter in
126 ~/.staticperl/perl. It downloads, configures, builds and installs the
127 perl interpreter if required.
128
129 Most of the following commands simply run one or more steps of this
130 sequence.
131
132 To force recompilation or reinstallation, you need to run staticperl
133 distclean first.
134
135 staticperl fetch
136 Runs only the download and unpack phase, unless this has already
137 happened.
138
139 staticperl configure
140 Configures the unpacked perl sources, potentially after downloading
141 them first.
142
143 staticperl build
144 Builds the configured perl sources, potentially after automatically
145 configuring them.
146
147 staticperl install
148 Wipes the perl installation directory (usually ~/.staticperl/perl)
149 and installs the perl distribution, potentially after building it
150 first.
151
152 staticperl cpan [args...]
153 Starts an interactive CPAN shell that you can use to install further
154 modules. Installs the perl first if necessary, but apart from that,
155 no magic is involved: you could just as well run it manually via
156 ~/.staticperl/perl/bin/cpan.
157
158 Any additional arguments are simply passed to the cpan command.
159
160 staticperl instcpan module...
161 Tries to install all the modules given and their dependencies, using
162 CPAN.
163
164 Example:
165
166 staticperl instcpan EV AnyEvent::HTTPD Coro
167
168 staticperl instsrc directory...
169 In the unlikely case that you have unpacked perl modules around and
170 want to install from these instead of from CPAN, you can do this
171 using this command by specifying all the directories with modules in
172 them that you want to have built.
173
174 staticperl clean
175 Runs make distclean in the perl source directory (and potentially
176 cleans up other intermediate files). This can be used to clean up
177 intermediate files without removing the installed perl interpreter.
178
179 staticperl distclean
180 This wipes your complete ~/.staticperl directory. Be careful with
181 this, it nukes your perl download, perl sources, perl distribution
182 and any installed modules. It is useful if you wish to start over
183 "from scratch" or when you want to uninstall staticperl.
184
185 PHASE 2 COMMANDS: BUILDING PERL BUNDLES
186 Building (linking) a new perl binary is handled by a separate script. To
187 make it easy to use staticperl from a chroot, the script is embedded
188 into staticperl, which will write it out and call for you with any
189 arguments you pass:
190
191 staticperl mkbundle mkbundle-args...
192
193 In the oh so unlikely case of something not working here, you can run
194 the script manually as well (by default it is written to
195 ~/.staticperl/mkbundle).
196
197 mkbundle is a more conventional command and expect the argument syntax
198 commonly used on UNIX clones. For example, this command builds a new
199 perl binary and includes Config.pm (for perl -V), AnyEvent::HTTPD, URI
200 and a custom httpd script (from eg/httpd in this distribution):
201
202 # first make sure we have perl and the required modules
203 staticperl instcpan AnyEvent::HTTPD
204
205 # now build the perl
206 staticperl mkperl -M'"Config_heavy.pl"' -MAnyEvent::Impl::Perl \
207 -MAnyEvent::HTTPD -MURI::http \
208 --add 'eg/httpd httpd.pm'
209
210 # finally, invoke it
211 ./perl -Mhttpd
212
213 As you can see, things are not quite as trivial: the Config module has a
214 hidden dependency which is not even a perl module (Config_heavy.pl),
215 AnyEvent needs at least one event loop backend that we have to specify
216 manually (here AnyEvent::Impl::Perl), and the URI module (required by
217 AnyEvent::HTTPD) implements various URI schemes as extra modules - since
218 AnyEvent::HTTPD only needs "http" URIs, we only need to include that
219 module. I found out about these dependencies by carefully watching any
220 error messages about missing modules...
221
222 OPTION PROCESSING
223 All options can be given as arguments on the command line (typically
224 using long (e.g. "--verbose") or short option (e.g. "-v") style). Since
225 specifying a lot of modules can make the command line very cumbersome,
226 you can put all long options into a "bundle specification file" (with or
227 without "--" prefix) and specify this bundle file instead.
228
229 For example, the command given earlier could also look like this:
230
231 staticperl mkperl httpd.bundle
232
233 And all options could be in httpd.bundle:
234
235 use "Config_heavy.pl"
236 use AnyEvent::Impl::Perl
237 use AnyEvent::HTTPD
238 use URI::http
239 add eg/httpd httpd.pm
240
241 All options that specify modules or files to be added are processed in
242 the order given on the command line (that affects the "--use" and
243 "--eval" options at the moment).
244
245 MKBUNDLE OPTIONS
246 --verbose | -v
247 Increases the verbosity level by one (the default is 1).
248
249 --quiet | -q
250 Decreases the verbosity level by one.
251
252 --strip none|pod|ppi
253 Specify the stripping method applied to reduce the file of the perl
254 sources included.
255
256 The default is "pod", which uses the Pod::Strip module to remove all
257 pod documentation, which is very fast and reduces file size a lot.
258
259 The "ppi" method uses PPI to parse and condense the perl sources.
260 This saves a lot more than just Pod::Strip, and is generally safer,
261 but is also a lot slower, so is best used for production builds.
262 Note that this method doesn't optimise for raw file size, but for
263 best compression (that means that the uncompressed file size is a
264 bit larger, but the files compress better, e.g. with upx).
265
266 Last not least, if you need accurate line numbers in error messages,
267 or in the unlikely case where "pod" is too slow, or some module gets
268 mistreated, you can specify "none" to not mangle included perl
269 sources in any way.
270
271 --perl
272 After writing out the bundle files, try to link a new perl
273 interpreter. It will be called perl and will be left in the current
274 working directory. The bundle files will be removed.
275
276 This switch is automatically used when staticperl is invoked with
277 the "mkperl" command (instead of "mkbundle"):
278
279 # build a new ./perl with only common::sense in it - very small :)
280 staticperl mkperl -Mcommon::sense
281
282 --use module | -Mmodule
283 Include the named module and all direct dependencies. This is done
284 by "require"'ing the module in a subprocess and tracing which other
285 modules and files it actually loads. If the module uses AutoLoader,
286 then all splitfiles will be included as well.
287
288 Example: include AnyEvent and AnyEvent::Impl::Perl.
289
290 staticperl mkbundle --use AnyEvent --use AnyEvent::Impl::Perl
291
292 Sometimes you want to load old-style "perl libraries" (.pl files),
293 or maybe other weirdly named files. To do that, you need to quote
294 the name in single or double quotes. When given on the command line,
295 you probably need to quote once more to avoid your shell
296 interpreting it. Common cases that need this are Config_heavy.pl and
297 utf8_heavy.pl.
298
299 Example: include the required files for perl -V to work in all its
300 glory (Config.pm is included automatically by this).
301
302 # bourne shell
303 staticperl mkbundle --use '"Config_heavy.pl"'
304
305 # bundle specification file
306 use "Config_heavy.pl"
307
308 The "-Mmodule" syntax is included as an alias that might be easier
309 to remember than "use". Or maybe it confuses people. Time will tell.
310 Or maybe not. Argh.
311
312 --eval "perl code" | -e "perl code"
313 Sometimes it is easier (or necessary) to specify dependencies using
314 perl code, or maybe one of the modules you use need a special use
315 statement. In that case, you can use "eval" to execute some perl
316 snippet or set some variables or whatever you need. All files
317 "require"'d or "use"'d in the script are included in the final
318 bundle.
319
320 Keep in mind that mkbundle will only "require" the modules named by
321 the "--use" option, so do not expect the symbols from modules you
322 "--use"'d earlier on the command line to be available.
323
324 Example: force AnyEvent to detect a backend and therefore include it
325 in the final bundle.
326
327 staticperl mkbundle --eval 'use AnyEvent; AnyEvent::detect'
328
329 # or like this
330 staticperl mkbundle -MAnyEvent --eval 'use AnyEvent; AnyEvent::detect'
331
332 Example: use a separate "bootstrap" script that "use"'s lots of
333 modules and include this in the final bundle, to be executed
334 automatically.
335
336 staticperl mkbundle --eval 'do "bootstrap"' --boot bootstrap
337
338 --boot filename
339 Include the given file in the bundle and arrange for it to be
340 executed (using a "require") before anything else when the new perl
341 is initialised. This can be used to modify @INC or anything else
342 before the perl interpreter executes scripts given on the command
343 line (or via "-e"). This works even in an embedded interpreter.
344
345 --add "file" | --add "file alias"
346 Adds the given (perl) file into the bundle (and optionally call it
347 "alias"). This is useful to include any custom files into the
348 bundle.
349
350 Example: embed the file httpd as httpd.pm when creating the bundle.
351
352 staticperl mkperl --add "httpd httpd.pm"
353
354 It is also a great way to add any custom modules:
355
356 # specification file
357 add file1 myfiles/file1
358 add file2 myfiles/file2
359 add file3 myfiles/file3
360
361 --binadd "file" | --add "file alias"
362 Just like "--add", except that it treats the file as binary and adds
363 it without any processing.
364
365 You should probably add a "/" prefix to avoid clashing with embedded
366 perl files (whose paths do not start with "/"), and/or use a special
367 directory, such as "/res/name".
368
369 You can later get a copy of these files by calling "staticperl::find
370 "alias"".
371
372 --static
373 When "--perl" is also given, link statically instead of dynamically.
374 The default is to link the new perl interpreter fully dynamic (that
375 means all perl modules are linked statically, but all external
376 libraries are still referenced dynamically).
377
378 Keep in mind that Solaris doesn't support static linking at all, and
379 systems based on GNU libc don't really support it in a usable
380 fashion either. Try uClibc if you want to create fully statically
381 linked executables, or try the "--staticlibs" option to link only
382 some libraries statically.
383
384 any other argument
385 Any other argument is interpreted as a bundle specification file,
386 which supports most long options (without extra quoting), one option
387 per line.
388
389 STATCPERL CONFIGURATION AND HOOKS
390 During (each) startup, staticperl tries to source the following shell
391 files in order:
392
393 /etc/staticperlrc
394 ~/.staticperlrc
395 $STATICPERL/rc
396
397 They can be used to override shell variables, or define functions to be
398 called at specific phases.
399
400 Note that the last file is erased during staticperl distclean, so
401 generally should not be used.
402
403 CONFIGURATION VARIABLES
404 Variables you *should* override
405 "EMAIL"
406 The e-mail address of the person who built this binary. Has no good
407 default, so should be specified by you.
408
409 "CPAN"
410 The URL of the CPAN mirror to use (e.g.
411 <http://mirror.netcologne.de/cpan/>).
412
413 "EXTRA_MODULES"
414 Additional modules installed during staticperl install. Here you can
415 set which modules you want have to installed from CPAN.
416
417 Example: I really really need EV, AnyEvent, Coro and AnyEvent::AIO.
418
419 EXTRA_MODULES="EV AnyEvent Coro AnyEvent::AIO"
420
421 Note that you can also use a "postinstall" hook to achieve this, and
422 more.
423
424 Variables you might *want* to override
425 "STATICPERL"
426 The directory where staticperl stores all its files (default:
427 ~/.staticperl).
428
429 "PERL_MM_USE_DEFAULT", "EV_EXTRA_DEFS", ...
430 Usually set to 1 to make modules "less inquisitive" during their
431 installation, you can set any environment variable you want - some
432 modules (such as Coro or EV) use environment variables for further
433 tweaking.
434
435 "PERL_VERSION"
436 The perl version to install - default is currently 5.12.2, but 5.8.9
437 is also a good choice (5.8.9 is much smaller than 5.12.2, while
438 5.10.1 is about as big as 5.12.2).
439
440 "PERL_PREFIX"
441 The prefix where perl gets installed (default: $STATICPERL/perl),
442 i.e. where the bin and lib subdirectories will end up.
443
444 "PERL_CONFIGURE"
445 Additional Configure options - these are simply passed to the perl
446 Configure script. For example, if you wanted to enable dynamic
447 loading, you could pass "-Dusedl". To enable ithreads (Why would you
448 want that insanity? Don't! Use forks instead!) you would pass
449 "-Duseithreads" and so on.
450
451 More commonly, you would either activate 64 bit integer support
452 ("-Duse64bitint"), or disable large files support (-Uuselargefiles),
453 to reduce filesize further.
454
455 "PERL_CPPFLAGS", "PERL_OPTIMIZE", "PERL_LDFLAGS", "PERL_LIBS"
456 These flags are passed to perl's Configure script, and are generally
457 optimised for small size (at the cost of performance). Since they
458 also contain subtle workarounds around various build issues,
459 changing these usually requires understanding their default values -
460 best look at the top of the staticperl script for more info on
461 these.
462
463 Variables you probably *do not want* to override
464 "MKBUNDLE"
465 Where staticperl writes the "mkbundle" command to (default:
466 $STATICPERL/mkbundle).
467
468 "STATICPERL_MODULES"
469 Additional modules needed by "mkbundle" - should therefore not be
470 changed unless you know what you are doing.
471
472 OVERRIDABLE HOOKS
473 In addition to environment variables, it is possible to provide some
474 shell functions that are called at specific times. To provide your own
475 commands, just define the corresponding function.
476
477 Example: install extra modules from CPAN and from some directories at
478 staticperl install time.
479
480 postinstall() {
481 rm -rf lib/threads* # weg mit Schaden
482 instcpan IO::AIO EV
483 instsrc ~/src/AnyEvent
484 instsrc ~/src/XML-Sablotron-1.0100001
485 instcpan Anyevent::AIO AnyEvent::HTTPD
486 }
487
488 postconfigure
489 Called after configuring, but before building perl. Current working
490 directory is the perl source directory.
491
492 Could be used to tailor/patch config.sh (followed by ./Configure -S)
493 or do any other modifications.
494
495 postbuild
496 Called after building, but before installing perl. Current working
497 directory is the perl source directory.
498
499 I have no clue what this could be used for - tell me.
500
501 postinstall
502 Called after perl and any extra modules have been installed in
503 $PREFIX, but before setting the "installation O.K." flag.
504
505 The current working directory is $PREFIX, but maybe you should not
506 rely on that.
507
508 This hook is most useful to customise the installation, by deleting
509 files, or installing extra modules using the "instcpan" or "instsrc"
510 functions.
511
512 The script must return with a zero exit status, or the installation
513 will fail.
514
515 ANATOMY OF A BUNDLE
516 When not building a new perl binary, "mkbundle" will leave a number of
517 files in the current working directory, which can be used to embed a
518 perl interpreter in your program.
519
520 Intimate knowledge of perlembed and preferably some experience with
521 embedding perl is highly recommended.
522
523 "mkperl" (or the "--perl" option) basically does this to link the new
524 interpreter (it also adds a main program to bundle.):
525
526 $Config{cc} $(cat bundle.ccopts) -o perl bundle.c $(cat bundle.ldopts)
527
528 bundle.h
529 A header file that contains the prototypes of the few symbols
530 "exported" by bundle.c, and also exposes the perl headers to the
531 application.
532
533 staticperl_init ()
534 Initialises the perl interpreter. You can use the normal perl
535 functions after calling this function, for example, to define
536 extra functions or to load a .pm file that contains some
537 initialisation code, or the main program function:
538
539 XS (xsfunction)
540 {
541 dXSARGS;
542
543 // now we have items, ST(i) etc.
544 }
545
546 static void
547 run_myapp(void)
548 {
549 staticperl_init ();
550 newXSproto ("myapp::xsfunction", xsfunction, __FILE__, "$$;$");
551 eval_pv ("require myapp::main", 1); // executes "myapp/main.pm"
552 }
553
554 staticperl_xs_init (pTHX)
555 Sometimes you need direct control over "perl_parse" and
556 "perl_run", in which case you do not want to use
557 "staticperl_init" but call them on your own.
558
559 Then you need this function - either pass it directly as the
560 "xs_init" function to "perl_parse", or call it from your own
561 "xs_init" function.
562
563 staticperl_cleanup ()
564 In the unlikely case that you want to destroy the perl
565 interpreter, here is the corresponding function.
566
567 PerlInterpreter *staticperl
568 The perl interpreter pointer used by staticperl. Not normally so
569 useful, but there it is.
570
571 bundle.ccopts
572 Contains the compiler options required to compile at least bundle.c
573 and any file that includes bundle.h - you should probably use it in
574 your "CFLAGS".
575
576 bundle.ldopts
577 The linker options needed to link the final program.
578
579 RUNTIME FUNCTIONALITY
580 Binaries created with "mkbundle"/"mkperl" contain extra functions, which
581 are required to access the bundled perl sources, but might be useful for
582 other purposes.
583
584 In addition, for the embedded loading of perl files to work, staticperl
585 overrides the @INC array.
586
587 $file = staticperl::find $path
588 Returns the data associated with the given $path (e.g.
589 "Digest/MD5.pm", "auto/POSIX/autosplit.ix"), which is basically the
590 UNIX path relative to the perl library directory.
591
592 Returns "undef" if the file isn't embedded.
593
594 @paths = staticperl::list
595 Returns the list of all paths embedded in this binary.
596
597 FULLY STATIC BINARIES - BUILDROOT
598 To make truly static (Linux-) libraries, you might want to have a look
599 at buildroot (<http://buildroot.uclibc.org/>).
600
601 Buildroot is primarily meant to set up a cross-compile environment
602 (which is not so useful as perl doesn't quite like cross compiles), but
603 it can also compile a chroot environment where you can use staticperl.
604
605 To do so, download buildroot, and enable "Build options => development
606 files in target filesystem" and optionally "Build options => gcc
607 optimization level (optimize for size)". At the time of writing, I had
608 good experiences with GCC 4.4.x but not GCC 4.5.
609
610 To minimise code size, I used "-pipe -ffunction-sections -fdata-sections
611 -finline-limit=8 -fno-builtin-strlen -mtune=i386". The "-mtune=i386"
612 doesn't decrease codesize much, but it makes the file much more
613 compressible.
614
615 If you don't need Coro or threads, you can go with "linuxthreads.old"
616 (or no thread support). For Coro, it is highly recommended to switch to
617 a uClibc newer than 0.9.31 (at the time of this writing, I used the
618 20101201 snapshot) and enable NPTL, otherwise Coro needs to be
619 configured with the ultra-slow pthreads backend to work around
620 linuxthreads bugs (it also uses twice the address space needed for
621 stacks).
622
623 If you use "linuxthreads.old", then you should also be aware that uClibc
624 shares "errno" between all threads when statically linking. See
625 <http://lists.uclibc.org/pipermail/uclibc/2010-June/044157.html> for a
626 workaround (And <https://bugs.uclibc.org/2089> for discussion).
627
628 "ccache" support is also recommended, especially if you want to play
629 around with buildroot options. Enabling the "miniperl" package will
630 probably enable all options required for a successful perl build.
631 staticperl itself additionally needs either "wget" (recommended, for
632 CPAN) or "curl".
633
634 As for shells, busybox should provide all that is needed, but the
635 default busybox configuration doesn't include comm which is needed by
636 perl - either make a custom busybox config, or compile coreutils.
637
638 For the latter route, you might find that bash has some bugs that keep
639 it from working properly in a chroot - either use dash (and link it to
640 /bin/sh inside the chroot) or link busybox to /bin/sh, using it's
641 built-in ash shell.
642
643 Finally, you need /dev/null inside the chroot for many scripts to work -
644 cp /dev/null output/target/dev or bind-mounting your /dev will both
645 provide this.
646
647 After you have compiled and set up your buildroot target, you can copy
648 staticperl from the "App::Staticperl" distribution or from your perl
649 f<bin> directory (if you installed it) into the output/target
650 filesystem, chroot inside and run it.
651
652 AUTHOR
653 Marc Lehmann <schmorp@schmorp.de>
654 http://software.schmorp.de/pkg/staticperl.html
655