# SPDX-License-Identifier: BSD-3-Clause
#
# Build the host-side CUDA bridge daemon.
#
# Kernels are written in CUDA C, compiled to PTX at build time by nvcc, and
# embedded into the binary by embed_ptx.py, so cuda_bridge needs only the
# driver's libcuda at runtime. kernels_ptx.h is a generated file; if it is
# already present the build works without nvcc.

CC      ?= cc
NVCC    ?= nvcc
PTX_ARCH ?= sm_75
CFLAGS  ?= -O2 -Wall -Wextra -std=gnu11
LDLIBS  ?= -lcuda

all: cuda_bridge

kernels.ptx: kernels.cu
	$(NVCC) -arch=$(PTX_ARCH) -ptx $< -o $@

kernels_ptx.h: kernels.ptx embed_ptx.py
	python3 embed_ptx.py kernels.ptx kernels_ptx.h

cuda_bridge: cuda_bridge.c kernels_ptx.h
	$(CC) $(CFLAGS) cuda_bridge.c -o $@ $(LDLIBS)

test: cuda_bridge
	./cuda_bridge --test

bridge-test: cuda_bridge
	python3 bridge_selftest.py

layout-check:
	python3 check_module_layout.py

mutate-test: cuda_bridge
	./cuda_bridge --mutation-test

bench: cuda_bridge
	./cuda_bridge --bench

clean:
	rm -f cuda_bridge kernels.ptx kernels_ptx.h

.PHONY: all test bridge-test layout-check mutate-test bench clean
