all:
include ../subdir-inc.make
#$(error $(TOP_SRCDIR))
CPPFLAGS += -DBUILD_libfossil
#CPPFLAGS += -I$(TOP_INCDIR)
#CPPFLAGS += -I$(TOP_SRCDIR)/include
#CPPFLAGS += -I$(TOP_SRCDIR)/src# workaround for in-tree sqlite3.h
EXTRA_LIBS := -lz
EXTRA_LIBS +=  $(LDFLAGS_MODULE_LOADER)

#INCLUDES_PATH ?= $(HOME)/include /usr/local/include /usr/include


# FSL.SRC.BASE = all sources which can go in the amalgamation build
# FSL.SRC = FSL.SRC.BASE + generated/added sources
FSL.SRC.BASE := \
	fsl.c \
	fsl_appendf.c \
	fsl_auth.c \
	fsl_bag.c \
	fsl_buffer.c \
	fsl_checkin.c \
	fsl_checkout.c \
	fsl_cli.c \
	fsl_content.c \
	fsl_config.c \
	fsl_cx.c \
	fsl_db.c \
	fsl_delta.c \
	fsl_diff.c \
	fsl_encode.c \
	fsl_event.c \
	fsl_ext_regexp.c \
	fsl_fs.c \
	fsl_glob.c \
	fsl_io.c \
	fsl_leaf.c \
	fsl_list.c \
	fsl_md5.c \
	fsl_mf.c \
	fsl_path.c \
	fsl_popen.c \
	fsl_pq.c \
	fsl_repo.c \
	fsl_schema.c \
	fsl_sha1.c \
	fsl_strftime.c \
	fsl_tag.c \
	fsl_ticket.c \
	fsl_utf8.c \
	fsl_vfile.c \
	fsl_vtbl_fossil_settings.c \
	fsl_wiki.c \
	fsl_zip.c

FSL.SRC := $(FSL.SRC.BASE) \
	fsl_jni.c \
	fsl_tcl.c

ifneq (,$(wildcard sqlite3.c))
  FSL.SRC += sqlite3.c
  HAVE_OWN_SQLITE := 1
else
  HAVE_OWN_SQLITE := 0
endif

FSL.OBJ := $(patsubst %.c,%.o,$(FSL.SRC))

########################################################################
# Transform schema SQL files into source form. We don't use fossil(1)'s
# approach because it relies on unspecified length limits on C string
# literals. Yeah, it works, but i'm funny about the C Standard.
#include $(TOP_SRCDIR_REL)/tools.make
DIR.TOOLS := $(TOP_SRCDIR_REL)/tools
BIN.TEXT2C := $(DIR.TOOLS)/text2c
$(BIN.TEXT2C): $(DIR.TOOLS)/text2c.c
	cc $< -o $@
CLEAN_FILES += $(BIN.TEXT2C)

define SQL2C
$(2):
$(1).c: $(2) $$(BIN.TEXT2C) $$(MAIN_MAKEFILES)
	@echo "Creating $$@ from $(2)..."; \
	{ \
		echo '/* Binary form of file $(2) */'; \
		echo '/** @page page_$(1) Schema: $$(notdir $(2))'; \
		echo '@code'; \
		cat "$(2)"; \
		echo ' @endcode'; \
		echo ' @see $$(subst _cstr,,$(1))()'; \
		echo '*/'; \
		$$(BIN.TEXT2C) $(1) < $(2); \
		echo '/* end of $(2) */'; \
	} > $$@;
FSL.SRC += $(1).c
FSL.SRC.BASE += $(1).c
FSL.OBJ += $(1).o
DISTCLEAN_FILES += $(1).c
sql: $(1).c
endef

SQL.DIR := $(TOP_SRCDIR_REL)/sql
$(eval $(call SQL2C,fsl_schema_config_cstr,$(SQL.DIR)/config.sql))
$(eval $(call SQL2C,fsl_schema_repo1_cstr,$(SQL.DIR)/repo-static.sql))
$(eval $(call SQL2C,fsl_schema_repo2_cstr,$(SQL.DIR)/repo-transient.sql))
$(eval $(call SQL2C,fsl_schema_checkout_cstr,$(SQL.DIR)/checkout.sql))
$(eval $(call SQL2C,fsl_schema_ticket_cstr,$(SQL.DIR)/ticket.sql))
$(eval $(call SQL2C,fsl_schema_ticket_reports_cstr,$(SQL.DIR)/ticket-reports.sql))
# end SQL transformation bits
########################################################################

# These are the flags used by fossil's embedded sqlite3:
sqlite3.o: CPPFLAGS+=-DSQLITE_OMIT_LOAD_EXTENSION=1 \
	-DSQLITE_THREADSAFE=0 \
	-DSQLITE_DEFAULT_FILE_FORMAT=4 \
	-DSQLITE_ENABLE_STAT3 \
	-DSQLITE_ENABLE_LOCKING_STYLE=0 

CLEAN_FILES += $(FSL.OBJ)
ifeq (1,$(HAVE_OWN_SQLITE))
# in-tree sqlite3
#  EXTRA_LIBS +=
else
# system/external sqlite3
  EXTRA_LIBS += -lpthread
endif

