CXX_STD = CXX17

# Optional libzstd (detected at build time, no ./configure script needed — keeps R CMD check off the
# checkbashisms path). libzarr's Zstd Zarr codec is zarr-python 3's DEFAULT v3 compressor, so a "wild" v3
# store is Zstd-encoded; when libzstd is present we enable the codec, else the build is gzip-only and
# reading a Zstd store errors clearly at READ time (not build time). pkg-config is the probe; the ifeq/
# $(shell) conditional needs GNU make (declared in DESCRIPTION SystemRequirements). Windows (Rtools) uses
# this file too — where its pkg-config can't find libzstd the build is simply gzip-only.
ZSTD_HAVE := $(shell pkg-config --exists libzstd 2>/dev/null && echo yes)
ifeq ($(ZSTD_HAVE),yes)
ZSTD_CPPFLAGS = -DLIBZARR_HAS_ZSTD $(shell pkg-config --cflags libzstd)
ZSTD_LIBS = $(shell pkg-config --libs libzstd)
endif

# zlib (bundled with R) reads gzip/zlib chunks; $(ZSTD_*) add the Zstd codec when libzstd was found above.
# _LIBCPP_DISABLE_DEPRECATION_WARNINGS: on macOS (libc++, Xcode 26.5+) the vendored nlohmann/json names
# std::basic_string<unsigned char>, instantiating the now-deprecated std::char_traits<unsigned char> — a
# compile WARNING that R CMD check flags on macOS. Silence it with libc++'s own macro (a preprocessor
# define, NOT an in-code diagnostic pragma, which CRAN's incoming pretest auto-rejects); it is an inert
# macro on gcc/MinGW libstdc++, so the Windows/Debian check is unchanged.
PKG_CPPFLAGS = -I../inst/include -DLSTAR_HAVE_ZLIB -DLIBZARR_HAS_ZLIB -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS $(ZSTD_CPPFLAGS)
# OpenMP: the streaming kernels (csc_col_mean_var, stream_col_stats, ...) are OpenMP-parallel; without
# these flags the #pragma omp directives are no-ops and the R build runs serial regardless of n_threads.
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
# std::filesystem is in libstdc++/libc++ on the supported C++17 toolchains (gcc >= 9.1, clang);
# no -lstdc++fs needed. zlib via -lz (R links it too); OpenMP link flag must match the compile flag
# (SHLIB_OPENMP_CXXFLAGS in both PKG_CXXFLAGS and PKG_LIBS, per Writing R Extensions 1.2.1.1).
PKG_LIBS = -lz $(ZSTD_LIBS) $(SHLIB_OPENMP_CXXFLAGS)

# Strip debug symbols from the built shared object. The -O3 C++ accelerator (with the header-only core +
# nlohmann/json) carries ~11MB of debug info; stripping keeps the installed package small (the CRAN
# "installed size" NOTE). `strip -S` removes only debug symbols and works on both GNU and BSD/macOS;
# guarded so it's a harmless no-op where strip is absent (e.g. Windows).
strippedLib: $(SHLIB)
	if command -v strip >/dev/null 2>&1; then strip -S $(SHLIB) 2>/dev/null || true; fi
.PHONY: strippedLib
all: $(SHLIB) strippedLib
