SHELL := @BIN_BASH@

# Tools. CC is standard. The rest are via cc-check-tools
CC = @CC@
RANLIB = @RANLIB@
AR = @AR@
STRIP = @STRIP@

# FLAGS/LIBS
CC_PROFILE_FLAG := @CC_PROFILE_FLAG@
CFLAGS += @CFLAGS@ $(CC_PROFILE_FLAG)
LDFLAGS += @LDFLAGS@ $(CC_PROFILE_FLAG)
LDLIBS += @LIBS@
#LDLIBS += -lsqlite3 -lz
LDFLAGS_MODULE_LOADER += @LDFLAGS_MODULE_LOADER@
ENABLE_CXX := @ENABLE_CXX@

# ??? HAVE_DLOPEN := @HAVE_DLOPEN@
HAVE_LIBDL := @HAVE_LIBDL@
HAVE_LIBLTDL := @HAVE_LIBLTDL@
#HAVE_LT_DLOPEN := @HAVE_LT_DLOPEN@

ENABLE_MODULES ?= @FSL_ENABLE_MODULE_LOADER@
#ifeq (1,$(HAVE_DLOPEN))
#  ENABLE_MODULES := 1
#endif
#ifeq (1,$(HAVE_LT_DLOPEN))
#  ENABLE_MODULES := 1
#endif

# Install destination
prefix = @prefix@
exec_prefix = @exec_prefix@
DESTDIR ?=

CONFIG.MAKE := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
#TOP_SRCDIR_ABS := $(dir $(realpath $(CONFIG.MAKE)))
TOP_SRCDIR_REL := $(patsubst %/,%,$(dir $(CONFIG.MAKE)))
TOP_SRCDIR_ABS := $(realpath $(TOP_SRCDIR_REL))
#$(error TOP_SRCDIR_REL=$(TOP_SRCDIR_REL))
# DIR.src := $(TOP_SRCDIR_REL)/src

# Project-specific CFLAGS
CPPFLAGS += -I$(DIR.include) -Isrc -Iextsrc
# ^^^ src is for in-tree sqlite3.h
# -I$(DIR.top)
# -I$(COMPAT_DIR)/javavm/export -I$(COMPAT_DIR)/tcl-8.6/generic
ifneq (,$(wildcard $(HOME)/include/*))
  # This is admittedly primarily for my own sake!
  CPPFLAGS += -I$(HOME)/include
endif

ifeq (,$(filter tcc,$(CC)))
  # Assume gcc-compatible compilation flags...
  CFLAGS += -Wall -Werror -Wsign-compare -pedantic
#??? -fno-strict-aliasing
  CFLAGS += @CC_FLAG_C99@
endif
# /tcc

# Enable MAKE_COMPILATION_DB = yes to generate a JSON compilation database. For
# each *.c source file being compiled, a corresponding JSON file will be created
# in the './compile_commands' sub-directory, and will be used to generate the
# 'compile_commands.json' compilation database in the repository root. This is
# only available with compilers that support the '-MJ' option (e.g., clang).
MAKE_COMPILATION_DB = @MAKE_COMPILATION_DB@

FSL_ENABLE_READLINE := @FSL_ENABLE_READLINE@
LDFLAGS_READLINE := @LIB_READLINE@

########################################################################
# client/fnc...
BUILD_CLIENT_FNC := @BUILD_CLIENT_FNC@
LDFLAGS_CURSES := @LIB_CURSES@

ifeq ($(MAKE_COMPILATION_DB),yes)
compdb_dir = compile_commands
mkdir_compdb = $(TOP_SRCDIR_REL)/$(compdb_dir)
$(mkdir_compdb):
	@mkdir -p $@

compdb_file = $(TOP_SRCDIR_REL)/$(compdb_dir)/$(subst /,-,$@.json)
compdb_args = -MJ $(compdb_file)
CFLAGS += $(compdb_args)
else
mkdir_compdb =
endif

########################################################################
# pass EXTRA_CFLAGS from the shell to extend CFLAGS.  Some flags
# reported/known to cause problems with certain gcc versions:
#
#  -Os -fstack-clash-protection -Wformat -Werror=format-security
CFLAGS += $(EXTRA_CFLAGS)

CLEAN_FILES += *.o lib*.a *.so

ShakeNMake.QUIET ?= @BUILD_QUIETLY@
ShakeNMake.ETAGS.DISABLE := 1
ShakeNMake.ETAGS.FILTER := | grep -v 'libfossil*.[ch]'

ifeq (.,$(TOP_SRCDIR_REL))
BIN_ETAGS := @BIN_ETAGS@
ifneq (,$(BIN_ETAGS))
TAGTHESE := $(shell find . -name '*.[ch]' -print)
TAGTHESE := $(filter-out $(wildcard ./libfossil[-.]*[ch]),$(TAGTHESE))
ifneq (,$(TAGTHESE))
    $(TAGTHESE):
    TAGFILE := TAGS
    $(TAGFILE): $(TAGTHESE) # $(MAKEFILE_LIST)
	@echo "Creating $@..."; \
	$(BIN_ETAGS) -f $@ $(TAGTHESE)
    tags: $(TAGFILE)
    CLEAN_FILES += $(TAGFILE)
    all: $(TAGFILE)
endif
endif
# ^^^ bin_etags
endif
# ^^^ TOP_SRCDIR_REL==.
