# Xabsl2Makefile
#
# Generates the documentation, intermediate code and debugging symbols for all an Xabsl2 project.
#
# author: Martin Ltzsch
# 
# This Makefile should be includedd into a custom Makefile inside the directory, where the XML sources are.
# Inside this custom Makefile these Variables have to be set:
#
# XSLT:                 An XSLT processor that can process XInclude statements (with necessary parameters)
# DOT:                  Path of the dot tool 
# DOTML_DIR:            Directory that contains the DotML Schemas and XSLT stylesheets
# SCHEMA_VALIDATOR:     Validates an XML file against the schemas referenced in the source file taking the input from stdin
# XABSL_DIR:            Directory of the XABSL Schemas and XSLT Stylesheets relative to the source files and the custom Makefile
# XABSL_OUTPUT_DIR:     Directory where the intermediate code and the debug symbols shall be generated
# DOC_OUTPUT_DIR:       Directory for the documentation output
# INSTANCE_DIR:         Directory containing the sources relative to the Xabsl2/xabsl-2.1 directory
# DEBUG_SYMBOLS:        Path of the debug symbols to be generated
# INTERMEDIATE_CODE:    The path of the intermediate code to be generated
# SYMBOL_FILES:         All symbol files
# BASIC_BEHAVIOR_FILES: All basic behavior files
# OPTION_FILES:         All Option files. The have to be all in a directory "Options" inside the directory of agents.xml
#



# Schema files (needed for validation)
SCHEMA_FILES     = $(XABSL_DIR)/xabsl-2.1.agent-collection.xsd \
                   $(XABSL_DIR)/xabsl-2.1.basic-behaviors.xsd \
                   $(XABSL_DIR)/xabsl-2.1.symbols.xsd \
                   $(XABSL_DIR)/xabsl-2.1.option.xsd \
                   $(XABSL_DIR)/xabsl-2.1.option-definitions.xsd \
                   $(XABSL_DIR)/xabsl-2.1.expressions.xsd \
                   $(XABSL_DIR)/xabsl-2.1.parameter.xsd 
                    


# General documentation output files
DOC_INDEX_HTML   = $(DOC_OUTPUT_DIR)/index.html
DOC_OPTION_TREE_XML = $(DOC_OUTPUT_DIR)/option-tree.xml
DOC_AGENTS_HTML = $(DOC_OUTPUT_DIR)/agents.html
DOC_SYMBOLS_INDEX_HTML = $(DOC_OUTPUT_DIR)/symbols.html
DOC_OPTION_INDEX_HTML = $(DOC_OUTPUT_DIR)/options.html
DOC_BASIC_BEHAVIOR_INDEX_HTML = $(DOC_OUTPUT_DIR)/basic-behaviors.html
DOC_STYLES_CSS   = $(DOC_OUTPUT_DIR)/styles.css
VALIDATION       = $(DOC_OUTPUT_DIR)/valid #This is just a temporary file to prevent make from building the target VALIDATION always


# Documentation XSLT Stylesheets
DOC_INDEX_XSL    = $(XABSL_DIR)/generate-documentation.index.xsl
DOC_MENU_XSL     = $(XABSL_DIR)/generate-documentation.menu.xsl
DOC_SYMBOLS_XSL  = $(XABSL_DIR)/generate-documentation.symbols.xsl
DOC_SYMBOLS_INDEX_XSL = $(XABSL_DIR)/generate-documentation.symbols-index.xsl
DOC_AGENTS_XSL    = $(XABSL_DIR)/generate-documentation.agents.xsl
DOC_BASIC_BEHAVIORS_XSL = $(XABSL_DIR)/generate-documentation.basic-behaviors.xsl
DOC_BASIC_BEHAVIOR_INDEX_XSL = $(XABSL_DIR)/generate-documentation.basic-behavior-index.xsl
DOC_OPTION_XSL   = $(XABSL_DIR)/generate-documentation.option.xsl
DOC_OPTION_INDEX_XSL = $(XABSL_DIR)/generate-documentation.option-index.xsl
DOC_OPTION_TREE_XSL = $(XABSL_DIR)/generate-documentation.option-tree.xsl
DOC_PSEUDO_CODE_XSL = $(XABSL_DIR)/generate-documentation.pseudo-code.xsl
DOC_PARAMETERS_XSL = $(XABSL_DIR)/generate-documentation.parameters.xsl
REMOVE_COMMENTS_XSL = $(XABSL_DIR)/remove-comments.xsl

