# The GLSL/Cg render chains of gfx/drivers/gl2.c and gfx/drivers/gl3.c
# against a scripted GL and shader backend: each test includes its driver
# source and gl_last_pass_fakes.h, which holds the GL entry points, the
# backend and the cases the two chains must decide alike.  The rest of the
# frontend is stubbed by the gl2_pbo_readback harness's stubs, shared so
# the harnesses stay in step.

include ../../sample_objdir.mk

TARGETS := gl2_last_pass_fbo_test gl3_last_pass_fbo_test

CORE_DIR          := ../../..
LIBRETRO_COMM_DIR := $(CORE_DIR)/libretro-common

SHARED_C := \
	../gl2_pbo_readback/stubs.c \
	$(CORE_DIR)/gfx/common/gl_common.c \
	$(CORE_DIR)/gfx/video_views.c \
	$(LIBRETRO_COMM_DIR)/gfx/gl_capabilities.c \
	$(LIBRETRO_COMM_DIR)/glsym/glsym_gl.c \
	$(LIBRETRO_COMM_DIR)/gfx/scaler/scaler.c \
	$(LIBRETRO_COMM_DIR)/gfx/scaler/scaler_int.c \
	$(LIBRETRO_COMM_DIR)/gfx/scaler/scaler_filter.c \
	$(LIBRETRO_COMM_DIR)/gfx/scaler/pixconv.c \
	$(LIBRETRO_COMM_DIR)/encodings/encoding_utf.c \
	$(LIBRETRO_COMM_DIR)/compat/compat_strl.c \
	$(LIBRETRO_COMM_DIR)/string/stdstring.c \
	$(LIBRETRO_COMM_DIR)/memmap/memalign.c

SHARED_OBJS := $(call sample_objs,$(SHARED_C:.c=.o))

INCDIRS := -I$(LIBRETRO_COMM_DIR)/include -I$(CORE_DIR) -I$(CORE_DIR)/deps -I$(CORE_DIR)/gfx/include
CFLAGS  += -Wall $(INCDIRS) -std=gnu99 -D_GNU_SOURCE \
	-DRARCH_INTERNAL -DHAVE_THREADS -DHAVE_CONFIGFILE -DHAVE_OPENGL \
	-DHAVE_OPENGL_CORE -DHAVE_GL_SYNC -DHAVE_GLSL -DHAVE_MENU
LDFLAGS += -lm -lpthread

ifneq ($(SANITIZER),)
   CFLAGS  := -fsanitize=$(SANITIZER) -fno-omit-frame-pointer -g1 $(CFLAGS)
   LDFLAGS := -fsanitize=$(SANITIZER) $(LDFLAGS)
else
   CFLAGS  += -g1 -O1
endif

all: $(TARGETS)

gl2_last_pass_fbo_test: gl2_last_pass_fbo_test.o $(SHARED_OBJS)
	$(CC) -o $@ $^ $(LDFLAGS)

gl3_last_pass_fbo_test: gl3_last_pass_fbo_test.o $(SHARED_OBJS)
	$(CC) -o $@ $^ $(LDFLAGS)

gl2_last_pass_fbo_test.o: $(CORE_DIR)/gfx/drivers/gl2.c $(CORE_DIR)/gfx/common/gl2_common.h gl_last_pass_fakes.h ../gl2_pbo_readback/gl11_fakes.h
gl3_last_pass_fbo_test.o: $(CORE_DIR)/gfx/drivers/gl3.c gl_last_pass_fakes.h ../gl2_pbo_readback/gl11_fakes.h

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

$(SAMPLE_OBJDIR)/%.o: $$(call sample_src,$$*).c
	@mkdir -p $(@D)
	$(CC) $(CFLAGS) -c -o $@ $<

check: $(TARGETS)
	@for t in $(TARGETS); do echo "$$t:"; ./$$t || exit 1; done

clean:
	rm -rf $(SAMPLE_OBJDIR)
	rm -f *.o $(TARGETS) $(addsuffix .exe,$(TARGETS))

.PHONY: all check clean
