ifndef config
  config=debug_gnu64
endif

.PHONY: all clean

sanitise =
defines =
includes = ./
data_dir = data
int_dir = intermediate
cflags = \
	$(includes) $(defines) $(conf_cflags) \
	-Wall -std=c90 -pedantic -static-libgcc $(sanitise)
lflags = $(conf_lflags) $(libs) -static-libgcc $(sanitise)
pack_cmd = ./packer
bmpconv_cmd = ./bmpconv

target=mallocbullet

ifeq ($(config), debug_gnu64)
  compiler = gcc
  defines = -DDEBUG -Dplat_sdl2 -Dplat_posix -Dplat_x86 -Dallow_floats
  includes = -I./
  conf_cflags = -g -O0 -m64
  conf_lflags = -m64 
  libs = -lm -lSDL2
endif

ifeq ($(config), release_gnu64)
  compiler = gcc
  defines = -DNDEBUG -Dplat_sdl2 -Dplat_posix -Dplat_x86 -Dallow_floats
  includes = -I./
  conf_cflags = -s -O3 -m64
  conf_lflags = -s -O3 -m64
  libs = -lm -lSDL2
endif

ifeq ($(config), debug_gnu32)
  compiler = gcc
  defines = -DDEBUG -Dplat_sdl2 -Dplat_posix -Dplat_x86 -Dallow_floats
  includes = -I./
  conf_cflags = -g -O0 -m32
  conf_lflags = -m32 
  libs = -lm -lSDL2
endif

ifeq ($(config), release_gnu32)
  compiler = gcc
  defines = -DNDEBUG -Dplat_sdl2 -Dplat_posix -Dplat_x86 -Dallow_floats
  includes = -I./
  conf_cflags = -s -O3 -m32
  conf_lflags = -s -O3 -m32
  libs = -lm -lSDL2
endif

ifeq ($(config), release_win32)
  target = mallocbullet.exe
  compiler = i686-w64-mingw32-gcc
  defines = -DNDEBUG -DSDL_MAIN_HANDLED -Dplat_sdl2 -Dplat_x86 -Dplat_windows -Dallow_floats
  includes = -I./ -I win/extern/SDL2/include
  conf_clfags = -s -O3 -m32 -static
  conf_lflags = -s -O3 -m32 -static
  libs = -Lwin/extern/SDL2/lib/gcc/x86 -lSDL2.dll -lSDL2main -lkernel32 -luser32 -lsetupapi
  pack_cmd = wine packer.exe
  bmpconv_cmd = wine bmpconv.exe
  postbuild = cp win/extern/SDL2/lib/gcc/x86/SDL2.dll ./
endif

ifeq ($(config), release_win64)
  target = mallocbullet.exe
  compiler = x86_64-w64-mingw32-gcc
  defines = -DNDEBUG -DSDL_MAIN_HANDLED -Dplat_sdl2 -Dplat_x86 -Dplat_windows -Dallow_floats
  includes = -I./ -I win/extern/SDL2/include
  conf_clfags = -s -O3 -m64 -static
  conf_lflags = -s -O3 -m64 -static -mwindows
  libs = -Lwin/extern/SDL2/lib/gcc/x64 -lSDL2.dll -lSDL2main -lkernel32 -luser32 -lsetupapi
  pack_cmd = wine packer.exe
  bmpconv_cmd = wine bmpconv.exe
  postbuild = cp win/extern/SDL2/lib/gcc/x64/SDL2.dll ./
endif

.PHONY: all clean pack_assets

all: $(target)

sources = \
  asset.c \
  main.c \
  malware.c \
  platform.c \
  rect.c \
  render.c \
  standard.c \


assets  = $(wildcard $(data_dir)/*)
objects = $(sources:%.c=%.o)

bitmaps = $(wildcard $(int_dir)/*.bmp)
images = $(bitmaps:$(int_dir)/%.bmp=$(data_dir)/%.img)

$(objects): %.o : %.c
	$(compiler) $(cflags) -MMD -MF $(@:%.o=%.d) -c $< -o $@ 

$(images): $(data_dir)%.img : $(int_dir)%.bmp | bmp_converter
	$(bmpconv_cmd) $< $@

bmp_converter:
	$(compiler) -O3 -Wall bmpconv.c -o bmpconv

assets: $(images)

pack_assets:
	$(compiler) -O3 -Wall pack.c -o packer
	$(pack_cmd) $(assets)

-include $(objects:%.o=%.d)

$(target): $(objects) assets pack_assets
	$(compiler) $(objects) $(lflags) -o $@
	$(postbuild)

clean:
	rm -f $(target)
	rm -f $(objects)
	rm -f $(objects:%.o=%.d)
	rm -f $(images)
	rm -f pack packer
	rm -f bmpconv