# This is a generic makefile for any single library module.
# Copyright (C) 2004-2008 by Pawel Pilarczyk.
# This is free software. No warranty. Consult 'license.txt' for details.
# Created on October 26, 2004 by PwP. Last revision: October 14, 2008.


# ===========================================================================
# Determine the target configuration (see the file 'makecfg' for details).
# ===========================================================================

include makecfg


# ===========================================================================
# Default module to compile (overridden in 'makefile').
# ===========================================================================

module = krak
MODULE = ${module}


# ===========================================================================
# Include external definitions.
# ===========================================================================

# define the home directory
HOME = ../

# include the compiler-dependent definitions
include config/${TARGET}

# include the definitions of paths
include makedirs


# ===========================================================================
# Main targets.
# ===========================================================================

# Determine the list of all the source files in the module.
SRCFILES = ${wildcard ${SRC}${MODULE}/*.cpp}
FILES = ${basename ${SRCFILES}}

# Determine the list of all the object files in the project.
OBJFILES = ${FILES:${SRC}%=${OBJ}%${OBJEXT}}

# Define the name of the library file corresponding to this module.
LIBRARY = ${LIB}lib${LIBNAME}${MODULE}${LIBEXT}

# The main targets: dependencies, objects, library.
.PHONY: ${MODULE}
${MODULE}: rundeps auto_dep/${MODULE} objdir libdir ${OBJFILES} ${LIBRARY}

# The object base directory.
.PHONY: objbasedir
objbasedirfile = $(wildcard ${OBJ})
objbasedir:
ifeq (${objbasedirfile},)
	-${MKDIR} ${OBJ}
else
endif

# The object subdirectory of the module.
.PHONY: objdir
objdirfile = $(wildcard ${OBJ}${MODULE})
objdir: objbasedir
ifeq (${objdirfile},)
	-${MKDIR} ${OBJ}${MODULE}
else
endif

# The file containing all the dependencies depends on the source CPP files.
auto_dep/${MODULE}: ${SRCFILES}

# Running "filedeps" to create the file containing all the dependencies.
.PHONY: rundeps
ifneq (${wildcard ${HOME}bin/filedeps*},)
rundeps:
	-${HOME}bin/filedeps --quiet -d -o auto_dep/${MODULE} \
	${INC:%=-I%} -s${SRC} ${FILES:%=-l%}
auto_dep/${MODULE}:
	-${HOME}bin/filedeps --quiet -d -o auto_dep/${MODULE} \
	${INC:%=-I%} -s${SRC} ${FILES:%=-l%}
	-touch auto_dep/${MODULE}
endif

# The library directory.
.PHONY: libdir
libdirfile = $(wildcard ${LIB})
libdir:
ifeq (${libdirfile},)
	-${MKDIR} ${LIB}
else
endif

# The library corresponding to this module.
${LIBRARY}: ${OBJFILES}
	${MAKELIB} ${LIBRARY} ${OBJFILES}
	${IDXLIB} ${LIBRARY}


# ===========================================================================
# Rules for MAKE on how to build object files.
# ===========================================================================

# prepare definitions for the dependencies in the external file
CCI = ${COMPILE} ${INC:%/=-I%} ${SYSINCL}
SRCLIB = ${SRC}
vpath %.h ${INC}
vpath %.hpp ${INC}
vpath %.hxx ${INC}

# include the dependencies of the files in this module
include auto_dep/${MODULE}


# That's all, folks!

