include ../../sample_objdir.mk

TARGET := task_queue_init_failure_test

LIBRETRO_COMM_DIR := ../../..

# Built with HAVE_THREADS, since the failure under test is the threaded
# init hook not producing a worker. sthread_create() is wrapped so the
# failure can be produced on demand.
SOURCES := \
	task_queue_init_failure_test.c \
	$(LIBRETRO_COMM_DIR)/queues/task_queue.c \
	$(LIBRETRO_COMM_DIR)/rthreads/rthreads.c \
	$(LIBRETRO_COMM_DIR)/features/features_cpu.c \
	$(LIBRETRO_COMM_DIR)/time/rtime.c

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

CFLAGS  += -Wall -pedantic -std=gnu99 -g1 -O1 -fno-omit-frame-pointer \
           -DHAVE_THREADS -I$(LIBRETRO_COMM_DIR)/include
LIBS    += -lpthread

# Apple's linker has no --wrap: there task_queue.c calls the wrapper by
# name, and the test's __real_ call names the real function.
ifneq ($(shell uname -s),Darwin)
   LDFLAGS += -Wl,--wrap=sthread_create
else
   TASK_QUEUE_O := $(call sample_objs,$(LIBRETRO_COMM_DIR)/queues/task_queue.o)
   $(TASK_QUEUE_O): CFLAGS += -Dsthread_create=__wrap_sthread_create
   $(TARGET).o: CFLAGS += -D__real_sthread_create=sthread_create
endif

ifneq ($(SANITIZER),)
   CFLAGS  := -fsanitize=$(SANITIZER) $(CFLAGS)
   LDFLAGS := -fsanitize=$(SANITIZER) $(LDFLAGS)
endif

all: $(TARGET)

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

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

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

check: $(TARGET)
	./$(TARGET)

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

.PHONY: all check clean
