#!/usr/bin/make -f
#############################################################################
#############################################################################
#
# lib/Makefile
#
#############################################################################
#############################################################################





#############################################################################
#
# variables, commands and options
#
#############################################################################




#############################################################################
# variables
#############################################################################

FILES:=example
OBJECTS_DEFAULT:=${patsubst %, %.o, ${FILES}}
OBJECTS_DEBUG:=${patsubst %, %.debug.o, ${FILES}}
TARGET_DEFAULT:=example
TARGET_DEBUG:=example_debug



#############################################################################
# commands and options
#############################################################################

# basic commands

CC:=gcc
LD:=${CC}
STRIP:=strip
TRUE:=true
RM:=rm -f

# options

CC_OPTIONS:=-O2 -Wall
LD_LIBRARY_DIRS:=-L.
LD_LIBRARIES:=-lisat3 -lm
LD_LIBRARIES_DEBUG:=-lisat3_debug -lm

# if the library is 32 bit, force using 32 bit (even in a 64 bit environment)

ifeq (${shell objdump -f libisat3.a 2>/dev/null | sed -n '/^.*file format/s///p' | sed 's/ //g' | sort -u},elf32-i386)
CC_OPTIONS+=-m32
LD_OPTIONS+=-m32
endif

# the complete commands used for compilation

CC_DEFAULT:=${CC} ${CC_OPTIONS}
CC_DEBUG:=${CC} ${CC_OPTIONS}

LD_DEFAULT:=${LD} ${LD_OPTIONS} ${LD_LIBRARY_DIRS}
LD_DEFAULT_LIBS:=${LD_LIBRARIES}
LD_DEBUG:=${LD} ${LD_OPTIONS} ${LD_LIBRARY_DIRS}
LD_DEBUG_LIBS:=${LD_LIBRARIES_DEBUG}

STRIP_DEFAULT:=${STRIP} -R .note -R .comment
STRIP_DEBUG:=${TRUE}




#############################################################################
#
# targets and rules
#
#############################################################################




#############################################################################
# common targets
#############################################################################
.PHONY: default all debug clean

default: ${TARGET_DEFAULT}

all: default

debug: ${TARGET_DEBUG}



#############################################################################
# rules for compiling object files
#############################################################################
%.o: %.c
	@echo ${CC} compiling $@
	${CC_DEFAULT} -c -o $@ $<

%.debug.o: %.c
	@echo ${CC} compiling $@
	${CC_DEBUG} -c -o $@ $<



#############################################################################
# rules for linking binaries or libraries
#############################################################################
${TARGET_DEFAULT}: ${OBJECTS_DEFAULT}
	@echo ${LD} linking $@
	${LD_DEFAULT} -s -o ${TARGET_DEFAULT} ${OBJECTS_DEFAULT} ${LD_DEFAULT_LIBS}
	@echo ${STRIP} mangling $@
	${STRIP_DEFAULT} ${TARGET_DEFAULT}

${TARGET_DEBUG}: ${OBJECTS_DEBUG}
	@echo ${LD} linking $@
	${LD_DEBUG} -s -o ${TARGET_DEBUG} ${OBJECTS_DEBUG} ${LD_DEBUG_LIBS}
	@echo ${STRIP} mangling $@
	${STRIP_DEBUG} ${TARGET_DEBUG}



#############################################################################
# cleanup
#############################################################################
clean:
	@echo cleaning up
	${RM} ${TARGET_DEFAULT} ${OBJECTS_DEFAULT}
	${RM} ${TARGET_DEBUG} ${OBJECTS_DEBUG}



#############################################################################
