# makefile.unx -- processed by configure script to form makefile

COMPILER = unix
UNIX_TOOLS = 1

# Pull in this variable from configure script before makefile.lst is included
# soi that we can set STATICLINK on time
SHARED = @SHARED@
ifndef SHARED
   STATICLINK = 1
endif

include make/makefile.gcc
include make/makefile.lst
include make/makefile.ver
EXT_HEADERS += $(EXT_HEADERS_PATH)/glx_ext_defs.h $(EXT_HEADERS_PATH)/glx_ext_api.h $(EXT_HEADERS_PATH)/glx_ext_list.h $(EXT_HEADERS_PATH)/glx_ext_alias.h


# Pull in variables from configure script
LIBS = @LIBS@
MESABUILD = @MESABUILD@
CPPFLAGS = $(addprefix -I, $(INCLUDE_DIR)) @CPPFLAGS@
LDFLAGS = @LDFLAGS@ -L$(LIB_DIR)
DEFS = @DEFS@
LIB_BUILDER = @LIB_BUILDER@
ALLEGRO_CONFIG = @ALLEGRO_CONFIG@

# Programs
CC = @CC@
CXX = @CXX@
DATA_INSTALLER = @INSTALL@ @INSTALL_DATA@ -D
PROG_INSTALLER = @INSTALL@ @INSTALL_PROGRAM@ -D


# Where to install the library and header file
DESTDIR =
prefix = @prefix@
exec_prefix = @exec_prefix@


# What to build
PLATFORM_SOURCE = @PLATFORM_SOURCE@
LIB_PATH_U = @LIB_PATH_U@
LIB_LINKS_U = @LIB_LINKS_U@

define LDCONFIG
   ldconfig    # works only if you are root
endef

HEADERS += include/alleggl_config.h
PLATFORM_OBJECT = $(addprefix $(OBJ_DIR)/, $(PLATFORM_SOURCE:.c=.o))
GENERAL_LIB_OBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(C_SOURCE:.c=.o)))
EXAMPLES = $(addprefix $(EXAMPLE_DIR)/, $(EXAMP_SOURCE:.c= ))

#-----------------------------------#
# --- Objects for common targets ---#

OBJECTS = $(PLATFORM_OBJECT) $(GENERAL_LIB_OBJS)
EXAMPLE_DIR_U = $(EXAMPLE_DIR)
COMPILER_INCLUDE_DIR_U = $(DESTDIR)@includedir@
COMPILER_LIB_DIR_U = $(DESTDIR)@libdir@
HEADERS_U = $(HEADERS)
EXT_HEADERS_U = $(EXT_HEADERS)
EXT_HEADERS_PATH_U = $(EXT_HEADERS_PATH)

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

ifdef DEBUGMODE
	CFLAGS = -g -W -Wall -Wno-unused
	CFLAGS += -DDEBUGMODE=$(DEBUGMODE)
	ifdef LOGLEVEL
		CFLAGS += -DLOGLEVEL=$(LOGLEVEL)
	endif
else
	CFLAGS = -O2 -Wall -ffast-math -fomit-frame-pointer
endif

ifdef SHARED
	LDLIBS = -l$(LIB_NAME)
else
	LDLIBS = $(LIB_PATH_U)
endif

ifdef DEBUGALLEG
	LDLIBS += `$(ALLEGRO_CONFIG) --libs debug` $(LIBS)
else
	LDLIBS += `$(ALLEGRO_CONFIG) --libs` $(LIBS)
endif

ifdef MESABUILD
	ifdef 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
		MESADIR_U = $(MESADIR)
		MESA_HEADERS = $(GL_HEADERS) @GLU_HEADERS@
		LIB_DIR_U = $(LIB_DIR)
		SRC_DIR_U = $(SRC_DIR)
		OBJ_DIR_U = $(OBJ_DIR)
		include make/makefile.gen
		GENERAL_LIB_OBJS += $(GL_OBJECTS) @BUILD_GLU@
	endif
endif

.PHONY: all
.PRECIOUS: %.o

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

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

include make/makefile.all

distclean: veryclean  @MESACLEAN@
	rm -f make/makefile.dep
	rm -f $(EXAMPLE_DIR)/dumbtest.bmp
	rm -f $(EXAMPLE_DIR)/screen.bmp
	rm -rf docs/top docs/txi
	rm -f config.* stamp-h makefile
	rm -f include/alleggl_config.h

depend:
	@echo "Generating dependencies..."
	@rm -f make/makefile.dep
	@echo "# Automatically generated by depend.sh" > make/makefile.dep
	@$(SHELL) -c 'misc/depend.sh include $(SRC_DIR) $(EXAMPLE_DIR)' >> make/makefile.dep

unixdist: distclean
	$(SHELL) -c './fix.sh unix dtou'
	-rm ../alleggl.tar.gz
	(cd .. && tar -czf alleggl.tar.gz alleggl)

windist: distclean
	$(SHELL) -c './fix.sh unix utod'
	-rm -f ../alleggl.zip
	(cd .. && zip -r alleggl.zip alleggl)

dist: unixdist windist

distlist:
	find . -name CVS -prune -o -print


include make/makefile.dep

$(LIB_PATH_U): $(GENERAL_LIB_OBJS) $(PLATFORM_OBJECT)
   define MAKE_LINKS
	$(foreach link, $(LIB_LINKS_U), - ln -sf $(notdir $(LIB_PATH_U)) $(link)
	)
   endef
	$(LIB_BUILDER) $@ $^
	- $(MAKE_LINKS)

$(EXAMPLE_DIR)/%: $(EXAMPLE_DIR)/%.c
	$(CC) -o $@ $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $< $(LDLIBS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(EXT_HEADERS)
	$(CC) -o $@ $(CPPFLAGS) $(CFLAGS) $(SHARED) -c $<
