#        Vulture's Makefile

ifdef BUILD

ifndef CXX
	CXX := g++
endif

LD := ld

ifndef SDL_CFLAGS
	ifndef SDL_CONFIG
		OS := $(shell uname -s)
		ifeq ($(OS),FreeBSD)
			SDL_CONFIG := sdl11-config
		else
			SDL_CONFIG := sdl-config
		endif
	endif
	SDL_CFLAGS = $(shell $(SDL_CONFIG) --cflags)
endif

CFLAGS := $(BASECFLAGS) $(SDL_CFLAGS) -DUSE_SDL_SYSCALLS -g3 -Wall

ifeq ($(BUILD), nethack)
	BUILDDIR := build_n
	CFLAGS += -DVULTURESEYE
else
	BUILDDIR := build_s
	CFLAGS += -DVULTURESCLAW
endif

# build target
OUTPUT_OBJ := $(BUILDDIR)/vultures.o


NHDIR := ../$(BUILD)
NHSRC := $(NHDIR)/src
INCLUDE := -I $(NHDIR)/include/  -I . -I $(BUILDDIR)/

# list of sources. list of objects and dependency files are autogenerated from it
VSOURCES := $(wildcard *.cpp) $(wildcard winclass/*.cpp)
VOBJ := $(patsubst %.cpp,$(BUILDDIR)/%.o, $(VSOURCES))
VDEPS := $(subst .o,.d, $(VOBJ))

VSOURCES += $(BUILDDIR)/vultures_tileconfig.parser.cpp $(BUILDDIR)/vultures_tileconfig.lexer.cpp
VOBJ += $(BUILDDIR)/vultures_tileconfig.parser.o $(BUILDDIR)/vultures_tileconfig.lexer.o


# how to genereate dependency info
define gendeps
	@mkdir -p $(BUILDDIR)/winclass
	@set -e; rm -f $@; \
	 $(CXX) -MM $(CFLAGS) $(INCLUDE) $< > $@.$$$$; \
	 sed 's,\($*\)\.o[ :]*,$(BUILDDIR)/\1.o $@ : ,g' < $@.$$$$ > $@; \
	 rm -f $@.$$$$
endef


.PHONY : all
all: $(OUTPUT_OBJ)
	@echo finished building the files for the vultures interface

$(OUTPUT_OBJ): $(VOBJ)
	@echo combining objects to $@
	@$(CXX) -nostdlib -Xlinker -r $(VOBJ) -lstdc++ -o $(OUTPUT_OBJ)

$(BUILDDIR)/vultures_tileconfig.parser.cpp $(BUILDDIR)/vultures_tileconfig.parser.h: vultures_tileconfig.parser.y
	@echo generating parser
	@mkdir -p $(BUILDDIR)/winclass
	@bison -d vultures_tileconfig.parser.y -o $(BUILDDIR)/vultures_tileconfig.parser.tmp.cpp
	@mv $(BUILDDIR)/vultures_tileconfig.parser.tmp.cpp $(BUILDDIR)/vultures_tileconfig.parser.cpp
	@mv $(BUILDDIR)/vultures_tileconfig.parser.tmp.hpp $(BUILDDIR)/vultures_tileconfig.parser.h

$(BUILDDIR)/vultures_tileconfig.lexer.cpp: vultures_tileconfig.lexer.l
	@echo generating lexer
	@mkdir -p $(BUILDDIR)/winclass
	@flex -o $@ $<

# including the dependency files will cause them to be built if they don't exist
-include $(VDEPS)

#dependency for $(BUILDDIR)/vultures_tileconfig.lexer.o must be given explicitly as it is not autogenerated
$(BUILDDIR)/vultures_tileconfig.lexer.o : $(BUILDDIR)/vultures_tileconfig.parser.h

# this dependency is necessary for parallel make to work: otherwise bison might
# be called twice and the 2 invocations will race
$(BUILDDIR)/vultures_tileconfig.parser.h : $(BUILDDIR)/vultures_tileconfig.parser.cpp

# additionally, all our objects depend on the Makefile:
$(VOBJ): GNUmakefile

# rules to generate the dependencies using the gendeps macro
$(BUILDDIR)/%.d: %.cpp
	$(gendeps)

# this will cause all regular files to be built
# NOTE: this rule will match for ../nethack/src/monst.c -> ../nethack/src/monst.o
#       too, if necessary. Normally monst.o will already exist when we get here,
#       but it may be useful when testing tiletrans...
%.o $(BUILDDIR)/%.o: %.cpp
	@echo compiling $<
	@mkdir -p $(BUILDDIR)/winclass
	$(CXX) $(CFLAGS) $(INCLUDE) -c $< -o $@
	

else # BUILD not defined
.PHONY:all
all:
	@echo "you must invoke this makefile with BUILD=(nethack|slashem)"
	@echo "so that we can find our includes..."
	@echo "you may also give additional parameters such as CFLAGS= etc"
endif


.PHONY:
clean:
	rm -rf build_n/ build_s/
