Skip to content

Commit eea05a5

Browse files
committed
Run examples test on WCC
* Maintain binary execution on test * Fix passing parent directory on `test-wcc-gen2` Compiling mandelbrot.c on emcc and running it causes `Permission Denied.` It is hard that array cannot use in shell script because Github Actions causes error.
1 parent 5bb6e8c commit eea05a5

8 files changed

Lines changed: 67 additions & 20 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ wcc-gen2: wcc
281281
$(MAKE) HOST_TARGET=wcc HOST_WCC="./wcc" CC="./wcc" WCC_TARGET= wcc-self-hosting
282282
.PHONY: test-wcc-gen2
283283
test-wcc-gen2: wcc-gen2
284-
$(MAKE) TARGET_CC="../tool/run-gen2wcc.sh" test-wcc-self-hosting
284+
XCC_TEST_EXAMPLES_DIR=/examples $(MAKE) TEST_CC="../tool/run-gen2wcc.sh --mapdir=/examples::../examples --" test-wcc-self-hosting
285285

286286
.PHONY: wcc-gen3
287287
wcc-gen3: wcc-gen2
@@ -297,7 +297,7 @@ wcc-self-hosting: $(WCC_TARGET)cc.wasm
297297

298298
.PHONY: test-wcc-self-hosting
299299
test-wcc-self-hosting:
300-
$(MAKE) -C tests clean && $(MAKE) WCC="$(TARGET_CC)" -C tests test-wcc
300+
$(MAKE) -C tests clean && $(MAKE) WCC="$(TEST_CC)" -C tests test-wcc
301301

302302
$(WCC_TARGET)cc.wasm: $(WCC_OBJS) # $(WCC_PARENT)
303303
$(HOST_WCC) -o $@ $(WCC_OBJS)

