#
# Makefile for the pwan
#


#
# define common commands
#

# quitet command - don't display progress
QUIET_CMD=@

CC = $(QUIET_CMD)g++
MKDEP = $(CC) -MM
ECHO = $(QUIET_CMD)echo
SED = sed
LD = $(QUIET_CMD)g++

FLEX = $(QUIET_CMD)flex
BISON= $(QUIET_CMD)bison

CFLAGS = -pedantic -g -Wall


#
# define source files
#

SRCS = $(wildcard *.cpp) ocl_lexan.c ocl_parser.c

OBJS = $(addsuffix .o, $(basename $(notdir $(SRCS)))) 


# include dependencies files
-include $(OBJS:.o=.d)


#
# create rules for different file types
#

#
# rule to generate files using flex and bison
#
ocl_lexan.c : ocl_lexan.l
	$(ECHO) "Calling flex to generate $@"
	$(FLEX) -d -o$@ ocl_lexan.l

ocl_parser.c : ocl_parser.y ocl_lexan.c
	$(ECHO) "Calling bison to generate $@"
	$(BISON) -d -o $@ ocl_parser.y

#
# dependencies file
# will contain make-compatible dependencies
#
# We must use sed to format the gcc output to be make-compatible
#
%.d : %.cpp
	$(ECHO) "Updating dependencies for $<"
	$(MKDEP) $(CFLAGS) $< | $(SED) -e 's|^$*.o|& $@| g' > $@

%.d : %.c
	$(ECHO) "Updating dependencies for $<"
	$(MKDEP) $(CFLAGS) $< | $(SED) -e 's|^$*.o|& $@| g' > $@

#
# rule to generate object file from source file
#
%.o : %.cpp
	$(ECHO) "Compiling $<"
	$(CC) -c $(CFLAGS) $< -o $@

%.o : %.c
	$(ECHO) "Compiling $<"
	$(CC) -c $(CFLAGS) $< -o $@

.PHONY : poclc
poclc : $(OBJS)
	$(ECHO) Linking...
	$(LD) $(OBJS) -o poclc

.PHONY : clean
clean : 
	$(ECHO) Cleaning...
	-rm *.o *.d *.c *.exe *~ ocl_parser.h





