#
# Makefile rules for the semigroups package
#
KEXT_NAME = semigroups

KEXT_CXXFLAGS = -I/usr/include/libsemigroups -I/usr/include/eigen3 -std=gnu++17 -O3
KEXT_LDFLAGS = -Wl,-rpath,/usr/lib64 -lsemigroups -lfmt

# configure settings
GAPPATH = /usr/lib64/gap
HPCOMBI_CONSTEXPR_FUN_ARGS = @HPCOMBI_CONSTEXPR_FUN_ARGS@
LIBSEMIGROUPS_HPCOMBI_ENABLED = @LIBSEMIGROUPS_HPCOMBI_ENABLED@
WITH_INCLUDED_LIBSEMIGROUPS = 
SYS_IS_CYGWIN = 
abs_top_builddir = /home/abuild/rpmbuild/BUILD/gap-semigroups-5.6.1-build/semigroups-5.6.1

# sources
KEXT_SOURCES =  src/bipart.cpp
KEXT_SOURCES += src/conglatt.cpp
KEXT_SOURCES += src/froidure-pin-fallback.cpp
KEXT_SOURCES += src/isomorph.cpp
KEXT_SOURCES += src/pkg.cpp
KEXT_SOURCES += src/to-gap.cpp

KEXT_SOURCES += src/init-cong.cpp
KEXT_SOURCES += src/init-froidure-pin-base.cpp
KEXT_SOURCES += src/init-froidure-pin-bipart.cpp
KEXT_SOURCES += src/init-froidure-pin-bmat.cpp
KEXT_SOURCES += src/init-froidure-pin-matrix.cpp
KEXT_SOURCES += src/init-froidure-pin-max-plus-mat.cpp
KEXT_SOURCES += src/init-froidure-pin-min-plus-mat.cpp
KEXT_SOURCES += src/init-froidure-pin-pbr.cpp
KEXT_SOURCES += src/init-froidure-pin-pperm.cpp
KEXT_SOURCES += src/init-froidure-pin-transf.cpp
KEXT_SOURCES += src/init-presentation.cpp
KEXT_SOURCES += src/init-sims.cpp
KEXT_SOURCES += src/init-to-congruence.cpp
KEXT_SOURCES += src/init-to-froidure-pin.cpp
KEXT_SOURCES += src/init-todd-coxeter.cpp

KEXT_SOURCES += gapbind14/src/gapbind14.cpp

KEXT_CPPFLAGS = -Igapbind14/include/

ifdef HPCOMBI_CONSTEXPR_FUN_ARGS
KEXT_CPPFLAGS += -DHPCOMBI_CONSTEXPR_FUN_ARGS
endif

ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
KEXT_CXXFLAGS += 
endif

ifdef WITH_INCLUDED_LIBSEMIGROUPS
# FIXME(later) all the include paths should point into bin/include/ and not to
# the sources, or otherwise we should stop make installing into bin
ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
KEXT_CPPFLAGS += -Ilibsemigroups/third_party/HPCombi/include
KEXT_CPPFLAGS += -Ilibsemigroups/third_party/HPCombi/third_party/
endif
KEXT_CPPFLAGS += -Ilibsemigroups/third_party/fmt-11.1.4/include
KEXT_CPPFLAGS += -Ilibsemigroups/third_party/magic_enum-0.9.7/include
KEXT_CPPFLAGS += -Ilibsemigroups/include
endif
KEXT_CPPFLAGS += -DFMT_HEADER_ONLY

ifdef KERNEL_DEBUG
KEXT_CPPFLAGS += -DDEBUG
else
KEXT_CPPFLAGS += -DNDEBUG
endif

KEXT_CXXFLAGS += $(KEXT_CPPFLAGS) # HACK: there is no KEXT_CPPFLAGS

# HACK: On cygwin, add this flag to work around issues where fork() sometimes
# fails on Cygwin when semigroups.so is loaded into GAP. This will hopefully
# become unnecessary with GAP 4.13, which will automatically add this to the
# flags.
ifdef SYS_IS_CYGWIN
KEXT_LDFLAGS += -Wl,--enable-auto-image-base
endif

# include shared GAP package build system
KEXT_USE_AUTOCONF = 1
include Makefile.gappkg

# ensure pkgconfig.h is built
$(KEXT_OBJS): gen/pkgconfig.h

ifdef WITH_INCLUDED_LIBSEMIGROUPS
# ensure libsemigroups is installed into bin before compiling
$(KEXT_OBJS): bin/include/libsemigroups/libsemigroups.hpp

# the following is only run if BUILT_SOURCES is wound up
bin/include/libsemigroups/libsemigroups.hpp:
	$(MAKE) -C libsemigroups install
	# Cygwin only looks for DLLs in the same directory as the executable
	# resides in. The following achieves that assuming that the GAP
	# being used was self-compiled by the user. This supports
	# both older GAPs with libtool (first) and GAP without libtool (second)
        # Additionally, we rebsae the dll with respect to the database to 
        # avoid fork conflicts
	if test -f bin/bin/cygsemigroups*.dll ; then rebase -s bin/bin/cygsemigroups*.dll ; fi
	if test -f bin/bin/cygsemigroups*.dll ; then if test -d $(GAPPATH)/.libs; then cp bin/bin/cygsemigroups*.dll $(GAPPATH)/.libs/ ; fi ; fi
	if test -f bin/bin/cygsemigroups*.dll ; then cp bin/bin/cygsemigroups*.dll $(GAPPATH)/ ; fi

endif

clean:
	find . -name '*.gcda' -delete

lint:
	etc/gaplint.sh
	etc/cpplint.sh

format:
	clang-format -i src/*.*pp

.PHONY: lint format

superclean: distclean
	git clean -xdf --exclude *.swp --exclude *.swo
.PHONY: superclean
