#!/usr/make
#
# Makefile for SQLITE
#
# This makefile is suppose to be configured automatically using the
# autoconf.  But if that does not work for you, you can configure
# the makefile manually.  Just set the parameters below to values that
# work well for your system.
#
# If the configure script does not work out-of-the-box, you might
# be able to get it to work by giving it some hints.  See the comment
# at the beginning of configure.in for additional information.
#

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

# C Compiler and options for use in building executables that
# will run on the platform that is doing the build.
#
BCC = @BUILD_CC@ @BUILD_CFLAGS@

# C Compile and options for use in building executables that 
# will run on the target platform.  (BCC and TCC are usually the
# same unless your are cross-compiling.)
#
TCC = @TARGET_CC@ @TARGET_CFLAGS@ -I. -I${TOP}/src

RANLIB=@RANLIB@
AR=@AR@

# Some standard variables and programs
#
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
INSTALL = @INSTALL@

# Compiler options needed for programs that use the readline() library.
#
READLINE_FLAGS = -DHAVE_READLINE=@TARGET_HAVE_READLINE@ @TARGET_READLINE_INC@

# The library that programs using readline() must link against.
#
LIBREADLINE = @TARGET_READLINE_LIBS@

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

# Flags controlling use of the in memory btree implementation
#
# SQLITE_OMIT_INMEMORYDB is defined in order to omit the in-memory
# red/black tree driver in the file btree_rb.c
#
# TEMP_STORE is 0 to force temporary tables to be in a file, 1 to
# default to file, 2 to default to memory, and 3 to force temporary
# tables to always be in memory.
#
INMEMORYDB = @INMEMORYDB@
INCOREFLAGS = -DTEMP_STORE=@TEMP_STORE@ @OMIT_INMEMORYDB@

#ifeq (${INMEMORYDB},0)
#INCOREFLAGS += -DSQLITE_OMIT_INMEMORYDB=1
#endif

# You should not have to change anything below this line
###############################################################################

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

# Only build the in-core DB if it is required.
#ifeq (${INMEMORYDB},1)
#LIBOBJ += btree_rb.lo
#endif

# 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/encode.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/tokenize.c \
  $(TOP)/src/trigger.c \
  $(TOP)/src/update.c \
  $(TOP)/src/util.c \
  $(TOP)/src/vacuum.c \
  $(TOP)/src/vdbe.c \
  $(TOP)/src/vdbeaux.c \
  $(TOP)/src/vdbe.h \
  $(TOP)/src/where.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

# 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 libsqlite.a sqlite@TARGET_EXEEXT@

Makefile: $(TOP)/Makefile.in
	./config.status

# 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

libsqlite.a:	$(LIBOBJ)
	rm -f $@
	${AR} cqv $@ ${LIBOBJ}
	${RANLIB} $@
#	$(LTLINK) -o libsqlite.la $(LIBOBJ) ${RELEASE} -rpath @exec_prefix@/lib \
#		-version-info "8:6:8"

sqlite@TARGET_EXEEXT@:	$(TOP)/src/shell.c libsqlite.a sqlite.h
	$(TCC) $(READLINE_FLAGS) -o sqlite $(TOP)/src/shell.c \
		libsqlite.a $(LIBREADLINE)

# 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) 
	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@BUILD_EXEEXT@:	$(TOP)/tool/lemon.c $(TOP)/tool/lempar.c
	$(BCC) -o lemon $(TOP)/tool/lemon.c
	cp $(TOP)/tool/lempar.c .

btree.o:	$(TOP)/src/btree.c $(HDR) $(TOP)/src/pager.h
	$(TCC) -c $(TOP)/src/btree.c

btree_rb.o:	$(TOP)/src/btree_rb.c $(HDR)
	$(TCC) -c $(TOP)/src/btree_rb.c

build.o:	$(TOP)/src/build.c $(HDR)
	$(TCC) -c $(TOP)/src/build.c

main.o:	$(TOP)/src/main.c $(HDR)
	$(TCC) -c ${INCOREFLAGS} $(TOP)/src/main.c

pager.o:	$(TOP)/src/pager.c $(HDR) $(TOP)/src/pager.h
	$(TCC) -c $(TOP)/src/pager.c

opcodes.o:	opcodes.c
	$(TCC) -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.o:	$(TOP)/src/os.c $(HDR)
	$(TCC) -c $(TOP)/src/os.c

parse.o:	parse.c $(HDR)
	$(TCC) -c parse.c

parse.h:	parse.c

