=head1 NAME staticperl - perl, libc, 50 modules all in one 500kb file =head1 SYNOPSIS staticperl help # print the embedded documentation staticperl fetch # fetch and unpack perl sources staticperl configure # fetch and then configure perl staticperl build # configure and then build perl staticperl install # build and then install perl staticperl clean # clean most intermediate files (restart at configure) staticperl distclean # delete everything installed by this script staticperl cpan # invoke CPAN shell staticperl instmod path... # install unpacked modules staticperl instcpan modulename... # install modules from CPAN staticperl mkbundle # see documentation staticperl mkperl # see documentation Typical Examples: staticperl install # fetch, configure, build and install perl staticperl cpan # run interactive cpan shell staticperl mkperl -M '"Config_heavy.pl"' # build a perl that supports -V staticperl mkperl -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI -MURI::http # build a perl with the above modules linked in =head1 DESCRIPTION This script helps you creating single-file perl interpreters, or embedding a pelr interpreter in your apps. Single-file means that it is fully self-contained - no separate shared objects, no autoload fragments, no .pm or .pl files are needed. And when linking statically, you can create (or embed) a single file that contains perl interpreter, libc, all the modules you need and all the libraries you need. With uclibc and upx on x86, you can create a single 500kb binary that contains perl and 50 modules such as AnyEvent, EV, IO::AIO, Coro and so on. Or any other choice of modules. The created files do not need write access to the filesystem (like PAR does). In fact, since this script is in many ways similar to PAR::Packer, here are the differences: =over 4 =item * The generated executables are much smaller than PAR created ones. Shared objects and the perl binary contain a lot of extra info, while the static nature of F allows the linker to remove all functionality and meta-info not required by the final executable. Even extensions statically compiled into perl at build time will only be present in the final executable when needed. In addition, F can strip perl sources much more effectively than PAR. =item * The generated executables start much faster. There is no need to unpack files, or even to parse Zip archives (which is slow and memory-consuming business). =item * The generated executables don't need a writable filesystem. F loads all required files directly from memory. There is no need to unpack files into a temporary directory. =item * More control over included files. PAR tries to be maintainance and hassle-free - it tries to include more files than necessary to make sure everything works out of the box. The extra files (such as the unicode database) can take substantial amounts of memory and filesize. With F, the burden is mostly with the developer - only direct compile-time dependencies and L are handled automatically. This means the modules to include often need to be tweaked manually. =item * PAR works out of the box, F does not. Maintaining your own custom perl build can be a pain in the ass, and while F tries to make this easy, it still requires a custom perl build and possibly fiddling with some modules. PAR is likely to produce results faster. =back =head1 HOW DOES IT WORK? Simple: F downloads, compile and installs a perl version of your choice in F<~/.staticperl>. You can add extra modules either by letting F install them for you automatically, or by using CPAN and doing it interactively. This usually takes 5-10 minutes, depending on the speed of your computer and your internet conenction. It is possible to do program development at this stage, too. Afterwards, you create a list of files and modules you want to include, and then either build a new perl binary (that acts just like a normla perl except everything is compiled in), or you create bundle files (basically C sources you can use to embed all files into your project). This step is very fast (a few seconds if PPI is not used for stripping, more seconds otherwise, as PPI is very slow), and can be tweaked and repeated as often as necessary. =head1 THE F SCRIPT This module installs a script called F into your perl binary directory. The script is fully self-contained, and can be used without perl (for example, in an uClibc chroot environment). In fact, it can be extracted from the C distribution tarball as F, without any installation. F interprets the first argument as a command to execute, optionally followed by any parameters. There are two command categories: the "phase 1" commands which deal with installing perl and perl modules, and the "phase 2" commands, which deal with creating binaries and bundle files. =head2 PHASE 1 COMMANDS: INSTALLING PERL The most important command is F, which does basically everything. The default is to download and install perl 5.12.2 and a few modules required by F itself, but all this can (and should) be changed - see L, below. The command staticperl install Is normally all you need: It installs the perl interpreter in F<~/.staticperl/perl>. It downloads, configures, builds and installs the perl interpreter if required. Most of the following commands simply run one or more steps of this sequence. To force recompilation or reinstalaltion, you need to run F first. =over 4 =item F Runs only the download and unpack phase, unless this has already happened. =item F Configures the unpacked perl sources, potentially after downloading them first. =item F Builds the configured perl sources, potentially after automatically configuring them. =item F Wipes the perl installation directory (usually F<~/.staticperl/perl>) and installs the perl distribution, potentially aftering building it first. =item F [args...] Starts an interactive CPAN shell that you cna use to install further modules. Installs the perl first if neccessary, but apart from that, no magic is involved: you could just as well run it manually via F<~/.staticperl/perl/bin/cpan>. Any additional arguments are simply passed to the F command. =item F module... Tries to install all the modules given and their dependencies, using CPAN. Example: staticperl instcpan EV AnyEvent::HTTPD Coro =item F directory... In the unlikely case that you have unpacked perl modules around and want to install from these instead of from CPAN, you cna do this using this command by specifying all the directories with modules in them that you want to have built. =item F Runs F in the perl source directory (and potentially cleans up other intermediate files). This can be used to clean up intermediate files without removing the installed perl interpreter. =item F This wipes your complete F<~/.staticperl> directory. Be careful with this, it nukes your perl download, perl sources, perl distribution and any installed modules. It is useful if you wish to start over "from scratch" or when you want to uninstall F. =back =head2 PHASE 2 COMMANDS: BUILDING PERL BUNDLES Building (linking) a new F binary is handled by a separate script. To make it easy to use F from a F, the script is embedded into F, which will write it out and call for you with any arguments you pass: staticperl mkbundle mkbundle-args... In the oh so unlikely case of something not working here, you can run the script manually as well (by default it is written to F<~/.staticperl/mkbundle>). F is a more conventional command and expect the argument syntax commonly used on unix clones. For example, this command builds a new F binary and includes F (for F), F, F and a custom F script (from F in this distribution): # first make sure we have perl and the required modules staticperl instcpan AnyEvent::HTTPD # now build the perl staticperl mkperl -M'"Config_heavy.pl"' -MAnyEvent::Impl::Perl \ -MAnyEvent::HTTPD -MURI::http \ --add 'eg/httpd httpd.pm' # finally, invoke it ./perl -Mhttpd As you can see, things are not quite as trivial: the L module has a hidden dependency which is not even a perl module (F), L needs at least one event loop backend that we have to specifymanually (here L), and the F module (required by L) implements various URI schemes as extra modules - since L only needs C URIs, we only need to include that module. =head3 OPTION PROCESSING All options can be given as arguments on the commandline (typically using long (e.g. C<--verbose>) or short option (e.g. C<-v>) style). Since specifying a lot of modules can make the commandlien very cumbersome, you can put all long options into a "bundle specification file" (with or without C<--> prefix) and specify this bundle file instead. For example, the command given earlier could also look like this: staticperl mkperl httpd.bundle And all options could be in F: use "Config_heavy.pl" use AnyEvent::Impl::Perl use AnyEvent::HTTPD use URI::http add eg/httpd httpd.pm All options that specify modules or files to be added are processed in the order given on the commandline (that affects the C<--use> and C<--eval> options at the moment). =head3 MKBUNDLE OPTIONS =over 4 =item --verbose | -v Increases the verbosity level by one (the default is C<1>). =item --quiet | -q Decreases the verbosity level by one. =item --strip none|pod|ppi Specify the stripping method applied to reduce the file of the perl sources included. The default is C, which uses the L module to remove all pod documenatiton, which is very fast and reduces filesize a lot. The C method uses L to parse and condense the perl sources. This saves a lot more than just L, and is generally safer, but is also a lot slower, so is best used for production builds. Last not least, in the unlikely case where C is too slow, or some module gets mistreated, you can specify C to not mangle included perl sources in any way. =item --perl After writing out the bundle files, try to link a new perl interpreter. It will be called F and will be left in the current working directory. The bundle files will be removed. This switch is automatically ued when F is invoked with the C command (instead of C): # build a new ./perl with only common::sense in it - very small :) staticperl mkperl -Mcommon::sense =item --use module | -Mmodule Include the named module and all direct dependencies. This is done by C'ing the module in a subprocess and tracing which other modules and files it actually loads. If the module uses L, then all splitfiles will be included as well. Example: include AnyEvent and AnyEvent::Impl::Perl. staticperl mkbundle --use AnyEvent --use AnyEvent::Impl::Perl Sometimes you want to load old-style "perl libraries" (F<.pl> files), or maybe other weirdly named files. To do that, you need to quote the name in single or double quoutes. When given on the commandline, you probably need to quote once more to avoid your shell interpreting it. Common cases that need this are F and F. Example: include the required files for F to work in all its glory (F is included automatically by this). # bourne shell staticperl mkbundle --use '"Config_heavy.pl"' # bundle specification file use "Config_heavy.pl" The C<-Mmodule> syntax is included as an alias that might be easier to remember than C. Or maybe it confuses people. Time will tell. Or maybe not. Argh. =item --eval "perl code" | -e "perl code" Sometimes it is easier (or necessary) to specify dependencies using perl code, or maybe one of the modules you use need a special use statement. In that case, you can use C to execute some perl snippet or set some variables or whatever you need. All files C'd or C'd in the script are included in the final bundle. Keep in mind that F will only C the modules named by the C<--use> option, so do not expect the symbols from modules you C<--use>'d earlier on the commandlien to be available. Example: force L to detect a backend and therefore include it in the final bundle. staticperl mkbundle --eval 'use AnyEvent; AnyEvent::detect' # or like this staticperl mkbundle -MAnyEvent --eval 'use AnyEvent; AnyEvent::detect' Example: use a separate "bootstrap" script that C's lots of modules and include this in the final bundle, to be executed automatically. staticperl mkbundle --eval 'do "bootstrap"' --boot bootstrap =item --boot filename Include the given file in the bundle and arrange for it to be executed (using a C) before anything else when the new perl is initialised. This can be used to modify C<@INC> or anything else before the perl interpreter executes scripts given on the commandline (or via C<-e>). This works even in an embedded interpreter. =item --add "file" | --add "file alias" Adds the given (perl) file into the bundle (and optionally call it "alias"). This is useful to include any custom files into the bundle. Example: embed the file F as F when creating the bundle. staticperl mkperl --add "httpd httpd.pm" It is also a great way to add any custom modules: # specification file add file1 myfiles/file1 add file2 myfiles/file2 add file3 myfiles/file3 =item --static When C<--perl> is also given, link statically instead of dynamically. The default is to link the new perl interpreter fully dynamic (that means all perl modules are linked statically, but all external libraries are still referenced dynamically). Keep in mind that Solaris doesn't support static linking at all, and systems based on GNU libc don't really support it in a usable fashion either. Try uClibc if you want to create fully statically linked executables, or try the C<--staticlibs> option to link only some libraries statically. =item any other argument Any other argument is interpreted as a bundle specification file, which supports most long options (without extra quoting), one option per line. =back =head2 F CONFIGURATION AND HOOKS During (each) startup, F tries to source the following shell files in order: /etc/staticperlrc ~/.staticperlrc $STATICPERL/rc They can be used to override shell variables, or define functions to be called at specific phases. Note that the last file is erased during F, so generally should not be used. =head3 CONFIGURATION VARIABLES =head4 Variables you I override =over 4 =item C The e-mail address of the person who built this binary. Has no good default, so should be specified by you. =back =head4 Variables you I to override =over 4 =item C The perl version to install - default is currently C<5.12.2>, but C<5.8.9> is also a good choice (5.8.9 is much smaller than 5.12.2, while 5.10.1 is about as big as 5.12.2). =item C The URL of the CPAN mirror to use (e.g. L). =item C, C, C, C These flags are passed to perl's F script, and are generally optimised for small size (at the cost of performance). Since they also contain subtle workarounds around various build issues, changing these usually requires understanding their default values - best look at the top of the F script for more info on these. =item C The directory where staticperl stores all its files (default: F<~/.staticperl>). =item C The prefix where perl get's installed (default: F<$STATICPERL/perl>), i.e. where the F and F subdirectories will end up. =item C, C, others Usually set to C<1> to make modules "less inquisitive" during their installation, you can set any environment variable you want - some modules (such as L or L) use environment variables for further tweaking. =item C Additional modules installed during F. Here you can set which modules you want have to installed from CPAN. Example: I really really need EV, AnyEvent, Coro and IO::AIO. EXTRA_MODULES="EV AnyEvent Coro IO::AIO" Note that you cna also use a C hook to achieve this, and more. =back =head4 Variables you I to override =over 4 =item C Where F writes the C command to (default: F<$STATICPERL/mkbundle>). =item C Additional modules needed by C - should therefore not be changed unless you know what you are doing. =back =head3 OVERRIDABLE HOOKS In addition to environment variables, it is possible to provide some shell functions that are called at specific times. To provide your own commands, justd efine the corresponding function. Example: install extra modules from CPAN and from some directories at F time. postinstall() { rm -rf lib/threads.* # weg mit Schaden instcpan IO::AIO EV instsrc ~/src/AnyEvent instsrc ~/src/XML-Sablotron-1.0100001 instcpan AnyEvent::HTTPD } =over 4 =item postconfigure Called after configuring, but before building perl. Current working directory is the perl source directory. Could be used to tailor/patch config.sh (followed by F<./Configure -S>) or do any other modifications. =item postbuild Called after building, but before installing perl. Current working directory is the perl source directory. I have no clue what this could be used for - tell me. =item postinstall Called after perl and any extra modules have been installed in C<$PREFIX>, but before setting the "installation O.K." flag. The current working directory is C<$PREFIX>, but maybe you should not rely on that. This hook is most useful to customise the installation, by deleting files, or installing extra modules using the C or C functions. The script must return with a zero exit status, or the installation will fail. =back =head1 AUTHOR Marc Lehmann http://software.schmorp.de/pkg/staticperl.html