#/**(c)
#
#  Copyright (C) 2006, 2007, 2008, 2009, 2010 Christian Wawersich, Michael Stilkerich, Ralf Ellner
#
#  This file is part of the KESO Java Runtime Environment.
#
#	KESO is free software: you can redistribute it and/or modify it under the
#  terms of the Lesser GNU General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option)
#  any later version.
#
#	KESO 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 Lesser General Public License for
#  more details. You should have received a copy of the GNU Lesser General
#  Public License along with KESO. If not, see <http://www.gnu.org/licenses/>.
#
#  Please contact keso\@cs.fau.de for more info.
#
#(c)**/

ifeq ($(KESOROOTPATH),)
	KESOROOTPATH := $(error Environment variable KESOROOTPATH is not set)
endif

KESOSRCPATH ?= $(KESOROOTPATH)/src

# KESORC should be set from the environment
# If it is not, a simple hello world will be built
KESORC      ?= "rc/trafficlight_mica.kcl"

# Output directory for generated C code
KESOOUTPATH ?= "$(KESOSRCPATH)/tmp/"

ifeq ($(JDK),)
	JDK := $(error Environment variable JDK is not set)
endif

JAVA    ?= $(JDK)/bin/java
JAVAC   ?= $(JDK)/bin/javac
JAVADOC ?= $(JDK)/bin/javadoc
JAVACC  ?= javacc

BUILDER := buildstamps/jino-$(GLBREV).jar
SRC=`find libs/core -name "*.java"`
SRC_BUILDER="keso/compiler/Main.java"
#JOPT_BUILDER=-d classes -bootclasspath ${JDK}/jre/lib/rt.jar -sourcepath .
JOPT_BUILDER=-d classes -sourcepath . #-Xlint:unchecked

.PHONY: all builder keso
all: keso

# imclasses
IMCLASSTEMPLATES=$(wildcard templates/*.pm)
buildstamps/stamp-imclasses: createNodes.pl $(IMCLASSTEMPLATES)
	@echo Creating intermediate classes
	@./createNodes.pl

# Config file parser
builder/keso/compiler/config/parser/ConfigReader.java: builder/keso/compiler/config/ConfigReader.jj
	@echo Creating parser
	cd builder; $(JAVACC) -OUTPUT_DIRECTORY=keso/compiler/config/parser keso/compiler/config/ConfigReader.jj

# Build the Java-2-C Compiler
builder:
	@rm -f $(BUILDER)
	@make $(BUILDER)


$(BUILDER): builder/keso/compiler/config/parser/ConfigReader.java buildstamps/stamp-imclasses
	@echo Compiling the Java-2-C Builder jino r$(GLBREV)
	@cd builder/ ; mkdir -p classes ; $(JAVAC) $(JOPT_BUILDER) $(SRC_BUILDER)
	@#@cd builder/ ; mkdir -p classes ; $(JAVAC) -Xlint:unchecked $(JOPT_BUILDER) $(SRC_BUILDER)
	@jar cf0 $(BUILDER) builder/classes/
	@echo

# build KESO system
keso: $(BUILDER)
	@echo Creating keso-system for $(KESORC)
	jino -bootmodules core -op $(KESOOUTPATH)/ -def $(KESORC)

# Targets to clean the source tree of everything that is autogenerated
clean: clean-autosrc clean-builder clean-doc clean-keso clean-kesoclasses
	rm -f tags cscope.*

clean-autosrc:
	xargs -a buildstamps/stamp-imclasses   rm -f 2>/dev/null || exit 0
	xargs -a buildstamps/stamp-kesoautosrc rm -f 2>/dev/null || exit 0
	rm -f buildstamps/*
	rm -f builder/keso/compiler/config/parser/*.java

clean-builder:
	rm -rf builder/classes

clean-doc:
	rm -rf doc/core/* doc/jino/*

clean-keso:
	rm -rf tmp/*

clean-kesoclasses:
	rm -rf classes

# Targets for building various documentation
.PHONY: keso-doc jino-doc
keso-doc:
	$(JAVADOC) -d doc/core `find libs/core -name "*.java"`

jino-doc:
	$(JAVADOC) \
		-private \
		-sourcepath builder/ \
		-d doc/jino \
		-use \
		-subpackages keso.classfile:keso.compiler:keso.util

GLBREV=2804
