
# tools

LINK		= gcc
CC		= gcc
CXX		= gcc

LEX		= flex
YACC		= bison

ECHO		= echo
MAKE		= make

CP		= cp
INSTALL		= install

# flags

RCFLAGS		=
RLFLAGS		= -L/usr/lib
RINCFLAGS	=
RLIBRARIES	= -lstdc++

DCFLAGS		= -g
DLFLAGS		= -L/usr/lib
DINCFLAGS	=
DLIBRARIES	= /usr/lib/libstdc++.a

# status

ifndef STATUS
STATUS		= Release
endif

ifeq ($(STATUS),Release)
CFLAGS		= $(RCFLAGS)
LFLAGS		= $(RLFLAGS)
INCFLAGS	= $(RINCFLAGS)
LIBRARIES	= $(RLIBRARIES)
endif

ifeq ($(STATUS),Debug)
CFLAGS		= $(DCFLAGS)
LFLAGS		= $(DLFLAGS)
INCFLAGS	= $(DINCFLAGS)
LIBRARIES	= $(DLIBRARIES)
endif

# variables

MODULES		= scanner parser db dbmanager generator

SOURCES		= FSMMain.cpp
OBJECTS		= FSMMain.o \
			scanner/FSMScanner.o \
			parser/FSMParser.o \
			db/FSMCallback.o \
			db/FSMDB.o \
			db/FSMDBException.o \
			db/FSMEvent.o \
			db/FSMState.o \
			db/FSMTransition.o \
			dbmanager/FSMDBManager.o \
			generator/FSMCGenerator.o \
			generator/FSMCppGenerator.o \
			generator/FSMFileGenerator.o \
			generator/FSMJavaGenerator.o
TARGETS		= fsmgenerator
OUTPUTS		= 

#rules

all:	$(TARGETS)

#################

fsmgenerator: FSMMain.o $(MODULES)
	$(MAKE) descend-all
	$(LINK) $(LFLAGS) -o $@ $(OBJECTS) $(LIBRARIES)

FSMMain.o: FSMMain.cpp
	$(CC) -c $(CFLAGS) $(INCFLAGS) -o $@ $?
	

###########

descend-all: 
ifneq ($(MODULES),) #checking for empty value
	-@(for d in $(MODULES); \
	do \
		$(ECHO) 'Building ===>' $$d; \
		cd $$d; \
		$(MAKE) all; \
		cd ..; \
	done)
endif

descend-clean: 
ifneq ($(MODULES),) #checking for empty value
	-@(for d in $(MODULES); \
	do \
		$(ECHO) 'Building ===>' $$d; \
		cd $$d; \
		$(MAKE) clean; \
		cd ..; \
	done)
endif

descend-cleanall: 
ifneq ($(MODULES),) #checking for empty value
	-@(for d in $(MODULES); \
	do \
		$(ECHO) 'Building ===>' $$d; \
		cd $$d; \
		$(MAKE) clean; \
		cd ..; \
	done)
endif

descend-install: 
ifneq ($(MODULES),) #checking for empty value
	-@(for d in $(MODULES); \
	do \
		$(ECHO) 'Building ===>' $$d; \
		cd $$d; \
		$(MAKE) install; \
		cd ..; \
	done)
endif

clean:
	$(MAKE) descend-clean
	rm -f core $(OBJECTS) *~

cleanall:
	$(MAKE) descend-cleanall
	rm -f $(TARGETS) $(OUTPUTS) core $(OBJECTS) *~

install:
	@$(INSTALL) -m755 $(TARGETS) /usr/local/bin/

test:	$(TARGETS)
	./fsmgenerator "test.fsm" "Cpp" "."

help:
	@echo "To compile, please, run: 'make all'"

PHONY-TARGETS = all \
		clean \
		cleanall \
		descend-all \
		descend-clean \
		install \
		help \
		test
.PHONY: $(PHONY-TARGETS)
