#
# $Id: Makefile,v 1.1.1.1 2002/09/30 15:08:41 smartine Exp $
#
# Copyright (c) 1999-2001 by David Brownell.  All Rights Reserved.
#
# This program is free software; you may use, copy, modify, and
# redistribute it under the terms of the LICENSE with which it was
# originally distributed.
#
# This program 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
# LICENSE for more details.
#

########################################################################
#
# NOTE:  With the exception of the xml.testing.Driver code,
# everything here is an OLD snapshot of software that maintained
# in the GNU JAXP project.  For current software versions, see
#
# 	http://www.gnu.org/software/classpathx/jaxp
#
########################################################################

#
# Autodetect UNIX vs Cygwin/Win32 ... Win32 JDK works fine from
# Cygwin (B20.1) but needs Win32 style paths.
#
ifeq ($(OSTYPE),cygwin32)
    SEP = ;
else
    SEP = :
endif

#
# Classpath is built up by automatic then manual steps.
# This distribution is self-sufficient; you can test
# a separately distributed SAX parser without using it
# to bootstrap the test harness.
#
CPATH :=	classes

#
# Use GCJ 3.x or none at all
# (Makefile won't build without JDK though)
#
GCJ_PREFIX=/usr/local

#
# What JDK is installed?  Builds work a bit differently... JDK 1.1
# needs classes.zip explicitly in CPATH.
#
JDK :=		$(filter-out java version, $(shell java -version 2>&1))
JDK :=		$(subst ",,$(JDK))

ifeq ($(findstring 1.1, $(JDK)),1.1)
    ifeq ($(origin JAVA_HOME),undefined)
	JAVA_HOME := $(patsubst %/bin/java,%,$(shell which java))
    endif
    CPATH := $(CPATH)$(SEP)$(JAVA_HOME)/lib/classes.zip
endif

#
# JFLAGS holds Java system properties controlling the SAX 
# implementation used to read the test case database and
# report template.
#
# Any nonvalidating SAX/SAX2 parser may be used so long as it reads
# all external entities and is otherwise "conformant enough" to read
# in the test database and report template.
#
# The parser under test is controlled separately, through a
# command line parameter.
#
JFLAGS =	\
    -Dorg.xml.sax.parser=xml.aelfred2.SAXDriver

#
# Add the vendor-specific SAX2 wrappers if you have the
# appropriate XML parser libraries in $CPATH (or as
# extensions). Ditto the servlet support and the
# HTML parser (uses documented Swing innards).
#
# FIXME:  the vendor wrappers should just vanish.  They
# aren't given to javadoc; pretty much dead code now,
# presumably all vendors do SAX2.
#
# FIXME:  likewise everything else except the bootstrap
# parser (with optional validation) and tester...
#

SOURCES := $(shell find src -name '*.java' -print | egrep -vi 'CallFilter|dom|Dom|/vendor/|Servlet|javax')
# SOURCES = `find src -name '*.java' -print`

# We make $(NAME).jar; the guts of this was
# previously "utilities.jar"
NAME =	utilities

PACKAGES = \
	org.xml.sax \
	org.xml.sax.helpers \
	org.xml.sax.ext \
	\
	xml.aelfred2 \
	xml.pipeline \
	xml.testing

##############################################################################
#
# main makefile targets
#
##############################################################################

default:
	@echo "Interesting targets:"
	@echo ' $ make sanity ... makes report-{nv,val}.html, using JDK'
	@echo ' $ make all ... makes '$(NAME).jar' and javadoc'
	@echo ' $ make clean ... gets rid of output files'
	@echo ' $ make gcj-sanity ... makes report-{nv,val}-gcj.html, using GCJ'
	@echo ' $ make gij-sanity ... makes report-{nv,val}-gij.html, using GIJ'
	@echo ' $ make gcj ... makes 'lib$(NAME).a, saxtest, doparse
	@echo ' $ make jarfile ... makes '$(NAME).jar
	@echo ' $ make javadoc ... makes apidoc and apidoc.zip'
	@echo "The report-*.html files check the AElfred2 reference parser."

jarfile: 	$(NAME).jar

all:	jarfile javadoc

gcj:	saxtest doparse

sanity:		report-nv.html		report-val.html
gcj-sanity:	report-nv-gcj.html	report-val-gcj.html
gij-sanity:	report-nv-gij.html	report-val-gij.html

clean:
	rm -rf report-*.html *~ core Log
	rm -rf classes $(NAME).jar apidoc apidoc.zip
	rm -rf saxtest doparse obj-static lib$(NAME).a


##############################################################################

$(NAME).jar:	classes
	cd classes ; jar cf ../$(NAME).jar xml org

classes:
	mkdir classes
ifeq ($(findstring 1.1, $(JDK)),1.1)
	CLASSPATH=$(CPATH)$(SEP)src javac -d classes $(SOURCES); \
	if [ $$? != 0 ]; then \
	    rm -rf classes ; \
	    exit 1; \
	fi
else
	javac -classpath "$(CPATH)" -d classes $(SOURCES); \
	if [ $$? != 0 ]; then \
	    rm -rf classes ; \
	    exit 1; \
	fi
endif


##############################################################################
#
# Sanity check the library build:  check the reference parsers 
#
# NOTE:  These test SAX2 conformance.  If you want a JAXP parser,
# build a custom wrapper for it.
#
##############################################################################


TEMPLATE =	../../xml/template.xml
TESTCASES =	../../xml/suite/xmlconf.xml

ifeq ($(findstring 1.1, $(JDK)),1.1)

report-nv.html: $(NAME).jar
	CLASSPATH=$(NAME).jar$(SEP)$(JAVA_HOME)/lib/classes.zip \
		java xml.testing.Driver \
		$(TESTCASES) xml.aelfred2.SAXDriver $(TEMPLATE) \
		"nv" \
		"AElfred2" \
		> report-nv.html
report-val.html: $(NAME).jar
	CLASSPATH=$(NAME).jar$(SEP)$(JAVA_HOME)/lib/classes.zip \
		java xml.testing.Driver \
		$(TESTCASES) xml.aelfred2.Validator $(TEMPLATE) \
		"val" \
		"AElfred2, with validation" \
		> report-val.html

else

report-nv.html: $(NAME).jar
	java -cp $(NAME).jar xml.testing.Driver \
		$(TESTCASES) xml.aelfred2.SAXDriver $(TEMPLATE) \
		"nv" \
		"AElfred2" \
		> report-nv.html
report-val.html: $(NAME).jar
	java -cp $(NAME).jar xml.testing.Driver \
		$(TESTCASES) xml.aelfred2.Validator $(TEMPLATE) \
		"val" \
		"AElfred2, with validation" \
		> report-val.html
endif
 
# These tests SHOULD provide the same results as
# when running with Sun's JDK ... assuming that the
# current version of GCJ is working well!

# NOTE:  JDK doesn't set timezone correctly, GCJ does; so the
# reported dates will frequently differ in that way.

# NOTE:  the exceptions prevent looping in two Japanese
# test files, encodings iso-2022-jp and shift_jis;
# no GCJ bug filed yet (problem isn't in iconv) 

EXCEPT =	xml.testing.Driver.exceptions=exceptions.xml

# GCJ:  GNU Compiler for Java
report-nv-gcj.html: saxtest
	GCJ_PROPERTIES=$(EXCEPT) ./saxtest \
		$(TESTCASES) xml.aelfred2.SAXDriver $(TEMPLATE) \
		"nv" \
		"AElfred2 (GCJ)" \
		> report-nv-gcj.html
report-val-gcj.html: saxtest
	GCJ_PROPERTIES=$(EXCEPT) ./saxtest \
		$(TESTCASES) xml.aelfred2.Validator $(TEMPLATE) \
		"val" \
		"AElfred2 (GCJ), with validation" \
		> report-val-gcj.html

# GIJ:  GNU Interpreter for Java
report-nv-gij.html: saxtest
	CLASSPATH=$(NAME).jar gij -D$(EXCEPT) xml.testing.Driver \
		$(TESTCASES) xml.aelfred2.SAXDriver $(TEMPLATE) \
		"nv" \
		"AElfred2 (GIJ)" \
		> report-nv-gij.html
report-val-gij.html: saxtest
	CLASSPATH=$(NAME).jar gij -D$(EXCEPT) xml.testing.Driver \
		$(TESTCASES) xml.aelfred2.Validator $(TEMPLATE) \
		"val" \
		"AElfred2 (GIJ), with validation" \
		> report-val-gij.html


##############################################################################
#
# Make javadoc
#
# NOTE:  You must manually add packages to the list here.
#
##############################################################################

javadoc:	apidoc
ifneq ($(findstring 1.1, $(JDK)),1.1)
	javadoc -d apidoc \
	    -windowtitle "XMLconf Java Library" \
	    -nodeprecatedlist \
	    -version -author \
	    -footer "<p>Source code is GPL'd at \
		<a href='http://xmlconf.sourceforge.net/'> \
	    	http://xmlconf.sourceforge.net</a>. \
		<br>This documentation was derived from that\
		source code on `date -I`.\
		</p>" \
	    -classpath "$(CPATH)$(SEP)src" \
	    \
	    -group "SAX2/XML Testing (Free Software)" xml.testing \
	    \
	    -group "SAX2/AElfred2 XML Utilities (Free Software)" \
	    	"xml:xml.pipeline:xml.aelfred2:xml.vendor" \
	    -group "DOM2, implementing DOM Level 2 (Free Software)" \
	    	"xml.dom:xml.dom.xhtml" \
	    \
	    -group "SAX and SAX2 (Free Software)" \
	    	"org.xml.sax:org.xml.sax.ext:org.xml.sax.helpers" \
	    -group "DOM Level 2 (W3C)" \
	    	"org.w3c.*" \
	    $(PACKAGES)
	zip -9qr apidoc.zip apidoc
else
	@echo "Requires JDK 1.2 (or later) javadoc."
endif
apidoc:
	-mkdir apidoc

##############################################################################

dump:
	@echo OSTYPE is ... "$(OSTYPE)"
	@echo SEP is ... "$(SEP)"
	@echo JDK is ... "$(JDK)"
	@echo CPATH is ... "$(CPATH)"
	@echo SOURCES is ... $(SOURCES)


##############################################################################
#
# Thise needs a current GCC 3.0 snapshot; the release works, though
# there are some bugs that the xmlconf suite will uncover.  Don't
# bother with older versions: 2.96rh doesn't quite work, and 2.95 isn't
# worth trying -- and both versions use different name mangling.
#
# Relevant GNATS PRs:  
#   2424 (workaround, rm "final" keyword in xml.testing.Driver)
#   3285 (DOM-only, see below)
#
##############################################################################

DIRS = src/org
DIRS += src/xml/aelfred2
DIRS += src/xml/testing

# GCC 3.0 doesn't compile DOM yet (inner class bug, PR 3285?
# DomNode compile doesn't find various DomEvent constructors)
# Once that works, the other three src/xml/Dom* classes should too.
# DIRS += src/xml/dom

GCJ_SOURCES := $(shell find $(DIRS) src/xml/pipeline -name '*.java' -print | grep -v Servlet)
# GCJ_SOURCES := $(shell find $(DIRS) -name '*.java' -print)
GCJ_SOURCES += \
	src/xml/DefaultHandler.java \
	src/xml/Resolver.java \
	src/xml/DoParse.java \
	src/xml/EchoHandler.java \
	src/xml/XhtmlEchoHandler.java

# EXCLUDING (current limited portability)
#	src/xml/vendor/*
#	src/xml/pipeline/XmlServlet.java

GCJ_OPTS = -Os -g -pipe


# GCJ_PROPERTIES= in env will set system properties

GCJ =	$(GCJ_PREFIX)/bin/gcj

lib$(NAME).a:	$(GCJ_SOURCES:src/%.java=obj-static/%.o)
	ar crv lib$(NAME).a $(shell find obj-static -name '*.o' -print)

obj-static/%.o : src/%.java
	@mkdir -p `dirname $@`
	$(GCJ) $(GCJ_OPTS) -c -Iclasses -o $@ $^

saxtest: classes lib$(NAME).a
	$(GCJ) --main=xml.testing.Driver \
	    -o saxtest \
	    -u '_ZN3xml7testing6Driver6class$$E' \
	    -u '_ZN3xml8aelfred29SAXDriver6class$$E' \
	    -u '_ZN3xml8aelfred29Validator6class$$E' \
	    lib$(NAME).a

doparse: classes lib$(NAME).a
	$(GCJ) --main=xml.DoParse \
	    -o doparse \
	    -u '_ZN3xml7DoParse6class$$E' \
	    -u '_ZN3xml8aelfred29SAXDriver6class$$E' \
	    -u '_ZN3xml8aelfred29Validator6class$$E' \
	    lib$(NAME).a
