#!/bin/sh #| cl-launch.sh -- shell wrapper for Common Lisp -*- Lisp -*- CL_LAUNCH_VERSION='4.1.5' license_information () { AUTHOR_NOTE="\ # Please send your improvements to the author: # fare at tunes dot org < http://www.cliki.net/Fare%20Rideau >. " SHORT_LICENSE="\ # cl-launch is available under the terms of the bugroff license. # http://tunes.org/legalese/bugroff.html # You may at your leisure use the MIT license instead < http://opensource.org/licenses/MIT > " WEB_SITE="# For the latest version of cl-launch, see its web page at: # http://www.cliki.net/cl-launch " LICENSE_COMMENT="\ # This software can be used in conjunction with any other software: # the result may consist in pieces of the two software glued together in # a same file, but even then these pieces remain well distinguished, and are # each available under its own copyright and licensing terms, as applicable. # The parts that come from the other software are subject to the terms of use # and distribution relative to said software, which may well be # more restrictive than the terms of this software (according to lawyers # and the armed henchmen they got the taxpayers to pay to enforce their laws). # The bits of code generated by cl-launch, however, remain available # under the terms of their own license, and you may service them as you wish: # manually, using cl-launch --update or whichever means you prefer. # That said, if you believe in any of that intellectual property scam, # you may be subject to the terms of my End-Seller License: # http://www.livejournal.com/users/fare/21806.html " DISCLAIMER="\ # This file was automatically generated and contains parts of CL-Launch " } license_information ### Settings for the current installation -- adjust to your convenience ### Or see documentation for using commands -B install and -B install_bin. DEFAULT_LISPS="sbcl ccl clisp abcl allegro lispworks scl cmucl ecl mkcl gcl xcl" DEFAULT_INCLUDE_PATH= DEFAULT_USE_CL_LAUNCHRC= DEFAULT_USE_CLBUILD= DEFAULT_USE_QUICKLISP= ### Initialize cl-launch variables unset \ SOFTWARE_BUILD_FORMS SOFTWARE_FINAL_FORMS SOFTWARE_INIT_FORMS \ SOURCE_REGISTRY INCLUDE_PATH LISPS WRAPPER_CODE \ OUTPUT_FILE UPDATE LISP_CONTENT \ LINE LINE1 LINE2 NO_QUIT \ TRIED_CONFIGURATION HAS_CONFIGURATION \ EXEC_LISP DO_LISP DUMP LOAD_IMAGE RESTART RESTART_PACKAGE IMAGE IMAGE_OPT \ EXTRA_CONFIG_VARIABLES \ EXECUTABLE_IMAGE STANDALONE_EXECUTABLE CL_LAUNCH_STANDALONE \ CL_LAUNCH_FILE __CL_ARGV0 IS_X_SCRIPT DISPATCH_ENTRY_P \ TEST_SHELLS TORIG IMPL LISPS="$DEFAULT_LISPS" INCLUDE_PATH="$DEFAULT_INCLUDE_PATH" USE_CL_LAUNCHRC="$DEFAULT_USE_CL_LAUNCHRC" USE_CLBUILD="$DEFAULT_USE_CLBUILD" USE_QUICKLISP="$DEFAULT_USE_QUICKLISP" UNREAD_DEPTH=0 OUTPUT_FILE="!" PACKAGE=cl-user RESTART_PACKAGE= INIT_PACKAGE= FINAL_PACKAGE= ### Other constants MAGIC_MD5SUM="65bcc57c2179aad145614ec328ce5ba8" # Greenspun's Tenth Rule... CONTENT_DISCLAIMER="\ ;;; THE SOFTWARE AFTER THIS MARKER AND TO THE END OF THE FILE IS NOT PART OF ;;; CL-LAUNCH BUT A PIECE OF SOFTWARE DISTINCT FROM CL-LAUNCH. IT IS OWNED BY ;;; BY ITS OWNERS AND IS SUBJECT ITS OWN INDEPENDENT TERMS OF AVAILABILITY." CONTENT_BEGIN_MARKER="\ ;;; ${MAGIC_MD5SUM} SOFTWARE WRAPPED BY CL-LAUNCH BEGINS HERE:" ### Help ## setup a few environment variables for the program BASIC_ENV_CODE='PROG="$0"' eval "$BASIC_ENV_CODE" PROGBASE="${0##*/}" # "$(basename "$0")" CL_LAUNCH_URL="http://fare.tunes.org/files/cl-launch/cl-launch.sh" HELP_HEADER="cl-launch.sh $CL_LAUNCH_VERSION" print_help_header () { ECHO "$HELP_HEADER" i=0 ; while [ "$i" -lt "${#HELP_HEADER}" ] ; do ECHOn "=" ; i="$[$i+1]" done ; ECHO } print_help () { cat < Note: if this help is too long for you, you may scroll back, or use: $PROGBASE --more-help | less EOF } print_more_help () { cat< si.lisp lispworks-7-0-0-x86-linux -siteinit - -init - -build si.lisp There is no standard name for a console-only variant of LispWorks; older versions of cl-launch assume a default \`lispworks\`; since cl-launch 4.1.2.1, \`lispworks-console\` is assumed instead, to avoid conflicts. You can control the name you use with the shell variable \`\$LISPWORKS\`, or you can just leave \`lispworks-console\` in your path, and use a symlink, copy, shell alias or trivial wrapper script to enable your favorite shorter name \`lispworks\`, \`lw\`, \`lwcon\`, \`lw-console\`, etc. Similarly, a mlisp image for allegro can be created as follows: alisp -e '(progn (build-lisp-image "sys:mlisp.dxl" :case-mode :case-sensitive-lower :include-ide nil :restart-app-function nil) (when (probe-file "sys:mlisp") (delete-file "sys:mlisp")) (sys:copy-file "sys:alisp" "sys:mlisp"))' Additionally, \`cl-launch\` supports the use of \`clbuild\` as a wrapper to invoke the Lisp implementation, with the \`--clbuild\` option. Supported shells ---------------- \`cl-launch\` was tested with all of \`posh\` 0.4.7, \`bash\` 2.05, \`bash\` 3.1, \`zsh\` 4.3.2, \`dash\` 0.5.3 and \`busybox\` 1.01 \`ash\`. Lisp implementation invocation ------------------------------ When a \`cl-launch\` generated script is invoked, the \`cl-launch\` shell wrapper will try to execute the Lisp code with the first Common Lisp implementation it finds in a given list, which can be specified through option \`--lisp\`. The runtime behaviour of the \`cl-launch\` shell wrapper is very configurable through a series of environment variables. These variables can be controlled by the user by exporting them in his environment, or they can be restricted at the time of script generation by using cl-launch option \`--wrap\`. If variable \`LISP\` is defined, the shell wrapper will first try the implementation named by variable \`LISP\`. If that fails, it will try the list of implementations provided at script generation time. The list of implementations generated will be the argument to option \`--lisp\` if specified. Otherwise, \`cl-launch\` will supply its default value. This default value for the current instance of \`cl-launch\` is: $DEFAULT_LISPS This \`LISP\` selection only happens at system preparation time. If you dump an image then the script will always use the Lisp implementation for which an image was dumped. If you don't then the user may override the implementation. Note that these are nicknames built into the \`cl-launch\` shell wrapper, and not necessarily names of actual binary. You may control the mapping of implementation nickname to actual binary pathname to call with an environment variable. For a given implementation nickname, the environment variable will be the capitalization of the given nickname. Hence, variable \`\$SBCL\` controls where to look for the \`sbcl\` implementation, and variable \`\$CMUCL\` controls where to look for the \`cmucl\` implementation. If a binary is found with a matching pathname (using the standard unix \`\$PATH\` as required), then said implementation will be used, using proper command line options, that may be overridden with an environment variable similar to the previous but with \`_OPTIONS\` appended to its name. Hence, \`\$CMUCL_OPTIONS\` for \`cmucl\`, \`\$CLISP_OPTIONS\` for \`clisp\`, etc. Sensible defaults are provided for each implementation, so as to execute the software in non-interactive mode, with debugger disabled, without reading user-specific configuration files, etc. If you want to insist on using a given implementation with given options, you may use option \`--lisp\` and \`--wrap\`, as follows: --lisp 'sbcl clisp' --wrap ' LISP= # do not allow the user to specify his implementation SBCL=/usr/bin/sbcl # not any experimental thing by the user SBCL_OPTIONS="--noinform --sysinit /dev/null --userinit /dev/null \\ --disable-debugger" # predictable Lisp state CLISP=/usr/bin/clisp # fall back on machines that lack SBCL CLISP_OPTIONS=" -norc --quiet --quiet" # configure ASDF: CL_SOURCE_REGISTRY=/usr/local/share/common-lisp/source//: # assuming precompiled fasls there: ASDF_OUTPUT_TRANSLATIONS=/my/cl/src:/my/fasl/cache: ' If you dump an image, you need not unset the \`LISP\` variable, but you might still want to override any user-specified \`SBCL\` and \`SBCL_OPTIONS\` (or corresponding variables for your selected implementation) from what the user may specify. Note that you can use option \`--wrap "\$(cat your_script)"\` to embed into your program a full fledged script from a file. Your script may do arbitrary computations before the shell wrapper is run. It may make some consistency checks and abort before to run Lisp. Or it may analyze invocation arguments and make according adjustments to Lisp implementation options. This can be useful for setting options that cannot be set from the Lisp code, such the path to a runtime image, interactive or non-interactive execution, size of heaps, locale settings for source file encoding, etc. Reading the source code of \`cl-launch\` can be completely crazy. You may have great fun understanding why things are how they are and adding features without breaking anything! However, adding support for a new CL implementation should be straightforward enough: just search the sources for \`clisp\` or \`sbcl\` and mimic what I did for them. Be sure to send me what will get your favorite Lisp flavor of the month rolling. Limited clbuild support ----------------------- \`cl-launch\` 2.12 and later support using \`clbuild\` as a wrapper to configure your Lisp implementation, with option \`--clbuild\` (which can be disabled with option \`--no-clbuild\` if it was enabled by default in your \`cl-launch\` installation). Note that when you use \`clbuild\`, you can no longer override implementation options with say \`SBCL_OPTIONS\`, as clbuild takes care of the options for you. Any implementation banner will not be removed unless you instruct clbuild to do so. Also, you cannot use clbuild with a non-executable image different from \`clbuild\`'s, which precludes image dumping with \`cmucl\` or \`allegro\` (\`allegro\` could probably be updated, but I don't have a recent licence to test and develop). \`clbuild\` support is not fully tested at this point. Please report any bug. Simple cl-launch scripts ------------------------ In simple cases, you may create a Common Lisp shell script with \`cl-launch\` without a script generation step, just because you'll spend a lot of time editing the script and distributing it, and little time waiting for script startup time anyway. This notably is a good idea if you're not spawning many instances of the same version of a script on a given computer. If that's what you want, you may use \`cl-launch\` as a script interpret the following way (stripping leading spaces): #!/path/to/cl-launch ...options... For instance, you may write the following script (stripping leading spaces): #!/usr/bin/cl --entry main (defun main (argv) (format t "Hello, World!~%~S~%" argv)) On a recent Linux kernel, the options may include spaces, parentheses, etc., provided they are quoted as in a shell script. Also, using \`-X\` as your very first option and \`--\` as your last will ensure that the script works even if its name starts with a \`(\` or a \`-\`, in addition to working with older versions of \`cl-launch\`. Note however that Darwin (MacOS X) and other BSD kernels or old Linux kernels don't like the \`#!\` interpreter to itself be interpreted. On these operating system kernels, the system administrator must compile and install a small shim written in C, \`cl-shim.c\`, that will handle the proper script invocation. Most kernels have restrictions on how they handle arguments to a \`#!\` script, that prevent e.g. using \`/usr/bin/env\` as a trampoline; however, you may use the fully portable solution as follows, where the \`":" ;\` ensures that the script should remain valid bilingual shell and Lisp code: #!/bin/sh ":" ; exec cl-launch -X -sp my-package -E main -- "\$0" \${1+"\$@"} || exit (Actually \`"\$@"\` instead of \`\${1+"\$@"}\` should work just fine, unless you have an antique shell.) Note that if you don't need Lisp code to be loaded from your script, with everything happening in the build specification, then you may instead use a simple \`#!/bin/sh\` shell script from which you: exec /path/to/cl-launch -x ... -- "\$@". Also, in case you can't rely on \`cl-launch\` being at a fixed path, or if your shell and/or kernel combination doesn't support using \`cl-launch\` as a script interpreter, then you may instead start your script with the following lines: #!/bin/sh ":" ; exec cl-launch -X -- "\$0" "\$@" || exit (format t "It works!~%") Note that a mainline Linux kernel only supports the recursive \`#!\` implicit in \`#!/usr/bin/cl-launch\` since 2.6.27.9. Dumping images -------------- You can dump an image (for static compilation and fast startup) with option \`--dump IMAGE\` where \`IMAGE\` specifies the path where the image will be dumped. If you use option \`--include PATH\` then the image will be loaded back from that specified directory instead of the directory where you dumped it. This is useful if you're preparing a script to be installed at another place maybe on another computer. This option is currently supported on all CL implementations available with \`cl-launch\`. As a limitation, \`LispWorks\` will print a banner on standard output, unless you use the standalone executable option below. As another limitation, \`ECL\` will not be able to dump an image when running from a previously dumped image (with \`--image\`). This is because of the link model of ECL, whereby you'd need to be able to locate which object files were used in linking the original image, keep track of these files, and prepend the list of them to to the object files linked into the dump. This is not conceptually impossible and patches are welcome. However, we hope to support that someday with a real build system that does it for you, such as XCVB. Standalone executables ---------------------- You can create standalone executables with the option \`--dump '!'\` (or by giving a \`--dump\` argument identical to the \`--output\` argument). This option is currently only supported with \`SBCL\`, \`ECL\`, \`CLISP\`, \`CMUCL\`, \`CCL\` and \`LispWorks\` Professional. Moreover \`CLISP\` has the issues below. \`CLISP\` standalone executables will react magically if invoked with options such as \`--clisp-help\` or \`--clisp-x '(sys::main-loop)'\`. That's a pretty far-fetched thing to hit by mistake, and the \`CLISP\` maintainers consider it a feature (I don't). Don't use such executables as \`setuid\`, and don't let untrusted users control arguments given to such executables that are run with extra privileges. cl-launch runtime API --------------------- \`cl-launch\` provides the following Lisp functions: Function \`cl-launch:compile-and-load-file\` takes as an argument a source pathname designator, and keyword arguments \`force-recompile\` (default \`NIL\`) and \`verbose\` (default \`NIL\`). It will arrange to compile the specified source file if it is explicitly requested, or if the file doesn't exist, or if the fasl is not up-to-date. It will compile and load with the specified verbosity. It will take use \`uiop:compile-file-pathname*\` to determine the fasl pathname. The following variables and functions previously provided by \`cl-launch\` have the following replacement from \`ASDF\` and \`UIOP\`: Variable \`cl-launch:*arguments*\` is replaced by \`uiop:*command-line-arguments*\`. Function \`cl-launch:getenv\` is replaced by \`uiop:getenv\`. Function \`cl-launch:load-system\` is replaced by \`asdf:load-system\`. Function \`cl-launch:quit\` is replaced by \`uiop:quit\` (beware: the lambda-list is slightly different). Additionally, environment variables \`CL_LAUNCH_PID\` and \`CL_LAUNCH_FILE\` will be set to the process ID and the script invocation filename respectively. Verbose output mode ------------------- If the shell variable \`CL_LAUNCH_VERBOSE\` is exported and non-\`nil\`, then \`cl-launch\` and the scripts it generates will produce an abundance of output, display such things as the Lisp invocation command, compiling and loading files with \`:verbose t\` and \`:print t\`, etc. This is only useful for debugging \`cl-launch\` and/or your build process. Option \`--verbose\` sets this variable, whereas option \`--quiet\` resets it. Makefile examples ----------------- ### Automatically download of the current version of cl-launch if not present cl-launch.sh: wget -O cl-launch.sh ${CL_LAUNCH_URL} chmod a+x cl-launch.sh ### Making a shell script executable from a simple Lisp file named foo.lisp foo.sh: cl-launch.sh foo.lisp ./cl-launch.sh --output foo.sh --file foo.lisp ### A more complex example using all options. run-foo.sh: cl-launch.sh preamble.lisp ./cl-launch.sh --output run-foo.sh \\ --file preamble.lisp --system foo \\ --init "(foo:main uiop:*command-line-arguments*)" \\ --source-registry \${PREFIX}/cl-foo/systems: \\ --lisp "ccl sbcl" --wrap 'SBCL=/usr/local/bin/sbcl-no-unicode' \\ --no-include ### An example with horrible nested makefile, shell and Lisp quoting hello: opera=wORlD ; ./cl-launch.sh --execute --init \\ "(format t \\"~25R~A~A~%\\" 6873049 #\\\\space '\$\$opera)" Caveat Lispor ------------- \`cl-launch\` begins evaluation of your Lisp software in the \`cl-user\` package, or whichever package you specify. By the time your initialization forms are evaluated, the package may or may not have changed, depending on the fine-grained semantics of \`load\`. Be sure to use \`in-package\` if these things matter. If you change the readtable, even weirder things may happen. There are lots of ways of making mistakes by improperly quoting things when you write shell commands. \`cl-launch\` does the right thing, but you still must be careful with the nested quoting mechanisms of \`make\`, shell, and Lisp. Here is a simple example use of cl-launch to quickly compare the result of a same computation on a variety of systems: for l in sbcl cmucl clisp gcl ccl ; do ./cl-launch.sh --lisp \$l --execute --init \\ '(format t "'\$l' ~A~%" most-positive-fixnum)' ; done Internally, \`cl-launch\` includes many self-test functions. You may for instance try (from a directory where it may create junk): ./cl-launch.sh -l 'sbcl cmucl clisp gclcvs' -B tests Share and Enjoy! EOF } show_help () { print_help_header echo print_help echo print_help_footer exit "${1:-0}" } show_more_help () { print_help_header echo print_help echo print_more_help echo print_help_footer exit "${1:-0}" } error_help () { show_help "${1:-2}" >& 2 } show_version () { echo "cl-launch ${CL_LAUNCH_VERSION} Supported implementations: sbcl, cmucl (lisp), clisp, ecl, ccl (openmcl), abcl, mkcl, xcl, gcl (gclcvs), allegro (alisp), lispworks, scl Local defaults for generated scripts: will search in this order these supported implementations: ${DEFAULT_LISPS}" if [ -z "$DEFAULT_INCLUDE_PATH" ] ; then echo "\ will generate self-contained scripts using option --no-include by default" else echo "\ will generate scripts by default with runtime dependencies using option --include ${DEFAULT_INCLUDE_PATH}" fi if [ -n "$DEFAULT_USE_CL_LAUNCHRC" ] ; then echo "\ will use /etc/cl-launchrc and ~/.cl-launchrc by default" else echo "\ will not use /etc/cl-launchrc and ~/.cl-launchrc by default" fi if [ -z "$DEFAULT_USE_CLBUILD" ] ; then echo "\ will generate scripts that do not use clbuild by default" else echo "\ will generate scripts that use clbuild by default" fi if [ -z "$DEFAULT_USE_QUICKLISP" ] ; then echo "\ will generate scripts that do not use quicklisp by default" else echo "\ will generate scripts that use quicklisp by default" fi echo exit } ### Generic debugging library excerpted from ~fare/etc/zsh/fun.* print_basic_functions () { cat <<'EOF' ECHOn () { printf '%s' "$*" ;} simple_term_p () { case "$1" in *[!a-zA-Z0-9-+_,.:=%/]*) return 1 ;; *) return 0 ;; esac } if [ -n "$BASH_VERSION$ZSH_VERSION" ] ; then kwote0 () { a="${1//\\/\\\\}" ; a="${a//\`/\\\`}" ; a="${a//\$/\\\$}" ; printf %s "${a//\"/\\\"}" ;} else kwote0 () { ECHOn "$1" | sed -e "s/\([\\\\\"\$\`]\)/\\\\\\1/g" ;} fi kwote () { if simple_term_p "$1" ; then ECHOn "$1" ; else kwote0 "$1" ; fi ;} load_form_0 () { echo "(cl:load $1 :verbose nil :print nil)" ;} load_form () { load_form_0 "\"$(kwote "$1")\"" ;} ECHO () { printf '%s\n' "$*" ;} DBG () { ECHO "$*" >& 2 ;} abort () { ERR="$1" ; shift ; DBG "$*" ; exit "$ERR" ;} ABORT () { abort 42 "$*" ;} EOF } eval "$(print_basic_functions)" kwote1 () { if simple_term_p "$1" ; then ECHOn "$1" else ECHOn "\"$(kwote0 "$1")\"" ; fi ;} SHOW () { ( set +x k="" ; for i ; do ECHOn "$k" ; kwote1 "$i" ; k=" " ; done ; echo ) } XDO () { SHOW "$@" >&2 ; "$@" ;} DO () { SHOW "$@" ; "$@" ;} EVAL () { ECHO "$*" ; eval "$*" ;} fullpath () { # If we were sure readlink is here, we could: for i ; do readlink -f "$i" ; done for i ; do case "$i" in /*) ECHO "$i" ;; *) ECHO "$PWD/$i" ;; esac ; done } print_var () { for var ; do eval "ECHO \"$var=\$(kwote1 \"\${$var}\")\"" ; done ;} create_file () { MOD="$1" OUT="$2" ; shift 2; TMPFILE="$OUT.tmp$$~" if "${@:-cat}" > "$TMPFILE" && chmod "$MOD" "$TMPFILE" && mv -f "$TMPFILE" "$OUT" ; then return 0 ; else rm -f "$TMPFILE" ; return 1 ; fi } ### Process options OPTION () { process_options "$@" ;} process_options () { while [ $# -gt 0 ] ; do x="$1" ; shift case "$x" in -h|"-?"|--help) show_help ;; -H|--more-help) show_more_help ;; -V|--version) show_version ;; -v|--verbose) export CL_LAUNCH_VERBOSE=t ;; -q|--quiet) unset CL_LAUNCH_VERBOSE ;; -e|--eval) add_build_form "(:eval \"(cl:in-package :$PACKAGE)$(kwote "$1")\")" ; shift ;; -L|--load) add_build_form "(:load \"$(kwote "$1")\" :$PACKAGE)" ; shift ;; -f|--file) add_build_form "(:load t :$PACKAGE)" ; set_lisp_content "$1" ; shift ;; -s|--system|--load-system) add_build_form "(:load-system \"$(kwote "$1")\")" ; shift ;; --require) add_build_form "(:require \"$(kwote "$1")\")" ; shift ;; -F|--final) add_final_form "$1" ; shift ;; -i|--init) add_init_form "$1" ; shift ;; -ip|--print) add_init_form "(princ(progn $1))(terpri)" ; shift ;; -iw|--write) add_init_form "(write(progn $1))(terpri)" ; shift ;; -p|--package) in_package "$1" ; shift ;; -sp|--system-package) add_build_form "(:load-system \"$1\")" in_package "$1" ; shift ;; "("*) add_init_form "(princ(progn $x))(terpri)" ;; -p|-pc|+p) ABORT "option $x is not supported anymore." \ "Use option -S instead." ;; --path|--path-current|--no-path) ABORT "option $x is not supported anymore." \ "Use option --source-registry instead." ;; -S|--source-registry) SOURCE_REGISTRY="$1" ; shift ;; -l|--lisp) LISPS="$1" ; shift ;; -w|--wrap) WRAPPER_CODE="$1" ; shift ;; -I|--include) INCLUDE_PATH="$1" ; shift ;; +I|--no-include) INCLUDE_PATH="" ;; -R|--rc) USE_CL_LAUNCHRC=t ;; +R|--no-rc) USE_CL_LAUNCHRC= ;; -b|--clbuild) USE_CLBUILD=t ;; +b|--no-clbuild) USE_CLBUILD= ;; -Q|--quicklisp) USE_QUICKLISP=t ;; +Q|--no-quicklisp) USE_QUICKLISP= ;; -o|--output) OUTPUT_FILE="$1" ; shift ;; -x|--execute) OUTPUT_FILE="!" ;; -u|--update) UPDATE="$1" ; shift ;; -m|--image) LOAD_IMAGE="$1" ; shift ;; -d|--dump) DUMP="$1" ; shift ;; -r|--restart) set_restart "$1" ; shift ;; -E|--entry) set_restart "(lambda()($1 uiop:*command-line-arguments*))" ; shift ;; -DE|--dispatch-entry) add_dispatch_entry "$1" "$PACKAGE" shift ;; -sm|--system-main) add_build_form "(:load-system \"$(kwote "$1")\")" set_entry "$1::main" ; shift ;; -Ds|--dispatch-system) sys="$(kwote "$1")" add_build_form "(:load-system \"$sys\")" add_dispatch_entry "$(basename $sys)" "$sys" ; shift ;; -B|--backdoor) "$@" ; exit ;; --) if [ -n "${IS_X_SCRIPT}" ] ; then add_build_form "(:load \"$(kwote "$1")\" :cl-user)" export __CL_ARGV0="$1" shift fi if [ "x${OUTPUT_FILE}" = "x!" ] ; then do_it "$@" else ABORT "Extra arguments given but not in --execute mode" fi ;; -X) OPTION -x IS_X_SCRIPT=y ;; -X' '*) # DBG "Working around sh script script limitation..." # The below gets the script arguments from the kernel-given argument: # OPTS="$x" ; eval "OPTION $OPTS \"\$@\"" # Unix kernels traditionally lump everything after the interpreter name # from the #! line up to 127 characters into one (optional) argument, # as defined in linux/{fs/binfmt_script.c,include/linux/binfmts.h}. # If we want to allow for a longer in-script command line argument, # and we do if we want to accommodate for inline Lisp code using -i # then we'd need to go fetch the full line and parse it. Here it is: OPTS="$(get_hashbang_arguments "$1")" eval "OPTION $OPTS \"\$@\"" ABORT "The cl-launch script $1 failed to use -X ... --" ;; -*) # Directly handle arguments in a #! script if [ -f "$1" ] ; then OPTS="$(get_hashbang_arguments "$1")" OPTx="$(stringbefore "${#x}" "$OPTS")" if [ "x$x" = "x$OPTx" ] ; then export __CL_ARGV0="$2" eval "OPTION $OPTS \"\$@\"" fi fi DBG "Invalid command line argument '$x'" ; mini_help_abort ;; *) unset IS_X_SCRIPT export __CL_ARGV0="$x" OPTION --load "$x" --execute -- "$@" ;; esac done } in_package () { PACKAGE="$1" } set_restart () { RESTART="$1" RESTART_PACKAGE="$PACKAGE" } set_entry () { set_restart "(cl:lambda()($1 uiop:*command-line-arguments*))" } add_dispatch_entry () { if [ -z "$DISPATCH_ENTRY_P" ] ; then add_build_form "(:load-system \"cl-launch/dispatch\")" DISPATCH_ENTRY_P=t fi if [ -z "$RESTART" ] ; then set_entry "cl-launch/dispatch:dispatch-entry" fi add_build_form "(:eval \"$(kwote "(cl-launch/dispatch:register-name/entry \"$(kwote "$1")\" :$2)")\")" } add_build_form () { SOFTWARE_BUILD_FORMS="$SOFTWARE_BUILD_FORMS${SOFTWARE_BUILD_FORMS+ }$1" } add_init_form () { if ! [ "${INIT_PACKAGE}" = "$PACKAGE" ] ; then package_form="(cl:in-package :$PACKAGE)" INIT_PACKAGE="$PACKAGE" else package_form="" fi SOFTWARE_INIT_FORMS="$SOFTWARE_INIT_FORMS${SOFTWARE_INIT_FORMS+ }${package_form}$1" } add_final_form () { if ! [ "${FINAL_PACKAGE}" = "$PACKAGE" ] ; then package_form="(cl:in-package :$PACKAGE)" FINAL_PACKAGE="$PACKAGE" else package_form="" fi SOFTWARE_FINAL_FORMS="$SOFTWARE_FINAL_FORMS${SOFTWARE_FINAL_FORMS+ }${package_form}$1" } set_lisp_content () { if [ -z "$1" ] ; then ABORT "Empty argument after -f --file" elif [ -n "$LISP_CONTENT" ] ; then ABORT "You may only use option -f --file once" else LISP_CONTENT="$1" fi } if [ -n "$BASH_VERSION$ZSH_VERSION" ] ; then stringbefore () { ECHOn "${2:0:$1}" ;} stringafter () { ECHOn "${2:$1}" ;} else stringbefore () { ECHOn "$2" | cut -c-"$1" ;} stringafter () { ECHOn "$2" | cut -c"$1"- ;} fi get_hashbang_arguments () { stringafter 2 "$(read LINE < "$1" ; ECHOn "$LINE")" | { read INTERP ARGUMENTS ; ECHOn "$ARGUMENTS" ;} } mini_help_abort () { DBG "$HELP_HEADER For help, invoke script with help argument: $PROG -h" ; ABORT } ### Do the job guess_defaults () { if [ -n "$UPDATE" ] ; then : "${OUTPUT_FILE:=$UPDATE}" fi } # Configuration system_preferred_lisps () { if [ "x${SOFTWARE_SYSTEM}" = "x$1" ] ; then shift LISPS="$*" DBG "User configuration for system ${SOFTWARE_SYSTEM} overrides LISPS with $(kwote1 "$LISPS")" fi } try_resource_file () { if [ -f "$1" ] ; then . "$1" fi } try_resource_files () { if [ -n "$USE_CL_LAUNCHRC" ] ; then try_resource_file /etc/cl-launchrc try_resource_file "$HOME/.cl-launchrc" fi } print_configuration () { print_var \ LISP_CONTENT SOFTWARE_SYSTEM SOFTWARE_INIT_FORMS SOFTWARE_FINAL_FORMS \ SOURCE_REGISTRY INCLUDE_PATH LISPS WRAPPER_CODE \ DUMP RESTART IMAGE_BASE IMAGE_DIR IMAGE \ $EXTRA_CONFIG_VARIABLES } include_configuration () { if [ -n "$UPDATE" ] ; then extract_configuration ECHO "$CONFIGURATION" eval "$CONFIGURATION" else extract_configuration print_configuration fi } ensure_configuration () { extract_configuration if [ -n "$UPDATE" ] ; then eval "$CONFIGURATION" adjust_configuration fi } adjust_configuration () { : INCLUDE_PATH="$INCLUDE_PATH" LISP_CONTENT="$LISP_CONTENT" if [ -n "$INCLUDE_PATH" ] ; then AUTHOR_NOTE= SHORT_LICENSE= LICENSE_COMMENT= fi } include_license () { if [ -n "$DISCLAIMER" ] ; then l= for i in "$DISCLAIMER" "$AUTHOR_NOTE" "$SHORT_LICENSE" "$LICENSE_COMMENT" do l="$l$i${i:+# }" ; done ECHOn "$l" fi hide_license } hide_license () { DISCLAIMER= AUTHOR_NOTE= SHORT_LICENSE= LICENSE_COMMENT= CONTENT_DISCLAIMER= } include_lisp_header () { if [ -z "$INCLUDE_PATH" ] ; then print_lisp_header else echo "#-cl-launch" load_form "$INCLUDE_PATH/launcher.lisp" fi } LAUNCH_FUN=cl-launch::run print_lisp_launch () { ECHOn "($LAUNCH_FUN" if [ -n "${SOURCE_REGISTRY}" ] ; then ECHOn " :source-registry \"$(kwote "$SOURCE_REGISTRY")\"" fi if [ -n "${USE_QUICKLISP}" ] ; then ECHOn " :quicklisp t" fi if [ -n "${SOFTWARE_BUILD_FORMS}" ] ; then ECHOn " :build '(${SOFTWARE_BUILD_FORMS})" fi if [ -n "${SOFTWARE_FINAL_FORMS}" ] ; then ECHOn " :final \"$(kwote "${SOFTWARE_FINAL_FORMS}")\"" fi if [ -n "${SOFTWARE_INIT_FORMS}" ] ; then ECHOn " :init \"$(kwote "${SOFTWARE_INIT_FORMS}")\"" fi if [ -n "${NO_QUIT}" ] ; then ECHOn " :quit nil" fi if [ -n "${DUMP}" ] ; then ECHOn " :dump \"$(kwote "${DUMP}")\"" fi if [ -n "${RESTART}" ] ; then ECHOn " :restart '(\"$(kwote "${RESTART}")\" . :${RESTART_PACKAGE})" fi ECHOn ")" } print_lisp_initialization () { echo "#-cl-launched" print_lisp_launch } print_lisp_content () { ECHO "$CONTENT_DISCLAIMER" ECHO "$CONTENT_BEGIN_MARKER" extract_lisp_content } include_lisp_content () { if [ -n "$LISP_CONTENT" ] ; then print_lisp_content ; fi } include_shell_wrapper () { ECHO "$BASIC_ENV_CODE" if [ -z "$INCLUDE_PATH" ] ; then print_shell_wrapper else ECHO ". $(kwote1 "$INCLUDE_PATH/wrapper.sh")" fi } include_script_configuration_and_headers () { ECHOn "\ #!/bin/sh #| CL-LAUNCH ${CL_LAUNCH_VERSION} CONFIGURATION " ; include_configuration ; ECHOn "\ # END OF CL-LAUNCH CONFIGURATION # This file was generated by cl-launch ${CL_LAUNCH_VERSION} " ; include_license } make_loader () { include_script_configuration_and_headers include_shell_wrapper ECHOn ' launch_self "$@" ABORT # |# ' ; include_lisp_stuff } include_lisp_stuff () { include_lisp_header ; ECHOn ' ;;;; CL-LAUNCH LISP INITIALIZATION CODE ' ; print_lisp_initialization ; ECHOn ' ;;;; END OF CL-LAUNCH LISP INITIALIZATION CODE ' ; include_lisp_content } READ () { if [ $UNREAD_DEPTH = 0 ] ; then read -r LINE elif [ $UNREAD_DEPTH = 1 ] ; then UNREAD_DEPTH=0 LINE="$LINE1" elif [ $UNREAD_DEPTH = 2 ] ; then UNREAD_DEPTH=1 LINE="$LINE1" LINE1="$LINE2" else ABORT "error: UNREAD_DEPTH=$UNREAD_DEPTH" fi } UNREAD () { if [ $UNREAD_DEPTH = 0 ] ; then UNREAD_DEPTH=1 LINE1="$1" elif [ $UNREAD_DEPTH = 1 ] ; then UNREAD_DEPTH=2 LINE2="$LINE1" LINE1="$1" else ABORT "error: UNREAD_DEPTH=$UNREAD_DEPTH" fi } extract_configuration () { TRIED_CONFIGURATION=t CONFIGURATION= READ || return : "READ => $LINE" L1="#!/bin/sh" case "$LINE" in "$L1") : "read the SHEBANG" ;; *) : "Not a shell script" ; UNREAD "$LINE" ; return 2 ;; esac if ! READ ; then UNREAD "$L1" ; return 2 ; fi : "READ => $LINE" case "$LINE" in "#| CL-LAUNCH"*" CONFIGURATION") : "read the CL comment start" ;; *) : "Not a cl-launch script" ; UNREAD "$LINE" ; UNREAD "$L1" ; return 2 ;; esac while READ && #: "READ => $LINE" && case "$LINE" in "# END OF CL-LAUNCH CONFIGURATION") ! : ;; *) : ;; esac do CONFIGURATION="$CONFIGURATION${CONFIGURATION:+ }$LINE" ; done HAS_CONFIGURATION=t } extract_lisp_content () { if [ -z "$TRIED_CONFIGURATION" ] ; then extract_configuration fi if [ -n "$HAS_CONFIGURATION" ] ; then skip_to_marker fi cat_with_unread } cat_with_unread () { while [ $UNREAD_DEPTH != 0 ] ; do READ : "READ => $LINE" ECHO "$LINE" done cat } skip_to_marker () { while READ && #: "READ => $LINE" && case "$LINE" in "$CONTENT_BEGIN_MARKER") ! : ;; *) : ;; esac do : ; done } create_output () { create_file 755 "$OUTPUT_FILE" "$@" } with_input_from () { IN="$1" ; shift case "$IN" in ""|/dev/null) : from null ; "$@" < /dev/null ;; -) : from stdin ; "$@" ;; *) : from file "$IN" ; "$@" < "$IN" ;; esac } with_input () { with_input_from "${UPDATE:-$LISP_CONTENT}" "$@" } with_output () { case "${OUTPUT_FILE}" in "") ABORT "output file not specified" ;; /dev/null) : ;; -) "$@" ;; *) create_output "$@" ;; esac } make_output_file () { if [ -n "$DUMP" ] ; then dump_image_and_continue else do_make_output_file fi } do_make_output_file () { with_output with_input make_loader } execute_code () { run_code "$@" } do_run_code () { eval "$(print_shell_wrapper_body)" if [ -n "$LISP_CONTENT" ] ; then export CL_LAUNCH_FILE="$LISP_CONTENT" else unset CL_LAUNCH_FILE fi LAUNCH_FUN='funcall(intern(string :run):cl-launch)' LAUNCH_FORMS="$(load_form "$PROG";print_lisp_launch)" try_all_lisps "$@" } run_code () { ### Note: when dumping an image, run_code gets locally redefined ### by do_dump_image_and_continue, and restored by do_dump_image do_run_code "$@" } dump_image_and_continue () { case "$UPDATE" in -) SOFTWARE_CODE="$(cat)" ECHO "$SOFTWARE_CODE" | do_dump_image_and_continue "$@" ;; *) do_dump_image_and_continue "$@" ;; esac } do_dump_image_and_continue () { ORIG_WRAPPER_CODE="$WRAPPER_CODE" run_code () { WRAPPER_CODE="$WRAPPER_CODE DO_LISP=do_dump_image" do_run_code "$@" } if [ "x$DUMP" = "x!" ] ; then if [ "x$OUTPUT_FILE" = "x!" ] ; then abort 14 "Image dump required but neither dump file nor output file specified" else DUMP="$OUTPUT_FILE" fi fi IMAGE= execute_code "$@" } do_dump_image () { : do_dump_image "$@" run_code () { do_run_code "$@" } if [ -n "$INCLUDE_PATH" ] ; then export CL_LAUNCH_HEADER="$INCLUDE_PATH/launcher.lisp" else export CL_LAUNCH_HEADER="$PROG" fi if [ "x$IMAGE_ARG" = "xNOT_SUPPORTED_YET" ] ; then abort 13 "Image dumping not supported with implementation $IMPL. Try specifying a supported implementation with option --lisp (or \$LISP)" fi if [ -n "$STANDALONE_EXECUTABLE" ] ; then if [ "x$DUMP" = "x$OUTPUT_FILE" ] ; then # disabled optimization: also for || [ "x$OUTPUT_FILE" = "x!" ] # as it doesn't play nice with older versions of SBCL, ECL, etc., # that do not support standalone executables. export CL_LAUNCH_STANDALONE=t fi else if [ "x$DUMP" = "x$OUTPUT_FILE" ] ; then abort 15 "This implementation does not support dumping a standalone executable image" fi fi license_information # Use LOAD_IMAGE if it exists compute_image_path "$LOAD_IMAGE" ( do_exec_lisp ) || abort 22 "Failed to dump an image" case "$UPDATE" in -) ECHO "$SOFTWARE_CODE" | use_dumped_image "$@" ;; *) use_dumped_image "$@" ;; esac } use_dumped_image () { : use_dumped_image $OUTPUT_FILE -- standalone: ${CL_LAUNCH_STANDALONE} compute_image_path "$DUMP" case "${CL_LAUNCH_STANDALONE}:${OUTPUT_FILE}" in :!) invoke_image "$@" ;; :*) make_image_invoker ;; t:!) if [ -n "$CL_LAUNCH_VERBOSE" ] ; then set -x ; fi ; ${IMAGE} "$@" ;; t:*) : nothing to do ;; esac } make_image_invoker () { WRAPPER_CODE="$ORIG_WRAPPER_CODE" with_output with_input make_image_invocation_script } compute_image_path () { if [ -n "$1" ] ; then IMAGE_BASE="$(basename "$1")" IMAGE_DIR="${INCLUDE_PATH:-$(dirname "$1")}" IMAGE=${IMAGE_DIR}/${IMAGE_BASE} else IMAGE_BASE= IMAGE_DIR= IMAGE= fi } prepare_invocation_configuration () { LISP=$IMPL EXTRA_CONFIG_VARIABLES="LISP $OPTIONS_ARG" if eval "[ -n \"\$$BIN_ARG\" ]" ; then EXTRA_CONFIG_VARIABLES="$EXTRA_CONFIG_VARIABLES $BIN_ARG" fi } make_image_invocation_script () { prepare_invocation_configuration include_script_configuration_and_headers make_image_invocation include_lisp_content } make_image_invocation () { include_shell_wrapper if [ "x$IMAGE_ARG" = xEXECUTABLE_IMAGE ] ; then echo "$BIN_ARG=$IMAGE" fi cat< $LOADFILE < /dev/null`" ; then return 0 else return 1 fi } trylisp () { IMPL="$1" ; shift implementation_${IMPL} "$@" } do_exec_lisp () { if [ -n "$IMAGE" ] ; then if [ "x$IMAGE_ARG" = xEXECUTABLE_IMAGE ] ; then LISP_BIN= IMAGE_OPT= else IMAGE_OPT="$IMAGE_ARG" fi fi $EXEC_LISP "$@" } no_implementation_found () { ABORT "$PROG: Cannot find a supported lisp implementation. Tried the following: $*" } ensure_implementation () { trylisp "$1" || no_implementation_found "$1" } try_all_lisps () { for l in $LISP $LISPS ; do if trylisp $l ; then $DO_LISP "$@" return 0 fi done no_implementation_found "$LISP $LISPS" } exec_lisp () { # SBCL wants only one form per --eval so we need put everything in one progn. # However we also want any in-package form to be evaluated before any of the # remaining forms is read, so we get it to be evaluated at read-time as the # first thing in the main progn. # GNU clisp allows multiple forms per -x but prints the result of every form # evaluated and so we also need put everything in a single progn, and that progn # must quit before it may return to the clisp frame that would print its value. # CMUCL allows multiple forms per -eval and won't print values, so is ok anyway. # I don't know about other Lisps, but they will all work this way. LAUNCH_FORM="${PROGN}${MAYBE_PACKAGE_FORM}${HASH_BANG_FORM}${LAUNCH_FORMS}${NGORP}" ### This is partial support for CLBUILD. if [ -n "$USE_CLBUILD" ] ; then if [ -z "$IMAGE_OPT" ] ; then OPTIONS= else ABORT "Cannot use clbuild with a non-executable image different from clbuild's" fi fi if [ -n "$CL_LAUNCH_VERBOSE" ] ; then set -x ; fi exec $LISP_BIN $IMAGE_OPT $IMAGE $OPTIONS $EVAL "$LAUNCH_FORM" $ENDARGS "$@" } launch_self () { LAUNCH_FORMS="$(load_form "$PROG")" try_all_lisps "$@" } invoke_image () { if [ "x$IMAGE_ARG" = xEXECUTABLE_IMAGE ] ; then LISP_BIN= IMAGE_OPT= else IMAGE_OPT="$IMAGE_ARG" fi PACKAGE_FORM= HASH_BANG_FORM= LAUNCH_FORMS="(uiop:restore-image)" "$EXEC_LISP" "$@" } export CL_LAUNCH_PID=$$ export CL_LAUNCH_FILE="$PROG" ## execute configuration-provided code eval "$WRAPPER_CODE" ### END OF CL-LAUNCH SHELL WRAPPER EOF } : ' Useful tidbit for dichotomy-debugging Lisp code: (defvar *x* 0) (defun xx () (format t "*x* ~D~%" (incf *x*) (finish-output))) (xx) (xx) ' cl_fragment () { if [ -n "$CL_HEADER" ] ; then ECHO "#-cl-launch" fi cat } do_print_lisp_implementation () { ECHO "$IMPL" } print_lisp_implementation () { eval "$(print_shell_wrapper_body)" DO_LISP=do_print_lisp_implementation try_all_lisps } do_print_lisp_binary_path () { ECHO "$LISP_BIN" } print_lisp_binary_path () { eval "$(print_shell_wrapper_body)" DO_LISP=do_print_lisp_binary_path try_all_lisps } print_lisp_header () { CL_HEADER=t print_lisp_code echo ";;;;; Return to the default package." echo "(cl:in-package :cl-user)" print_lisp_code_bottom } print_lisp_launcher () { CL_HEADER= print_lisp_code echo ; echo "(cl-launch::compute-arguments)" print_lisp_code_bottom } print_lisp_setup () { OPTION -q print_lisp_launcher OPTION -x -s asdf -i "(let ((*package* (find-package :cl-launch))) (format t \"(cl-launch::initialize-asdf ~S)~%\" \"$(kwote $SOURCE_REGISTRY)\"))" -- } print_lisp_code () { echo "#+xcvb (module (:build-depends-on () :depends-on (\"/asdf\")))" echo "#| ;;; cl-launch ${CL_LAUNCH_VERSION} lisp header" include_license # HACK: this whole file is itself readable as Lisp code, and its meaning # is then that of the cl-launch lisp header code enclosed herein. # |# Last bit of Karma cat<<'NIL' |# ;;;; Silence our lisp implementation for quiet batch use... #| We'd like to evaluate as little as possible of the code without compilation. This poses a typical bootstrapping problem: the more sophistication we want to distinguish what to put where in what dynamic environment, the more code we have to evaluate before we may actually load compiled files. And, then, it is a waste of time to try to compile said code into a file. Moving things to the shell can only help so much, and reduces flexibility. Our best bet is to tell sbcl or cmucl to not try to optimize too hard. |# NIL ":" 't #-cl-launch ;'; cl_fragment<<'NIL' (eval-when (:compile-toplevel :load-toplevel :execute) ;; Configure the printer (setf *print-readably* nil ; allegro 5.0 may bork without this *print-level* nil) ;; Hush the compiler and loader. (setf *load-verbose* nil *compile-verbose* nil *compile-print* nil *load-print* nil) ;; The code below exemplifies how to try super-hard to load ASDF 3 from standard locations, ;; by trying nice methods first, then increasingly desperate ones. ;; Stage 1 is to load ASDF at all. ;; Stage 2 is to upgrade to whichever ASDF installation the user has configured (if any). ;; Versions older than ASDF 3.1 need to be told about ~/common-lisp/ ;; ASDF 1 has no output translation layer, so can be configured to load ASDF 3 ;; only if ASDF 3 is in a predictable place under the user's homedir, thus ;; ~/common-lisp/asdf/ or ~/.local/share/common-lisp/source/asdf/ only. (block nil (let ((required-asdf-version "3.1.2") (verbose *load-verbose*)) (labels ((asdf-symbol (name) (and (find-package :asdf) (find-symbol (string name) :asdf))) (asdf-call (name &rest args) (apply (asdf-symbol name) args)) (asdf-version () (when (find-package :asdf) (or (symbol-value (or (asdf-symbol '*asdf-version*) (asdf-symbol '*asdf-revision*))) "1.0"))) (maybe-display (message) (when (and verbose message) (format t "~&~A~%" message))) (call-maybe-verbosely (message function &rest args) (cond (verbose (maybe-display message) (apply function args)) (t #+abcl ;; Bug in ABCL 1.3.0: without this, loading asdf.fasl shows warnings (let* ((uc (asdf-symbol '*uninteresting-conditions*)) (vars (when uc (list uc))) (vals (when uc (list (cons 'warning (symbol-value uc)))))) (progv vars vals (handler-bind ((warning #'muffle-warning)) (apply function args)))) #-abcl (handler-bind ((warning #'muffle-warning)) (apply function args))))) (try-stage-1 (message function) (ignore-errors (call-maybe-verbosely (format nil "Trying to ~A" message) function)) (maybe-done-stage-1)) (subpath (parent &key directory name type) (merge-pathnames (make-pathname :defaults parent :name name :type type :version nil :directory (cons :relative directory)) parent)) (build/asdf.lisp (x) (subpath x :directory '("build") :name "asdf" :type "lisp")) (visible-default-user-asdf-directory () (subpath (user-homedir-pathname) :directory '("common-lisp" "asdf"))) (visible-default-user-asdf-lisp () (build/asdf.lisp (visible-default-user-asdf-directory))) (hidden-default-user-asdf-directory () (subpath (user-homedir-pathname) :directory '(".local" "share" "common-lisp" "asdf"))) (hidden-default-user-asdf-lisp () (build/asdf.lisp (hidden-default-user-asdf-directory))) (try-file-stage-1 (name explanation base root sub) (try-stage-1 (format nil "load ASDF from ~A/ under the ~A CL source directory ~A~{~A/~}" name explanation root sub) #'(lambda () (load (build/asdf.lisp (subpath base :directory (append sub (list name)))))))) (stage-1 () ;; Try to load ASDF at all, any ASDF. (try-stage-1 ;; Do nothing if ASDF is already loaded "use an already loaded ASDF" (constantly nil)) (try-stage-1 "require ASDF from the implementation" ;; Most implementations provide ASDF, but while most of them are case-insensitive, ;; CLISP is case-sensitive, so we need to specify a lowercase string, ;; and not the keyword :asdf or symbol 'asdf. ;; All recent implementations provide ASDF 3, ;; but older variants in the wild may only provide ASDF 2 or even ASDF 1. #'(lambda () (funcall 'require "asdf"))) (try-file-stage-1 "asdf" "default (visible)" (user-homedir-pathname) "~/" '("common-lisp")) (try-file-stage-1 "asdf" "default (hidden)" (user-homedir-pathname) "~/" '(".local" "share" "common-lisp")) #+(or unix linux bsd darwin) (progn (loop :for (name path) :in '(("local system" ("local")) ("managed system" ())) :do (loop :for sub :in '(() ("source")) :do (try-file-stage-1 "asdf" name #p"/" "/" (append '("usr") path '("share" "common-lisp") sub)))) (try-file-stage-1 "cl-asdf" "managed system" #p"/" "/" '("usr" "share" "common-lisp" "source"))) (error "Could not load ASDF.")) (maybe-done-stage-1 () ;; If we have ASDF, then go to stage 2: have it upgrade itself. (when (member :asdf *features*) (maybe-display (format nil "Found ASDF ~A" (asdf-version))) (stage-2))) ;; doesn't return. (centrally-register (path) (let ((r (asdf-symbol '*central-registry*))) (pushnew path (symbol-value r)) (pushnew (subpath path :directory '("uiop")) (symbol-value r)))) (configure-asdf () ;; configure older versions of ASDF, as needed (cond ((probe-file (visible-default-user-asdf-lisp)) (unless (member :asdf3.1 *features*) (maybe-display "Telling this old ASDF about your ~/common-lisp/asdf/") (centrally-register (visible-default-user-asdf-directory)))) ((probe-file (hidden-default-user-asdf-lisp)) (unless (member :asdf2 *features*) (maybe-display "Telling this antique ASDF about your ~/.local/share/common-lisp/asdf/") (centrally-register (hidden-default-user-asdf-directory)))))) (maybe-done-stage-2 () (when (ignore-errors (asdf-call 'version-satisfies (asdf-version) required-asdf-version)) (when verbose (format t "~&Victory! We now have ASDF ~A~%" (asdf-version))) (return))) (stage-2 () ;; We have ASDF, now have it upgrade itself. (configure-asdf) (when (asdf-call 'find-system :asdf nil) (call-maybe-verbosely "Trying to upgrade ASDF" (asdf-symbol 'operate) (asdf-symbol 'load-op) :asdf) :verbose nil) (maybe-done-stage-2) (error "We could only load ASDF ~A but we need ASDF ~A" (asdf-version) required-asdf-version))) ;; :asdf3.1 is both more recent than required and self-upgrading, so doesn't need this dance ;; :asdf3 is self-upgrading but might not be recent enough (it could be a pre-release). (unless (member :asdf3.1 *features*) (call-maybe-verbosely nil #'stage-1))))) (in-package :asdf)) NIL ":" 't #-cl-launch ;'; cl_fragment<<'NIL' ;; Because of ASDF upgrade punting, this ASDF package may be a new one. (eval-when (:compile-toplevel :load-toplevel :execute) (unless (or #+asdf3 (uiop:version<= "3.1.2" (asdf:asdf-version))) (error "cl-launch requires ASDF 3.1.2 or later"))) NIL ":" 't #-cl-launch ;'; cl_fragment<<'NIL' ;;;; Create cl-launch with UIOP. (progn (uiop:define-package :cl-launch (:use :common-lisp :uiop :asdf) (:export #:compile-and-load-file)) (in-package :cl-launch)) NIL ":" 't #-cl-launch ;'; cl_fragment<<'NIL' ;;;; cl-launch initialization code (progn (defvar *cl-launch-header* nil) ;; name of the file with this Lisp header (if any) (defvar *cl-launch-file* nil) ;; name of the file with the user code (if any) (defvar *verbose* nil) (defun dump-stream-to-file (i n) (with-output-file (o n :if-exists :rename-and-delete) (copy-stream-to-stream i o))) (defun dump-sexp-to-file (x n) (with-output-file (o n :if-exists :rename-and-delete) (write x :stream o :pretty t :readably t))) (defvar *temporary-filenames* nil) (defvar *temporary-file-prefix* (native-namestring (subpathname *temporary-directory* (strcat "cl-launch-" (getenvp "CL_LAUNCH_PID"))))) (defun make-temporary-filename (x) (parse-native-namestring (strcat *temporary-file-prefix* x))) (defun register-temporary-filename (n) (push n *temporary-filenames*) n) (defun temporary-filename (x) (register-temporary-filename (make-temporary-filename x))) (defun temporary-file-from-foo (dumper arg x) (let ((n (temporary-filename x))) (funcall dumper arg n) n)) (defun temporary-file-from-stream (i x) (temporary-file-from-foo #'dump-stream-to-file i x)) (defun temporary-file-from-string (i x) (temporary-file-from-foo #'(lambda (i n) (with-output-file (o n :if-exists :rename-and-delete) (princ i o))) i x)) (defun temporary-file-from-sexp (i x) (temporary-file-from-foo #'dump-sexp-to-file i x)) (defun temporary-file-from-code (i x) (if (stringp i) (temporary-file-from-string i x) (temporary-file-from-sexp i x))) (defun temporary-file-from-file (f x) (with-open-file (i f :direction :input :if-does-not-exist :error) (temporary-file-from-stream i x))) (defun ensure-lisp-file-name (x &optional (name "load.lisp")) (if (equal (pathname-type x) "lisp") x (temporary-file-from-file x name))) (defun ensure-lisp-loadable (x) (cond ((eq x t) (ensure-lisp-loadable (or *cl-launch-file* (error "Missing CL_LAUNCH_FILE")))) ((equal x "-") *standard-input*) ((or (streamp x) (pathnamep x)) x) ((stringp x) (ensure-absolute-pathname (parse-native-namestring x) #'getcwd)))) (defun ensure-lisp-file (x &optional (name "load.lisp")) (let ((x (ensure-lisp-loadable x))) (etypecase x (stream (temporary-file-from-stream x name)) (pathname (ensure-lisp-file-name x name))))) (defun cleanup-temporary-files () (loop :for n = (pop *temporary-filenames*) :while n :do (delete-file-if-exists n))) (defun file-newer-p (new-file old-file) "Returns true if NEW-FILE is strictly newer than OLD-FILE." (> (file-write-date new-file) (file-write-date old-file))) (defun compile-and-load-file (source &key force-recompile (verbose *verbose*) (load t) output-file) "compiles and load specified SOURCE file, if either required by keyword argument FORCE-RECOMPILE, or not yet existing, or not up-to-date. Keyword argument VERBOSE specifies whether to be verbose. Returns two values: the fasl path, and T if the file was (re)compiled" ;; When in doubt, don't trust - recompile. Indeed, there are ;; edge cases cases when on the first time of compiling a simple ;; auto-generated file (e.g. from the automated test suite), the ;; fasl ends up being written to disk within the same second as the ;; source was produced, which cannot be distinguished from the ;; reverse case where the source code was produced in the same split ;; second as the previous version was done compiling. Could be ;; tricky if a big system needs be recompiled as a dependency on an ;; automatically generated file, but for cl-launch those ;; dependencies are not detected anyway (BAD). If/when they are, and ;; lacking better timestamps than the filesystem provides, you ;; should sleep after you generate your source code. (let* (#+gcl (maybe-delete (unless (equal (pathname-type source) "lisp") (let ((temp (make-temporary-filename (strcat (pathname-name source) ".lisp")))) (copy-file source temp) (setf source temp)))) (truesource (truename source)) (fasl (or output-file (compile-file-pathname* truesource))) (compiled-p (when (or force-recompile (not (probe-file fasl)) (not (file-newer-p fasl source))) (ensure-directories-exist fasl) (multiple-value-bind (path warnings failures) (compile-file* truesource :output-file fasl) (declare (ignorable warnings failures)) (unless (equal (truename fasl) (truename path)) (error "cl-launch: file compiled to ~A, expected ~A" path fasl)) (when failures (error "cl-launch: failures while compiling ~A" source))) t))) (when load (load* fasl :verbose verbose)) #+gcl (delete-file-if-exists maybe-delete) (values fasl compiled-p))) (defun load-file (source &key output-file) (declare (ignorable output-file)) #-(or gcl (and ecl (not dlopen))) (compile-and-load-file source :verbose *verbose* :output-file output-file) #+gcl (let* ((pn (parse-namestring source))) ; when compiling, gcl 2.6 will always (if (pathname-type pn) ; add a type .lsp if type is missing, to avoid compilation (compile-and-load-file source :verbose *verbose* :output-file output-file) (load source :verbose *verbose*))) #+(and ecl (not dlopen)) (load source :verbose *verbose*)) (defun compute-arguments () (setf *cl-launch-file* (getenvp "CL_LAUNCH_FILE") *cl-launch-header* (getenvp "CL_LAUNCH_HEADER") *verbose* (when (getenvp "CL_LAUNCH_VERBOSE") t))) (asdf::register-preloaded-system "cl-launch") (defun load-sys (system) (if (find-package :quicklisp) (let ((*standard-output* (make-broadcast-stream))) (symbol-call :quicklisp :quickload system)) (asdf:load-system system))) ;;; We need this on all implementations when dumping an image, ;;; so that --eval and --file statements may properly depend ;;; on previously loaded systems, etc. ;;; To do it right, though, we want to only create a file ;;; for the --eval statement if needed by ECL... (defvar *dependency-counter* 0) (defun cl-launch-files () (when (pathnamep *cl-launch-file*) (list *cl-launch-file*))) (defun make-temporary-system (stem rdeps options) ;; Make a temporary system with given name stem and options ;; return the new list of dependencies, i.e. a singleton of the actual system name. (let ((sys (strcat "cl-launch-" stem))) (eval `(handler-bind ((warning #'muffle-warning)) (defsystem ,sys :version "0" :author "cl-launch user" :description "generated by cl-launch" :pathname ,*temporary-directory* :depends-on ,(reverse rdeps) ,@options) (defmethod input-files ((o operation) (s (eql (find-system ,sys)))) ',(cl-launch-files)))) (list sys))) (defclass asdf::cl-source-file-in-package (cl-source-file) ((package :initarg :package :reader component-package))) (defclass asdf::cl-source-code (source-file) ((code :initarg :code :initform nil :reader component-code) (package :initarg :package :reader component-package))) (handler-bind ((warning #'muffle-warning)) (defmethod perform :around ((o compile-op) (c asdf::cl-source-file-in-package)) (let ((*package* (find-package (component-package c)))) (call-next-method))) (defmethod component-pathname ((c asdf::cl-source-code)) nil) (defmethod perform ((o compile-op) (c asdf::cl-source-code))) (defmethod perform ((o load-op) (c asdf::cl-source-code)) (let ((*package* (find-package (component-package c)))) (eval-thunk (or (component-code c) (component-name c)))))) (defun make-dependency-system (rdeps options) ;; Make a system for given dependencies, ;; return the new list of dependencies, i.e. a singleton of the system name. (let ((name (format nil "build-~D" *dependency-counter*))) (incf *dependency-counter*) (make-temporary-system name rdeps options))) (defun make-dependency (dump fun arg pkg previous) ;; Make a dependency, return the new list of dependencies (ecase fun ((:load) (let ((x (ensure-lisp-loadable arg))) (if (or (pathnamep x) #+(or ecl mkcl) dump) (let ((load-file (ensure-lisp-file x (format nil "load-~D.lisp" *dependency-counter*)))) (make-dependency-system previous `(:components ((:cl-source-file-in-package ,(pathname-name load-file) :package ,pkg :pathname ,(truename load-file)))))) (make-dependency dump :eval `(load* ,x) pkg previous)))) ((:eval) (if (and #+(or ecl mkcl) (not dump)) (make-dependency-system previous `(:components ((:cl-source-code ,(format nil "eval-~D" *dependency-counter*) :code ,arg :package :cl-user)))) #+(or ecl mkcl) (with-input (i (temporary-file-from-code arg (format nil "eval-~D.lisp" *dependency-counter*))) (make-dependency dump :load i :cl-user previous)))) ((:require) (cons `(:require ,arg) previous)) ((:load-system) (cons arg previous)))) (defun build-program (dump build restart final init quit) (unwind-protect (let* ((*compile-verbose* *verbose*) #+ecl (c::*suppress-compiler-warnings* (not *verbose*)) #+ecl (c::*suppress-compiler-notes* (not *verbose*)) (standalone (and (getenvp "CL_LAUNCH_STANDALONE") t)) (op (if standalone 'program-op 'image-op)) (dependencies (loop :with r = () :for (fun arg pkg) :in `((:load-system "asdf") ,@(when dump ;; do we still want to include cl-launch in the dumped code, #+(or ecl mkcl) ;; now that all the relevant runtime support is in UIOP? (let ((header *cl-launch-header*)) ;; maybe for dependency timestamp? (setf *features* (remove :cl-launch *features*)) `((:load ,(ensure-lisp-file header "header.lisp") :cl-user)))) ,@build ,(let ((footer `(setf *package* (find-package :cl-user) *image-dumped-p* ,(when dump (if standalone :executable t)) *image-entry-point* ,(when restart `(ensure-function ,(car restart) :package ,(cdr restart))) *image-prelude* ,init *image-postlude* ,final *lisp-interaction* ,(not quit)))) `(:eval ,footer :cl-user))) :do (setf r (make-dependency dump fun arg pkg r)) :finally (return r))) (program-sys (make-temporary-system "program" dependencies `(:serial t :entry-point ,(when restart `(lambda () (funcall (ensure-function ,(car restart) :package ,(cdr restart))))) ;; For SBCL and other platforms that die on dump-image, clean before the end: :perform (image-op :before (o c) (setf *features* (remove :cl-launched *features*)) (cleanup-temporary-files)) ,@(when dump `(:output-files (,op (o c) (values (list ,(ensure-absolute-pathname dump #'getcwd)) t)))))))) (load-sys program-sys) ;; Give quicklisp a chance to download things (when dump (operate op program-sys))) (cleanup-temporary-files)) (unless dump (restore-image)) (quit 0)) (defun load-quicklisp () (block nil (flet ((try (x) (when (probe-file* x) (return (load* x))))) (try (subpathname (user-homedir-pathname) "quicklisp/setup.lisp")) (try (subpathname (user-homedir-pathname) ".quicklisp/setup.lisp")) (error "Couldn't find quicklisp in your home directory. ~ Go get it at http://www.quicklisp.org/beta/index.html")))) (defun run (&key quicklisp source-registry build dump restart final init (quit t)) (setf *lisp-interaction* (not quit)) (with-fatal-condition-handler () (pushnew :cl-launched *features*) (compute-arguments) (when source-registry (initialize-source-registry source-registry)) (when quicklisp (load-quicklisp)) (build-program dump build restart final init quit))) (pushnew :cl-launch *features*)) NIL #| } print_lisp_code_bottom () { # |# ":" ; cat<<'NIL' ;;; END OF CL-LAUNCH LISP HEADER NIL #| } ### There we are. Now do the job [ $# -gt 0 ] || mini_help_abort all "$@" ; exit # |# ; What follows is Lisp code available when loading this file