# XSLT Stylesheet for debug symbols
DEBUG_SYMBOLS_XSL = $(XABSL_DIR)/generate-debug-symbols.xsl

# XSLT Stylesheet for intermediate code
INTERMEDIATE_CODE_XSL = $(XABSL_DIR)/generate-intermediate-code.xsl

# First validate the files, then generate debug symbols, intermediate code
all: $(DOC_OUTPUT_DIR) $(VALIDATION) $(DEBUG_SYMBOLS) $(INTERMEDIATE_CODE) #DOCUMENTATION
	@if test -f agents.xinclude-processed.xml; then echo removing `pwd`/agents.xinclude-processed.xml; rm agents.xinclude-processed.xml; fi 

# Some shortcuts for common targets:
IC:    $(INTERMEDIATE_CODE)
DS:    $(DEBUG_SYMBOLS)
IC_DS: $(DEBUG_SYMBOLS) $(INTERMEDIATE_CODE) $(VALIDATION) 
VALID: $(VALIDATION) 
DOC:   DOCUMENTATION	
AGENTS: $(DOC_AGENTS_HTML);

agents.xinclude-processed.xml: agents.xml options.xml symbol-and-basic-behavior-files.dtd $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES) $(REMOVE_COMMENTS_XSL)
	@echo generating `pwd`/agents.xinclude-processed.xml 
	@$(XSLT) -o agents.xinclude-processed.xml $(REMOVE_COMMENTS_XSL) agents.xml
	

# Documenation needs and output directory, the .css file, index pages and a page for 
# each symbols, basic-behaviors, and options file.
DOCUMENTATION: $(DOC_OUTPUT_DIR)/svg \
               $(DOC_OPTION_TREE_XML) \
               $(DOC_STYLES_CSS) \
               $(DOC_INDEX_HTML) \
               $(DOC_SYMBOLS_INDEX_HTML) \
               $(DOC_BASIC_BEHAVIOR_INDEX_HTML) \
               $(DOC_OPTION_INDEX_HTML) \
               $(shell echo $(SYMBOL_FILES)         |sed 's%[^ ]*/%%g' |sed "s%\([^ ]*\)\.xml%$(DOC_OUTPUT_DIR)/symbols.\1.html%g") \
               $(shell echo $(BASIC_BEHAVIOR_FILES) |sed 's%[^ ]*/%%g' |sed "s%\([^ ]*\)\.xml%$(DOC_OUTPUT_DIR)/basic-behaviors.\1.html%g") \
               $(shell echo $(OPTION_FILES)         |sed 's%[^ ]*/%%g' |sed "s%\([^ ]*\)\.xml%$(DOC_OUTPUT_DIR)/option.\1.html%g") \
               $(DOC_AGENTS_HTML) \

# Generic rule for option documentation               
$(DOC_OUTPUT_DIR)/option.%.html: Options/%.xml \
               $(DOC_OPTION_XSL) $(DOC_OPTION_TREE_XSL) $(DOC_PSEUDO_CODE_XSL) $(DOC_PARAMETERS_XSL) $(DOC_MENU_XSL)
	@echo $@ 
	@$(XSLT) --stringparam option-tree-xml $(INSTANCE_DIR)/$(DOC_OPTION_TREE_XML) -o $@.temp $(DOC_OPTION_XSL) $< 
	@$(XSLT) -o $@ $(DOTML_DIR)/embed-svg-graphics.xsl $@.temp
	@DOTML_DIR=$(DOTML_DIR) DOT=$(DOT) DOTML_XSLT="$(XSLT) (XSL) (INPUT)" $(DOTML_DIR)/generate-svg-graphics.bash $@.temp $(DOC_OUTPUT_DIR)
	@rm $@.temp 

# Generic rule for symbol documentation
$(DOC_OUTPUT_DIR)/symbols.%.html: $(DOC_MENU_XSL) $(SYMBOL_FILES) $(DOC_SYMBOLS_XSL) 
	@echo $@
	@$(XSLT) -o $@ $(DOC_SYMBOLS_XSL) $(shell echo " " $(SYMBOL_FILES) | sed "s=.* \([^ ]*$*.xml\).*=\1=;")
	
