$(call ShakeNMake.CALL.CLEAN-SET,f-apps)
ifneq (1,$(MAKING_CLEAN))
f-apps: all
endif
fapps.MAKEFILE := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))

fapps.ADDED_LIBF_STATIC := 0
ifeq (,$(CC_PROFILE_FLAG))
  fapps.LDFLAGS := -L$(DIR.top) -lfossil
else
  # For profiling builds to report useful stuff we have to statically
  # link...
  fapps.LDFLAGS := $(LIBF_STATIC_CLIENT_LDFLAGS)
  fapps.ADDED_LIBF_STATIC := 1
endif

ifeq (0,@LIBFOSSIL_SHARED@)
  # In static build, we need some extra flags...
  fapps.LDFLAGS += $(LDFLAGS_MODULE_LOADER)
ifeq (0,$(fapps.ADDED_LIBF_STATIC))
  fapps.LDFLAGS += $(LIBF_STATIC_CLIENT_LDFLAGS)
endif
endif

TEST_BINS_LDFLAGS := $(fapps.LDFLAGS) @SH_LINKFLAGS@
TEST_BINS_DEPS := $(libfossil.OBJ)
TEST_BINS_DEPS += $(libfossil.library)

########################################################################
# Sets up binary build rules for binary named $1. This only works
# for the basic test binaries which all have common compile/link
# bits. Assumes sources are in $1.c.
define RULES_F_BIN
# $(1).bin := $$(DIR.f-apps)/$(1)
$(1).BIN.OBJECTS += $(1).o
$$($(1).BIN.OBJECTS): $$(fapps.MAKEFILE)
$(1).BIN.LDFLAGS += $$(TEST_BINS_LDFLAGS)
$(call ShakeNMake.CALL.RULES.BIN,$(1))
$$($(1).BIN): $$(TEST_BINS_DEPS)
all: $$($(1).BIN)
CLEAN.f-apps += $$($(1).BIN) $$($(1).BIN.OBJECTS)
ifneq (,$(CC_PROFILE_FLAG))
$$($(1).BIN.OBJECTS): $$(LIBF_STATIC_CLIENT_OBJ_DEPS)
endif
endef

# Binaries which might get installed...
F_BINS.1 := $(patsubst %,$(DIR.f-apps)/%,\
  f-acat \
  f-add \
  f-adiff \
  f-annotate \
  f-aparse \
  f-ci \
  f-ciwoco \
  f-co \
  f-config \
  f-delta \
  f-extract \
  f-ls \
  f-merge \
  f-new \
  f-open \
  f-parseparty \
  f-query \
  f-rebuild \
  f-rename \
  f-repostat \
  f-resolve \
  f-revert \
  f-rm \
  f-status \
  f-tag \
  f-timeline \
  f-update \
  f-vdiff \
  f-wiki \
  f-zip)

# Test/non-installed binaries...
F_BINS.2 := $(patsubst %,$(DIR.f-apps)/%,\
  f-event \
  f-sanity \
  f-sizeof \
  f-_scratchpad \
  f-_template \
  f-test-cidiff \
  f-test-ciwoco \
  f-test-diff-builder \
  f-test-line-diff \
  f-test-merge3 \
  f-test-reserved-name-check)

########################################################################
# Optional curses-specific bits...
ifneq (,$(LIB_CURSES))
  $(DIR.f-apps)/diff-builder-ncu.o: CFLAGS+=$(CFLAGS_CURSES)
  ftcb := $(DIR.f-apps)/f-test-custom-builder
  F_BINS.2 += $(ftcb)
  $(ftcb).BIN.LDFLAGS += $(LIB_CURSES)
  $(ftcb).BIN.OBJECTS += $(DIR.f-apps)/fsl-ncurses.o
  $(ftcb).o: CFLAGS+=$(CFLAGS_CURSES)

  $(DIR.f-apps)/f-vdiff.BIN.LDFLAGS += $(LIB_CURSES)
  $(DIR.f-apps)/fsl-ncurses.o: CFLAGS+=$(CFLAGS_CURSES)
  $(DIR.f-apps)/f-vdiff.BIN.OBJECTS += $(DIR.f-apps)/fsl-ncurses.o
  $(DIR.f-apps)/f-vdiff.o: CFLAGS+=$(CFLAGS_CURSES)
  $(DIR.f-apps)/f-vdiff.o: CPPFLAGS+=-DHAS_NCURSES
endif
# /curses
########################################################################

$(eval $(call ADD_C_DEPS,f-apps,$(wildcard $(DIR.f-apps)/*.c)))
f-apps: deps-f-apps

ShakeNMake.install.bins += $(F_BINS.1)
$(foreach fbin,$(F_BINS.1) $(F_BINS.2),$(eval $(call RULES_F_BIN,$(fbin))))
CLEAN.fnc += $(wildcard $(DIR.f-apps)/*~)
