#*****************************************************************************
#
#	Makefile
#
#	Function:	Build the project
#
#	Started:	07.12.2008
#	Finished:	x
#
#	Copyright 2008 - 2009 Gerald Wodni
#
#	This file is part of the GForth-C-Interface.
#
#	GForth-C-Interface is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 3 of the License, or
#	(at your option) any later version.
#
#	GForth-C-Interface is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#*****************************************************************************

# specify include directories
HEADERS_DIRECTORIES = /usr/include /usr/include/linux /usr/include/SDL

# enable this if you compile stuff which relies on the gcc-version
USE_GCC_HEADERS = true

# C-Compiler
CC = gcc
# Compiler options during compilation
COMPILE_OPTIONS = -x c -pedantic -Wall

# SWIG-Compiler
SWIG = swig
# Swig Options ( to create fsi-files )
SWIG_OPTIONS = -gforth -stackcomments -enumcomments -fsi-output -includeall -forthifyfunctions

#-- Do not edit below this line --

ifeq ($(USE_GCC_HEADERS),true)
	GCC_TARGET := $(shell gcc -v 2>&1 | grep Target | sed "s/Target:\s*//")
	GCC_VERSION := $(shell gcc -v 2>&1 | grep "gcc version" | sed "s/gcc version \([^ ]\+\).*/\1/")
	GCC_INCLUDEDIR := /usr/lib/gcc/$(GCC_TARGET)/$(GCC_VERSION)/include/
	HEADERS_DIRECTORIES := $(HEADERS_DIRECTORIES) $(GCC_INCLUDEDIR)
endif

# Add Header-Directories to Swig-include-path
SWIG_INCLUDE_DIRS := $(foreach i, $(HEADERS_DIRECTORIES), -I$i)
SWIG_OPTIONS += $(SWIG_INCLUDE_DIRS)

# Create fsi-files out of HEADERS
I_FILES := $(wildcard *.i)
FSI_FILES := $(patsubst %.i, %.fsi, $(I_FILES))
FSC_FILES := $(patsubst %.i, %.fsc, $(I_FILES))
FS_FILES := $(patsubst %.i, %.fs, $(I_FILES))

all: $(FSC_FILES)

fs-files:	$(FS_FILES)

i-files: $(I_FILES)

fsi-files: $(FSI_FILES)

makefile-debug:
	@echo :: Swig-include-dires:
	@echo $(SWIG_INCLUDE_DIRS)
	@echo :: I-files:
	@echo "$(I_FILES)"
	@echo :: Gcc-stuff:
	@echo $(GCC_VERSION)
	@echo $(GCC_TARGET)
	@echo $(GCC_INCLUDEDIR)

%.fsi: %.i
	$(SWIG) $(SWIG_OPTIONS) -o $@ $<

%.fsc: %.fsi
	$(CC) $(COMPILE_OPTIONS) -o $@ $<

%.fs: %.fsc
	./$< > $@

# Cleaning up

.PHONY: clean
clean:
	rm -f $(FSC_FILES)
	rm -f $(FS_FILES)

.PHONY: depclean
depclean:
	rm -f $(FSI_FILES)

clean-all: clean depclean