# Generic rule for basic behavior documentation
$(DOC_OUTPUT_DIR)/basic-behaviors.%.html: $(DOC_MENU_XSL) \
               $(BASIC_BEHAVIOR_FILES) $(DOC_BASIC_BEHAVIORS_XSL) $(DOC_PARAMETERS_XSL)
	@echo $@
	@$(XSLT) -o $@ $(DOC_BASIC_BEHAVIORS_XSL) $(shell echo " " $(BASIC_BEHAVIOR_FILES) | sed "s=.* \([^ ]*$*.xml\).*=\1=;")
	
# Generates the index page
$(DOC_INDEX_HTML): $(DOC_MENU_XSL) $(DOC_INDEX_XSL) agents.xml 
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_INDEX_HTML) 
	@$(XSLT) -o $(DOC_INDEX_HTML) $(DOC_INDEX_XSL) agents.xinclude-processed.xml

# Generates the option index page
$(DOC_OPTION_INDEX_HTML): options.xml $(DOC_OPTION_INDEX_XSL) $(DOC_MENU_XSL) 
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_OPTION_INDEX_HTML) 
	@$(XSLT) -o $(DOC_OPTION_INDEX_HTML) $(DOC_OPTION_INDEX_XSL) options.xml 

# Generates the agent index page
$(DOC_AGENTS_HTML): agents.xml $(DOC_AGENTS_XSL) $(DOC_MENU_XSL) $(DOC_OPTION_TREE_XSL)
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_AGENTS_HTML) 
	@$(XSLT) --stringparam option-tree-xml $(INSTANCE_DIR)/$(DOC_OPTION_TREE_XML) -o $(DOC_OUTPUT_DIR)/agents.temp.xml $(DOC_AGENTS_XSL) agents.xinclude-processed.xml 
	@$(XSLT) -o $(DOC_OUTPUT_DIR)/agents.html $(DOTML_DIR)/embed-svg-graphics.xsl $(DOC_OUTPUT_DIR)/agents.temp.xml
	@DOTML_DIR=$(DOTML_DIR) DOT=$(DOT) DOTML_XSLT="$(XSLT) (XSL) (INPUT)" $(DOTML_DIR)/generate-svg-graphics.bash $(DOC_OUTPUT_DIR)/agents.temp.xml $(DOC_OUTPUT_DIR)
	@rm $(DOC_OUTPUT_DIR)/agents.temp.xml 

# Generates the basic behavior index page
$(DOC_BASIC_BEHAVIOR_INDEX_HTML): $(DOC_BASIC_BEHAVIOR_INDEX_XSL) $(DOC_MENU_XSL) $(BASIC_BEHAVIOR_FILES) 
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_BASIC_BEHAVIOR_INDEX_HTML) 
	@$(XSLT) -o $(DOC_BASIC_BEHAVIOR_INDEX_HTML) $(DOC_BASIC_BEHAVIOR_INDEX_XSL) agents.xinclude-processed.xml

# Generates the symbols index page
$(DOC_SYMBOLS_INDEX_HTML): $(DOC_SYMBOLS_INDEX_XSL) $(DOC_MENU_XSL) $(SYMBOL_FILES)  
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_SYMBOLS_INDEX_HTML) 
	@$(XSLT) -o $(DOC_SYMBOLS_INDEX_HTML) $(DOC_SYMBOLS_INDEX_XSL) agents.xinclude-processed.xml

# Generates a intermediate xml file for the option trees of all options
$(DOC_OPTION_TREE_XML): $(OPTION_FILES) $(DOC_OPTION_TREE_XSL)
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_OPTION_TREE_XML)
	@$(XSLT) -o $(DOC_OPTION_TREE_XML) $(DOC_OPTION_TREE_XSL) agents.xinclude-processed.xml

# Validatates agents.xml and included files
$(VALIDATION): $(SCHEMA_FILES) \
               agents.xml options.xml $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES) 
	@make -s agents.xinclude-processed.xml
	@if test -d $(VALIDATION); then rm $(VALIDATION); fi
	@echo validating `pwd`/agents.xinclude-processed.xml 
	@cat agents.xinclude-processed.xml | $(SCHEMA_VALIDATOR)
	@echo "This file indicates that agents.xml was valid at last call of make. You can remove it if you want." > $(VALIDATION) 

