#!/bin/sh LUA_INTERPRETER=${LUA_INTERPRETER:-lua} case "$LUA_INTERPRETER" in /*) ;; *) ENV_WRAPPER=${ENV_WRAPPER:-"$(command -v env)"} ;; esac cratera_path_for_display="$(env -i "$LUA_INTERPRETER" src/host/genpath.lua display)" do_build() { mkdir -p out/lua/cratera/prebuilt/ || exit 1 mkdir -p out/cratera/cratera.cratera.d || exit 1 mkdir -p out/bin || exit 1 # worst tool for this job cp -R src/cratera out/lua/ || exit 1 cp -R src/cratera.cratera.d out/cratera/ || exit 1 if [ "$ENV_WRAPPER" = '' ]; then printf "#!%s\n" "$LUA_INTERPRETER" >out/bin/cratera || exit 1 else printf "#!%s %s\n" "$ENV_WRAPPER" "$LUA_INTERPRETER" >out/bin/cratera || exit 1 fi tail -n +2 src/bin/cratera.lua >>out/bin/cratera || exit 1 chmod +x out/bin/cratera || exit 1 if [ -n "${DEFAULT_CRATERA_PATH+x}" ]; then env -i "DEFAULT_CRATERA_PATH=$DEFAULT_CRATERA_PATH" "$LUA_INTERPRETER" src/host/genpath.lua >out/lua/cratera/prebuilt/path.lua || exit 1 else env -i "$LUA_INTERPRETER" src/host/genpath.lua >out/lua/cratera/prebuilt/path.lua || exit 1 fi } test_wrapper() { eval "$*" || { printf 'Test failed:' printf ' %s' "$@" printf '\n' exit 2 } } do_test() { # FIXME this does NOT handle LUA_PATH correctly. # FIXME nor LUA_INIT. mkdir -p out/test || exit 2 ( export LUA_PATH='./out/lua/?.lua;./out/lua/?/init.lua;;' export CRATERA_PATH='./test/?.cratera;./out/cratera/?.cratera;;' # these tests use the regular lua interpreter "$LUA_INTERPRETER" test/testp.lua || exit 2 "$LUA_INTERPRETER" test/testc.lua || exit 2 "$LUA_INTERPRETER" test/testbc.lua || exit 2 "$LUA_INTERPRETER" test/test_bootstrap.lua || exit 2 # these tests use the cratera interpreter test_wrapper 'out/bin/cratera -v | grep Cratera' test_wrapper 'out/bin/cratera -invalid 2>&1 | grep usage' test_wrapper 'out/bin/cratera -void 2>&1 | grep usage' test_wrapper 'out/bin/cratera -e 2>&1 | grep usage' test_wrapper '! out/bin/cratera -e '\''error("hello")'\''' test_wrapper 'out/bin/cratera -e '\''arg[3] = nil'\'' -e '\''print("still prints")'\' test_wrapper 'out/bin/cratera -e '\''print(craterapath)'\''' test_wrapper 'out/bin/cratera test/tests.cratera' test_wrapper 'out/bin/cratera test/interp-error.cratera string 2>&1 | grep '\''5: string'\' test_wrapper 'out/bin/cratera test/interp-error.cratera nil 2>&1 | grep '\''nil value'\' test_wrapper 'out/bin/cratera test/interp-error.cratera table 2>&1 | grep '\''table value'\' test_wrapper 'out/bin/cratera test/interp-error.cratera metaerror 2>&1 | grep '\''overflow\|handling'\' test_wrapper 'printf '\''_PROMPT=setmetatable({}, {__tostring=error})\n'\'' | out/bin/cratera -i 2>&1 ' # | grep '\''overflow\|handling'\' ) || exit 2 } do_interp() { shift LUA_PATH='./out/lua/?.lua;./out/lua/?/init.lua;;' CRATERA_PATH='./test/?.cratera;./out/cratera/?.cratera;;' out/bin/cratera "$@" || exit 3 } case "$1" in -h|--help|help) printf '%s\n' \ 'The Cratera "Build System"' \ 'To build, use '\''./build.sh build'\''. Files will be put in '\''out/'\''.' \ 'To run tests, use '\''./build.sh test'\'', this will also rebuild cratera.' \ 'To run tests without rebuilding, use '\''./build.sh test-only'\''.' \ ' (Note: you must build at least once before using this option.)' \ 'The currently selected Lua interpreter is:' \ "$(printf ' LUA_INTERPRETER=%s' "$LUA_INTERPRETER")" \ 'The currently selected env wrapper is:' \ "$(printf ' ENV_WRAPPER=%s' "$ENV_WRAPPER")" \ 'The default Cratera path is:' \ "$(printf ' CRATERA_DEFAULT_PATH=%s' "$cratera_path_for_display")" \ ;; interp) do_build do_interp "$@" ;; interp-only) do_interp "$@" ;; test-only) do_test ;; test) do_build do_test ;; build|'') do_build ;; *) printf '%s\n' 'sorry' ;; esac