ifeq ($(OS),Windows_NT)
    DETECTED_OS := windows
else
    UNAME_S := $(shell uname -s)

    ifeq ($(UNAME_S),Darwin)
        DETECTED_OS := macos
    else ifeq ($(UNAME_S),Linux)
        DETECTED_OS := linux
    else
        DETECTED_OS := unknown
	endif
endif

CC = clang

UFLAGS =
# NOTE: We comply with C89 (plain C), however the standard has no 64-bit
#       support. But, every compiler at that time supported long long,
#       hence we ignore the incompliance in our code.
# NOTE: We allow function declrations with specifying void
#       (ex, void test() is allowed).
FLAGS = -ansi -pedantic -pedantic-errors -Wno-long-long \
		-Wno-strict-prototypes -Wall -Wextra -fno-caret-diagnostics \
		-fno-show-column -Wno-missing-field-initializers \
		-Wno-unused-function -Wno-unused-parameter -Wno-unused-variable \
		-Wno-unused-but-set-variable

ifeq ($(DETECTED_OS),macos)
	UFLAGS = -glldb
	INCLUDE = -I./sdl/include/ 
	LIBS = ./sdl/lib/arm-macos/libSDL2.a -lm
	FRAMEWORKS = -framework Cocoa -framework IOKit -framework CoreAudio \
				 -framework AudioToolbox -framework ForceFeedback \
				 -framework Carbon -framework GameController \
				 -framework Metal -framework QuartzCore \
				 -framework CoreHaptics
else ifeq ($(DETECTED_OS),linux)
	UFLAGS = -ggdb -D_POSIX_C_SOURCE=200809L 
	INCLUDE = -I./sdl/include/
	LIBS = ./sdl/lib/x86_64-linux/libSDL2.a -lm -lX11
	FRAMEWORKS = 
else ifeq ($(DETECTED_OS),windows)
	INCLUDE = 
	LIBS = 
	FRAMEWORKS = 
else
	INCLUDE = 
	LIBS = 
	FRAMEWORKS = 
endif

EXE=handmadehero

default: libhandmade $(EXE)

libhandmade: handmade.c
	@ $(CC) $(FLAGS) $(UFLAGS) -fPIC -shared $(INCLUDE) $^ $(LIBS) \
		$(FRAMEWORKS) -o libhandmade.so

$(EXE): sdl_handmade.c libhandmade
	@ $(CC) $(FLAGS) $(UFLAGS) $(INCLUDE) sdl_handmade.c $(LIBS) \
		$(FRAMEWORKS) -o $@

run:
	@ ./$(EXE)

tags: $(OBJS)
	ctags -R *.h *.c

clean:
	@rm -rf $(EXE) $(EXE).dSYM libhandmade.so libhandmade.so.dSYM