# Generates the debug symbols
$(DEBUG_SYMBOLS): $(DEBUG_SYMBOLS_XSL) agents.xml options.xml $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES)  
	@make -s $(XABSL_OUTPUT_DIR) 
	@make -s agents.xinclude-processed.xml
	@echo $(DEBUG_SYMBOLS) 
	@$(XSLT) -o $(DEBUG_SYMBOLS) $(DEBUG_SYMBOLS_XSL) agents.xinclude-processed.xml
		
# Generates the intermediate code
$(INTERMEDIATE_CODE): $(INTERMEDIATE_CODE_XSL) agents.xml options.xml $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES)  
	@make -s $(XABSL_OUTPUT_DIR) 
	@make -s agents.xinclude-processed.xml
	@echo $(INTERMEDIATE_CODE) 
	@$(XSLT) -o $(INTERMEDIATE_CODE) $(INTERMEDIATE_CODE_XSL) agents.xinclude-processed.xml
		
# Generates the output directory for intermediate code and debug symbols
$(XABSL_OUTPUT_DIR):
	@if !(test -d $(XABSL_OUTPUT_DIR)); then echo $(XABSL_OUTPUT_DIR); mkdir $(XABSL_OUTPUT_DIR); fi

# Generates the output directory for documentation
$(DOC_OUTPUT_DIR): 
	@echo $(DOC_OUTPUT_DIR)
	@mkdir $(DOC_OUTPUT_DIR)
	
# Copies documentation.css to the documentation output directory
$(DOC_STYLES_CSS): $(XABSL_DIR)/documentation.css 
	@echo $(DOC_STYLES_CSS) 
	@cp $(XABSL_DIR)/documentation.css $(DOC_OUTPUT_DIR)/styles.css 

# The path where the SVG graphics generated for the documentation of options and agents are stored.
$(DOC_OUTPUT_DIR)/svg: 
	@echo $(DOC_OUTPUT_DIR)/svg
	@mkdir -p $(DOC_OUTPUT_DIR)/svg

rebuild: clean
	@make all

# Removes the documentation output directory, the debug symbols and the intermediate code.	
clean:
	@rm -rf $(DOC_OUTPUT_DIR)
	@rm -f $(DEBUG_SYMBOLS)
	@rm -f $(INTERMEDIATE_CODE)
	@rm -f valid
	
