# =================================================
#	@(#)makefile	1.4					20200731
#	@(#)makefile	1.3		AKK/ACT		20170507
#	@(#)makefile	1.2		AKK/ACT		20170329
# =================================================

# ---------------------------------------------------------------
# PROJECT-SPECIFIC DEFINITIONS
# ---------------------------------------------------------------

# Adjust these lines according to your project

# To add new targets, simply copy one of the below targets
# and change the names and paths to match your own

# ==== Dotenv targets ====

.PHONY		: dotenv dotenv-clean

dotenv		: export GENDIR:=../parser/dotenv
dotenv		: export GRAMMAR_G4:=Dotenv.g4
dotenv		:
	@$(MAKE) -e _antlr

dotenv-clean: export GENDIR:=../parser/dotenv
dotenv-clean: export GRAMMAR_G4:=Dotenv.g4
dotenv-clean:
	@$(MAKE) -e _clean

# ==== Line targets ====

.PHONY		: line line-clean

line		: export GENDIR:=../parser/line
line		: export GRAMMAR_G4:=Line.g4
line		:
	@$(MAKE) -e _antlr

line-clean	: export GENDIR:=../parser/line
line-clean	: export GRAMMAR_G4:=Line.g4
line-clean	:
	@$(MAKE) -e _clean

# ==== Variables ====

# Where to find the ANTLR4 executable in your system
ANTLR4		:= /usr/local/bin/antlr4

# The target language is C++
ANTLR4FLAGS := -Dlanguage=Cpp

# Some more ANTLR4 options:
# Do not generate Visitor classes
ANTLR4FLAGS += -no-visitor
# Do generate Listener classes
ANTLR4FLAGS += -listener

# ---------------------------------------------------------------
# Changes below this line are rarely needed
# ---------------------------------------------------------------

# ==== ANTLR4 stuff ====

# This lets 'make' derive the base name of that grammar
GRAMMAR 	:= $(GRAMMAR_G4:.g4=)

# If GENDIR was defined then this tells ANTLR4 that
# it should place the generated files there
ifneq ($(strip $(GENDIR) ),)
ANTLR4FLAGS += -o $(GENDIR)
endif

# The minimal set of generated files needed for any grammar
ifneq ($(strip $(GENDIR) ),)	# if GENDIR was defined
NEEDED.h	:= $(GENDIR)/$(GRAMMAR)Lexer.h   $(GENDIR)/$(GRAMMAR)Parser.h
NEEDED.cpp	:= $(GENDIR)/$(GRAMMAR)Lexer.cpp $(GENDIR)/$(GRAMMAR)Parser.cpp
else
NEEDED.h	:= $(GRAMMAR)Lexer.h   $(GRAMMAR)Parser.h
NEEDED.cpp	:= $(GRAMMAR)Lexer.cpp $(GRAMMAR)Parser.cpp
endif

# Make a list of all the needed source files
NEEDED		:= $(NEEDED.h) $(NEEDED.cpp)

# Which generated files really *do* exist (e.g. for clean-up)
ifneq ($(strip $(GENDIR) ),)	# if GENDIR was defined
GENERATED	:= $(wildcard $(GENDIR)/$(GRAMMAR)*.h) \
			   $(wildcard $(GENDIR)/$(GRAMMAR)*.cpp) \
			   $(wildcard $(GENDIR)/$(GRAMMAR)*.interp) \
			   $(wildcard $(GENDIR)/$(GRAMMAR)*.tokens)
else
GENERATED	:= $(wildcard $(GRAMMAR)*.h) \
			   $(wildcard $(GRAMMAR)*.cpp) \
			   $(wildcard $(GRAMMAR)*.interp) \
			   $(wildcard $(GRAMMAR)*.tokens)
endif

# ---------------------------------------------------------------
# MAKE TARGETS
# ---------------------------------------------------------------

.PHONY		: DEFAULT help

# The default target tells the user about the available targets
DEFAULT		: help

# Give some help
help		:
	@echo "The targets to make are:"
	@echo "    make dotenv: the files generated by ANTLR4 for Dotenv"
	@echo "    make line  : the files generated by ANTLR4 for Line"
	@echo "For clean-up there are four more targets:"
	@echo "    make clean-dotenv    : remove Dotenv .o files"
	@echo "    make pristine-dotenv	: also remove the Dotenv generated files"
	@echo "    make clean-line		: remove Line .o files"
	@echo "    make pristine-line	: also remove the Line generated files"

.PHONY		: _antlr _clean

# A pseudo-target to force generating needed files
_antlr		: $(NEEDED)

# A pseudo-target to clean up things
_clean		:				# if there are any generated files
ifneq ($(strip $(GENERATED) ),)
	-rm -rf $(GENERATED)
endif

# ---------------------------------------------------------------

# How to make or update the generated files
$(NEEDED)	: $(GRAMMAR).g4
	@echo "## Creating the antlr generated files"
	$(ANTLR4) $(ANTLR4FLAGS) $(GRAMMAR).g4

# =================================================
# Changelog:
# 20200731	- adapted to cpp-dotenv needs for having
#			  more than one grammar and no compilation
#			  targets
# 20170707	- now supports having the users source
#			  files in a separate 'src' subdirectory
# 20170329	- now supports having the generated
#			  antlr4 files in a subdirectory
#			- now 'include' is silent
#		  	- move some comments more verbose
#			- added an experimental 'debug' target
# =================================================
