NO_PIE=$(shell $(CC) -v 2>&1|grep -q pie && echo "-no-pie")
CFLAGS=-g $(ARCH_FLAGS) $(add_CPPFLAGS) $(NO_PIE) -I/opt/cprocsp/include/cpcsp -I/opt/cprocsp/include -I/opt/cprocsp/include/interfaces -I/opt/cprocsp -I/opt/cprocsp/include/asn1c/rtsrc -DLINUX -DUNIX -DSIZEOF_VOID_P=$(SIZEOF_VOID_P) 
KDIR=$(shell test -z "$(kernel_source)" && for BUILDDIR in /lib/modules/`uname -r`/build /usr/src/linux-`uname -r` /usr/src/linux-headers-`uname -r` /usr/src/kernels/`uname -r`;do test -d $$BUILDDIR || test -h $$BUILDDIR && echo $$BUILDDIR && break;done || test -d "$(kernel_source)" || test -h $(kernel_source) && echo "$(kernel_source)")
KMAJ=$(shell uname -r|cut -d. -f1)
ifeq ($(KMAJ),2)
SD=SUBDIRS
MD=modules
else
SD=M
MD="KBUILD_EXTRA_SYMBOLS=$(MODVERSFILE)"
endif
CFLAGS_MODULE=$(CFLAGS) -DCSP_LITE -DKSP_LITE -DCSP_DRIVER -DMODULE

all: drtktls.ko WebClientKrn WebClientKrnEmul

WebClientKrn: WebClientKrn.c WCUserIoctl.c WCMemSTDC.c $(CSP_LIB)/libcpcdrv_emul.a

	$(CC) $(CFLAGS) -g -DSIZEOF_VOID_P=$(SIZEOF_VOID_P) -o $@ $^ -L$(CSP_LIB) $(CSP_EXTRA_LIBS) -lcapi10 -lpthread -lssp -lcapi20 -lrdrsup
SSP_LIB=$(CSP_LIB)/libsspdrv_user.a 
#SSP_LIB=$(CSP_LIB)/libsspdrv.a 

WebClientKrnEmul: WebClientKrn.c WCMemSTDC.c WCKernelPartEmul.o $(SSP_LIB) $(CSP_LIB)/libcpcdrv_emul.a
	$(CC) $(CFLAGS) -g -DKRN_EMUL -DSIZEOF_VOID_P=$(SIZEOF_VOID_P) -o $@ $^ -L$(CSP_LIB) $(CSP_EXTRA_LIBS) -lcapi10 -lpthread -lssp -lcapi20 -lrdrsup

WCKernelPartEmul.o: WCKernelPart.c
	$(CC) $(CFLAGS) $(add_CPPFLAGS) -g -c -DKRN_EMUL -DCSP_LITE -DCSP_DRIVER -DEMUL_DRIVER -DKSP_LITE -DSIZEOF_VOID_P=$(SIZEOF_VOID_P) -o $@ $^

WCKernelPart.o: WCKernelPart.c
	$(CC) $(CFLAGS) $(add_CPPFLAGS) -g -c -DCSP_LITE -DKSP_LITE -DCSP_DRIVER -DSIZEOF_VOID_P=$(SIZEOF_VOID_P) -o $@ $^


drtktls.ko: drtktls_linuxk.c WCKernelPart.c Module.symvers
	ln -sf Makefile.linux Makefile
	if test -z "$(KDIR)";then echo "No suitable kernel found";exit 1;fi
	$(MAKE) -C $(KDIR) $(SD)=$(shell pwd) $(MD) CFLAGS_MODULE="$(CFLAGS_MODULE)" || exit 1
	rm Makefile 
Module.symvers:
	cp $(MODVERSFILE) Module.symvers
clean:
	rm -f *.o *.ko WebClientKrn Makefile Module.symvers

