# references/Makefile -- the external K_nu supplier objects, built with the flags of
# the published comparison (-march=native -O2, no -std); nothing links libmatern.

# The first rule is the `$(OBJECTS): common/cpu_supplier.h` line, not `all`.
.DEFAULT_GOAL := all

# No -std: the GNU dialect defaults to -ffp-contract=fast, while -std=c11 changes
# the arithmetic of these ports and hides M_PI (check-flags counts the FMAs).
CFLAGS   = -march=native -O2 -Wall
CXXFLAGS = -std=c++17 -march=native -O2 -Wall

# For the objects' build records: what -march=native resolved to and the build host's CPU, so
# that an external built on one machine and run on another is visible in the run (evaluated
# once, on first use).
march_of = $(shell $(1) -march=native -Q --help=target 2>/dev/null | awk '$$1 == "-march=" {print $$2; exit}')
BUILD_HOST_CPU = $(eval BUILD_HOST_CPU := $(shell sed -n 's/^model name[[:space:]]*: //p' /proc/cpuinfo 2>/dev/null \
    | head -1 | tr -d "\"'\\\\"))$(BUILD_HOST_CPU)
C_HOST_DEFS   = $(eval C_HOST_DEFS := -DBENCH_BUILD_MARCH='"$(call march_of,$(CC))"' \
    -DBENCH_BUILD_HOST_CPU='"$(BUILD_HOST_CPU)"')$(C_HOST_DEFS)
CXX_HOST_DEFS = $(eval CXX_HOST_DEFS := -DBENCH_BUILD_MARCH='"$(call march_of,$(CXX))"' \
    -DBENCH_BUILD_HOST_CPU='"$(BUILD_HOST_CPU)"')$(CXX_HOST_DEFS)

INCLUDES = -Icommon

# KAUST CuBesselK carries "All rights reserved" and is not distributed; its rows are
# built only when the port exists (top level: make kaust).
# V=yes (also 1, on, true, y) prints the compile commands.
YES_WORDS = yes y 1 on true Yes Y On True YES ON TRUE
Q := $(if $(filter $(YES_WORDS),$(strip $(V))),,@)
KAUST_PORT := $(wildcard build/kaust/kaust_derived.h)
KAUST_SRC  := $(if $(KAUST_PORT),kaust_logbesselk/kaust_port.c)

# GSL: one built by `make externals` (gsl/<version>/), else the system GSL.  GSL_WHERE, the
# copy used, and GSL_LIBRARY, the library file with its package or build flags and its FMA
# count (gsl_flags.sh --library, run once and only by the cpu_gsl.o rules), go into the
# object's build record.
GSL_FLAGS   = bash ../benchmark/tools/gsl_flags.sh
GSL_PRESENT := $(shell $(GSL_FLAGS) --exists && echo yes)
ifeq ($(GSL_PRESENT),yes)
GSL_CFLAGS := $(shell $(GSL_FLAGS) --cflags)
GSL_WHERE  := $(shell $(GSL_FLAGS) --where)
GSL_LIBRARY = $(eval GSL_LIBRARY := $(shell CC="$(CC)" $(GSL_FLAGS) --library))$(GSL_LIBRARY)
endif

# C++ counts as present only if it compiles std::cyl_bessel_k (libc++ has no C++17
# special functions); tools/requirements.sh applies the same test.
CXX_PRESENT := $(shell printf '\043include <cmath>\nint main(){return (int)std::cyl_bessel_k(1.5,2.0);}\n' \
                 | $(CXX) -std=c++17 -x c++ - -o /dev/null >/dev/null 2>&1 && echo yes)

