#
# Makefile rules for the cvec package
#
# This requires GNU make (just as the main GAP does).
#
# For simple packages one should only need to modify the MODULE_NAME
# and the SOURCES variable.
#
MODULE_NAME = cvec

SOURCES = src/cvec.c

########################################################################
# Do not edit below unless you know what you're doing
########################################################################

########################################################################
# Some variables that come from GAP via our configure script
########################################################################
GAPPATH = /build/gap/src/gap-4.11.1
GAPARCH = aarch64-unknown-linux-gnu-default64-kv7

BINARCHDIR = bin/aarch64-unknown-linux-gnu-default64-kv7
GAPINSTALLIB = $(BINARCHDIR)/$(MODULE_NAME).so

MKDIR_P = /bin/mkdir -p

GAP = /build/gap/src/gap-4.11.1/gap
GAC = /build/gap/src/gap-4.11.1/gac

all: $(GAPINSTALLIB)
.PHONY: all

########################################################################
# Object files
# For each file FOO.c in SOURCES, add gen/FOO.lo to OBJS; similar
# for .cc files
########################################################################
OBJS = $(patsubst %.cc,gen/%.lo,$(patsubst %.c,gen/%.lo,$(SOURCES)))

########################################################################
# Quiet rules.
#
# Replace regular output with quiet messages, unless V is set,
# e.g. "make V=1"
########################################################################
ifneq ($(findstring $(MAKEFLAGS),s),s)
ifndef V
QUIET_GAC     = @echo "   GAC     $< => $@";
LIBTOOL          += --silent
endif
endif

########################################################################
# Rules for automatic header dependency tracking.
# This is based on the GAP build system.
########################################################################

# List of all (potential) dependency directories, derived from OBJS
# and SOURCES (the latter for generated sources, which may also involve
# dependency tracking)
DEPDIRS = $(sort $(dir $(SOURCES) $(OBJS)))
ALL_DEP_FILES = $(wildcard $(addsuffix /*.d,$(DEPDIRS)))

# Include the dependency tracking files.
-include $(ALL_DEP_FILES)

# Mark *.d files as PHONY. This stops make from trying to
# recreate them (which it can't), and in particular from looking for potential
# source files. This can save quite a bit of disk access time.
.PHONY: $(ALL_DEP_FILES)

# The following flags instruct the compiler to enable advanced
# dependency tracking. Supported by GCC 3 and newer; clang; Intel C
# compiler; and more.
DEPFLAGS = -MQ "$@" -MMD -MP -MF $(@D)/$(*F).d

# build rule for C code
gen/%.lo: %.c
	@$(QUIET_GAC)$(GAC) -d -p "$(DEPFLAGS)" -c $< -o $@

# build rule for C++ code
gen/%.lo: %.cc
	@$(QUIET_GAC)$(GAC) -d -p "$(DEPFLAGS)" -c $< -o $@

# build rule for linking all object files together into a kernel extension
$(GAPINSTALLIB): $(OBJS)
	@$(MKDIR_P) $(BINARCHDIR)
	@$(QUIET_GAC)$(GAC) -d -p "$(DEPFLAGS)" $(OBJS) -o $@

clean:
	rm -rf $(BINARCHDIR)
	rm -rf gen

distclean:
	rm -rf bin gen Makefile
	(cd doc && ./clean)

doc: default
	$(GAP) makedoc.g

check: default
	$(GAP) tst/testall.g

Makefile: configure Makefile.in
	./configure "/build/gap/src/gap-4.11.1"

.PHONY: default clean distclean doc check

########################################################################
# Makefile debugging trick:
# call print-VARIABLE to see the runtime value of any variable
########################################################################
print-%:
	@echo '$*=$($*)'