#
# Change Log:
#
# $Log: Xabsl2Makefile,v $
# Revision 1.3  2004/05/27 13:56:22  dueffert
# missing dependency added
#
# Revision 1.2  2003/10/26 22:49:41  loetzsch
# created ATH2004BehaviorControl from GT2003BehaviorControl
#  - strongly simplified option graph
#  - moved some symbols from GT2003 to CommonXabsl2Symbols
#  - moved some basic behaviors from GT2003 to CommonXabsl2BasicBehaviors
#
# cloned ATH2004 three times (BB2004, DDD2004, MSH2004)
#
# Revision 1.1  2003/10/07 10:13:25  cvsadm
# Created GT2004 (M.J.)
#
# Revision 1.7  2003/09/23 19:10:29  loetzsch
# removed GT2003 paths
#
# Revision 1.6  2003/09/20 16:34:16  loetzsch
# renamed "following-option-..." to "subsequent-option-.." and
# "following-basic-behavior-.." to "subsequent-basic-behavior-.."
# for consistency with publications
#
# Revision 1.5  2003/07/23 18:16:39  loetzsch
# made faster, agents.html is a part of the documentation target again
#
# Revision 1.4  2003/07/21 19:18:06  loetzsch
# - Xabsl2 AND and OR operators now can contain more than 2 operands
# - speed improvements and cleanup
#
# Revision 1.3  2003/07/08 01:20:48  loetzsch
# bug fix
#
# Revision 1.2  2003/07/07 21:24:55  loetzsch
# temporarily removed the creation of agents.html, because its too slow
#
# Revision 1.1.1.1  2003/07/02 09:40:29  cvsadm
# created new repository for the competitions in Padova from the 
# tamara CVS (Tuesday 2:00 pm)
#
# removed unused solutions
#
# Revision 1.2  2003/06/25 01:27:40  loetzsch
# uncommented prerequisite DOCUMENTATION of target "all"
#
# Revision 1.1  2003/06/25 01:19:50  loetzsch
# created xabsl 2.1
# Differences to xabsl2.0: No predefined conditions, documentation uses new dot
#
# Revision 1.30  2003/06/21 10:48:32  loetzsch
# no message
#
# Revision 1.29  2003/06/16 12:32:52  risler
# no message
#
# Revision 1.28  2003/06/15 22:42:22  loetzsch
# no message
#
# Revision 1.27  2003/05/20 20:14:13  roefer
# Xabsl Doc svg-path creation fixed
#
# Revision 1.26  2003/05/19 10:38:53  risler
# xabsl documentation only build in configuration xabsl2 documentation (again)
#
# Revision 1.25  2003/05/18 22:39:20  loetzsch
# added the option-tree.xsl to the prerequisites of the agents.html target
#
# Revision 1.24  2003/05/13 13:44:22  risler
# xabsl documentation only build in configuration xabsl2 documentation
#
# Revision 1.22  2003/05/12 09:50:33  risler
# doxygen comments
#
# Revision 1.21  2003/05/03 15:14:03  loetzsch
# GT2003BehaviorControl first draft
#
# Revision 1.20  2003/04/25 16:40:23  juengel
# Added dependency for DOCUMENTATION
#
# Revision 1.19  2003/04/23 17:57:25  loetzsch
# agents.xinclude-processed.xml is removed at the end of the make process
#
# Revision 1.18  2003/04/23 15:33:58  loetzsch
# made much faster again
#
# Revision 1.17  2003/04/21 20:53:33  loetzsch
# made validation and code generation faster
# - the element xabsl:agent doesn't contain any options anymore
# - options are now included to agents.xml instead into several agents
# - removed unused options
#
# Revision 1.16  2003/03/30 21:41:53  loetzsch
# made faster again
#
# Revision 1.15  2003/03/30 20:57:59  loetzsch
# bug fixed in generation of conditions index page
# Validation now is done only once
#
# Revision 1.14  2003/03/25 11:25:38  loetzsch
# fixed a bug in the option documentation
#
# Revision 1.13  2003/03/12 11:18:26  dueffert
# Xabsl2 rebuild added
#
# Revision 1.12  2003/01/23 15:27:07  loetzsch
# added $(AGENT_FILES) as prerequesite to target agents.html
#
# Revision 1.11  2003/01/20 18:32:49  loetzsch
# VALIDATION target now has $(DOC_OUTPUT_DIR) as prerequisiste
#
# Revision 1.10  2003/01/20 16:42:25  loetzsch
# agent.xincluded-processed is removed during build
#
# Revision 1.9  2003/01/20 14:53:18  loetzsch
# file valid is in doc directory now
# agent.xincluded-processed is removed after build
#
# Revision 1.8  2003/01/20 14:33:22  loetzsch
# file valid is in doc directory now
# agent.xincluded-processed is removed after build
#
# Revision 1.7  2003/01/20 14:16:16  loetzsch
# 8.3 filenames for intermediate code and debug symbols
#
# Revision 1.6  2003/01/19 22:57:09  loetzsch
# cloned Humboldt2003BehaviorControl to
# DarmstadtDribblingDackelsBehaviorControl
#
# Revision 1.5  2003/01/18 19:38:10  loetzsch
# agent documentation now is always recompiled when an option changes.
#
# Revision 1.4  2003/01/18 18:42:48  loetzsch
# - agents.xinclude-processed.xml is now created by applying a stylesheet that removes all comments
# - the menu for the documentation is not loaded from an xml file any mor
#   but created created different for each documentation type.
# - instead of one big "index.html", which contains a complete table of contents,
#   a few seperate index pages are created
# - The option trees are painted by using a precompiled xml file "option-tree.xml"
#   to improve compile speed.
#
# Revision 1.3  2003/01/08 15:10:09  loetzsch
# started intermediate code generation
#
# Revision 1.2  2002/12/16 14:17:17  loetzsch
# comments added
#
# Revision 1.1  2002/12/12 22:43:14  loetzsch
# modularized Makefile
#
#
