#
#  Rules for building the Allegro library with QNX. This file is included
#  by the primary makefile, and should not be used directly.
#
#  The "depend" target uses sed.
#
#  See makefile.all for a list of the available targets.



# -------- define some variables that the primary makefile will use --------

PLATFORM = QNX
CC = gcc
OBJ_DIR = obj/qnx/$(VERSION)
LIB_NAME = lib/qnx/lib$(VERSION).a
EXE = 
OBJ = .o
HTML = html

PLATFORM_DIR = obj/qnx

UNIX_TOOLS = 1



# -------- give a sensible default target for make without any args --------

.PHONY: _default

_default: default



# -------- decide what compiler options to use --------

ifdef WARNMODE
   WFLAGS = -Wall -W -Werror -Wno-unused
else
   WFLAGS = -Wall -Wno-unused
endif

ifdef TARGET_ARCH_COMPAT
   TARGET_ARCH = $(GCC_MTUNE)=$(TARGET_ARCH_COMPAT)
else
   ifdef TARGET_ARCH_EXCL
      TARGET_ARCH = -march=$(TARGET_ARCH_EXCL)
   else
      TARGET_ARCH = $(GCC_MTUNE)=i586
   endif
endif

ifndef TARGET_OPTS
   TARGET_OPTS = -O2 -funroll-loops -ffast-math
endif

OFLAGS = $(TARGET_ARCH) $(TARGET_OPTS)

CFLAGS = -DALLEGRO_LIB_BUILD



ifdef DEBUGMODE

# -------- debugging build --------
CFLAGS += -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS) -g
SFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS)
LFLAGS = -g

else
ifdef PROFILEMODE

# -------- profiling build --------
CFLAGS += $(WFLAGS) $(OFLAGS) -pg
SFLAGS = $(WFLAGS)
LFLAGS = -pg

else

# -------- optimised build --------
CFLAGS += $(WFLAGS) $(OFLAGS) -fomit-frame-pointer
SFLAGS = $(WFLAGS)
ifndef SYMBOLMODE
   LFLAGS += -s
endif

endif
endif



# -------- list platform specific objects and programs --------

VPATH = src/qnx src/unix src/misc

ifdef ALLEGRO_USE_C

# ------ build a C-only version ------

VPATH += src/c
MY_OBJECTS = $(C_OBJECTS) cmiscs
CFLAGS += -DALLEGRO_USE_C

else

# ------ build the normal asm version ------

VPATH += src/i386
MY_OBJECTS = $(I386_OBJECTS)

endif # ALLEGRO_USE_C

OBJECT_LIST = $(COMMON_OBJECTS) $(MY_OBJECTS) $(basename $(notdir $(ALLEGRO_SRC_QNX_FILES)))

LIBRARIES = -lm -lph -lasound



# -------- rules for installing and removing the library files --------

INSTALLDIR = /usr
LIBDIR = lib
INCDIR = include

$(INSTALL_DIR)/lib/lib$(VERSION).a: $(LIB_NAME)
	cp $(LIB_NAME) $(INSTALL_DIR)/lib

$(INSTALL_DIR)/bin/allegro-config:
	cp misc/allegro-config-qnx.sh $(INSTALL_DIR)/bin/allegro-config
	chmod +x $(INSTALL_DIR)/bin/allegro-config


HEADERS = $(INSTALL_DIR)/include/qnxalleg.h                  \
          $(INSTALL_DIR)/include/allegro/platform/aintqnx.h  \
          $(INSTALL_DIR)/include/allegro/platform/al386gcc.h \
          $(INSTALL_DIR)/include/allegro/platform/alqnxcfg.h \
          $(INSTALL_DIR)/include/allegro/platform/alplatf.h  \
          $(INSTALL_DIR)/include/allegro/platform/astdint.h  \
          $(INSTALL_DIR)/include/allegro/platform/alqnx.h

INSTALL_FILES = $(INSTALL_DIR)/lib/lib$(VERSION).a \
                $(HEADERS)                         \
                $(INSTALL_DIR)/bin/allegro-config