parse.c:	$(TOP)/src/parse.y lemon@BUILD_EXEEXT@
	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
	echo >>config.h
	rm -f temp.c temp

sqlite.h:	$(TOP)/src/sqlite.h.in 
	sed -e s/--VERS--/`cat ${TOP}/VERSION`/ \
            -e s/--ENCODING--/$(ENCODING)/ \
                 $(TOP)/src/sqlite.h.in >sqlite.h

tokenize.o:	$(TOP)/src/tokenize.c $(HDR)
	$(TCC) -c $(TOP)/src/tokenize.c

util.o:	$(TOP)/src/util.c $(HDR)
	$(TCC) -c $(TOP)/src/util.c

vdbe.o:	$(TOP)/src/vdbe.c $(VDBEHDR)
	$(TCC) -c $(TOP)/src/vdbe.c

vdbeaux.o:	$(TOP)/src/vdbe.c $(VDBEHDR)
	$(TCC) -c $(TOP)/src/vdbeaux.c

where.o:	$(TOP)/src/where.c $(HDR)
	$(TCC) -c $(TOP)/src/where.c

copy.o:	$(TOP)/src/copy.c $(HDR)
	$(TCC) -c $(TOP)/src/copy.c

date.o:	$(TOP)/src/date.c $(HDR)
	$(TCC) -c $(TOP)/src/date.c

delete.o:	$(TOP)/src/delete.c $(HDR)
	$(TCC) -c $(TOP)/src/delete.c

encode.o:	$(TOP)/src/encode.c
	$(TCC) -c $(TOP)/src/encode.c

expr.o:	$(TOP)/src/expr.c $(HDR)
	$(TCC) -c $(TOP)/src/expr.c

func.o:	$(TOP)/src/func.c $(HDR)
	$(TCC) -c $(TOP)/src/func.c

hash.o:	$(TOP)/src/hash.c $(HDR)
	$(TCC) -c $(TOP)/src/hash.c

insert.o:	$(TOP)/src/insert.c $(HDR)
	$(TCC) -c $(TOP)/src/insert.c

random.o:	$(TOP)/src/random.c $(HDR)
	$(TCC) -c $(TOP)/src/random.c

select.o:	$(TOP)/src/select.c $(HDR)
	$(TCC) -c $(TOP)/src/select.c

table.o:	$(TOP)/src/table.c $(HDR)
	$(TCC) -c $(TOP)/src/table.c

trigger.o:	$(TOP)/src/trigger.c $(HDR)
	$(TCC) -c $(TOP)/src/trigger.c

update.o:	$(TOP)/src/update.c $(HDR)
	$(TCC) -c $(TOP)/src/update.c

vacuum.o:	$(TOP)/src/vacuum.c $(HDR)
	$(TCC) -c $(TOP)/src/vacuum.c

pragma.o:	$(TOP)/src/pragma.c $(HDR)
	$(TCC) -c $(TOP)/src/pragma.c

printf.o:	$(TOP)/src/printf.c $(HDR)
	$(TCC) -c $(TOP)/src/printf.c

attach.o:	$(TOP)/src/attach.c $(HDR)
	$(TCC) -c $(TOP)/src/attach.c

auth.o:	$(TOP)/src/auth.c $(HDR)
	$(TCC) -c $(TOP)/src/auth.c

install:	sqlite libsqlite.a sqlite.h
	$(INSTALL) -d $(DESTDIR)$(exec_prefix)/bin
	$(INSTALL) sqlite $(DESTDIR)$(exec_prefix)/bin

#	$(INSTALL) -d $(DESTDIR)$(exec_prefix)/lib
#	$(INSTALL) libsqlite.a $(DESTDIR)$(exec_prefix)/lib
#	$(INSTALL) -d $(DESTDIR)$(prefix)/include
#	$(INSTALL) -m 0644 sqlite.h $(DESTDIR)$(prefix)/include
#	$(INSTALL) -d $(DESTDIR)$(exec_prefix)/lib/pkgconfig; 
#	$(INSTALL) -m 0644 sqlite.pc $(DESTDIR)$(exec_prefix)/lib/pkgconfig; 

clean:	
	rm -f *.o sqlite@TARGET_EXEEXT@ libsqlite.a sqlite.h opcodes.*
	rm -f lemon@BUILD_EXEEXT@ lempar.c parse.* sqlite*.tar.gz
	rm -f $(PUBLISH)
	rm -f *.da *.bb *.bbg gmon.out
	rm -rf doc

distclean:	clean
	rm -f config.log config.status Makefile config.h
