ifneq (${KERNELRELEASE},)

# KERNELRELEASE defined: we are being compiled as part of the Kernel
        obj-m := drum_hello.o ui.o

else

# We are being compiled as a module: use the Kernel build system

	KERNEL_SOURCE := /usr/src/linux-headers-$(shell uname -r)
        PWD := $(shell pwd)

CC = gcc
CFLAGS = -Wall -Wextra -O2 -I$(HOME)/.local/include
LDFLAGS_USB = -lusb-1.0 -lpthread -lm

default: module hello drum ui_demo

module:
	${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

hello: hello.c drum_hello.h
	$(CC) $(CFLAGS) -o hello hello.c

drum: drum.c drum.h seq_load.c seq_load.h state_to_tiles.c state_to_tiles.h
	$(CC) $(CFLAGS) -o drum drum.c seq_load.c state_to_tiles.c $(LDFLAGS_USB)

ui_demo: ui_demo.c ui.h
	$(CC) $(CFLAGS) -o ui_demo ui_demo.c

clean:
	${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
	${RM} hello drum ui_demo

.PHONY: default module clean

endif
