#!/usr/make
#
# Makefile for SQLITE
#
# This is a template makefile for SQLite.  Most people prefer to
# use the autoconf generated "configure" script to generate the
# makefile automatically.  But that does not work for everybody
# and in every situation.  If you are having problems with the
# "configure" script, you might want to try this makefile as an
# alternative.  Create a copy of this file, edit the parameters
# below and type "make".
#

#### The SQLite version number
#
SQLITE_VERSION = 2.8.16
SQLITE_DLLVERS = 28
SQLITE_DLL_DEF = sqlite_dll_os2.def

#### The toplevel directory of the source tree.  This is the directory
#    that contains this "Makefile.in" and the "configure.in" script.
#
TOP = ../sqlite

#### for OMF linked .exes, we need to explicitly set the stack size
#
CCX_STACK = -Zstack 0x1000

#### C Compiler and options for use in building executables that
#    will run on the platform that is doing the build.
#
BCC = gcc -O3 -fomit-frame-pointer -mprobe -Zmt -Zomf
#BCC = /opt/ancic/bin/c89 -0

#### If the target operating system supports the "usleep()" system
#    call, then define the HAVE_USLEEP macro for all C modules.
#
USLEEP = 
#USLEEP = -DHAVE_USLEEP=1

#### If you want the SQLite library to be safe for use within a 
#    multi-threaded program, then define the following macro
#    appropriately:
#
THREADSAFE = -DTHREADSAFE=1
#THREADSAFE = -DTHREADSAFE=0

#### Specify any extra linker options needed to make the library
#    thread safe
#
#THREADLIB = -lpthread
THREADLIB = 

#### Leave MEMORY_DEBUG undefined for maximum speed.  Use MEMORY_DEBUG=1
#    to check for memory leaks.  Use MEMORY_DEBUG=2 to print a log of all
#    malloc()s and free()s in order to track down memory leaks.
#    
#    SQLite uses some expensive assert() statements in the inner loop.
#    You can make the library go almost twice as fast if you compile
#    with -DNDEBUG=1
#
#OPTS = -DMEMORY_DEBUG=2
#OPTS = -DMEMORY_DEBUG=1
#OPTS = 
OPTS = -DNDEBUG=1 -DSQLITE_DISABLE_LFS

#### C Compile and options for use in building executables that 
#    will run on the target platform.  This is usually the same
#    as BCC, unless you are cross-compiling.
#
TCC = gcc -s -O3 -fomit-frame-pointer -mprobe -Zmt -Zomf
#TCC = gcc -O6
#TCC = gcc -g -O0 -Wall
#TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage
#TCC = /opt/mingw/bin/i386-mingw32-gcc -O6
#TCC = /opt/ansic/bin/c89 -O +z -Wl,-a,archive

#### Tools used to build a static library.
#
AR = ar cr
#AR = /opt/mingw/bin/i386-mingw32-ar cr
RANLIB = ar s
#RANLIB = ranlib
#RANLIB = /opt/mingw/bin/i386-mingw32-ranlib
OMFAR = emxomfar cr
OMFRLIB = emxomfar s


#### Extra compiler options needed for programs that use the TCL library.
#
TCL_FLAGS = -DNO_TCL=1
#TCL_FLAGS = -DSTATIC_BUILD=1
#TCL_FLAGS = -I/home/drh/tcltk/8.4linux
#TCL_FLAGS = -I/home/drh/tcltk/8.4win -DSTATIC_BUILD=1
#TCL_FLAGS = -I/home/drh/tcltk/8.3hpux

#### Linker options needed to link against the TCL library.
#
LIBTCL =
#LIBTCL = -ltcl -lm -ldl
#LIBTCL = /home/drh/tcltk/8.4linux/libtcl8.4g.a -lm -ldl
#LIBTCL = /home/drh/tcltk/8.4win/libtcl84s.a -lmsvcrt
#LIBTCL = /home/drh/tcltk/8.3hpux/libtcl8.3.a -ldld -lm -lc

#### Compiler options needed for programs that use the readline() library.
#
#READLINE_FLAGS =
#READLINE_FLAGS = -DHAVE_READLINE=1 -I/usr/include/readline
READLINE_FLAGS = -DHAVE_READLINE=1

#### Linker options needed by programs using readline() must link against.
#
#LIBREADLINE =
LIBREADLINE = -lreadline -lncurses

#### Should the database engine assume text is coded as UTF-8 or iso8859?
#
# ENCODING  = UTF8
ENCODING = ISO8859

