# Microsoft NMake makefile to build Saffron.

CFLAGS=/nologo /O2 /EHsc /GR-
!IFNDEF MMC
MMC=mercury
!ENDIF
!IFDEF GRADE
GRADE_OPT=--grade=$(GRADE)
!ENDIF
!IFDEF PARALLEL
PARALLEL_OPT=-j $(PARALLEL)
!ENDIF

.SUFFIXES:

MMC_OPTS=--use-grade-subdirs --intermodule-optimization -O 7 --output-compile-error-lines 1024 --cflag /DGLOW_DLL
SAFFRON_MMC_OPTS=$(MMC_OPTS) $(GRADE_OPT) $(PARALLEL_OPT)

ALL=build_saffron
all: $(ALL)

UTILS=build_saffron_window build_saffron_glow build_saffron_tga
utils: $(UTILS)

DEMOS=build_saffron_cube_demo
demos: $(DEMOS)

MMATH_LINK_FLAGS=--search-lib-files-dir "$(MAKEDIR)\mmath" --init-file "$(MAKEDIR)\mmath\mmath.init"
# Used for utils, tests, and demos. That's why it requires the ROOT variable to be set in shell.
SAFFRON_LINK_FLAGS=--search-lib-files-dir "$(MAKEDIR)" --init-file "$(MAKEDIR)\saffron.init"
GLOW_INCLUDE_FLAGS=--c-include-dir "$(MAKEDIR)\glow"
# Transunit, used for testing.
TRANSUNIT_LINK_FLAGS=--search-lib-files-dir "$(MAKEDIR)\test\transunit" --init-file "$(MAKEDIR)\test\transunit\transunit.init" --link-object "$(MAKEDIR)\test\transunit\transunit.lib"
# TGA, used for some demos and has its own tests.
SAFFRON_TGA_LINK_FLAGS=--search-lib-files-dir "$(MAKEDIR)\util\tga" --init-file "$(MAKEDIR)\util\tga\saffron_tga.init"
SAFFRON_GLOW_LINK_FLAGS=--search-lib-files-dir "$(MAKEDIR)\util\glow" --init-file "$(MAKEDIR)\util\glow\saffron_glow.init" $(GLOW_INCLUDE_FLAGS)
SAFFRON_WINDOW_LINK_FLAGS=--search-lib-files-dir "$(MAKEDIR)\util\window" --init-file "$(MAKEDIR)\util\window\saffron_window.init"

# Primary libraries.
# Note that, for compatibility with older Mercury compilers, we try to copy XYZ.lib to libXYZ.lib.
saffron.lib: build_saffron
build_saffron: build_mmath
	$(MMC) $(SAFFRON_MMC_OPTS) $(MMATH_LINK_FLAGS) --make libsaffron

mmath\mmath.lib: build_mmath
build_mmath:
	cd mmath && $(MMC) $(SAFFRON_MMC_OPTS) --make libmmath

#Utils
glow\glow.dll: build_glow
glow\glow_dll.lib: build_glow
build_glow:
	cd glow && $(MAKE) /nologo /fnmakefile glow.dll

util\glow\saffron_glow.lib: build_saffron_glow
build_saffron_glow: build_saffron_window build_saffron
	cd util\glow && $(MMC) $(SAFFRON_MMC_OPTS) $(MMATH_LINK_FLAGS) $(SAFFRON_LINK_FLAGS) $(SAFFRON_WINDOW_LINK_FLAGS) $(GLOW_INCLUDE_FLAGS) --make libsaffron_glow

util\window\saffron_window.lib: build_saffron_window
build_saffron_window:
	cd util\window && $(MMC) $(SAFFRON_MMC_OPTS) $(MMATH_LINK_FLAGS) $(SAFFRON_LINK_FLAGS) --make libsaffron_window

build_saffron_tga:
	cd util\tga && $(MMC) $(SAFFRON_MMC_OPTS) --make libsaffron_tga

# Demos
demo\glow.dll: glow\glow.dll
	COPY /Y glow\glow.dll demo\glow.dll

