1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | staticperl - perl, libc, 50 modules all in one 500kb file |
3 | staticperl - perl, libc, 100 modules, all in one 500kb file |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | staticperl help # print the embedded documentation |
7 | staticperl help # print the embedded documentation |
8 | staticperl fetch # fetch and unpack perl sources |
8 | staticperl fetch # fetch and unpack perl sources |
… | |
… | |
32 | fully self-contained - no separate shared objects, no autoload fragments, |
32 | fully self-contained - no separate shared objects, no autoload fragments, |
33 | no .pm or .pl files are needed. And when linking statically, you can |
33 | no .pm or .pl files are needed. And when linking statically, you can |
34 | create (or embed) a single file that contains perl interpreter, libc, all |
34 | create (or embed) a single file that contains perl interpreter, libc, all |
35 | the modules you need and all the libraries you need. |
35 | the modules you need and all the libraries you need. |
36 | |
36 | |
37 | With F<uClibc> and F<upx> on x86, you can create a single 500kb binary that |
37 | With F<uClibc> and F<upx> on x86, you can create a single 500kb binary |
38 | contains perl and 50 modules such as AnyEvent, EV, IO::AIO, Coro and so |
38 | that contains perl and 100 modules such as POSIX, AnyEvent, EV, IO::AIO, |
39 | on. Or any other choice of modules. |
39 | Coro and so on. Or any other choice of modules. |
40 | |
40 | |
41 | The created files do not need write access to the file system (like PAR |
41 | The created files do not need write access to the file system (like PAR |
42 | does). In fact, since this script is in many ways similar to PAR::Packer, |
42 | does). In fact, since this script is in many ways similar to PAR::Packer, |
43 | here are the differences: |
43 | here are the differences: |
44 | |
44 | |
… | |
… | |
81 | Maintaining your own custom perl build can be a pain in the ass, and while |
81 | Maintaining your own custom perl build can be a pain in the ass, and while |
82 | F<staticperl> tries to make this easy, it still requires a custom perl |
82 | F<staticperl> tries to make this easy, it still requires a custom perl |
83 | build and possibly fiddling with some modules. PAR is likely to produce |
83 | build and possibly fiddling with some modules. PAR is likely to produce |
84 | results faster. |
84 | results faster. |
85 | |
85 | |
|
|
86 | Ok, PAR never has worked for me out of the box, and for some people, |
|
|
87 | F<staticperl> does work out of the box, as they don't count "fiddling with |
|
|
88 | module use lists" against it, but nevertheless, F<staticperl> is certainly |
|
|
89 | a bit more difficult to use. |
|
|
90 | |
86 | =back |
91 | =back |
87 | |
92 | |
88 | =head1 HOW DOES IT WORK? |
93 | =head1 HOW DOES IT WORK? |
89 | |
94 | |
90 | Simple: F<staticperl> downloads, compile and installs a perl version of |
95 | Simple: F<staticperl> downloads, compile and installs a perl version of |
… | |
… | |
184 | command by specifying all the directories with modules in them that you |
189 | command by specifying all the directories with modules in them that you |
185 | want to have built. |
190 | want to have built. |
186 | |
191 | |
187 | =item F<staticperl clean> |
192 | =item F<staticperl clean> |
188 | |
193 | |
189 | Runs F<make distclean> in the perl source directory (and potentially |
194 | Deletes the perl source directory (and potentially cleans up other |
190 | cleans up other intermediate files). This can be used to clean up |
195 | intermediate files). This can be used to clean up files only needed for |
191 | intermediate files without removing the installed perl interpreter. |
196 | building perl, without removing the installed perl interpreter, or to |
|
|
197 | force a re-build from scratch. |
|
|
198 | |
|
|
199 | At the moment, it doesn't delete downloaded tarballs. |
192 | |
200 | |
193 | =item F<staticperl distclean> |
201 | =item F<staticperl distclean> |
194 | |
202 | |
195 | This wipes your complete F<~/.staticperl> directory. Be careful with this, |
203 | This wipes your complete F<~/.staticperl> directory. Be careful with this, |
196 | it nukes your perl download, perl sources, perl distribution and any |
204 | it nukes your perl download, perl sources, perl distribution and any |
… | |
… | |
287 | is also a lot slower, so is best used for production builds. Note that |
295 | is also a lot slower, so is best used for production builds. Note that |
288 | this method doesn't optimise for raw file size, but for best compression |
296 | this method doesn't optimise for raw file size, but for best compression |
289 | (that means that the uncompressed file size is a bit larger, but the files |
297 | (that means that the uncompressed file size is a bit larger, but the files |
290 | compress better, e.g. with F<upx>). |
298 | compress better, e.g. with F<upx>). |
291 | |
299 | |
|
|
300 | Last not least, if you need accurate line numbers in error messages, |
292 | Last not least, in the unlikely case where C<pod> is too slow, or some |
301 | or in the unlikely case where C<pod> is too slow, or some module gets |
293 | module gets mistreated, you can specify C<none> to not mangle included |
302 | mistreated, you can specify C<none> to not mangle included perl sources in |
294 | perl sources in any way. |
303 | any way. |
295 | |
304 | |
296 | =item --perl |
305 | =item --perl |
297 | |
306 | |
298 | After writing out the bundle files, try to link a new perl interpreter. It |
307 | After writing out the bundle files, try to link a new perl interpreter. It |
299 | will be called F<perl> and will be left in the current working |
308 | will be called F<perl> and will be left in the current working |
… | |
… | |
382 | # specification file |
391 | # specification file |
383 | add file1 myfiles/file1 |
392 | add file1 myfiles/file1 |
384 | add file2 myfiles/file2 |
393 | add file2 myfiles/file2 |
385 | add file3 myfiles/file3 |
394 | add file3 myfiles/file3 |
386 | |
395 | |
|
|
396 | =item --binadd "file" | --add "file alias" |
|
|
397 | |
|
|
398 | Just like C<--add>, except that it treats the file as binary and adds it |
|
|
399 | without any processing. |
|
|
400 | |
|
|
401 | You should probably add a C</> prefix to avoid clashing with embedded |
|
|
402 | perl files (whose paths do not start with C</>), and/or use a special |
|
|
403 | directory, such as C</res/name>. |
|
|
404 | |
|
|
405 | You can later get a copy of these files by calling C<staticperl::find |
|
|
406 | "alias">. |
|
|
407 | |
387 | =item --static |
408 | =item --static |
388 | |
409 | |
389 | When C<--perl> is also given, link statically instead of dynamically. The |
410 | When C<--perl> is also given, link statically instead of dynamically. The |
390 | default is to link the new perl interpreter fully dynamic (that means all |
411 | default is to link the new perl interpreter fully dynamic (that means all |
391 | perl modules are linked statically, but all external libraries are still |
412 | perl modules are linked statically, but all external libraries are still |
… | |
… | |
428 | =item C<EMAIL> |
449 | =item C<EMAIL> |
429 | |
450 | |
430 | The e-mail address of the person who built this binary. Has no good |
451 | The e-mail address of the person who built this binary. Has no good |
431 | default, so should be specified by you. |
452 | default, so should be specified by you. |
432 | |
453 | |
|
|
454 | =item C<CPAN> |
|
|
455 | |
|
|
456 | The URL of the CPAN mirror to use (e.g. L<http://mirror.netcologne.de/cpan/>). |
|
|
457 | |
|
|
458 | =item C<EXTRA_MODULES> |
|
|
459 | |
|
|
460 | Additional modules installed during F<staticperl install>. Here you can |
|
|
461 | set which modules you want have to installed from CPAN. |
|
|
462 | |
|
|
463 | Example: I really really need EV, AnyEvent, Coro and AnyEvent::AIO. |
|
|
464 | |
|
|
465 | EXTRA_MODULES="EV AnyEvent Coro AnyEvent::AIO" |
|
|
466 | |
|
|
467 | Note that you can also use a C<postinstall> hook to achieve this, and |
|
|
468 | more. |
|
|
469 | |
433 | =back |
470 | =back |
434 | |
471 | |
435 | =head4 Variables you might I<want> to override |
472 | =head4 Variables you might I<want> to override |
436 | |
473 | |
437 | =over 4 |
474 | =over 4 |
438 | |
475 | |
|
|
476 | =item C<STATICPERL> |
|
|
477 | |
|
|
478 | The directory where staticperl stores all its files |
|
|
479 | (default: F<~/.staticperl>). |
|
|
480 | |
|
|
481 | =item C<PERL_MM_USE_DEFAULT>, C<EV_EXTRA_DEFS>, ... |
|
|
482 | |
|
|
483 | Usually set to C<1> to make modules "less inquisitive" during their |
|
|
484 | installation, you can set any environment variable you want - some modules |
|
|
485 | (such as L<Coro> or L<EV>) use environment variables for further tweaking. |
|
|
486 | |
439 | =item C<PERLVER> |
487 | =item C<PERL_VERSION> |
440 | |
488 | |
441 | The perl version to install - default is currently C<5.12.2>, but C<5.8.9> |
489 | The perl version to install - default is currently C<5.12.2>, but C<5.8.9> |
442 | is also a good choice (5.8.9 is much smaller than 5.12.2, while 5.10.1 is |
490 | is also a good choice (5.8.9 is much smaller than 5.12.2, while 5.10.1 is |
443 | about as big as 5.12.2). |
491 | about as big as 5.12.2). |
444 | |
492 | |
445 | =item C<CPAN> |
|
|
446 | |
|
|
447 | The URL of the CPAN mirror to use (e.g. L<http://mirror.netcologne.de/cpan/>). |
|
|
448 | |
|
|
449 | =item C<EXTRA_MODULES> |
|
|
450 | |
|
|
451 | Additional modules installed during F<staticperl install>. Here you can |
|
|
452 | set which modules you want have to installed from CPAN. |
|
|
453 | |
|
|
454 | Example: I really really need EV, AnyEvent, Coro and IO::AIO. |
|
|
455 | |
|
|
456 | EXTRA_MODULES="EV AnyEvent Coro IO::AIO" |
|
|
457 | |
|
|
458 | Note that you can also use a C<postinstall> hook to achieve this, and |
|
|
459 | more. |
|
|
460 | |
|
|
461 | =item C<PERL_MM_USE_DEFAULT>, C<EV_EXTRA_DEFS>, ... |
|
|
462 | |
|
|
463 | Usually set to C<1> to make modules "less inquisitive" during their |
|
|
464 | installation, you can set any environment variable you want - some modules |
|
|
465 | (such as L<Coro> or L<EV>) use environment variables for further tweaking. |
|
|
466 | |
|
|
467 | =item C<STATICPERL> |
|
|
468 | |
|
|
469 | The directory where staticperl stores all its files |
|
|
470 | (default: F<~/.staticperl>). |
|
|
471 | |
|
|
472 | =item C<PREFIX> |
493 | =item C<PERL_PREFIX> |
473 | |
494 | |
474 | The prefix where perl gets installed (default: F<$STATICPERL/perl>), |
495 | The prefix where perl gets installed (default: F<$STATICPERL/perl>), |
475 | i.e. where the F<bin> and F<lib> subdirectories will end up. |
496 | i.e. where the F<bin> and F<lib> subdirectories will end up. |
|
|
497 | |
|
|
498 | =item C<PERL_CONFIGURE> |
|
|
499 | |
|
|
500 | Additional Configure options - these are simply passed to the perl |
|
|
501 | Configure script. For example, if you wanted to enable dynamic loading, |
|
|
502 | you could pass C<-Dusedl>. To enable ithreads (Why would you want that |
|
|
503 | insanity? Don't! Use L<forks> instead!) you would pass C<-Duseithreads> |
|
|
504 | and so on. |
|
|
505 | |
|
|
506 | More commonly, you would either activate 64 bit integer support |
|
|
507 | (C<-Duse64bitint>), or disable large files support (-Uuselargefiles), to |
|
|
508 | reduce filesize further. |
476 | |
509 | |
477 | =item C<PERL_CPPFLAGS>, C<PERL_OPTIMIZE>, C<PERL_LDFLAGS>, C<PERL_LIBS> |
510 | =item C<PERL_CPPFLAGS>, C<PERL_OPTIMIZE>, C<PERL_LDFLAGS>, C<PERL_LIBS> |
478 | |
511 | |
479 | These flags are passed to perl's F<Configure> script, and are generally |
512 | These flags are passed to perl's F<Configure> script, and are generally |
480 | optimised for small size (at the cost of performance). Since they also |
513 | optimised for small size (at the cost of performance). Since they also |
… | |
… | |
517 | instcpan Anyevent::AIO AnyEvent::HTTPD |
550 | instcpan Anyevent::AIO AnyEvent::HTTPD |
518 | } |
551 | } |
519 | |
552 | |
520 | =over 4 |
553 | =over 4 |
521 | |
554 | |
|
|
555 | =item preconfigure |
|
|
556 | |
|
|
557 | Called just before running F<./Configur> in the perl source |
|
|
558 | directory. Current working directory is the perl source directory. |
|
|
559 | |
|
|
560 | This can be used to set any C<PERL_xxx> variables, which might be costly |
|
|
561 | to compute. |
|
|
562 | |
522 | =item postconfigure |
563 | =item postconfigure |
523 | |
564 | |
524 | Called after configuring, but before building perl. Current working |
565 | Called after configuring, but before building perl. Current working |
525 | directory is the perl source directory. |
566 | directory is the perl source directory. |
526 | |
567 | |
527 | Could be used to tailor/patch config.sh (followed by F<./Configure -S>) or |
568 | Could be used to tailor/patch config.sh (followed by F<sh Configure -S>) |
528 | do any other modifications. |
569 | or do any other modifications. |
529 | |
570 | |
530 | =item postbuild |
571 | =item postbuild |
531 | |
572 | |
532 | Called after building, but before installing perl. Current working |
573 | Called after building, but before installing perl. Current working |
533 | directory is the perl source directory. |
574 | directory is the perl source directory. |
… | |
… | |
548 | The script must return with a zero exit status, or the installation will |
589 | The script must return with a zero exit status, or the installation will |
549 | fail. |
590 | fail. |
550 | |
591 | |
551 | =back |
592 | =back |
552 | |
593 | |
|
|
594 | =head1 ANATOMY OF A BUNDLE |
|
|
595 | |
|
|
596 | When not building a new perl binary, C<mkbundle> will leave a number of |
|
|
597 | files in the current working directory, which can be used to embed a perl |
|
|
598 | interpreter in your program. |
|
|
599 | |
|
|
600 | Intimate knowledge of L<perlembed> and preferably some experience with |
|
|
601 | embedding perl is highly recommended. |
|
|
602 | |
|
|
603 | C<mkperl> (or the C<--perl> option) basically does this to link the new |
|
|
604 | interpreter (it also adds a main program to F<bundle.>): |
|
|
605 | |
|
|
606 | $Config{cc} $(cat bundle.ccopts) -o perl bundle.c $(cat bundle.ldopts) |
|
|
607 | |
|
|
608 | =over 4 |
|
|
609 | |
|
|
610 | =item bundle.h |
|
|
611 | |
|
|
612 | A header file that contains the prototypes of the few symbols "exported" |
|
|
613 | by bundle.c, and also exposes the perl headers to the application. |
|
|
614 | |
|
|
615 | =over 4 |
|
|
616 | |
|
|
617 | =item staticperl_init () |
|
|
618 | |
|
|
619 | Initialises the perl interpreter. You can use the normal perl functions |
|
|
620 | after calling this function, for example, to define extra functions or |
|
|
621 | to load a .pm file that contains some initialisation code, or the main |
|
|
622 | program function: |
|
|
623 | |
|
|
624 | XS (xsfunction) |
|
|
625 | { |
|
|
626 | dXSARGS; |
|
|
627 | |
|
|
628 | // now we have items, ST(i) etc. |
|
|
629 | } |
|
|
630 | |
|
|
631 | static void |
|
|
632 | run_myapp(void) |
|
|
633 | { |
|
|
634 | staticperl_init (); |
|
|
635 | newXSproto ("myapp::xsfunction", xsfunction, __FILE__, "$$;$"); |
|
|
636 | eval_pv ("require myapp::main", 1); // executes "myapp/main.pm" |
|
|
637 | } |
|
|
638 | |
|
|
639 | =item staticperl_xs_init (pTHX) |
|
|
640 | |
|
|
641 | Sometimes you need direct control over C<perl_parse> and C<perl_run>, in |
|
|
642 | which case you do not want to use C<staticperl_init> but call them on your |
|
|
643 | own. |
|
|
644 | |
|
|
645 | Then you need this function - either pass it directly as the C<xs_init> |
|
|
646 | function to C<perl_parse>, or call it from your own C<xs_init> function. |
|
|
647 | |
|
|
648 | =item staticperl_cleanup () |
|
|
649 | |
|
|
650 | In the unlikely case that you want to destroy the perl interpreter, here |
|
|
651 | is the corresponding function. |
|
|
652 | |
|
|
653 | =item PerlInterpreter *staticperl |
|
|
654 | |
|
|
655 | The perl interpreter pointer used by staticperl. Not normally so useful, |
|
|
656 | but there it is. |
|
|
657 | |
|
|
658 | =back |
|
|
659 | |
|
|
660 | =item bundle.ccopts |
|
|
661 | |
|
|
662 | Contains the compiler options required to compile at least F<bundle.c> and |
|
|
663 | any file that includes F<bundle.h> - you should probably use it in your |
|
|
664 | C<CFLAGS>. |
|
|
665 | |
|
|
666 | =item bundle.ldopts |
|
|
667 | |
|
|
668 | The linker options needed to link the final program. |
|
|
669 | |
|
|
670 | =back |
|
|
671 | |
|
|
672 | =head1 RUNTIME FUNCTIONALITY |
|
|
673 | |
|
|
674 | Binaries created with C<mkbundle>/C<mkperl> contain extra functions, which |
|
|
675 | are required to access the bundled perl sources, but might be useful for |
|
|
676 | other purposes. |
|
|
677 | |
|
|
678 | In addition, for the embedded loading of perl files to work, F<staticperl> |
|
|
679 | overrides the C<@INC> array. |
|
|
680 | |
|
|
681 | =over 4 |
|
|
682 | |
|
|
683 | =item $file = staticperl::find $path |
|
|
684 | |
|
|
685 | Returns the data associated with the given C<$path> |
|
|
686 | (e.g. C<Digest/MD5.pm>, C<auto/POSIX/autosplit.ix>), which is basically |
|
|
687 | the UNIX path relative to the perl library directory. |
|
|
688 | |
|
|
689 | Returns C<undef> if the file isn't embedded. |
|
|
690 | |
|
|
691 | =item @paths = staticperl::list |
|
|
692 | |
|
|
693 | Returns the list of all paths embedded in this binary. |
|
|
694 | |
|
|
695 | =back |
|
|
696 | |
|
|
697 | =head1 FULLY STATIC BINARIES - BUILDROOT |
|
|
698 | |
|
|
699 | To make truly static (Linux-) libraries, you might want to have a look at |
|
|
700 | buildroot (L<http://buildroot.uclibc.org/>). |
|
|
701 | |
|
|
702 | Buildroot is primarily meant to set up a cross-compile environment (which |
|
|
703 | is not so useful as perl doesn't quite like cross compiles), but it can also compile |
|
|
704 | a chroot environment where you can use F<staticperl>. |
|
|
705 | |
|
|
706 | To do so, download buildroot, and enable "Build options => development |
|
|
707 | files in target filesystem" and optionally "Build options => gcc |
|
|
708 | optimization level (optimize for size)". At the time of writing, I had |
|
|
709 | good experiences with GCC 4.4.x but not GCC 4.5. |
|
|
710 | |
|
|
711 | To minimise code size, I used C<-pipe -ffunction-sections -fdata-sections |
|
|
712 | -finline-limit=8 -fno-builtin-strlen -mtune=i386>. The C<-mtune=i386> |
|
|
713 | doesn't decrease codesize much, but it makes the file much more |
|
|
714 | compressible. |
|
|
715 | |
|
|
716 | If you don't need Coro or threads, you can go with "linuxthreads.old" (or |
|
|
717 | no thread support). For Coro, it is highly recommended to switch to a |
|
|
718 | uClibc newer than 0.9.31 (at the time of this writing, I used the 20101201 |
|
|
719 | snapshot) and enable NPTL, otherwise Coro needs to be configured with the |
|
|
720 | ultra-slow pthreads backend to work around linuxthreads bugs (it also uses |
|
|
721 | twice the address space needed for stacks). |
|
|
722 | |
|
|
723 | If you use C<linuxthreads.old>, then you should also be aware that |
|
|
724 | uClibc shares C<errno> between all threads when statically linking. See |
|
|
725 | L<http://lists.uclibc.org/pipermail/uclibc/2010-June/044157.html> for a |
|
|
726 | workaround (And L<https://bugs.uclibc.org/2089> for discussion). |
|
|
727 | |
|
|
728 | C<ccache> support is also recommended, especially if you want |
|
|
729 | to play around with buildroot options. Enabling the C<miniperl> |
|
|
730 | package will probably enable all options required for a successful |
|
|
731 | perl build. F<staticperl> itself additionally needs either C<wget> |
|
|
732 | (recommended, for CPAN) or C<curl>. |
|
|
733 | |
|
|
734 | As for shells, busybox should provide all that is needed, but the default |
|
|
735 | busybox configuration doesn't include F<comm> which is needed by perl - |
|
|
736 | either make a custom busybox config, or compile coreutils. |
|
|
737 | |
|
|
738 | For the latter route, you might find that bash has some bugs that keep |
|
|
739 | it from working properly in a chroot - either use dash (and link it to |
|
|
740 | F</bin/sh> inside the chroot) or link busybox to F</bin/sh>, using it's |
|
|
741 | built-in ash shell. |
|
|
742 | |
|
|
743 | Finally, you need F</dev/null> inside the chroot for many scripts to work |
|
|
744 | - F<cp /dev/null output/target/dev> or bind-mounting your F</dev> will |
|
|
745 | both provide this. |
|
|
746 | |
|
|
747 | After you have compiled and set up your buildroot target, you can copy |
|
|
748 | F<staticperl> from the C<App::Staticperl> distribution or from your |
|
|
749 | perl f<bin> directory (if you installed it) into the F<output/target> |
|
|
750 | filesystem, chroot inside and run it. |
|
|
751 | |
553 | =head1 AUTHOR |
752 | =head1 AUTHOR |
554 | |
753 | |
555 | Marc Lehmann <schmorp@schmorp.de> |
754 | Marc Lehmann <schmorp@schmorp.de> |
556 | http://software.schmorp.de/pkg/staticperl.html |
755 | http://software.schmorp.de/pkg/staticperl.html |