# You should not have to change anything below this line
###############################################################################
###############################################################################
# The following macros should be defined before this script is
# invoked:
#
# TOP              The toplevel directory of the source tree.  This is the
#                  directory that contains this "Makefile.in" and the
#                  "configure.in" script.
#
# BCC              C Compiler and options for use in building executables that
#                  will run on the platform that is doing the build.
#
# USLEEP           If the target operating system supports the "usleep()" system
#                  call, then define the HAVE_USLEEP macro for all C modules.
#
# THREADSAFE       If you want the SQLite library to be safe for use within a 
#                  multi-threaded program, then define the following macro
#                  appropriately:
#
# THREADLIB        Specify any extra linker options needed to make the library
#                  thread safe
#
# OPTS             Extra compiler command-line options.
#
# EXE              The suffix to add to executable files.  ".exe" for windows
#                  and "" for Unix.
#
# TCC              C Compiler and options for use in building executables that 
#                  will run on the target platform.  This is usually the same
#                  as BCC, unless you are cross-compiling.
#
# AR               Tools used to build a static library.
# RANLIB
#
# TCL_FLAGS        Extra compiler options needed for programs that use the
#                  TCL library.
#
# LIBTCL           Linker options needed to link against the TCL library.
#
# READLINE_FLAGS   Compiler options needed for programs that use the
#                  readline() library.
#
# LIBREADLINE      Linker options needed by programs using readline() must
#                  link against.
#
# ENCODING         "UTF8" or "ISO8859"
#
# Once the macros above are defined, the rest of this make script will
# build the SQLite library and testing tools.
################################################################################

# This is how we compile
#
TCCX = $(TCC) $(OPTS) $(CCX_STACK) $(THREADSAFE) $(USLEEP) -I. -I$(TOP)/src

# Object files for the SQLite library.
#
LIBOBJ = attach.obj auth.obj btree.obj btree_rb.obj build.obj copy.obj \
         date.obj delete.obj expr.obj func.obj hash.obj insert.obj \
         main.obj opcodes.obj os.obj pager.obj parse.obj pragma.obj printf.obj random.obj \
         select.obj table.obj tokenize.obj trigger.obj update.obj util.obj \
         vacuum.obj vdbe.obj vdbeaux.obj where.obj tclsqlite.obj

# All of the source code files.
#
SRC = \
  $(TOP)/src/attach.c \
  $(TOP)/src/auth.c \
  $(TOP)/src/btree.c \
  $(TOP)/src/btree.h \
  $(TOP)/src/btree_rb.c \
  $(TOP)/src/build.c \
  $(TOP)/src/copy.c \
  $(TOP)/src/date.c \
  $(TOP)/src/delete.c \
  $(TOP)/src/expr.c \
  $(TOP)/src/func.c \
  $(TOP)/src/hash.c \
  $(TOP)/src/hash.h \
  $(TOP)/src/insert.c \
  $(TOP)/src/main.c \
  $(TOP)/src/os.c \
  $(TOP)/src/pager.c \
  $(TOP)/src/pager.h \
  $(TOP)/src/parse.y \
  $(TOP)/src/pragma.c \
  $(TOP)/src/printf.c \
  $(TOP)/src/random.c \
  $(TOP)/src/select.c \
  $(TOP)/src/shell.c \
  $(TOP)/src/sqlite.h.in \
  $(TOP)/src/sqliteInt.h \
  $(TOP)/src/table.c \
  $(TOP)/src/tclsqlite.c \
  $(TOP)/src/tokenize.c \
  $(TOP)/src/trigger.c \
  $(TOP)/src/update.c \
  $(TOP)/src/util.c \
  $(TOP)/src/vacuum.c \
  $(TOP)/src/vdbe.c \
  $(TOP)/src/vdbe.h \
  $(TOP)/src/vdbeaux.c \
  $(TOP)/src/vdbeInt.h \
  $(TOP)/src/where.c

# Source code to the test files.
#
TESTSRC = \
  $(TOP)/src/btree.c \
  $(TOP)/src/func.c \
  $(TOP)/src/os.c \
  $(TOP)/src/pager.c \
  $(TOP)/src/test1.c \
  $(TOP)/src/test2.c \
  $(TOP)/src/test3.c \
  $(TOP)/src/md5.c

# Header files used by all library source files.
#
HDR = \
   sqlite.h  \
   $(TOP)/src/btree.h \
   config.h \
   $(TOP)/src/hash.h \
   opcodes.h \
   $(TOP)/src/os.h \
   $(TOP)/src/sqliteInt.h  \
   $(TOP)/src/vdbe.h  \
   parse.h

