#!/usr/bin/make # help out emacs
#
# Top-level autosetup-filtered Makefile for libfossil. This particular
# build is for Unix platforms with GNU Make 4+.
all:
ROOT.MAKEFILE := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
DISTCLEAN.root += $(ROOT.MAKEFILE)
TOP_DIR := $(patsubst %/,%,$(dir $(ROOT.MAKEFILE)))

#ShakeNMake.CISH_SOURCES := $(wildcard *.c) $(wildcard $(SRC.DIR)/*.c)

########################################################################
# Some common vars used by shakenmake.make and/or the sub-makes
# (make-*.make).
DIR.top := @srcdir@
DIR.src := @srcdir@/src
DIR.client := @srcdir@/client
DIR.tools := @srcdir@/tools
DIR.include := @srcdir@/include
DIR.bindings := @srcdir@/bindings
DIR.f-apps := @srcdir@/f-apps
LIB_CURSES := @LIB_CURSES@
CFLAGS_CURSES := @CFLAGS_CURSES@
ShakeNMake.CISH_SOURCES := $(patsubst %~,%,$(wildcard \
  $(DIR.src)/*.[ch] \
  $(DIR.f-apps)/*.[ch] \
  $(DIR.client)/fnc/*.[ch] \
  $(DIR.bindings)/cpp/*.[ch]pp \
  $(DIR.tools)/*.[ch] \
  $(DIR.include)/*.h* \
  $(DIR.include)/fossil-scm/*.h* \
))
include config.make
include shakenmake.make
########################################################################
# PACKAGE.C_DEPS.MAKEFILES is a list of makefiles which sub-makes may
# use as dependencies for their obj/lib files, stripped of the
# dependencies makefile, as that file, in turn, depends on the files
# it scans for dependencies.
ifneq (,$(ShakeNMake.CISH_DEPS_FILE))
  PACKAGE.C_DEPS.MAKEFILES := $(subst $(ShakeNMake.CISH_DEPS_FILE),,$(MAKEFILE_LIST))
else
  PACKAGE.C_DEPS.MAKEFILES :=
endif
########################################################################
# Subdir/module builds...
#
# Maintenance reminders: all relevant symbols in the sub-makefiles
# (make-XXX.make) MUST be unique across the whole global
# namespace. Similarly, all relevant paths must point to that
# subdir. That is particularly of interest with the BIN/DLL/LIB and
# CLEAN.xxx rules, which may require another level of $(var)
# indirection for that purpose. It's also important when using
# CPPFLAGS and such, to keep subdir-specific flags from leaking across
# makefiles. Similarly, when using ShakeNMake.install.XXX, the vars
# must appended to instead of overwritten.
########################################################################
# INCLUDE_SINGLE_SUBMAKE includes file make-$(1).make if it exists,
# else this is a no-op. It protects against multiple inclusion.
INCLUDED_SUBMAKES :=
define INCLUDE_SINGLE_SUBMAKE
  SINGLE.$(1) := $$(wildcard make-$(1).make)
  ifneq (,$$(SINGLE.$(1)))
  ifneq ($(1),$$(strip $$(filter $(1),$$(INCLUDED_SUBMAKES))))
    $(1):# kludge
    ifneq (,$$(strip $$(filter distclean-$(1) clean-$(1),$(MAKECMDGOALS))))
      MAKING_CLEAN.$(1):=1
    endif
    INCLUDED_SUBMAKES+=$(1)
    $$(info Importing sub-make: $(1))
    include $$(SINGLE.$(1))
  endif
  endif
endef
# If passed an explicit make target matching make-XXX.make then
# include that makefile. Note that we must always include
# make-libf.make because all sub-makes depend on those bits.
$(eval $(call INCLUDE_SINGLE_SUBMAKE,libf))
ifneq (,$(MAKECMDGOALS))
  $(foreach f,$(MAKECMDGOALS),$(eval $(call INCLUDE_SINGLE_SUBMAKE,$(f))))
endif
INCLUDED_SUBMAKES:=$(strip $(INCLUDED_SUBMAKES))
#$(info INCLUDED_SUBMAKES=$(INCLUDED_SUBMAKES))
ifneq (libf-only,$(strip $(filter libf-only,$(INCLUDED_SUBMAKES))))
ifeq (libf,$(INCLUDED_SUBMAKES))
  # ^^^^ no submakes were explicitly called for, so include them all...
  themakes := f-apps
  ifeq (1,$(BUILD_CLIENT_FNC))
    themakes += client-fnc
  endif
  ifneq (,$(CXX))
    themakes += binding-cpp
  endif
  $(foreach f,$(themakes),$(eval $(call INCLUDE_SINGLE_SUBMAKE,$(f))))
#  undefine themakes
  #$(info MAKEFILE_LIST=$(MAKEFILE_LIST))
endif
endif
#undefine INCLUDED_SUBMAKES
#undefine INCLUDE_SINGLE_SUBMAKE
# /subdir builds
########################################################################

$(call ShakeNMake.CALL.CLEAN-SET,root)
DISTCLEAN.root += config.make
# All GENERATED make-*.make files...
DISTCLEAN.root += make-libf.make \
  make-f-apps.make \
  make-client-fnc.make \
  make-binding-cpp.make


ifeq ($(MAKE_COMPILATION_DB),yes)
all: compile_commands.json
compile_commands.json:
	@$(RM) $@
	sed -e '1s/^/[\'$$'\n''/' -e '$$s/,$$/\'$$'\n'']/' $(compdb_dir)/*.o.json > $@+
	@if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi
endif

AUTOCONFIG_H := $(DIR.include)/fossil-scm/autoconfig.h
DISTCLEAN.root += $(AUTOCONFIG_H) $(ROOT.MAKEFILE) config.log \
  autosetup/jimsh0 \
	$(wildcard compile_commands/*) compile_commands.json+

# automake compatibility. do nothing for all these targets
#EMPTY_AUTOMAKE_TARGETS := dvi pdf ps info html tags ctags mostlyclean maintainer-clean check installcheck installdirs \
# install-pdf install-ps install-info install-html -install-dvi uninstall install-exec install-data distdir
#.PHONY: $(EMPTY_AUTOMAKE_TARGETS)
#$(EMPTY_AUTOMAKE_TARGETS):

## @top_srcdir@/Makefile.in: # b/c AUTODEPS contains this name (it probably shouldn't)
#$(FSL.OBJ): @AUTODEPS@ @top_srcdir@/Makefile @top_srcdir@/config.make

# Reconfigure if needed
#ifeq ($(findstring clean,$(MAKECMDGOALS)),)
#@top_srcdir@/config.make: @AUTODEPS@ @top_srcdir@/config.make.in
#	@@AUTOREMAKE@
#endif

# Or on demand
reconfig:
	@AUTOREMAKE@

CLEAN.root += $(wildcard $(DIR.top)/*~)
