
COMPILER = djgpp

include make/makefile.lst

#-------------------------------#
# --- Compiler optimizations ---#

GCC_OPT  := -mcpu=i586
ifdef PENTIUMONLY
GCC_OPT  := -march=i586
endif
ifdef PPROONLY
GCC_OPT  := -march=i686
endif

HEADERS += $(GL_HEADERS) $(GLU_HEADERS)


ifdef DEBUGMODE

FLAGS = -DDEBUGMODE -g -W -Wno-unused

ifdef LOGLEVEL
FLAGS += -DLOGLEVEL=$(LOGLEVEL)
endif

else

FLAGS = -Wall -O2 -ffast-math -fomit-frame-pointer $(GCC_OPT)

endif

SRC_DIR_U = $(subst \,/,$(SRC_DIR))
SRC_DIR_D = $(subst /,\,$(SRC_DIR))

OBJ_DIR_U = $(subst \,/,$(OBJ_DIR))
OBJ_DIR_D = $(subst /,\,$(OBJ_DIR))

LIB_DIR_U = $(subst \,/,$(LIB_DIR))
LIB_DIR_D = $(subst /,\,$(LIB_DIR))

EXAMPLE_DIR_U = $(subst \,/,$(EXAMPLE_DIR))
EXAMPLE_DIR_D = $(subst /,\,$(EXAMPLE_DIR))

LIB_PATH_U = $(subst \,/,$(LIB_DIR))/lib$(LIB_NAME).a
LIB_PATH_D = $(subst /,\,$(LIB_DIR))\lib$(LIB_NAME).a

HEADERS_U = $(subst \,/,$(HEADERS))
HEADERS_D = $(subst /,\,$(HEADERS))

SOURCE = $(C_SOURCE) $(DOS_SOURCE)
OBJECTS = $(addprefix $(OBJ_DIR_U)/, $(addsuffix .o, $(basename $(notdir $(SOURCE)))))
EXAMPLES = $(addsuffix .exe, $(basename $(addprefix $(EXAMPLE_DIR)/, $(EXAMP_SOURCE))))

DJ_LINK_LIB = $(addprefix -l, $(LINK_LIB)) $(GLU_LIB) $(GL_LIB)


CFLAGS = -DALLEGROGL_GENERIC_DRIVER $(FLAGS)
CPPFLAGS = -I. -I$(INCLUDE_DIR) -I$(INCLUDE) -I$(INCDIR)
LDLIBS = $(LIB_PATH_U) $(DJ_LINK_LIB)


#--------------------------------------#
#-- Check for environment variables ---#

.PHONY: baddjgpp badmesa

ifdef DJDIR
  DJDIR_U = $(subst \,/,$(DJDIR))
  DJDIR_D = $(subst /,\,$(DJDIR))
else
baddjpp:
	@echo DJGPP Installation Error: DJDIR not defined.
endif

ifdef MESADIR
  MESADIR_U = $(subst \,/,$(MESADIR))
  MESADIR_D = $(subst /,\,$(MESADIR))
  ifneq ($(wildcard $(MESADIR)/docs/RELNOTES-4.0.2),)
    CFLAGS += -DMESA_4_0_2
  endif
  ifneq ($(wildcard $(MESADIR)/docs/RELNOTES-5.0),)
    CFLAGS += -DMESA_5_0
  endif
else
badmesa:
	@echo Please specify your MESA directory via MESADIR=. See the readme.dj file!
endif

include make/makefile.gen


lib: $(LIB_PATH_U)
examples: $(EXAMPLES)

#----------------------------#
#---- Various targets -------#

clean:
	@echo Cleaning up...
ifdef UNIX_TOOLS
	- rm -f $(OBJ_DIR_U)/*.o
	- rm -f $(EXAMPLE_DIR_U)/*.o
else
	- del $(OBJ_DIR_D)\*.o
	- del $(EXAMPLE_DIR_D)\*.o
endif

veryclean: clean
ifdef UNIX_TOOLS
	- rm -f $(LIB_PATH_U)
	- rm -f $(EXAMPLE_DIR_U)/*.exe
	- rm -rf docs/html
	- rm -rf docs/rtf
else
	- del $(LIB_PATH_D)
	- del $(EXAMPLE_DIR_D)\*.exe
	- deltree /y docs\html
	- deltree /y docs\rtf
endif

install: $(LIB_PATH_U) $(HEADERS_U)
	copy $(LIB_DIR_D)\*.a $(DJDIR_D)\lib
	@echo $(foreach file, $(HEADERS_D), $(shell copy $(file) $(DJDIR_D)\include)) > nul
	mkdir -p $(DJDIR_D)\include\GL
	copy include\GL\*.h $(DJDIR_D)\include\GL
	copy $(MESADIR_D)\include\GL\gl.h $(DJDIR_D)\include\GL
	copy $(MESADIR_D)\include\GL\glext.h $(DJDIR_D)\include\GL
	copy $(MESADIR_D)\include\GL\glu.h $(DJDIR_D)\include\GL
	@echo -
	@echo AllegroGL is now installed.

uninstall:
	- del $(MINGDIR_D)\lib\$(notdir $(LIB_PATH_U))
	@echo $(foreach file, $(HEADERS_D), $(shell del $(DJDIR_D)\include\$(notdir $(file)))) >  nul
	@echo -
	@echo AllegroGL is uninstalled.

include $(MESADIR_D)\src\depend

$(OBJ_DIR_U)/amesa.o: $(SRC_DIR_U)/amesa/amesa.c $(INCLUDE)/GL/amesa.h $(SRC_DIR_U)/amesa/raster.h $(SRC_DIR_U)/amesa/generic.h

$(OBJ_DIR_U)/raster.o: $(SRC_DIR_U)/amesa/raster.c $(INCLUDE)/GL/amesa.h $(SRC_DIR_U)/amesa/raster.h $(SRC_DIR_U)/amesa/generic.h

$(OBJ_DIR_U)/djgpp.o: $(SRC_DIR_U)/djgpp.c $(INCLUDE)/GL/amesa.h $(HEADERS_U)

$(OBJ_DIR_U)/%.o: $(SRC_DIR_U)/%.c $(addprefix include/,$(C_DEPEND))
	gcc -c $< -o $@ $(CFLAGS) $(CPPFLAGS)

$(LIB_PATH_U): $(OBJECTS) $(HEADERS_U)
	ar rs $@ $(OBJECTS)
	
$(EXAMPLE_DIR)/%.exe: $(EXAMPLE_DIR)/%.c $(HEADERS_U) $(LIB_PATH_U) $(GL_LIB) $(GLU_LIB)
	gcc $< -o $@ $(CFLAGS) $(CPPFLAGS) $(LDLIBS)