# Header files used by the VDBE submodule
#
VDBEHDR = \
   $(HDR) \
   $(TOP)/src/vdbeInt.h

# auxiliary tools
#
SQLITE_TOOLS = diffdb.exe showdb.exe showjournal.exe

# rule to make a.out objects from OMF objects
%.o:	%.obj
	emxaout $<

# This is the default Makefile target.  The objects listed here
# are what get build when you type just "make" with no arguments.
#
all:	sqlite.h config.h sqlite$(SQLITE_DLLVERS).dll sqlite.lib sqlite.exe sqlite.a

# Generate the file "last_change" which contains the date of change
# of the most recently modified source code file
#
last_change:	$(SRC)
	cat $(SRC) | grep '$$Id: ' | sort +4 | tail -1 \
          | awk '{print $$5,$$6}' >last_change

sqlite$(SQLITE_DLLVERS).dll:	$(LIBOBJ) $(SQLITE_DLL_DEF)
	gcc -Zdll -Zomf -Zso -Zsys -o sqlite$(SQLITE_DLLVERS).dll $(LIBOBJ) $(SQLITE_DLL_DEF)

sqlite.lib:	$(SQLITE_DLL_DEF)
	emximp -o $@ $<

sqlite.a:	$(SQLITE_DLL_DEF)
	emximp -o $@ $<

sqlite.exe:	$(TOP)/src/shell.c sqlite.lib sqlite.h
	$(TCCX) -Zcrtdll $(READLINE_FLAGS) -o sqlite.exe $(TOP)/src/shell.c \
		-L. -lsqlite $(LIBREADLINE) $(THREADLIB)

# This target creates a directory named "tsrc" and fills it with
# copies of all of the C source code and header files needed to
# build on the target system.  Some of the C source code and header
# files are automatically generated.  This target takes care of
# all that automatic generation.
#
target_source:	$(SRC) $(VDBEHDR) opcodes.c
	rm -rf tsrc
	mkdir tsrc
	cp $(SRC) $(VDBEHDR) tsrc
	rm tsrc/sqlite.h.in tsrc/parse.y
	cp parse.c opcodes.c tsrc

# Rules to build the LEMON compiler generator
#
lemon.exe:	$(TOP)/tool/lemon.c $(TOP)/tool/lempar.c
	$(BCC) -o lemon.exe $(TOP)/tool/lemon.c
	cp $(TOP)/tool/lempar.c .

btree.obj:	$(TOP)/src/btree.c $(HDR) $(TOP)/src/pager.h
	$(TCCX) -c $(TOP)/src/btree.c

btree_rb.obj:	$(TOP)/src/btree_rb.c $(HDR)
	$(TCCX) -c $(TOP)/src/btree_rb.c

build.obj:	$(TOP)/src/build.c $(HDR)
	$(TCCX) -c $(TOP)/src/build.c

main.obj:	$(TOP)/src/main.c $(HDR)
	$(TCCX) -c $(TOP)/src/main.c

pager.obj:	$(TOP)/src/pager.c $(HDR) $(TOP)/src/pager.h
	$(TCCX) -c $(TOP)/src/pager.c

opcodes.obj:	opcodes.c
	$(TCCX) -c opcodes.c

opcodes.c:	$(TOP)/src/vdbe.c
	echo /* Automatically generated file.  Do not edit */ >opcodes.c
	echo char *sqliteOpcodeNames[] = { "???",  >>opcodes.c
	grep "^case OP_" $(TOP)/src/vdbe.c | \
	  sed -e "s/^.*OP_/  \"/" -e "s/:.*$$/\", /" >>opcodes.c
	echo }; >>opcodes.c

opcodes.h:	$(TOP)/src/vdbe.h
	echo /* Automatically generated file.  Do not edit */ >opcodes.h
	grep "^case OP_" $(TOP)/src/vdbe.c | \
	  sed -e s/:// | \
	  awk '{printf "#define %-30s %3d\n", $$2, ++cnt}' >>opcodes.h

os.obj:	$(TOP)/src/os.c $(HDR)
	$(TCCX) -c $(TOP)/src/os.c

parse.obj:	parse.c $(HDR)
	$(TCCX) -c parse.c

parse.h:	parse.c

parse.c:	$(TOP)/src/parse.y lemon.exe
	cp $(TOP)/src/parse.y .
	./lemon parse.y