install: generic-install
	@echo The $(DESCRIPTION) $(PLATFORM) library has been installed.

UNINSTALL_FILES = $(INSTALL_DIR)/lib/liballeg.a     \
                  $(INSTALL_DIR)/lib/liballd.a      \
                  $(INSTALL_DIR)/lib/liballp.a      \
                  $(HEADERS)                        \
                  $(INSTALL_DIR)/bin/allegro-config

uninstall: generic-uninstall
	@echo All gone!



# -------- test capabilities --------

TEST_CPP = $(CC) -fno-exceptions -c src/misc/test.cpp -o $(PLATFORM_DIR)/test.o

include makefile.tst



# -------- finally, we get to the fun part... --------

define MAKE_LIB
   ar rs $(LIB_NAME) $(OBJECTS)
endef

COMPILE_FLAGS = $(subst src/,-DALLEGRO_SRC ,$(findstring src/, $<))$(CFLAGS)

$(OBJ_DIR)/%.o: %.c
	$(CC) $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.cpp
	$(CC) -fno-exceptions $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.s
	$(CC) $(SFLAGS) -I. -I./include -x assembler-with-cpp -o $@ -c $<

demo/demo: $(OBJECTS_DEMO) $(LIB_NAME)
	$(CC) $(LFLAGS) -o $@ $(OBJECTS_DEMO) $(LIB_NAME) $(LIBRARIES)

*/%: $(OBJ_DIR)/%.o $(LIB_NAME)
	$(CC) $(LFLAGS) -o $@ $< $(LIB_NAME) $(LIBRARIES)

obj/qnx/asmdef.inc: obj/qnx/asmdef
	obj/qnx/asmdef obj/qnx/asmdef.inc

obj/qnx/asmdef: src/i386/asmdef.c include/*.h include/allegro/*.h obj/qnx/asmcapa.h
	$(CC) -O $(WFLAGS) -I. -I./include -o obj/qnx/asmdef src/i386/asmdef.c

define LINK_WITHOUT_LIB
   $(CC) $(LFLAGS) -o $@ $^
endef

PLUGIN_LIB = lib/qnx/lib$(VERY_SHORT_VERSION)dat.a
PLUGINS_H = obj/qnx/plugins.h
PLUGIN_DEPS = $(LIB_NAME) $(PLUGIN_LIB)
PLUGIN_SCR = scr

define GENERATE_PLUGINS_H
   cat tools/plugins/*.inc > obj/qnx/plugins.h
endef

define MAKE_PLUGIN_LIB
   ar rs $(PLUGIN_LIB) $(PLUGIN_OBJS)
endef

define LINK_WITH_PLUGINS
   $(CC) $(LFLAGS) -o $@ $< $(strip $(PLUGIN_LIB) $(addprefix @,$(PLUGIN_SCRIPTS)) $(LIB_NAME) $(LIBRARIES))
endef



# -------- generate automatic dependencies --------

DEPEND_PARAMS = -MM -MG -I. -I./include -DSCAN_DEPEND -DALLEGRO_QNX

depend:
	$(CC) $(DEPEND_PARAMS) src/*.c src/qnx/*.c src/unix/*.c src/i386/*.c src/misc/*.c demo/*.c > _depend.tmp
	$(CC) $(DEPEND_PARAMS) docs/src/makedoc/*.c examples/*.c setup/*.c tests/*.c tools/*.c tools/plugins/*.c >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) -x c tests/*.cpp >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) -x assembler-with-cpp src/i386/*.s src/qnx/*.s src/misc/*.s >> _depend.tmp
	sed -e "s/^[a-zA-Z0-9_\/]*\///" _depend.tmp > _depend2.tmp
	sed -e "s/^\([a-zA-Z0-9_]*\.o *:\)/obj\/qnx\/alleg\/\1/" _depend2.tmp > obj/qnx/alleg/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o *:\)/obj\/qnx\/alld\/\1/" _depend2.tmp > obj/qnx/alld/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o *:\)/obj\/qnx\/allp\/\1/" _depend2.tmp > obj/qnx/allp/makefile.dep
	rm _depend.tmp _depend2.tmp
