SHELL=/bin/sh

APP_NAME=wow

OS := linux
LIBPATH =

CC := gcc
INCLUDE := -I include
LIB := -lraylib -lm -lyeslib
CFLAGS := -g -O2
OBJC_FLAGS := -fconstant-string-class=YSConstantString -fno-strict-aliasing -fexceptions -fobjc-exceptions -fobjc-std=objc1

#dirs
SRC=src
BUILD=build

C_FILES := $(shell find $(SRC) -name '*.c')
H_FILES := $(shell find $(SRC) -name '*.h')
M_FILES := $(shell find $(SRC) -name '*.m')
O_FILES := $(patsubst $(SRC)/%.m, $(BUILD)/%.o, $(M_FILES))
O_FILES += $(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(C_FILES))

ifeq ($(OS), windows)
	CC=x86_64-w64-mingw32-gcc
	LIBPATH := -L lib/win64
	LIB += -lopengl32 -lgdi32 -lwinmm -luser32 -l:objc-1.dll
endif

ifeq ($(OS), linux)
	LIBPATH := -L lib/linux64
	LIB += -l:libobjc.so
endif

.PHONY: all clean setup

all: $(APP_NAME)

$(APP_NAME): $(BUILD)/$(O_FILES)
	$(CC) $(INCLUDE) $(CFLAGS) $(OBJC_FLAGS) $(O_FILES) -o $(APP_NAME) $(LIBPATH) $(LIB)

$(BUILD)/%.o: $(SRC)/%.m
	$(CC) $(INCLUDE) $(CFLAGS) $(OBJC_FLAGS) -c $< -o $@

$(BUILD)/%.o: $(SRC)/%.c
	$(CC) $(INCLUDE) $(CFLAGS) -c $< -o $@

setup:
	$(shell mkdir -p $(BUILD))

clean:
	$(shell rm -rf $(APP_NAME) $(O_FILES))