TARGET=release
CFLAGS=$(ENV_CFLAGS)
LFLAGS=
debug_COMPILE=gcc -c
release_COMPILE=gcc -c
debug_LINK=ar rc
release_LINK=ar rc
debug_uns_LINK=ar rc
release_uns_LINK=ar rc
debug_RANLIB=ranlib
release_RANLIB=ranlib
debug_uns_RANLIB=ranlib
release_uns_RANLIB=ranlib
COMPILE=$($(TARGET)_COMPILE)
LINK=$($(TARGET)_LINK)
RANLIB=$($(TARGET)_RANLIB)

##########################
#
# makes debug by default
# NOTE: you need to mkdir debug
# since the makefile won't create the directory itself
#
##########################

# any build
all_CFLAGS=-I .

# debug build
debug_CFLAGS=$(all_CFLAGS) -g -D_DEBUG
debug_LFLAGS=
debug_uns_CFLAGS=$(debug_CFLAGS)
debug_uns_LFLAGS=$(debug_LFLAGS)

# release build
release_CFLAGS=$(all_CFLAGS)      -DNDEBUG  # -O3 breaks something
release_LFLAGS=
release_uns_CFLAGS=$(release_CFLAGS)
release_uns_LFLAGS=$(release_LFLAGS)

# figure out build-specific flags
target_CFLAGS=$($(TARGET)_CFLAGS)
target_LFLAGS=$($(TARGET)_LFLAGS)

# all .cpp files (or .C files)
CPPFILES=cfsqp.c qld.c

# include files that pretty much everything includes.
# FOR LAZINESS, just put every include file, so we don't have to worry about the dependencies too much
COMMON_INCLUDE_FILES=cfsqpusr.h

# extra dependencies (too lazy for this)
# StaticHuffman_StoreWeights.cpp : StaticHuffman_StoreWeights.h

# automatically inferred stuff
CPP_O_FILES=$(CPPFILES:%.c=$(TARGET)/%.o)
OBJS=$(CPP_O_FILES)
EXE=$(TARGET)/cfsqp.a

all : $(EXE)

$(CPP_O_FILES) : $(COMMON_INCLUDE_FILES)

$(EXE) : $(OBJS)
	$(LINK) $(EXE) $(target_LFLAGS) $(LFLAGS) $(OBJS)
	$(RANLIB) $(EXE)

$(TARGET)/%.o : %.c
	$(COMPILE) $(target_CFLAGS) $(CFLAGS) -o $@ $<

clean:
	rm -r $(TARGET)/*

dump_vars :
	echo "CPPFILES"
	echo $(CPPFILES)
	echo "COMMON_INCLUDE_FILES"
	echo $(COMMON_INCLUDE_FILES)
	echo "CPP_O_FILES"
	echo $(CPP_O_FILES)
	echo "EXE"
	echo $(EXE)