# The CPU Boost row is Boost 1.75.0 or absent.  The compile refuses a
# boost/version.hpp other than BOOST_CPU_VERSION (CPATH can put another Boost ahead of -I).
# BOOST_CPU_WHERE, the directory and its origin, goes into the object's build record.
ifeq ($(CXX_PRESENT),yes)
BOOST_CPU_DIR := $(shell CXX="$(CXX)" BOOST_CPU_ROOT="$(BOOST_CPU_ROOT)" bash ../benchmark/tools/boost_detect.sh --select cpu)
BOOST_CPU_WHERE := $(shell CXX="$(CXX)" BOOST_CPU_ROOT="$(BOOST_CPU_ROOT)" bash ../benchmark/tools/boost_detect.sh --where cpu)
endif
ifneq ($(BOOST_CPU_DIR),)
BOOST_CPU_VERSION := $(shell sed -n 's/^\#define BOOST_VERSION  *\([0-9]*\).*/\1/p' $(BOOST_CPU_DIR)/boost/version.hpp | head -1)
BOOST_CXXFLAGS := -DCPU_SUPPLIER_WITH_BOOST -DCPU_SUPPLIER_BOOST_EXPECT=$(BOOST_CPU_VERSION) -I$(BOOST_CPU_DIR)
endif

# SciPy: the xsf headers fetch_scipy.sh places (scipy.special.kv / kve); a C++ object, built
# when the headers and a C++17 compiler are there.
SCIPY_XSF     := $(wildcard scipy/upstream/xsf/include/xsf/bessel.h)
SCIPY_VERSION := $(if $(SCIPY_XSF),$(shell cat scipy/upstream/VERSION))

OBJECTS = build/cpu_bessels_jl.o \
          build/cpu_bessels_old.o \
          build/build_info_c.o
DEFINES =
LINK_LIBS =
ifeq ($(CXX_PRESENT),yes)
OBJECTS   += build/cpu_cxx.o
DEFINES   += -DCPU_SUPPLIER_WITH_CXX $(if $(BOOST_CPU_DIR),-DCPU_SUPPLIER_WITH_BOOST)
LINK_LIBS += -lstdc++
endif
ifneq ($(KAUST_SRC),)
OBJECTS += build/kaust_port.o
DEFINES += -DCPU_SUPPLIER_WITH_KAUST
endif
ifeq ($(CXX_PRESENT)$(if $(SCIPY_XSF),yes),yesyes)
OBJECTS   += build/cpu_scipy.o
DEFINES   += -DCPU_SUPPLIER_WITH_SCIPY
endif
ifeq ($(GSL_PRESENT),yes)
OBJECTS   += build/cpu_gsl.o
DEFINES   += -DCPU_SUPPLIER_WITH_GSL
LINK_LIBS += $(shell $(GSL_FLAGS) --libs)
endif

$(OBJECTS): common/cpu_supplier.h

# The drivers read build/defines.txt and build/link.txt, so detection happens here
# only; the explicit object list keeps a leftover object of another configuration out.
.PHONY: all clean check-flags FORCE
all: build/objects.stamp
	@printf '%s\n' "$(strip $(DEFINES))" > build/defines.txt
	@printf '%s\n' "$(strip $(addprefix references/,$(OBJECTS)) $(LINK_LIBS))" > build/link.txt

# The report, only when an object was (re)built: the drivers call `all` on every build.
build/objects.stamp: $(OBJECTS)
	@echo "built $(words $?) of the $(words $(OBJECTS)) supplier objects in references/build/"
	@[ "$(CXX_PRESENT)" = yes ] || echo "  (cpu_cxx.o skipped: no C++ compiler $(CXX); no std:: and Boost rows)"
	@[ "$(CXX_PRESENT)" != yes ] || [ -n "$(BOOST_CPU_DIR)" ] || echo "  (Boost row skipped: no Boost 1.75.0 selected; std:: row built; make externals adds it)"
	@[ -z "$(BOOST_CPU_DIR)" ] || echo "  Boost row: $(BOOST_CPU_DIR) (BOOST_VERSION $(BOOST_CPU_VERSION))"
	@[ "$(GSL_PRESENT)" = yes ] || echo "  (cpu_gsl.o skipped: no GSL found; make externals builds one)"
	@[ -n "$(SCIPY_XSF)" ] || echo "  (cpu_scipy.o skipped: no SciPy xsf headers; bash references/fetch_scipy.sh)"
	@touch $@

build/cpu_bessels_jl.o: bessels_jl/port/cpu_bessels_jl.c
	@mkdir -p build
	$(Q)$(CC) $(CFLAGS) $(INCLUDES) -Ibessels_jl/port -c $< -o $@

