###############################################################################
# The following macros should be defined before this script is
# invoked:
#
# DOC              The toplevel directory of the documentation source tree.
#
# SRC              The toplevel directory of the source code source tree.
#
# TCLSQLITE3C      Name of the amalgamation source file
#
# TH3              The toplevel directory for TH3.  May be an empty string.
#
# SLT              The toplevel directory for SQLLogicTest.  May be an
#                  empty string
#
# TCLFLAGS         Extra C-compiler options needed to link against TCL
#
# TCLSTUBFLAGS     Extra C-compiler options needed to link a TCL extension
#
# NAWK             Nawk compatible awk program.  Older (obsolete?) solaris
#                  systems need this to avoid using the original AT&T AWK.
#
# CC               A C-compiler and arguments for building utility programs
#
# Once the macros above are defined, the rest of this make script will
# build the SQLite library and testing tools.
################################################################################

default:	
	@echo 'make base;       # Build base documents'
	@echo 'make evidence;   # Gather evidence marks'
	@echo 'make matrix;     # Build the traceability matrix'
	@echo 'make all;        # Do all of the above'
	@echo 'make spell;      # Spell check generated docs'
	@echo 'make fast;       # Build documentation only - no requirements'
	@echo 'make schema;     # Run once to initialize the build process'

all:	base evidence format_evidence matrix doc

private:	base evidence private_evidence matrix doc

fast:	base doc

tclsh:	$(TCLSQLITE3C)
	$(CC) -g -o tclsh -DSQLITE_ENABLE_FTS3 -DTCLSH=1 -DSQLITE_TCLMD5 $(TCLINC) $(TCLSQLITE3C) $(TCLFLAGS)

tclsqlite3.fts3:	$(TCLSQLITE3C) $(DOC)/search/searchc.c
	$(CC) -static -O2 -o tclsqlite3.fts3 -I. -DSQLITE_ENABLE_FTS3 $(TCLINC) $(DOC)/search/searchc.c $(TCLSQLITE3C) $(TCLFLAGS)

sqlite3.h:	tclsh $(SRC)/src/sqlite.h.in $(SRC)/manifest.uuid $(SRC)/VERSION
	./tclsh $(SRC)/tool/mksqlite3h.tcl $(SRC) | \
	sed 's/^SQLITE_API //' >sqlite3.h

# Generate the directory into which generated documentation files will
# be written.
#
docdir:
	mkdir -p doc doc/c3ref doc/matrix doc/matrix/c3ref

# This rule generates all documention files from their sources.  The
# special markup on HTML files used to identify testable statements and
# requirements are retained in the HTML and so the HTML generated by
# this rule is not suitable for publication.  This is the first step
# only.
#
base:	tclsh sqlite3.h docdir always parsehtml.so
	rm -rf doc/images
	cp -r $(DOC)/images doc
	mkdir doc/images/syntax
	cp $(DOC)/art/syntax/*.gif doc/images/syntax
	cp $(DOC)/rawpages/* doc
	./tclsh $(DOC)/wrap.tcl $(DOC) $(SRC) doc $(DOC)/pages/*.in
	cp doc/fileformat2.html doc/fileformat.html

# Strip the special markup in HTML files that identifies testable statements
# and requirements.
#
doc:	always $(DOC)/remove_carets.sh
	sh $(DOC)/remove_carets.sh doc

# Spell check generated docs.
#
spell: $(DOC)/spell_chk.sh $(DOC)/custom.txt
	sh $(DOC)/spell_chk.sh doc '*.html' $(DOC)/custom.txt

# Construct the database schema.
#
schema:	tclsh
	./tclsh $(DOC)/schema.tcl

# The following rule scans sqlite3.c source text, the text of the TCL
# test cases, and (optionally) the TH3 test case sources looking for
# comments that identify assertions and test cases that provide evidence
# that SQLite behaves as it says it does.  See the comments in 
# scan_test_cases.tcl for additional information.
#
# The output file evidence.txt is used by requirements coverage analysis.
#
SCANNER = $(DOC)/scan_test_cases.tcl

evidence:	tclsh
	./tclsh $(SCANNER) -reset src $(SRC)/src/*.[chy]
	./tclsh $(SCANNER) src $(SRC)/ext/fts3/*.[ch]
	./tclsh $(SCANNER) src $(SRC)/ext/rtree/*.[ch]
	./tclsh $(SCANNER) tcl $(SRC)/test/*.test
	if test '' != '$(TH3)'; then \
	  ./tclsh $(SCANNER) th3 $(TH3)/mkth3.tcl; \
	  ./tclsh $(SCANNER) th3 $(TH3)/base/*.c; \
	  ./tclsh $(SCANNER) th3/req1 $(TH3)/req1/*.test; \
	  ./tclsh $(SCANNER) th3/cov1 $(TH3)/cov1/*.test; \
	  ./tclsh $(SCANNER) th3/stress $(TH3)/stress/*.test; \
	fi
	if test '' != '$(SLT)'; then \
	  ./tclsh $(SCANNER) slt $(SLT)/test/evidence/*.test; \
	fi

# Copy and HTMLize evidence files
#
FMT = $(DOC)/format_evidence.tcl

format_evidence: tclsh
	rm -fr doc/matrix/ev/*
	./tclsh $(FMT) src doc/matrix $(SRC)/src/*.[chy]
	./tclsh $(FMT) src doc/matrix $(SRC)/ext/fts3/*.[ch]
	./tclsh $(FMT) src doc/matrix $(SRC)/ext/rtree/*.[ch]
	./tclsh $(FMT) tcl doc/matrix $(SRC)/test/*.test
	if test '' != '$(SLT)'; then \
	  ./tclsh $(FMT) slt doc/matrix $(SLT)/test/evidence/*.test; \
	fi

private_evidence: format_evidence
	./tclsh $(FMT) th3 doc/matrix $(TH3)/mkth3.tcl
	./tclsh $(FMT) th3/req1 doc/matrix $(TH3)/req1/*.test
	./tclsh $(FMT) th3/cov1 doc/matrix $(TH3)/cov1/*.test

# Generate the traceability matrix
#
matrix:	
	rm -rf doc/matrix/images
	cp -r doc/images doc/matrix
	./tclsh $(DOC)/matrix.tcl

# Build the fts3 database used by the search script
#
parsehtml.so: $(DOC)/search/parsehtml.c
	gcc -g -shared -fPIC -DUSE_TCL_STUBS $(TCLINC) -I. -I$(SRC)/ext/fts3 $(DOC)/search/parsehtml.c $(TCLSTUBFLAGS) -o parsehtml.so

searchdb: parsehtml.so tclsh
	./tclsh $(DOC)/search/buildsearchdb.tcl
	cp $(DOC)/search/search.tcl doc/search
	chmod +x doc/search

always:	

clean:	
	rm -rf tclsh doc sqlite3.h