# Shared resources between demos and test.
# All of these are copied from the test directory to the demo directory.
demo\res\crate.tga: test\res\tga\crate.tga
	COPY /Y test\res\tga\crate.tga demo\res\crate.tga

demo\res\ctc24.tga: test\res\tga\ctc24.tga
	COPY /Y test\res\tga\ctc24.tga demo\res\ctc24.tga

build_saffron_cube_demo: demo\res\crate.tga build_saffron build_saffron_tga build_saffron_window build_saffron_glow glow\glow_dll.lib demo\glow.dll
	cd demo && $(MMC) $(SAFFRON_MMC_OPTS) -E --make saffron_cube_demo $(MMATH_LINK_FLAGS) $(SAFFRON_LINK_FLAGS) $(SAFFRON_GLOW_LINK_FLAGS) $(SAFFRON_WINDOW_LINK_FLAGS) $(SAFFRON_TGA_LINK_FLAGS) -L "$(MAKEDIR)\glow" -lglow_dll --link-object "$(MAKEDIR)\mmath\libmmath.lib" --link-object "$(MAKEDIR)\libsaffron.lib" --link-object "$(MAKEDIR)\util\window\libsaffron_window.lib" --link-object "$(MAKEDIR)\util\glow\libsaffron_glow.lib" --link-object "$(MAKEDIR)\util\tga\libsaffron_tga.lib" -lOpenGL32

# Testing
build_transunit:
	cd test/transunit && $(MMC) $(SAFFRON_MMC_OPTS) --make libtransunit $(STATIC_MMC_FLAGS)

test_saffron_tga: build_transunit build_saffron_tga
	cd test && $(MMC) $(SAFFRON_MMC_OPTS) --make saffron_tga_test $(TRANSUNIT_LINK_FLAGS) $(SAFFRON_TGA_LINK_FLAGS)
	cd test\res/\ga && "$(MAKEDIR)\test\saffron_tga_test"

# Clean
# We set Path to be empty so that if we are building from an MSYS or Cygwin
# prompt, we don't get their commands.

clean_mmath:
	set "Path=" && RMDIR /Q /S mmath\Mercury
	set "Path=" && DEL /Q mmath\mmath.init mmath\*.lib mmath\*.mh mmath\*.err

clean_saffron:
	set "Path=" && RMDIR /Q /S Mercury
	set "Path=" && DEL /Q saffron.init *.lib *.mh *.err

clean_glow:
	cd glow && $(MAKE) /nologo /fnmakefile clean

clean_saffron_glow:
	set "Path=" && RMDIR /Q /S util\glow\Mercury
	set "Path=" && DEL /Q util\glow\saffron_glow.init util\glow\*.lib util\glow\*.mh util\glow\*.err

clean_saffron_tga:
	set "Path=" && RMDIR /Q /S util\tga\Mercury
	set "Path=" && DEL /Q util\tga\saffron_tga.init util\tga\*.lib util\tga\*.mh util\tga\*.err

clean_saffron_window:
	set "Path=" && RMDIR /Q /S util\window\Mercury
	set "Path=" && DEL /Q util\window\saffron_window.init util\window\*.lib util\window\*.mh util\window\*.err

clean_test_saffron_tga:
	set "Path=" && DEL /Q util\tga\saffron_tga.init util\tga\s*.lib util\tga\*.mh util\tga\*.err

clean_saffron_cube_demo:
	set "Path=" && DEL /Q demo\safron_cube_demo.exe

CLEAN=clean_mmath clean_saffron clean_saffron_glow clean_saffron_window clean_saffron_tga clean_glow clean_test_saffron_tga clean_saffron_cube_demo
clean: $(CLEAN)
	set "Path=" && DEL /Q demo\res\crate.tga demo\res\ctc24.tga
	set "Path=" && RMDIR /Q /S test\Mercury
	set "Path=" && DEL /Q test\*.mh test\*.err
	set "Path=" && RMDIR /Q /S demo\Mercury
	set "Path=" && DEL /Q demo\*.mh demo\*.err demo\*.dll

