Makefile 3.5 KiB raw
1
# Builds the RISC-V emulator.
2
3
PREFIX        ?= $(HOME)/bin
4
CC            := clang
5
CPPFLAGS      := -iquote .
6
CFLAGS        := -fvisibility=hidden -std=c99 -O3 -g \
7
                 -Wall -Wextra -Wpedantic \
8
                 -Wformat=2 -Wformat-security \
9
                 -Wnull-dereference \
10
                 -Wno-format-nonliteral \
11
                 -Wcast-align \
12
                 -Wunused -Wuninitialized \
13
                 -Wmissing-field-initializers \
14
                 -fno-common -fstack-protector-all \
15
                 -mcmodel=medium \
16
                 -march=native -flto
17
BLAKE3_CFLAGS := $(shell pkg-config --cflags libblake3)
18
BLAKE3_LIBS   := $(shell pkg-config --libs libblake3)
19
LDFLAGS       := -fuse-ld=lld -Wl,-z,stack-size=33554432
20
EMULATOR_SRCS := emulator.c abi_host.c abi_linux.c image_native.c image_rad0.c \
21
                 image_raw.c host_posix.c jit.c profile.c debug_map.c riscv/debug.c io.c riscv.c \
22
                 vector.c machine.c machine_cpu.c machine_csr.c machine_devices.c machine_fdt.c
23
24
default: bin/emulator
25
26
bin/emulator: $(EMULATOR_SRCS) profile.h debug_map.h jit.h bitmanip.h vector.h muldiv.h color.h types.h io.h host.h hosted.h riscv.h riscv/debug.h machine.h machine_internal.h
27
	@echo "cc    emulator => $@"
28
	@mkdir -p bin
29
	@$(CC) $(CPPFLAGS) $(CFLAGS) $(BLAKE3_CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) $(BLAKE3_LIBS) -o $@
30
31
install: bin/emulator
32
	@echo "copy  bin/emulator => $(PREFIX)/emulator"
33
	@mkdir -p $(PREFIX)
34
	@cp bin/emulator $(PREFIX)/emulator
35
36
fmt:
37
	git ls-files "*.c" "*.h" | xargs clang-format -i
38
39
clean:
40
	@rm -f bin/emulator
41
42
.PHONY: default clean install
43
.SUFFIXES:
44
.DELETE_ON_ERROR:
45
.SILENT:
46
47
# Check integer vector execution with host memory diagnostics.
48
bin/vector-test: test/vector.c vector.c vector.h types.h
49
	@mkdir -p bin
50
	@$(CC) $(CPPFLAGS) -std=c99 -Wall -Wextra -g -fsanitize=address,undefined test/vector.c vector.c -o $@
51
52
bin/vector-machine-test: test/vector_machine.c vector.c vector.h machine_cpu.c machine_csr.c machine_devices.c machine.c machine_fdt.c machine_internal.h machine.h types.h bitmanip.h muldiv.h
53
	@mkdir -p bin
54
	@$(CC) $(CPPFLAGS) -std=c99 -Wall -Wextra -g -fsanitize=address,undefined test/vector_machine.c vector.c machine_cpu.c machine_csr.c machine_devices.c machine.c machine_fdt.c -o $@
55
56
test-vector: bin/vector-test bin/vector-machine-test test-jit
57
	@ASAN_OPTIONS=detect_leaks=0 timeout 30 bin/vector-test
58
	@ASAN_OPTIONS=detect_leaks=0 timeout 30 bin/vector-machine-test
59
60
.PHONY: test-vector
61
62
# Compare hosted vector translation with architectural interpreter state.
63
bin/jit-vector-test: test/jit_vector.c jit.c jit.h profile.h vector.c vector.h riscv.c riscv.h riscv/debug.c riscv/debug.h bitmanip.h color.h types.h
64
	@mkdir -p bin
65
	@$(CC) $(CPPFLAGS) -std=c99 -O2 -g -Wall -Wextra -fsanitize=address,undefined test/jit_vector.c jit.c vector.c riscv.c riscv/debug.c -o $@
66
67
test-jit: bin/jit-vector-test test-jit-calls
68
	@ASAN_OPTIONS=detect_leaks=0 timeout 60 $<
69
70
# Compare translated and interpreted runs of calls, returns and LR/SC.
71
bin/jit-calls-gen: test/jit_calls.c
72
	@mkdir -p bin
73
	@$(CC) $(CPPFLAGS) -std=c99 -Wall -Wextra -g $< -o $@
74
75
test-jit-calls: bin/emulator bin/jit-calls-gen
76
	@bin/jit-calls-gen bin/jit-calls.bin
77
	@jit=$$(bin/emulator -run -count-instructions bin/jit-calls.bin 2>&1; \
78
	        echo "status $$?"); \
79
	 int=$$(bin/emulator -run -count-instructions -no-jit bin/jit-calls.bin 2>&1; \
80
	        echo "status $$?"); \
81
	 if [ "$$jit" != "$$int" ]; then \
82
	     echo "JIT call checks failed: '$$jit' != '$$int'"; exit 1; \
83
	 fi; \
84
	 echo "JIT call checks passed"
85
86
.PHONY: test-jit test-jit-calls