#
# Makefile for the MIDAS analyzer component of ROOTANA
#
# Options:
# make NO_ROOT=1 # build without ROOT support
# make NO_MIDAS=1 # build without MIDAS support
#
# Build configurations:
# - as part of midas: should have ../include/midas.h
# - with midas at $MIDASSYS: should have $(MIDASSYS)/include/midas.h
# - standalone: packages mxml, mjson, mvodb, midasio must be cloned next to manalyzer (should have ../mjson/mjson.h, etc)
#

CXXFLAGS += -std=c++17 -g -O2 -Wall -Wuninitialized

# check for ROOT

ifdef NO_ROOT
ROOTSYS:=
else
ifneq ($(ROOTSYS),)
HAVE_ROOT:=1
endif
endif

# check for MIDAS

ifdef NO_MIDAS
MIDASSYS:=
else
ifneq (,$(wildcard ../include/midas.h))
MIDASSYS:=..
HAVE_MIDAS:=1
else
ifneq (,$(wildcard $(MIDASSYS)/include/midas.h))
HAVE_MIDAS:=1
endif
endif
endif


# find ROOT

ifdef HAVE_ROOT
ROOTCFLAGS := $(shell root-config --cflags)
ROOTFEATURES := $(shell root-config --features)
ROOTLIBDIR := $(shell root-config --libdir)
ROOTLIBS   := -L$(ROOTLIBDIR) $(shell root-config --libs) -lThread -ltbb
ROOTGLIBS  := -L$(ROOTLIBDIR) $(shell root-config --glibs) -lThread -ltbb
#ROOTRPATH  := -Wl,-rpath,$(ROOTLIBDIR) ### ROOT uses symlinks and RPATH cannot find them, use LD_LIBRARY_PATH instead. K.O. Jul 2021

CXXFLAGS += -DHAVE_ROOT
CXXFLAGS += $(ROOTCFLAGS)

CT_ROOTCFLAGS := -DHAVE_ROOT -I$(shell root-config --incdir)

HAVE_ROOT_HTTP := $(findstring http,$(ROOTFEATURES))

ifdef HAVE_ROOT_HTTP
CXXFLAGS += -DHAVE_THTTP_SERVER
ROOTLIBS += -lRHTTP
CT_ROOTCFLAGS += -DHAVE_THTTP_SERVER
endif

LIBS += $(ROOTGLIBS)
LIBS += $(ROOTLIBS)
LIBS += $(ROOTRPATH)

endif

ifdef HAVE_MIDAS

# build with MIDAS

CXXFLAGS += -DHAVE_MIDAS -I$(MIDASSYS)/include -I$(MIDASSYS)/midasio -I$(MIDASSYS)/mvodb -I$(MIDASSYS)/mxml -I$(MIDASSYS)/mjson

# check that we have tmfe.h that supports TMEventBuffer

ifneq (,$(wildcard $(MIDASSYS)/include/tmfe.h))
ifneq (,$(shell grep TMEventBuffer $(MIDASSYS)/include/tmfe.h))
CXXFLAGS += -DHAVE_TMFE
endif
endif

LIBS += -L$(MIDASSYS)/lib -lmidas

else

# standalone build

CXXFLAGS += -I../midasio -I../mvodb -I../mxml -I../mjson

OBJS += midasio.o lz4frame.o xxhash.o lz4.o lz4hc.o
OBJS += mvodb.o nullodb.o mxmlodb.o mjsonodb.o mjson.o mxml.o

%.o: ../midasio/%.cxx
	$(CXX) -o $@ $(CXXFLAGS) -c $<

%.o: ../midasio/%.c
	$(CXX) -o $@ $(CXXFLAGS) -c $<

%.o: ../mvodb/%.cxx
	$(CXX) -o $@ $(CXXFLAGS) -c $<

%.o: ../mjson/%.cxx
	$(CXX) -o $@ $(CXXFLAGS) -c $<

%.o: ../mxml/%.cxx
	$(CXX) -o $@ $(CXXFLAGS) -c $<

endif

# select the main program - local custom main()
# or standard main() from rootana

MAIN := manalyzer_main.o

# build against a local copy of manalyzer.o

MANA := manalyzer.o

# uncomment and define analyzer modules here

#MODULES += agbars_module.o
#ALL     += barsana.exe