libfossil.DLL.OBJECTS := $(FSL.OBJ)
libfossil.DLL.LDFLAGS := @SH_LDFLAGS@ $(EXTRA_LIBS)
libfossil.LIB.OBJECTS := $(libfossil.DLL.OBJECTS)
########################################################################
# Shared lib
ifeq (1,@LIBFOSSIL_SHARED@)
$(eval $(call ShakeNMake.CALL.RULES.DLLS,libfossil))
all: $(libfossil.DLL)
$(libfossil.DLL): $(libfossil.DLL.OBJECTS)
endif

# Static lib
ifeq (1,@LIBFOSSIL_STATIC@)
$(eval $(call ShakeNMake.CALL.RULES.LIBS,libfossil))
all: $(libfossil.LIB)
$(libfossil.LIB): $(libfossil.LIB.OBJECTS)
endif

########################################################################
# A quick-n-dirty amalgamation build...
INCD := $(TOP_INCDIR)/fossil-scm
AMAL_D := $(TOP_SRCDIR_REL)
AMAL_C := $(AMAL_D)/libfossil.c
AMAL_H := $(AMAL_D)/libfossil.h
AMAL_CONF.H := $(AMAL_D)/libfossil-config.h
CLEAN_FILES += $(AMAL_H) $(AMAL_C) $(AMAL_CONF.H)

AMAL_C.SRC := \
	$(FSL.SRC.BASE)

ifeq (0,1)
# the tcl bits currently break the amalgamation
AMAL_C.SRC += \
	$(COMPAT_DIR)/javavm/export/jni_md.h \
	$(COMPAT_DIR)/javavm/export/jni.h \
	$(COMPAT_DIR)/tcl-8.6/generic/tcl.h \
	$(COMPAT_DIR)/tcl-8.6/generic/tclDecls.h \
	$(COMPAT_DIR)/tcl-8.6/generic/tclPlatDecls.h
endif

# ACHTUNG: in AMAL_H.SRC, $(AMAL_CONF.H) MUST be included first.
AMAL_H.SRC := \
	$(INCD)/fossil-config.h \
	$(INCD)/fossil.h \
	$(INCD)/fossil-util.h \
	$(INCD)/fossil-core.h \
	$(INCD)/fossil-db.h \
	$(INCD)/fossil-hash.h \
	$(INCD)/fossil-content.h \
	$(INCD)/fossil-confdb.h \
	$(INCD)/fossil-path.h \
	$(INCD)/fossil-internal.h \
	$(INCD)/fossil-auth.h \
	$(INCD)/fossil-pages.h \
	$(INCD)/fossil-cli.h
$(AMAL_H.SRC):
$(AMAL_C.SRC):
$(AMAL_H): $(MAKEFILE_LIST) $(AMAL_H.SRC) $(AMAL_CONF.H)
$(AMAL_C): $(MAKEFILE_LIST) $(AMAL_H) $(AMAL_C.SRC)
$(AMAL_CONF.H): $(MAIN_MAKEFILES)
	@echo "Generating $@ ..."
	cd $(TOP_SRCDIR_REL) && sh configure --amal

$(AMAL_H): 
	@echo "Creating $@..."
	@{ \
		echo '#if !defined(FSL_AMALGAMATION_BUILD)'; \
		echo '#define FSL_AMALGAMATION_BUILD 1'; \
		echo '#endif'; \
		echo '#include "$(notdir $(AMAL_CONF.H))"'; \
	} > $@
	@{ \
		for i in $(AMAL_H.SRC); do \
			echo "/* start of file $$i */"; \
			cat $$i; \
			echo "/* end of file $$i */"; \
		done; \
	} | sed  \
		 -e '/[ ]*#[ ]*include[ ]*.*fossil.*\.h[>"]/d' \
		 -e '/[ ]*#[ ]*include[ ]*.*".*config\.h[>"]/d' \
		>> $@

$(AMAL_C): $(AMAL_H)
	@echo "Creating $@..."
	@echo '#include "$(notdir $(AMAL_H))"' > $@
	@{ \
		for i in $(AMAL_C.SRC); do \
			echo "/* start of file $$i */"; \
			cat $$i; \
			echo "/* end of file $$i */"; \
		done; \
	} | sed \
		 -e '/[ ]*#[ ]*include[ ]*.*fossil.*\.h[>"]/d' \
		>> $@

AMAL_CPP_FLAGS := -I$(TOP_INCDIR)
AMAL_CPP_FLAGS += -I.# for local sqlite3.h
amal: $(AMAL_C)
	@if which gcc >/dev/null; then \
		echo "Trying GCC C89..."; \
		gcc -c -pedantic -Wstrict-aliasing -Wall -Werror -std=c89 -Wno-long-long $(AMAL_CPP_FLAGS) $(AMAL_C) || exit;\
		echo "Trying GCC C99..."; \
		gcc -c -pedantic -Wstrict-aliasing -Wall -Werror -std=c99 $(AMAL_CPP_FLAGS) $(AMAL_C) || exit; \
	fi
	@if which tcc >/dev/null; then \
		echo "Trying tcc..."; \
		tcc -c -pedantic -Wall -Werror $(AMAL_CPP_FLAGS) $(AMAL_C) || exit; \
		echo "Man, that was FAST!"; \
	 fi
	@if which clang >/dev/null; then \
		echo "Trying clang..."; \
		clang -c -pedantic -Wall -Werror $(AMAL_CPP_FLAGS) $(AMAL_C) || exit; \
	fi
# /amalgamation
########################################################################
