summary refs log tree commit diff stats
path: root/build.sh
blob: 5efef89b9e138500d7992588af42f3e0d7608b91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/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'\'
		test_wrapper 'out/bin/cratera test/line-numbers.cratera'
		test_wrapper 'out/bin/cratera test/strings.cratera'
		test_wrapper 'out/bin/cratera test/defaultbucket.cratera'
		test_wrapper 'out/bin/cratera test/bucketmap-tests.cratera'
	) || 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