build/cpu_bessels_old.o: bessels_jl/port/cpu_bessels_old.c
	@mkdir -p build
	$(Q)$(CC) $(CFLAGS) $(INCLUDES) -Ibessels_jl/port -c $< -o $@

# Compiled with the suppliers' flags, so the build record it reports is theirs.
build/build_info_c.o: common/build_info_c.c common/build_info.h
	@mkdir -p build
	$(Q)$(CC) $(CFLAGS) $(C_HOST_DEFS) $(INCLUDES) -c $< -o $@

# The port is never distributed and no rule here makes it.
build/kaust_port.o: kaust_logbesselk/kaust_port.c build/kaust/kaust_derived.h
	@mkdir -p build
	$(Q)$(CC) $(CFLAGS) $(INCLUDES) -Ibuild/kaust -c $< -o $@

# Configuration stamps, rewritten only on change: another GSL or Boost, another origin of
# the same directory, or another GSL library record rebuilds the object.  make rebuilds only
# on a strictly newer prerequisite and some file systems store whole seconds, so stamp_newer
# makes a rewritten stamp newer than a listed file written in the same second.
stamp_newer = for f in $(1); do [ ! -e $$f ] || [ $@ -nt $$f ] || { sleep 1; touch $@; break; }; done
build/gsl.config: FORCE
	@mkdir -p build
	@echo "$(GSL_CFLAGS) $(GSL_WHERE) $(GSL_LIBRARY)" | cmp -s - $@ \
	    || { echo "$(GSL_CFLAGS) $(GSL_WHERE) $(GSL_LIBRARY)" > $@; $(call stamp_newer,build/cpu_gsl.o); }

build/cpu_gsl.o: gsl/cpu_gsl.c build/gsl.config common/build_info.h
	@mkdir -p build
	$(Q)$(CC) $(CFLAGS) $(C_HOST_DEFS) $(INCLUDES) $(GSL_CFLAGS) -DCPU_SUPPLIER_GSL_SOURCE='"$(GSL_WHERE)"' \
	    -DCPU_SUPPLIER_GSL_LIBRARY='"$(GSL_LIBRARY)"' -c $< -o $@

build/cxx.config: FORCE
	@mkdir -p build
	@echo "$(CXX) $(BOOST_CXXFLAGS) $(BOOST_CPU_WHERE)" | cmp -s - $@ \
	    || { echo "$(CXX) $(BOOST_CXXFLAGS) $(BOOST_CPU_WHERE)" > $@; $(call stamp_newer,build/cpu_cxx.o); }

# std::cyl_bessel_k, and Boost (headers, BSL-1.0) when one is selected; its directory and
# origin go into the object's build record.
build/cpu_cxx.o: cpp_stdlib/cpu_cxx.cpp build/cxx.config common/build_info.h
	@mkdir -p build
	$(Q)$(CXX) $(CXXFLAGS) $(CXX_HOST_DEFS) $(BOOST_CXXFLAGS) $(if $(BOOST_CPU_DIR),-DCPU_SUPPLIER_BOOST_SOURCE='"$(BOOST_CPU_WHERE)"') \
	    $(INCLUDES) -c $< -o $@

# scipy.special.kv / kve: SciPy's xsf headers, unchanged, with the C++ suppliers' flags plus
# -ffp-contract=off (the SciPy 1.18.1 wheel's special-function module contains no FMA
# instructions): with contraction off, this object's
# kv and kve equal the installed wheel's bit for bit on every point of the 16 accuracy datasets
# (make scipy-check); with contraction on, 0-52 % of a dataset's points differ, by up to
# 5.7e-14 relative.  The other compiled externals keep contraction on.
build/cpu_scipy.o: scipy/cpu_scipy.cpp scipy/upstream/VERSION common/build_info.h
	@mkdir -p build
	$(Q)$(CXX) $(CXXFLAGS) $(CXX_HOST_DEFS) -ffp-contract=off -Iscipy/upstream/xsf/include -DCPU_SUPPLIER_SCIPY_VERSION='"$(SCIPY_VERSION)"' \
	    -DCPU_SUPPLIER_SCIPY_SOURCE='"references/scipy/upstream (PyPI scipy-$(SCIPY_VERSION).tar.gz, $(shell cut -c1-16 scipy/upstream/SHA256))"' \
	    $(INCLUDES) -c $< -o $@