# mjsroot

ifdef HAVE_ROOT
ALL += mjsroot.exe
endif

# analyzer with just the event dump module

ALL += manalyzer_test.exe

# examples

EXAMPLE_ALL += manalyzer_example_cxx.exe
EXAMPLE_ALL += manalyzer_example_esoteric.exe
EXAMPLE_ALL += manalyzer_example_flow.exe
EXAMPLE_ALL += manalyzer_example_flow_queue.exe
ifdef HAVE_ROOT
EXAMPLE_ALL += manalyzer_example_root.exe
EXAMPLE_ALL += manalyzer_example_root_graphics.exe
endif
#EXAMPLE_ALL += manalyzer_example_order.exe

all:: $(EXAMPLE_ALL)
all:: $(MODULES)
all:: $(ALL)

CMAKE := cmake

cmake: $(GIT_SUBMODULES)
	mkdir -p build
	cd build; $(CMAKE) ..; $(MAKE) VERBOSE=1; $(MAKE) install

cclean:
	-rm -rf build bin lib include

test:
	./bin/manalyzer_test.exe --demo
	./bin/manalyzer_example_cxx.exe --demo
	./bin/manalyzer_example_flow.exe --demo
	./bin/manalyzer_example_flow_queue.exe --demo
	./bin/manalyzer_example_root.exe --demo
	./bin/manalyzer_example_root_graphics.exe --demo

mjsroot.exe: %.exe: %.o
	$(CXX) -o $@ $< $(CXXFLAGS) $(LIBS) -lm -lz -lpthread

%.exe: %.o manalyzer_main.o $(OBJS)
	$(CXX) -o $@ manalyzer_main.o $< $(CXXFLAGS) $(OBJS) $(LIBS) -lm -lz -lpthread

$(EXAMPLE_ALL): %.exe: %.o $(MAIN) $(MANA) $(OBJS)
	$(CXX) -o $@ $(MAIN) $(MANA) $< $(CXXFLAGS) $(OBJS) $(LIBS) -lm -lz -lpthread

manalyzer_example_order.exe: $(MAIN) $(MANA) manalyzer_example_cxx.o manalyzer_example_flow.o $(OBJS)
	$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS) -lm -lz -lpthread

%.exe: $(MAIN) $(MANA) $(MODULES) $(OBJS)
	$(CXX) -o $@ $(MAIN) $(MANA) $(MODULES) $(CXXFLAGS) $(OBJS) $(LIBS) -lm -lz -lpthread

%.o: %.cxx
	$(CXX) -o $@ $(CXXFLAGS) -c $<

html/index.html:
	-mkdir html
	-make -k dox
	touch html/index.html

dox:
	doxygen

#####################################################################
# clang-tidy static code analysis
#
#

CT_INCLUDE := -I/usr/include/c++/11 -I/usr/include/x86_64-linux-gnu/c++/11 -I../midasio -I../mvodb -I../include
CT_INCLUDE += $(CT_ROOTCFLAGS)

#CT_CHECKS  := -checks=-clang-analyzer-deadcode.DeadStores,-clang-analyzer-security.insecureAPI.strcpy,clang-analyzer-nullability.NullableReturnedFromNonnull,-
#CT_CHECKS  := -checks=clang-analyzer-core.NullDereference
#CT_CHECKS  := --checks="*"

%.tidy: %.cxx
	-clang-tidy $^ $(CT_CHECKS) -- $(CT_INCLUDE) | grep -i -e warning -e error

%.tidy-full: %.cxx
	-clang-tidy $^ $(CT_CHECKS) -- $(CT_INCLUDE)

CT_SRC :=
CT_SRC += manalyzer.cxx manalyzer_main.cxx
CT_SRC += mjsroot.cxx
CT_SRC += $(wildcard manalyzer_example*.cxx)

#clang-tidy:
#	echo $(CT_SRC)

clang-tidy: $(subst .cxx,.tidy,$(CT_SRC))

clang-tidy-full: $(subst .cxx,.tidy-full,$(CT_SRC))

#####################################################################


clean::
	-rm -f *.o *.a *.exe

clean::
	-rm -f $(ALL)

clean::
	-rm -f $(EXAMPLE_ALL)

clean::
	-rm -rf *.exe.dSYM

clean::
	-rm -rf html

# end
