# 							-*- mode: makefile -*-
# Do not put GCC specific flags here. Put them instead in CFLAGS and
# CXXFLAGS in ~/.R/Makevars
# In particular, add the following to ~/.R/Makevars:
# LTO_OPT=-flto
LTO=$(LTO_OPT)
MOOCORE_DEBUG ?=0
PKG_CPPFLAGS=-DR_PACKAGE -DDEBUG=$(MOOCORE_DEBUG) -I./libmoocore/
PKG_CFLAGS+=$(C_VISIBILITY)

MOOCORE_SRC_FILES = hv3dplus.c hv4d.c hv_contrib.c hv.c hvapprox.c hvc3d.c pareto.c whv.c whv_hype.c avl.c eaf3d.c eaf.c io.c rng.c mt19937/mt19937.c
SOURCES = $(MOOCORE_SRC_FILES:%=libmoocore/%) init.c Rmoocore.c
OBJECTS = $(SOURCES:.c=.o)

.PHONY: all clean exes

all: exes $(SHLIB)

# We need this to force building order in parallel builds
$(SHLIB): $(OBJECTS)

# Compile exes only when MOOCORE_DEBUG=0. Otherwise we get problems with
# coverage and sanitizers.  We must use a shell 'if', not a make 'ifeq',
# because make parses this file before ~/.R/Makevars is applied.
exes: $(SHLIB)
	@if [ "$(MOOCORE_DEBUG)" = "0" ]; then \
	  $(MAKE) -C libmoocore all march=none CC="$(CC)" CFLAGS="$(CFLAGS)" WARN_CFLAGS="" OPT_CFLAGS="" DEBUG=$(MOOCORE_DEBUG) EXE=$(EXEEXT) BINDIR=$(PWD); \
	  $(RM) libmoocore/*.o; \
        fi

clean:
	@-$(RM) $(SHLIB) $(OBJECTS)
	$(MAKE) -C libmoocore clean EXE=$(EXEEXT) BINDIR=$(PWD)
