# SPDX-License-Identifier: BSD-2-Clause
# 
# Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# makefile for libcif++

.PHONY: firstTarget
firstTarget: all

CXX					= @CXX@
CXXFLAGS			= @CXXFLAGS@ @BOOST_CPPFLAGS@ @LIBBZ2_CPPFLAGS@
LDFLAGS				= @LDFLAGS@ @LIBS@ @BOOST_LDFLAGS@ @LIBBZ2_LDFLAGS@
LIBS				= @LIBS@ \
					  @BOOST_IOSTREAMS_LIB@ 

prefix				= @prefix@
exec_prefix			= @exec_prefix@
libdir				= @libdir@
includedir			= @includedir@
datarootdir			= @datarootdir@
datadir				= @datadir@
pkgconfigdir		= $(libdir)/pkgconfig

LIB_NAME			= @PACKAGE_NAME@
LIB_TARGET			= $(LIB_NAME).la

CCP4DIR				= @CCP4@
CLIBD				?= $(CCP4DIR)/lib/data

DATADIR				= $(datadir)/libcifpp

ifeq "$(CHECK_CONFIG)" "1"

GNUmakefile: config.status GNUmakefile.in
	$(SHELL) ./config.status

config.status: configure
	$(SHELL) ./config.status --recheck

configure: configure.ac
	autoconf

LIBTOOL_DEPS = @LIBTOOL_DEPS@
libtool: $(LIBTOOL_DEPS)
	$(SHELL) ./config.status --recheck

endif

# libtool stuff

LIBTOOL = $(SHELL) ./libtool
CXXCOMPILE = $(LIBTOOL) --silent --tag=CXX --mode=compile $(CXX) $(CXXFLAGS)
CXXLINK = $(LIBTOOL) --silent --tag=CXX --mode=link $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@

# main build variables
CXXFLAGS            += -I. -pthread -I include/
CXXFLAGS            += -Wall -Wno-multichar

# Use the DEBUG flag to build debug versions of the code
DEBUG               = @DEBUG@

ifeq "$(DEBUG)" "1"
DEFINES				+= DEBUG
CXXFLAGS            += -g -O0
LDFLAGS				+= -g
else
CXXFLAGS			+= -O2
DEFINES				+= NDEBUG
endif

# targets

VPATH += src:test

CXXFLAGS			+= $(DEFINES:%=-D%)

OBJDIR = obj
ifeq "$(DEBUG)" "1"
	OBJDIR	:= $(OBJDIR).dbg
endif

$(OBJDIR):
	mkdir -p $(OBJDIR)

LIBCIF_SRC		= AtomType.cpp \
				  Cif2PDB.cpp \
				  Cif++.cpp \
				  CifParser.cpp \
				  CifUtils.cpp \
				  CifValidator.cpp \
				  Compound.cpp \
				  PDB2Cif.cpp \
				  PDB2CifRemark3.cpp \
				  Point.cpp \
				  Secondary.cpp \
				  Structure.cpp \
				  Symmetry.cpp \
				  TlsParser.cpp

SOURCES			= $(addprefix src/,$(LIBCIF_SRC))
OBJECTS			= $(addprefix $(OBJDIR)/, $(notdir $(SOURCES:%.cpp=%.lo)))

ifneq "$(CCP4DIR)" ""

# Special rules to generate symmetry operation number table
tools/symop-map-generator: tools/symop-map-generator.cpp

src/SymOpTable_data.cpp: tools/symop-map-generator $(CLIBD)/symop.lib
	tools/symop-map-generator > $@

$(OBJDIR)/Symmetry.lo: src/SymOpTable_data.cpp

endif

$(LIB_TARGET): $(OBJECTS)
	$(CXXLINK) -rpath $(libdir) $(OBJECTS) $(LIBS)

lib: $(LIB_TARGET)
.PHONY: libs

all: lib
.PHONY: all

-include $(OBJECTS:%.lo=%.d)

$(OBJECTS:.lo=.d):

$(OBJDIR)/%.lo: %.cpp | $(OBJDIR)
	@ echo ">>" $<
	@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(OBJDIR)/$*.d -c -o $@ $<

$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
	@ echo ">>" $<
	@ $(CXX) $(CXXFLAGS) -MT $@ -MD -MP -MF $(OBJDIR)/$*.d -c -o $@ $<

.PHONY: clean
clean:
	rm -rf .libs $(OBJDIR)/* $(LIB_TARGET)

.PHONY: distclean
distclean: clean
	rm -f libtool config.lt
	rm -f config.status config.cache config.log configure.lineno config.status.lineno
	rm -f GNUmakefile

# Test rules

define TEST_template =

-include $$(OBJDIR)/$(1)-test.d

$(1)_OBJECTS = $$(OBJDIR)/$(1)-test.o

test/$(1)-test: $(LIB_TARGET) $$($(1)_OBJECTS)
	@ echo ">>> building $(1)-test"
	$(LIBTOOL) --silent --tag=CXX --mode=link $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $$@ $$($(1)_OBJECTS) -L.libs -lcif++ $(LIBS)

.PHONY: $(1)-test
$(1)-test: test/$(1)-test
	cd test; ./$(1)-test $$($(1)_PARAMS)

endef

TESTS = unit

$(foreach part,$(TESTS),$(eval $(call TEST_template,$(part))))

.PHONY: test
test: $(TESTS:%=%-test)

HEADERS = \
	AtomType.hpp \
	CifParser.hpp \
	Compound.hpp \
	PDB2CifRemark3.hpp \
	Structure.hpp \
	Cif2PDB.hpp \
	CifUtils.hpp \
	Config.hpp \
	Point.hpp \
	Symmetry.hpp \
	Cif++.hpp \
	CifValidator.hpp \
	PDB2Cif.hpp \
	Secondary.hpp \
	TlsParser.hpp

.PHONY: install-lib
install-lib: lib
	install -d $(libdir)
	$(LIBTOOL) --mode=install install $(LIB_TARGET) $(libdir)
	install -d $(DATADIR)/dictionaries
	for d in mmcif_ddl.dic  mmcif_pdbx.dic; do \
		install -m644 rsrc/dictionaries/$$d $(DATADIR)/dictionaries/$$d; \
	done

.PHONY: install-dev
install-dev:
	install -d $(includedir)/cif++
	for f in $(HEADERS); do install include/cif++/$$f $(includedir)/cif++/$$f; done
	install -d $(pkgconfigdir)
	install -m 644 $(LIB_NAME).pc $(pkgconfigdir)/$(LIB_NAME).pc

.PHONY: install
install: install-lib install-dev

dist-clean: clean

FORCE:
