#
# Makefile rules for the semigroups package
#
abs_top_builddir = /home/abuild/rpmbuild/BUILD/gap-semigroups-5.6.2-build/semigroups-5.6.2
top_builddir = .
abs_builddir = /home/abuild/rpmbuild/BUILD/gap-semigroups-5.6.2-build/semigroups-5.6.2
builddir = .

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
WITH_INCLUDED_LIBSEMIGROUPS = 
SYS_IS_CYGWIN = 
abs_top_builddir = /home/abuild/rpmbuild/BUILD/gap-semigroups-5.6.2-build/semigroups-5.6.2

# 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/

include /usr/include/libsemigroups/hpcombi.mk

ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
  KEXT_CXXFLAGS += $(HPCOMBI_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 prefix='${abs_builddir}/bin' includedir='${abs_builddir}/bin/include' libdir='${abs_builddir}/bin/lib'
	#
	# The Win32 dynamic loader only looks for DLLs in the same directory as
	# the executable. As a consequence, libtool puts DLLs into
	# "${libdir}/../bin" (for both Cygwin and MinGW), and this path
	# extension is not configurable. This explains why we test for bin/bin/
	# rather than bin/lib.
	#
	# Additionally, we rebase 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

# Try to remove the DLL from both of the two places it might be found, as
# described in the block of comments above. This is only relevant when using
# Cygwin.
	rm -f $(GAPPATH)/.libs/cygsemigroups*.dll
	rm -f $(GAPPATH)/cygsemigroups*.dll

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
