.INTERMEDIATE: drivers/$(DRIVER)/x86/drvgen.exe glide3x.dxe

GLIDESDK ?= g3sdk

CFLAGS += -I$(GLIDESDK)/include
LDFLAGS += -L$(GLIDESDK)/lib
LDLIBS += -lglide3x
ASFLAGS += -Idrivers/$(DRIVER)/x86/

DRIVER_SOURCES += \
	drivers/$(DRIVER)/drv_api.c \
	drivers/$(DRIVER)/drv_cb.c \
	drivers/$(DRIVER)/drv_setup.c \
	drivers/$(DRIVER)/drv_tex.c \
	drivers/$(DRIVER)/drv_texman.c \
	drivers/$(DRIVER)/drv_tri.c \
	drivers/$(DRIVER)/drv_vb.c

ifeq ($(X86),1)
DRIVER_SOURCES += \
	drivers/$(DRIVER)/x86/k3d_emit.asm \
	drivers/$(DRIVER)/x86/sse_emit.asm

drivers/$(DRIVER)/drv_api.o: drivers/$(DRIVER)/drvchk.h
endif

drivers/$(DRIVER)/x86/k3d_emit.o: drivers/$(DRIVER)/x86/k3d_emit.asm drivers/$(DRIVER)/x86/k3d_vbtmp.asm drivers/$(DRIVER)/x86/drv.inc
	$(AS) -o $@ $(ASFLAGS) $<

drivers/$(DRIVER)/x86/sse_emit.o: drivers/$(DRIVER)/x86/sse_emit.asm drivers/$(DRIVER)/x86/sse_vbtmp.asm drivers/$(DRIVER)/x86/drv.inc
	$(AS) -o $@ $(ASFLAGS) $<

drivers/$(DRIVER)/x86/drv.inc: drivers/$(DRIVER)/x86/drvgen.exe
	$< -o $@
drivers/$(DRIVER)/drvchk.h: drivers/$(DRIVER)/x86/drvgen.exe
	$< -c -o $@

drivers/$(DRIVER)/x86/drvgen.exe: drivers/$(DRIVER)/x86/drvgen.c glinternal.h main/context.h drivers/$(DRIVER)/drv.h
	$(HOST_CC) -o $@ $(CFLAGS) $<

# dxe3gen needs glide3x.dxe when generating the import lib libigl.a.
# so we generate a dummy glide3x.dxe to cover common glide3x exports
# and all possible libc dependencies:
lib/gl.dxe: glide3x.dxe
drivers/$(DRIVER)/glide3x_dxe.o: drivers/$(DRIVER)/glide3x_dxe.c
	$(CC) -O0 -Wall -o $@ -c $<
glide3x.dxe: drivers/$(DRIVER)/glide3x_dxe.o
	-$(DXE3GEN) -o glide3x.dxe -E _gr -E _gu -U $<

clean::
	-$(RM) drivers/$(DRIVER)/x86/k3d_emit.o
	-$(RM) drivers/$(DRIVER)/x86/sse_emit.o
	-$(RM) drivers/$(DRIVER)/glide3x_dxe.o
	-$(RM) drivers/$(DRIVER)/drvchk.h
	-$(RM) drivers/$(DRIVER)/x86/drv.inc

