# -------------------------
# Makefile for LX-Tokenizer
# -------------------------

# ----------------
# 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

# ------------
# Flex options
#  -B : Batch scanner (non-interactive)
#  -p : Performance report (use twice for extra information)
#  -s : Default rule causes an error (useful to find holes in rule set)
#LEXFLAGS = -B -p
LEXFLAGS = -I -pp -8 -Cr

# ----------------
# Main application
APP = tokenizer

# -----------
# Source file
SRC = tokenizer.lex

# --------------------------------
# Parser files (generated by flex)
PARSER_SRC = parser.c
PARSER_OBJ = parser.o
PARSER_ALL = $(PARSER_SRC) $(PARSER_OBJ)

# ------------
# Object files
OBJS = avlt.o


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


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

# ---------------
# Tokenizer (top)
$(APP): $(SRC) $(OBJS)
	@echo
	@echo [1m--- Building and compiling $(APP) parser ---[0m
	flex $(LEXFLAGS) -o$(PARSER_SRC) $(SRC)
	gcc $(CFLAGS) -c $(PARSER_SRC)
	@echo [1m--- Creating $(APP) executable ---[0m
	gcc $(CFLAGS) -o $(APP) $(OBJS) $(PARSER_OBJ) -lfl
	@rm -f $(PARSER_ALL) 

# -----------------------------
# Auxiliary functions/libraries
avlt.o: avlt.c
	@echo
	@echo [1m--- Compiling AVL tree library ---[0m
	gcc $(CFLAGS) -c avlt.c

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

