include ../../sample_objdir.mk

TARGET := overlay_apng_test

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

# The real APNG decoder and the real gfx_surface; the video driver is
# the stub in stubs_retroarch.c, as in the other overlay samples.
SOURCES := \
	overlay_apng_test.c \
	stubs_retroarch.c \
	$(REPO_ROOT)/gfx/gfx_surface.c \
	$(REPO_ROOT)/gfx/gfx_surface_player.c \
	$(LIBRETRO_COMM_DIR)/formats/video_player.c \
	$(LIBRETRO_COMM_DIR)/formats/image_transfer.c \
	$(REPO_ROOT)/libretro-common/formats/image/image_yuv_blit.c \
	$(LIBRETRO_COMM_DIR)/compat/compat_strl.c \
	$(LIBRETRO_COMM_DIR)/compat/fopen_utf8.c \
	$(LIBRETRO_COMM_DIR)/encodings/encoding_crc32.c \
	$(LIBRETRO_COMM_DIR)/encodings/encoding_deflate.c \
	$(LIBRETRO_COMM_DIR)/encodings/encoding_utf.c \
	$(LIBRETRO_COMM_DIR)/features/features_cpu.c \
	$(LIBRETRO_COMM_DIR)/file/file_path.c \
	$(LIBRETRO_COMM_DIR)/file/file_path_io.c \
	$(LIBRETRO_COMM_DIR)/formats/image_texture.c \
	$(LIBRETRO_COMM_DIR)/formats/image_texture_set.c \
	$(LIBRETRO_COMM_DIR)/formats/png/rpng.c \
	$(LIBRETRO_COMM_DIR)/formats/png/rpng_apng.c \
	$(LIBRETRO_COMM_DIR)/lists/string_list.c \
	$(LIBRETRO_COMM_DIR)/memmap/memmap.c \
	$(LIBRETRO_COMM_DIR)/streams/file_stream.c \
	$(LIBRETRO_COMM_DIR)/streams/trans_stream.c \
	$(LIBRETRO_COMM_DIR)/streams/trans_stream_deflate.c \
	$(LIBRETRO_COMM_DIR)/streams/trans_stream_pipe.c \
	$(LIBRETRO_COMM_DIR)/string/rstrtod.c \
	$(LIBRETRO_COMM_DIR)/string/stdstring.c \
	$(LIBRETRO_COMM_DIR)/time/rtime.c \
	$(LIBRETRO_COMM_DIR)/vfs/vfs_implementation.c

OBJS := $(call sample_objs,$(SOURCES:.c=.o))

# image_texture.c is here for the narrowing gfx_surface.c calls; its
# loaders are dropped at the link rather than chased through the
# transfer code.
CFLAGS  += -Wall -std=gnu99 -g1 -O1 -DHAVE_RPNG -ffunction-sections \
           -I$(REPO_ROOT) -I$(LIBRETRO_COMM_DIR)/include
LDFLAGS += -Wl,--gc-sections -lm

ifeq ($(OS),Windows_NT)
CFLAGS += -flto
LDFLAGS += -flto
endif

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

all: $(TARGET) fixtures/anim.png

fixtures/anim.png: mkfixture.py
	@mkdir -p fixtures
	python3 mkfixture.py

$(TARGET): $(OBJS)
	$(CC) -o $@ $(OBJS) $(LDFLAGS)

check: all
	./$(TARGET) fixtures/anim.png

clean:
	rm -rf $(SAMPLE_OBJDIR)
	rm -f $(OBJS) $(TARGET)

.PHONY: all check clean

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