examples/mandelbrot.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ int main(int argc, char *argv[]) {
7171

7272
FILE *fp = fopen("mandelbrot.ppm", "wb");
7373
if (fp == NULL) {
74+
perror("fopen");
7475
exit(1);
7576
}
7677
fprintf(fp, "P6\n%d %d\n255\n", (int)W, (int)H);

tests/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ WCC:=../wcc
160160
WCC_TESTS:=valtest dvaltest fvaltest
161161

162162
.PHONY: test-wcc
163-
test-wcc: test-wcc-sh $(foreach D, $(WCC_TESTS), $(addprefix test-wcc-,$(D)))
163+
test-wcc: test-wcc-sh $(foreach D, $(WCC_TESTS), $(addprefix test-wcc-,$(D))) test-wcc-examples
164164
@echo 'All tests PASS!'
165165

166166
.PHONY: test-wcc-sh
@@ -171,6 +171,14 @@ test-wcc-sh:
171171
AOUT="$(AWASM)" RUN_AOUT="../tool/runwasi $(AWASM)" \
172172
./test.sh
173173

174+
.PHONY: test-wcc-examples
175+
test-wcc-examples:
176+
@echo '## example_test.sh'
177+
@$(eval AWASM := $(shell basename `mktemp -u`).wasm)
178+
@XCC="$(WCC)" RE_SKIP='\/\/-WCC' \
179+
AOUT="$(AWASM)" RUN_AOUT="../tool/runwasi --dir=. $(AWASM)" \
180+
./example_test.sh
181+
174182
valtest_WCCSRCS:=$(VAL_SRCS)
175183
dvaltest_WCCSRCS:=$(FVAL_SRCS)
176184
fvaltest_WCCSRCS:=$(FVAL_SRCS)

tests/cpptest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function try_run() {
1515

1616
echo -e "$input" | $CPP $ppflags | $CC -o "$AOUT" -Wall -Werror -xc - || exit 1
1717

18-
$RUN_AOUT
18+
./"$AOUT"
1919
local actual="$?"
2020

2121
local err=''; [[ "$actual" == "$expected" ]] || err="${expected} expected, but ${actual}"

tests/example_test.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ set -o pipefail
44

55
source ./test_sub.sh
66

7+
XCC_TEST_EXAMPLES_DIR=${XCC_TEST_EXAMPLES_DIR:-../examples}
8+
79
function no_flonum() {
810
echo -e "#include <stdio.h>\nint main(){\n#ifdef __NO_FLONUM\nputs(\"true\");\n#endif\nreturn 0;}" | \
9-
$XCC -xc - && ${RUN_EXE} ./a.out || exit 1
11+
$XCC -o "$AOUT" -xc - && ${RUN_AOUT} || exit 1
1012
}
1113

1214
function test_all() {
1315
begin_test_suite "All"
1416

15-
try_file 'hello' 'Hello, world!' ../examples/hello.c
16-
try_file 'fib' 832040 ../examples/fib.c
17-
try_file 'echo' 'foo bar baz' ../examples/echo.c foo bar baz
17+
try_file 'hello' 'Hello, world!' "${XCC_TEST_EXAMPLES_DIR}"/hello.c
18+
try_file 'fib' 832040 "${XCC_TEST_EXAMPLES_DIR}"/fib.c
19+
try_file 'echo' 'foo bar baz' "${XCC_TEST_EXAMPLES_DIR}"/echo.c foo bar baz
1820

1921
if [ "$(no_flonum)" != "true" ]; then
20-
try_cmp 'mandelbrot' 'mandel256.ppm' 'mandelbrot.ppm' ../examples/mandelbrot.c 100 256 256
22+
try_cmp 'mandelbrot' 'mandel256.ppm' 'mandelbrot.ppm' "${XCC_TEST_EXAMPLES_DIR}"/mandelbrot.c \
23+
100 256 256
2124
fi
2225

2326
end_test_suite

tests/test_sub.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function end_test() {
4242
AOUT=${AOUT:-$(basename "$(mktemp -u)")}
4343
XCC=${XCC:-../xcc}
4444
CPP=${CPP:-../cpp}
45-
RUN_AOUT=${RUN_AOUT:-./"$AOUT"}
45+
RUN_AOUT=${RUN_AOUT:-"$RUN_EXE ./$AOUT"}
4646

4747
CC=${CC:-cc}
4848
REMOVE_TOP_LINEMARKER="tail -n +2" # First line of preprocessor output is linemarker, so remove it.
@@ -112,7 +112,7 @@ function try_file() {
112112

113113
declare -a args=( "$@" )
114114
local actual
115-
actual=$(${RUN_EXE} ./"$AOUT" "${args[@]:3}") > /dev/null 2>&1 || {
115+
actual=$(${RUN_AOUT} "${args[@]:3}") > /dev/null 2>&1 || {
116116
end_test 'Exec failed'
117117
return
118118
}
@@ -135,7 +135,7 @@ function try_cmp() {
135135
}
136136

137137
declare -a args=( "$@" )
138-
${RUN_EXE} ./"$AOUT" "${args[@]:4}" > /dev/null 2>&1 || {
138+
${RUN_AOUT} "${args[@]:4}" > /dev/null 2>&1 || {
139139
end_test 'Exec failed'
140140
return
141141
}

tool/run-gen2wcc.sh

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,50 @@ set -e
44

55
PRJROOT=$(cd "$(dirname "$0")/..";pwd)
66
WCCWASM=$PRJROOT/cc.wasm
7+
CWD=.
8+
EXTRA_OPTS=''
9+
10+
while [ $# -gt 0 ] # (( $# > 0 ))
11+
do
12+
case $1 in
13+
-C | --current-dir | --current-dir=*)
14+
if [[ "$1" =~ ^--current-dir= ]]; then
15+
CWD=${1#--current-dir=}
16+
elif [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
17+
echo "'current-dir' requires an argument." 1>&2
18+
exit 1
19+
else
20+
CWD="$2"
21+
shift
22+
fi
23+
;;
24+
--dir=* | --mapdir=*)
25+
EXTRA_OPTS="${EXTRA_OPTS} ""$1" # Escape required?
26+
;;
27+
--)
28+
shift
29+
break
30+
;;
31+
-*)
32+
echo "$1: Unkonwn option." 1>&2
33+
exit 1
34+
;;
35+
*)
36+
break
37+
;;
38+
esac
39+
shift
40+
done
741

842
if [ ! -e "$WCCWASM" ]; then
9-
echo "Run 'make wcc-gen2' command to create executable" 1>&2
10-
exit 1
43+
echo "Run 'make wcc-gen2' command to create executable" 1>&2
44+
exit 1
1145
fi
1246

1347
"$PRJROOT/tool/runwasi" \
14-
--dir=. \
15-
--dir=/tmp \
16-
"--mapdir=/usr/include::$PRJROOT/include" \
17-
"--mapdir=/usr/lib::$PRJROOT/lib" \
18-
"$WCCWASM" -- "$@"
48+
--dir="${CWD}" \
49+
--dir=/tmp \
50+
"--mapdir=/usr/include::$PRJROOT/include" \
51+
"--mapdir=/usr/lib::$PRJROOT/lib" \
52+
${EXTRA_OPTS} \
53+
"$WCCWASM" -- "$@"

tool/runwasi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function getRealpaths(map) {
4242

4343
if (program.args.length <= 0) {
4444
program.help()
45-
process.exit(1)
45+
// unreachable.
4646
}
4747

4848
const wasmFileName = program.args[0]

0 commit comments

Comments
 (0)