# The config.h file will contain a single #define that tells us how
# many bytes are in a pointer.  This only works if a pointer is the
# same size on the host as it is on the target.  If you are cross-compiling
# to a target with a different pointer size, you'll need to manually
# configure the config.h file.
#
config.h:	
	echo #include ^<stdio.h^> >temp.c
	echo int main(){printf( >>temp.c
	echo "#define SQLITE_PTR_SZ %d",sizeof(char*)); >>temp.c
	echo exit(0);} >>temp.c
	$(BCC) -o temp temp.c
	temp >config.h
	rm -f temp.c temp.exe

sqlite.h:	$(TOP)/src/sqlite.h.in 
	sed -e s/--VERS--/$(SQLITE_VERSION)/ \
            -e s/--ENCODING--/$(ENCODING)/ \
                 $(TOP)/src/sqlite.h.in >sqlite.h

tokenize.obj:	$(TOP)/src/tokenize.c $(HDR)
	$(TCCX) -c $(TOP)/src/tokenize.c

trigger.obj:	$(TOP)/src/trigger.c $(HDR)
	$(TCCX) -c $(TOP)/src/trigger.c

util.obj:	$(TOP)/src/util.c $(HDR)
	$(TCCX) -c $(TOP)/src/util.c

vacuum.obj:	$(TOP)/src/vacuum.c $(HDR)
	$(TCCX) -c $(TOP)/src/vacuum.c

vdbe.obj:	$(TOP)/src/vdbe.c $(VDBEHDR)
	$(TCCX) -c $(TOP)/src/vdbe.c

vdbeaux.obj:	$(TOP)/src/vdbeaux.c $(VDBEHDR)
	$(TCCX) -c $(TOP)/src/vdbeaux.c

where.obj:	$(TOP)/src/where.c $(HDR)
	$(TCCX) -c $(TOP)/src/where.c

copy.obj:	$(TOP)/src/copy.c $(HDR)
	$(TCCX) -c $(TOP)/src/copy.c

date.obj:	$(TOP)/src/date.c $(HDR)
	$(TCCX) -c $(TOP)/src/date.c

delete.obj:	$(TOP)/src/delete.c $(HDR)
	$(TCCX) -c $(TOP)/src/delete.c

expr.obj:	$(TOP)/src/expr.c $(HDR)
	$(TCCX) -c $(TOP)/src/expr.c

func.obj:	$(TOP)/src/func.c $(HDR)
	$(TCCX) -c $(TOP)/src/func.c

hash.obj:	$(TOP)/src/hash.c $(HDR)
	$(TCCX) -c $(TOP)/src/hash.c

insert.obj:	$(TOP)/src/insert.c $(HDR)
	$(TCCX) -c $(TOP)/src/insert.c

random.obj:	$(TOP)/src/random.c $(HDR)
	$(TCCX) -c $(TOP)/src/random.c

select.obj:	$(TOP)/src/select.c $(HDR)
	$(TCCX) -c $(TOP)/src/select.c

table.obj:	$(TOP)/src/table.c $(HDR)
	$(TCCX) -c $(TOP)/src/table.c

update.obj:	$(TOP)/src/update.c $(HDR)
	$(TCCX) -c $(TOP)/src/update.c

tclsqlite.obj:	$(TOP)/src/tclsqlite.c $(HDR)
	$(TCCX) $(TCL_FLAGS) -c $(TOP)/src/tclsqlite.c

pragma.obj:	$(TOP)/src/pragma.c $(HDR)
	$(TCCX) $(TCL_FLAGS) -c $(TOP)/src/pragma.c

printf.obj:	$(TOP)/src/printf.c $(HDR)
	$(TCCX) $(TCL_FLAGS) -c $(TOP)/src/printf.c

attach.obj:	$(TOP)/src/attach.c $(HDR)
	$(TCCX) -c $(TOP)/src/attach.c

auth.obj:	$(TOP)/src/auth.c $(HDR)
	$(TCCX) -c $(TOP)/src/auth.c

tclsqlite.exe:	$(TOP)/src/tclsqlite.c sqlite.lib
	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite.exe \
		$(TOP)/src/tclsqlite.c -L. -lsqlite $(LIBTCL)

testfixture.exe:	$(TOP)/src/tclsqlite.c sqlite.lib $(TESTSRC)
	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 -DSQLITE_TEST=1 -o testfixture.exe \
		$(TESTSRC) $(TOP)/src/tclsqlite.c \
		-L. -lsqlite $(LIBTCL)

fulltest:	testfixture.exe sqlite.exe
	./testfixture.exe $(TOP)/test/all.test

test:	testfixture.exe sqlite.exe
	./testfixture.exe $(TOP)/test/quick.test

