# Definitions

CC = gcc
RM = rm
MKDIR = mkdir
CFLAGS = -I$(INC_DIR) -Wall -pedantic -O3
LDFLAGS =

#   the following automatic-variables are used:
#   	$@ is the target name 
#   	$^ is the list of all prerequisites, duplicates removed
COMPILE = $(CC) $(LDFLAGS) $(CFLAGS) -o $(BIN_DIR)/$@
#
#   the following is not used because we need to work with Make v2.7.2.3
#   which does not support it:
#   	$^ is the list of all prerequisites, duplicates removed

# project directory prefix
PROJ_PRE = .
# project directory, followed by '/'
PROJ_DIR = $(PROJ_PRE)/

# directory suffixes
INC_DIR = $(PROJ_DIR)src/include
BIN_POST = bin
BIN_DIR = $(PROJ_DIR)$(BIN_POST)

VPATH = $(PROJ_DIR)src;$(PROJ_DIR)src/include;$(PROJ_DIR)src/tests;$(PROJ_DIR)src/level1;$(PROJ_DIR)src/global;$(BIN_DIR)

OBJboth = arghelp.o bintree.o bufferio.o header.o prefixio.o treeio.o error.o\
          myvalues.o globals.o str.o procargs.o maketree.o minheap.o fileio.o

OBJhuff = compress.o expand.o view.o encode.o decode.o

OBJcm = chop.o merge.o

OBJtests = random.o _misc.o _error.o _bintree.o _bufferi.o _minheap.o\
           _maketre.o _encdec.o _header.o _treeio.o _fileio.o

GLOBAL = error.h globals.h myvalues.h

# Projects

all: chopmerge huffman tests

CHOPMERGE_EXE = chopmerg.exe
HUFF_EXE = huff.exe

.PHONY: PRE chopmerge huffman tests

chopmerge: PRE $(CHOPMERGE_EXE)
huffman  : PRE $(HUFF_EXE)
tests    : PRE _misc.exe _error.exe _bintree.exe _bufferi.exe _minheap.exe\
           _maketre.exe _encdec.exe _header.exe _treeio.exe _prefio.exe\
           _fileio.exe
PRE      : 
	-$(MKDIR) bin

# Executables

OBJ_CHOPMERGE_EXE = chopmerg.o $(OBJboth) $(OBJcm)
$(CHOPMERGE_EXE): $(OBJ_CHOPMERGE_EXE)
	$(COMPILE) $(OBJ_CHOPMERGE_EXE)

OBJ_HUFF_EXE = huff.o $(OBJboth) $(OBJhuff)
$(HUFF_EXE): $(OBJ_HUFF_EXE)
	$(COMPILE) $(OBJ_HUFF_EXE)

OBJ_misc.exe = _misc.o myvalues.o
_misc.exe: $(OBJ_misc.exe) chk_lfn.h
	$(COMPILE) $(OBJ_misc.exe)

OBJ_error.exe = _error.o error.o
_error.exe: $(OBJ_error.exe)
	$(COMPILE) $(OBJ_error.exe)

OBJ_bintree.exe =_bintree.o bintree.o error.o random.o
_bintree.exe: $(OBJ_bintree.exe)
	$(COMPILE) $(OBJ_bintree.exe)

OBJ_bufferi.exe =_bufferi.o bufferio.o random.o
_bufferi.exe: $(OBJ_bufferi.exe)
	$(COMPILE) $(OBJ_bufferi.exe)

OBJ_minheap.exe = _minheap.o minheap.o error.o bintree.o random.o
_minheap.exe: $(OBJ_minheap.exe)
	$(COMPILE) $(OBJ_minheap.exe)

OBJ_maketre.exe = _maketre.o bintree.o maketree.o minheap.o error.o random.o
_maketre.exe: $(OBJ_maketre.exe)
	$(COMPILE) $(OBJ_maketre.exe)

OBJ_encdec.exe = _encdec.o encode.o decode.o maketree.o minheap.o error.o\
           bintree.o bufferio.o random.o fileio.o
_encdec.exe: $(OBJ_encdec.exe)
	$(COMPILE) $(OBJ_encdec.exe)

OBJ_header.exe = _header.o header.o
_header.exe: $(OBJ_header.exe)
	$(COMPILE) $(OBJ_header.exe)

OBJ_treeio.exe = _treeio.o treeio.o maketree.o minheap.o error.o bintree.o\
             bufferio.o random.o
_treeio.exe: $(OBJ_treeio.exe)
	$(COMPILE) $(OBJ_treeio.exe)

OBJ_prefio.exe = _prefio.o prefixio.o header.o treeio.o maketree.o minheap.o\
           error.o bintree.o bufferio.o random.o
_prefio.exe: $(OBJ_prefio.exe)
	$(COMPILE) $(OBJ_prefio.exe)

OBJ_fileio.exe = _fileio.o fileio.o random.o
_fileio.exe: $(OBJ_fileio.exe)
	$(COMPILE) $(OBJ_fileio.exe)

# Objects
# because of the implicit rules, we can omit the base .c files

# globals

error.o   : error.h      $(GLOBAL)
globals.o : globals.h
myvalues.o: myvalues.h   chk_lfn.h

# level1

huff.o     : arghelp.h    view.h    compress.h expand.h  procargs.h  $(GLOBAL)
chopmerg.o : arghelp.h    procargs.h $(GLOBAL)

arghelp.o  : arghelp.h    $(GLOBAL)
view.o     : view.h       header.h   $(GLOBAL)
compress.o : compress.h   encode.h   prefixio.h   header.h   str.h   bintree.h\
	     $(GLOBAL)
expand.o   : expand.h     decode.h   prefixio.h   header.h   str.h   bintree.h\
             $(GLOBAL)
procargs.o : procargs.h   str.h      $(GLOBAL)

chop.o     : prefixio.h   $(GLOBAL)
merge.o    : prefixio.h   $(GLOBAL)

# other

bintree.o  : bintree.h    $(GLOBAL)
bufferio.o : bufferio.h   $(GLOBAL)
minheap.o  : minheap.h    $(GLOBAL)

header.o   : header.h     bintree.h   $(GLOBAL)
prefixio.o : prefixio.h   header.h    $(GLOBAL)
treeio.o   : treeio.h     bintree.h   bufferio.h   $(GLOBAL)
encode.o   : encode.h     bintree.h   bufferio.h   $(GLOBAL)
decode.o   : decode.h     bintree.h   bufferio.h   $(GLOBAL)
maketree.o : maketree.h   bintree.h   $(GLOBAL)
str.o      : str.h        $(GLOBAL)
fileio.o   : fileio.h     $(GLOBAL)

# header dependencies
# myvalues.h : chk_lfn.h

# Cleaning

# command-argument for removing the binaries directory in UNIX, DOS
UNIX_ARG=$(BIN_DIR)/*.exe
ARG="$(PROJ_PRE)\$(BIN_POST)\*.exe"

.PHONY: clean
clean:
	-$(RM) *.o
	-$(RM) $(ARG)

# changes
# -------

# 17/01/2003: moved 'chk_lfn.h' as a dependency of 'myvalues.o' instead
#             of 'myvalues.h' (as the latter is not a target)
#
#             added 'chk_lfn.h' as a dependency of '_misc.exe'