# FMA instruction counts of the C ports (Bessels.jl, and KAUST when ported), GNU dialect
# against -std=c11.
FLAG_SOURCES := $(KAUST_SRC) \
                bessels_jl/port/cpu_bessels_jl.c \
                bessels_jl/port/cpu_bessels_old.c

check-flags: $(if $(KAUST_SRC),build/kaust/kaust_derived.h)
	@mkdir -p build
	@echo "  fused multiply-adds, GNU dialect (as measured) vs -std=c11:"
	@tg=0; ti=0; \
	 for src in $(FLAG_SOURCES); do \
	     $(CC) $(CFLAGS) $(INCLUDES) -Ibessels_jl/port -Ibuild/kaust -S $$src -o build/flags_gnu.s; \
	     $(CC) $(CFLAGS) -std=c11 -D_GNU_SOURCE $(INCLUDES) -Ibessels_jl/port -Ibuild/kaust \
	         -S $$src -o build/flags_iso.s; \
	     gnu=$$(grep -cE 'vfn?m(add|sub)' build/flags_gnu.s); \
	     iso=$$(grep -cE 'vfn?m(add|sub)' build/flags_iso.s); \
	     printf '    %-34s %4s -> %-4s\n' "$$(basename $$src)" "$$gnu" "$$iso"; \
	     tg=$$((tg+gnu)); ti=$$((ti+iso)); \
	 done; \
	 echo "    $$(printf '%-34s %4s -> %-4s' TOTAL $$tg $$ti)"; \
	 if [ -z "$(KAUST_SRC)" ]; then \
	     echo "  (KAUST not ported; make kaust at the top level)"; \
	 fi; \
	 if [ "$$tg" = "$$ti" ]; then \
	     echo "  the dialect does not change the arithmetic"; \
	 else \
	     echo "  the dialect changes the arithmetic; CFLAGS carries no -std"; \
	 fi

# Checks the KAUST upstream files against the sha256 table of
# kaust_logbesselk/PROVENANCE.md; neither downloads nor ports.
UPSTREAM_KAUST = kaust_logbesselk/upstream/logbesselk.cu \
                 kaust_logbesselk/upstream/logbesselk.h

.PHONY: verify-upstream
verify-upstream:
	@if [ ! -e kaust_logbesselk/upstream/logbesselk.cu ] && [ ! -e kaust_logbesselk/upstream/logbesselk.h ]; then \
	    echo "  KAUST upstream sources not present; nothing to verify (make kaust at the top level)."; \
	    exit 0; \
	 fi; \
	 doc=kaust_logbesselk/PROVENANCE.md; rc=0; \
	 for f in $(UPSTREAM_KAUST); do \
	    base=$$(basename $$f); rel=$${f#kaust_logbesselk/}; \
	    if [ ! -e $$f ]; then echo "  $$base: missing (both files are needed)"; rc=1; continue; fi; \
	    got=$$(sha256sum $$f | cut -d' ' -f1); \
	    want=$$(awk -v b="$$rel" '$$0 ~ "`" b "`" { if (match($$0, /[0-9a-f]{32,}/)) { print substr($$0, RSTART, RLENGTH); exit } }' $$doc); \
	    if [ -z "$$want" ]; then \
	        echo "  $$base: no digest recorded in $$doc"; rc=1; \
	    elif [ "$$got" = "$$want" ]; then \
	        echo "  $$base: ok  $$got"; \
	    else \
	        echo "  $$base: mismatch"; echo "      file: $$got"; echo "      doc : $$want"; rc=1; \
	    fi; \
	done; \
	if [ $$rc -ne 0 ]; then \
	    echo "  the placed upstream files are not the ones PROVENANCE.md records"; exit 1; \
	fi

# Regenerates the Bessels.jl coefficient header from bessels_jl/upstream/.
.PHONY: extract
extract:
	cd bessels_jl/port && ./extract_upstream.sh

clean:
	rm -rf build