index.html:	$(TOP)/www/index.tcl last_change
	tclsh $(TOP)/www/index.tcl `cat $(TOP)/VERSION` >index.html

sqlite.html:	$(TOP)/www/sqlite.tcl
	tclsh $(TOP)/www/sqlite.tcl >sqlite.html

c_interface.html:	$(TOP)/www/c_interface.tcl
	tclsh $(TOP)/www/c_interface.tcl >c_interface.html

changes.html:	$(TOP)/www/changes.tcl
	tclsh $(TOP)/www/changes.tcl >changes.html

lang.html:	$(TOP)/www/lang.tcl
	tclsh $(TOP)/www/lang.tcl >lang.html

vdbe.html:	$(TOP)/www/vdbe.tcl
	tclsh $(TOP)/www/vdbe.tcl >vdbe.html

arch.html:	$(TOP)/www/arch.tcl
	tclsh $(TOP)/www/arch.tcl >arch.html

arch.png:	$(TOP)/www/arch.png
	cp $(TOP)/www/arch.png .

opcode.html:	$(TOP)/www/opcode.tcl $(TOP)/src/vdbe.c
	tclsh $(TOP)/www/opcode.tcl $(TOP)/src/vdbe.c >opcode.html

mingw.html:	$(TOP)/www/mingw.tcl
	tclsh $(TOP)/www/mingw.tcl >mingw.html

tclsqlite.html:	$(TOP)/www/tclsqlite.tcl
	tclsh $(TOP)/www/tclsqlite.tcl >tclsqlite.html

speed.html:	$(TOP)/www/speed.tcl
	tclsh $(TOP)/www/speed.tcl >speed.html

faq.html:	$(TOP)/www/faq.tcl
	tclsh $(TOP)/www/faq.tcl >faq.html

formatchng.html:	$(TOP)/www/formatchng.tcl
	tclsh $(TOP)/www/formatchng.tcl >formatchng.html

conflict.html:	$(TOP)/www/conflict.tcl
	tclsh $(TOP)/www/conflict.tcl >conflict.html

download.html:	$(TOP)/www/download.tcl
	tclsh $(TOP)/www/download.tcl >download.html

omitted.html:	$(TOP)/www/omitted.tcl
	tclsh $(TOP)/www/omitted.tcl >omitted.html

datatypes.html:	$(TOP)/www/datatypes.tcl
	tclsh $(TOP)/www/datatypes.tcl >datatypes.html

quickstart.html:	$(TOP)/www/quickstart.tcl
	tclsh $(TOP)/www/quickstart.tcl >quickstart.html

fileformat.html:	$(TOP)/www/fileformat.tcl
	tclsh $(TOP)/www/fileformat.tcl >fileformat.html

nulls.html:	$(TOP)/www/nulls.tcl
	tclsh $(TOP)/www/nulls.tcl >nulls.html

# tools
#
diffdb.exe:	$(TOP)/tool/diffdb.c sqlite.lib sqlite.h
	$(TCCX) -Zcrtdll -o $@ $(TOP)/tool/diffdb.c -L. -lsqlite

showdb.exe:	$(TOP)/tool/showdb.c sqlite.lib sqlite.h
	$(TCCX) -Zcrtdll -o $@ $(TOP)/tool/showdb.c -L. -lsqlite

showjournal.exe:	$(TOP)/tool/showjournal.c sqlite.lib sqlite.h
	$(TCCX) -Zcrtdll -o $@ $(TOP)/tool/showjournal.c -L. -lsqlite

# Files to be published on the website.
#
DOC = \
  index.html \
  sqlite.html \
  changes.html \
  lang.html \
  opcode.html \
  arch.html \
  arch.png \
  vdbe.html \
  c_interface.html \
  mingw.html \
  tclsqlite.html \
  download.html \
  speed.html \
  faq.html \
  formatchng.html \
  conflict.html \
  omitted.html \
  datatypes.html \
  quickstart.html \
  fileformat.html \
  nulls.html

doc:	$(DOC)
	mkdir doc
	mv $(DOC) doc

install:	sqlite sqlite.lib sqlite.h
	mv sqlite /usr/bin
	mv sqlite.lib /usr/lib
	mv sqlite.h /usr/include

clean:	
	rm -f *.obj sqlite.exe sqlite.lib sqlite.h sqlite$(SQLITE_DLLVERS).dll
	rm -f sqlite.a lemon lemon.exe lempar.c parse.*
	rm -f $(PUBLISH) opcodes.* config.h
	rm -f *.da *.bb *.bbg gmon.out
	rm -rf tsrc
