# ----------------------------------------
# Makefile for the Chunker (1-nl and 2-nl)
# ----------------------------------------


# ----------------
# Compiler options
#  -g    : Create debugging info. (for gdb)
#  -pg   : Create profiling info. (for gprof)
#  -O3   : Maximum optimization
#  -Wall : All warnings
#  -pipe : Use pipes instead of temporary file
CFLAGS = -pipe -Wall -O3
# -m64


# ------------
# Flex options
#  -B : Batch scanner (non-interactive)
#  -p : Performance report (use twice for extra information)
#LEXFLAGS = -B -p
LEXFLAGS = -I -p


# ------------
# Applications
chunker_one_App = chunker-one
chunker_two_App = chunker-two
APPS = $(chunker_one_App) $(chunker_two_App)

# ------------
# Object files
chunker_one_Objs = chunker-one_Parser.o 
chunker_two_Objs = chunker-two_Parser.o 
OBJS = $(chunker_one_Objs) $(chunker_two_Objs)

# ----------------------------------------
# Parser/scanner files (generated by flex)
chunker_one_Parser = chunker-one_Parser.c
chunker_two_Parser = chunker-two_Parser.c
PARSERS = $(chunker_one_Parser) $(chunker_two_Parser)


# =======
#  Rules
# =======


# ---------
# Top level
all: $(APPS)
	@echo
	@echo [1m--- Done! ---[0m

# --------------
# Chunkers (one)
$(chunker_one_App): chunker_one-nl.lex
	@echo
	@echo [1m--- Building and compiling $(chunker_one_App) parser ---[0m
	flex $(LEXFLAGS) -ochunker-one_Parser.c chunker_one-nl.lex
	gcc $(CFLAGS) -c chunker-one_Parser.c
	@echo [1m--- Creating $(chunker_one_App) executable ---[0m
	gcc $(CFLAGS) --static -o $(chunker_one_App) $(chunker_one_Objs) -lfl
	@rm -f chunker-one_Parser.c $(chunker_one_Objs)

# --------------
# Chunkers (two)
$(chunker_two_App): chunker_two-nl.lex
	@echo
	@echo [1m--- Building and compiling $(chunker_two_App) parser ---[0m
	flex $(LEXFLAGS) -ochunker-two_Parser.c chunker_two-nl.lex
	gcc $(CFLAGS) -c chunker-two_Parser.c
	@echo [1m--- Creating $(chunker_two_App) executable ---[0m
	gcc $(CFLAGS) --static -o $(chunker_two_App) $(chunker_two_Objs) -lfl
	@rm -f chunker-two_Parser.c $(chunker_two_Objs)

# -------------
# Phony targets
.PHONY: clean strip
clean:
	@echo
	@echo [1m--- Cleaning files ---[0m
	-rm -f *~ $(APPS) $(OBJS) $(PARSERS) gmon.out core
strip:
	@echo
	@echo [1m--- Stripping executables ---[0m
	strip $(APPS)
