1LUCET_ROOT:=$(abspath ../..)
2
3WASI_SDK?=/opt/wasi-sdk
4WASI_CC?=$(WASI_SDK)/bin/clang
5BINARYEN_DIR?=/opt/binaryen
6WASM_OPT?=$(shell command -v wasm-opt || echo $(BINARYEN_DIR)/bin/wasm-opt)
7
8LUCETC:=$(LUCET_ROOT)/target/release/lucetc
9
10LUCET_RUNTIME:=$(LUCET_ROOT)/lucet-runtime
11LUCET_WASI:=$(LUCET_ROOT)/lucet-wasi
12SIGHTGLASS:=$(LUCET_ROOT)/sightglass
13
14
15SHOOTOUT:=$(LUCET_ROOT)/sightglass/benchmarks/shootout
16SHOOTOUT_SRCS:=$(shell ls $(SHOOTOUT)/*.c)
17SHOOTOUT_NATIVE_OBJS:=
18SHOOTOUT_LUCET_OBJS:=
19
20LUCETC_FLAGS:=--opt-level speed_and_size --min-reserved-size 4294967296
21COMMON_CFLAGS:=--std=c99 -Ofast -Wall -W -I$(SIGHTGLASS)/include
22
23SHOOTOUT_NATIVE_CFLAGS:=-march=native -fPIC \
24	-DIMPL_REFERENCE -DUSE_LEND \
25	-Dmalloc=lend_malloc -Dcalloc=lend_calloc -Dfree=lend_free
26
27BINARYEN_VERSION=86
28
29ifdef CI
30	SIGHTGLASS_ARGS:=--quick
31endif
32
33default: run
34
35.PHONY: bench
36bench: run
37
38.PHONY: run
39run: build
40	$(LUCET_ROOT)/target/release/sightglass -c sightglass.toml $(SIGHTGLASS_ARGS)
41
42.PHONY: build
43build: $(LUCETC)
44build: build/native/implementation.so
45build: build/lucet/implementation.so
46build: build/lucet/module.so
47build: $(LUCET_ROOT)/target/release/sightglass
48
49# With the xenial gcc (5.4.0) this pins the CPU for minutes if above -O1, so its getting downgraded to -O1
50build/native/shootout/switch2.o: $(SHOOTOUT)/switch2.c
51	@mkdir -p $(@D)
52	$(CC) $(SHOOTOUT_NATIVE_CFLAGS) $(COMMON_CFLAGS) -O1 -c $^ -o $@
53
54build/native/shootout/%.o: $(SHOOTOUT)/%.c
55	@mkdir -p $(@D)
56	$(CC) $(SHOOTOUT_NATIVE_CFLAGS) $(COMMON_CFLAGS) -c $^ -o $@
57
58build/native/implementation.so: $(patsubst %.c, %.o, $(addprefix build/native/shootout/, $(notdir $(SHOOTOUT_SRCS))))
59	@mkdir -p $(@D)
60	$(CC) -shared -o $@ $^
61
62build/lucet/shootout/%.o: $(SHOOTOUT)/%.c
63	@mkdir -p $(@D)
64	$(WASI_CC) $(COMMON_CFLAGS) -c $^ -o $@
65
66build/lucet/module.wasm.unoptimized: $(patsubst %.c, %.o, $(addprefix build/lucet/shootout/, $(notdir $(SHOOTOUT_SRCS))))
67	@mkdir -p $(@D)
68	$(WASI_CC) $^ -o $@ -nostartfiles -Wl,--no-entry -Wl,--export-all
69
70build/lucet/module.wasm: build/lucet/module.wasm.unoptimized
71	$(WASM_OPT) -mvp --disable-mutable-globals -O4 -o $@ $^
72
73build/lucet/module.so: build/lucet/module.wasm
74	@mkdir -p $(@D)
75	$(LUCETC) $(LUCETC_FLAGS) --emit=so \
76		--bindings=bindings.json \
77		--bindings=$(LUCET_WASI)/bindings.json \
78		-o $@
79
80build/lucet/module.clif: build/lucet/module.wasm
81	@mkdir -p $(@D)
82	$(LUCETC) $(LUCETC_FLAGS) --emit=clif \
83		--bindings=bindings.json \
84		--bindings=$(LUCET_WASI)/bindings.json \
85		-o $@
86
87build/lucet/%.o: %.c
88	@mkdir -p $(@D)
89	$(CC) --std=c99 -fPIC -D_GNU_SOURCE -g3 $(COMMON_CFLAGS) \
90		-fvisibility=default \
91		-I$(LUCET_RUNTIME)/include \
92		-I$(LUCET_WASI)/include \
93		-DWASM_MODULE=$(abspath build/lucet/module.so) \
94		-c $^ -o $@
95
96build/lucet/implementation.so: $(LUCET_ROOT)/target/release/liblucet_runtime.so
97build/lucet/implementation.so: $(LUCET_ROOT)/target/release/liblucet_wasi.so
98build/lucet/implementation.so: build/lucet/wrapper.o
99build/lucet/implementation.so: build/lucet/hostcalls.o
100	@mkdir -p $(@D)
101	$(CC) -rdynamic -shared \
102		build/lucet/wrapper.o build/lucet/hostcalls.o \
103		-L $(LUCET_ROOT)/target/release \
104		-Wl,-rpath $(LUCET_ROOT)/target/release \
105		-llucet_runtime -llucet_wasi -ldl -o $@
106
107$(LUCET_ROOT)/target/release/sightglass:
108	cargo build --release -p sightglass
109
110$(LUCETC):
111	cargo build --release -p lucetc
112
113.PHONY: $(LUCET_ROOT)/target/release/liblucet_runtime.so
114$(LUCET_ROOT)/target/release/liblucet_runtime.so:
115	cargo build --release -p lucet-runtime
116
117
118.PHONY: $(LUCET_ROOT)/target/release/liblucet_wasi.so
119$(LUCET_ROOT)/target/release/liblucet_wasi.so:
120	cargo build --release -p lucet-wasi
121
122.PHONY: clean
123clean:
124	-rm -rf build
125