# vim: ft=make ts=4 sw=4 noet cindent
#---------------------------------------------------------------------------
# Makefile for SICS
# machine-dependent part for Redhat Linux with AFS at PSI
#
# Mark Koennecke 1996-2001
# Markus Zolliker, March 2003
#==========================================================================

.PHONY: sct

default: all

# ANSTO rules and variables
PSI_SLIBS = ../libsics.a ../matrix/libmatrix.a
PSI_LIBS =  \
       $(LIB_TCL8) $(LIB_HDF5) -lpthread \
       -ldl -lz  -lm  -lc $(LIB_MXML) $(LIB_JSON)
CC       = gcc
INCFLAGS = -Ihardsup  -I.. $(INC_HDF5) $(INC_TCL8)
# NOTE -std=gnu99 adds c99 support with gnu extensions, it eliminates implicit function warnings
# for c99 functions like roundf.  We don't use -std=c99 because it generates compiler errors in
# code modules that use the timeval struct without including sys/time.h, c99 also generates
# implicit function declaration warnings on string functions like strdup.

# On different systems, there may be different versions of libraries or they
# may be in different locations. This code tries to find the include files and
# libraries required in descending order of preference.

libsearcha =  $(wildcard /usr/lib*/lib$(1).a)
libsearchso =  $(wildcard /usr/lib*/lib$(1).so /usr/lib*/lib$(1).so.? /usr/lib*/lib$(1).so.??*)
libsearch =  $(firstword $(call libsearcha,$(1)) $(call libsearchso,$(1)))
libsearch1 =  $(firstword $(call libsearch,$(1)))
incsearch =  $(wildcard /usr/include/$(1) /usr/local/include/$(1))
incsearch1 =  $(firstword $(call incsearch,$(1)))

ifeq (1,$(VERBOSE))
mylibs := tcl8.5 hdf5 mxml json ghttp
$(foreach mylib,$(mylibs),$(info libsearch($(mylib)) = $(call libsearch,$(mylib))))
$(foreach mylib,$(mylibs),$(info libsearch1($(mylib)) = $(call libsearch1,$(mylib))))

myincs := tcl.h tcl8.5/tcl.h tcl8.4/tcl.h hdf5.h mxml.h json/json.h ghttp.h
$(foreach myinc,$(myincs),$(info incsearch($(myinc)) = $(call incsearch,$(myinc))))
$(foreach myinc,$(myincs),$(info incsearch1($(myinc)) = $(call incsearch1,$(myinc))))
endif

# TCL 8.5 or 8.4
LIB_TCL8 = $(firstword $(call libsearchso,tcl8.5) $(call libsearchso,tcl8.4))
INC_TCL8 = $(firstword $(call incsearch1,tcl.h) $(call incsearch1,tcl8.5/tcl.h) $(call incsearch1,tcl8.4/tcl.h))

ifeq (,$(LIB_TCL8))
  $(warning LIB_TCL8 not found)
else
  $(info LIB_TCL8 = $(LIB_TCL8))
endif

ifeq (,$(INC_TCL8))
  $(warning INC_TCL8 not found)
else
  $(info TCL8 is $(INC_TCL8))
  ifeq ("$(dir $(INC_TCL8))","/usr/include/")
    $(info TCL8 in /usr/include)
    INC_TCL8 :=
  else
    INC_TCL8 := -isystem $(dir $(INC_TCL8))
  endif
  $(info INC_TCL8 = $(INC_TCL8))
endif

# HDF5
LIB_HDF5 := $(call libsearch1,hdf5)
ifeq (,$(LIB_HDF5))
  $(warning LIB_HDF5 not found)
else
  $(info LIB_HDF5 = $(LIB_HDF5))
endif
#
INC_HDF5 := $(call incsearch1,hdf5.h)
ifeq (,$(INC_HDF5))
  $(warning INC_HDF5 not found)
else
  $(info HDF5 is $(INC_HDF5))
  ifeq ("$(dir $(INC_HDF5))","/usr/include/")
    $(info HDF5 in /usr/include)
    INC_HDF5 :=
  else
    INC_HDF5 := -I$(dir $(INC_HDF5))
  endif
  $(info INC_HDF5 = $(INC_HDF5))
endif

# JSON-C
LIB_JSON = $(firstword $(call libsearch1,json-c) $(call libsearch1,json))
ifeq (,$(LIB_JSON))
  $(warning LIB_JSON not found)
else
  $(info LIB_JSON = $(LIB_JSON))
endif

# MXML
LIB_MXML := $(call libsearch1,mxml)
ifeq (,$(LIB_MXML))
  $(warning LIB_MXML not found)
else
  $(info LIB_MXML = $(LIB_MXML))
endif

# GHTTP
LIB_HTTP := $(call libsearch1,ghttp)
ifeq (,$(LIB_HTTP))
  $(warning LIB_HTTP not found)
else
  $(info LIB_HTTP = $(LIB_HTTP))
endif

#
DEBUGFLAGS = -Wall -Wextra -Wno-unused -Wno-comment -Wno-switch
CFLAGS = -g -std=gnu99
CFLAGS += $(INCFLAGS)
CFLAGS += $(DEBUGFLAGS)
CFLAGS += -MMD

SITENAME = _ansto
SITEDIR  = site_ansto
SITELIB  = libansto.a
SITEHARDSUP = hardsup/libhlib.a
HARDSUPDIR = $(SITEDIR)/hardsup
SUBLIBS = libansto.a hardsup/libhlib.a $(PSI_SLIBS) libansto.a hardsup/libhlib.a $(PSI_LIBS)

.SUFFIXES:
.SUFFIXES: .c .o .f

GHTTP_LIBS = $(LIB_HTTP)

#OBJ= site_ansto.o batch.o buffer.o ruli.o protocol.o sinfox.o \
#        motor_asim.o motor_dmc2280.o motor_pdx.o \
#        itc4.o itc4driv.o \
#        hm_mrpd.o scan_mrpd.o


OBJ= site_ansto.o anstoutil.o\
    ansto_sctdriveadapter.o\
	ansto_tclmotdriv.o\
    sctemonadapter.o\
    motor_asim.o motor_dmc2280.o\
    lh45.o lh45driv.o \
    lakeshore340.o lakeshore340driv.o \
    west4100.o west4100driv.o \
    nhq200.o \
    orhvps.o \
    ls340.o \
    fsm.o \
    counterdriv.o \
    safetyplc.o \
    anstohttp.o \
    hmcontrol_ansto.o\
    lssmonitor.o \
    beamstopaction.o action.o \
    tclClock.o tclDate.o tclUnixTime.o

all: | config libsics libhardsup SICServer

SICServer:  ../SICSmain.o libansto.a hardsup/libhlib.a ../libsics.a ../matrix/libmatrix.a | config libsics libhardsup
	$(CC) -g -o SICServer  ../SICSmain.o $(SUBLIBS) $(GHTTP_LIBS)

config:
	@bash make_config.sh

xref: all
	python xref.py $(SUBLIBS) $(GHTTP_LIBS)

sct:
	find . -name "*.sct" -exec python instrument/util/gen_sct.py {} \;

#TODO Add targets for other instruments
echidna: all
	make -C instrument/hrpd
koala: all
	make -C instrument/qld
kowari: all
	make -C instrument/rsd
platypus: all
	make -C instrument/reflectometer
quokka: all
	make -C instrument/sans
taipan: all
	make -C instrument/tas
wombat: all
	make -C instrument/hipd
lyrebird: all
	make -C instrument/lyrebird
pelican: all
	make -C instrument/pelican
kookaburra: all
	make -C instrument/kookaburra
dingo: all
	make -C instrument/dingo
bilby: all
emu: all

libansto.a: $(OBJ)
	rm -f libansto.a
	ar cr libansto.a $(OBJ)
	ranlib libansto.a

libhardsup:
	$(MAKE) -C hardsup/ INC_TCL8="$(INC_TCL8)" "CC=${CC}"

libsics:
	$(MAKE) -C .. -f site_ansto/Makefile_SICS INC_TCL8="$(INC_TCL8)" INC_HDF5="$(INC_HDF5)" "CC=${CC}" all

anstohm.o : anstohm.cpp
	$(CXX) -c anstohm.cpp $(CFLAGS) $(CXXFLAGS) -Wno-all

hm_mrpd.o : hm_mrpd.cpp
	$(CXX) -c hm_mrpd.cpp $(CFLAGS) $(CXXFLAGS) -Wno-all

hm_asim.o : hm_asim.cpp
	$(CXX) -c hm_asim.cpp $(CFLAGS) $(CXXFLAGS) -Wno-all

anstoclean:
	$(MAKE) -C hardsup clean
	rm -f SICServer *.a *.o *.d

sicsclean:
	$(MAKE) -C .. -f site_ansto/Makefile_SICS clean

clean: anstoclean sicsclean

MZOBJ=fsm.o logger.o sugar.o pardef.o ease.o strobj.o oxinst.o  \
	  ipsdriv.o ilmdriv.o itcdriv.o ighdriv.o euro2kdriv.o modbus.o

incflags:
	@echo $(subst -isystem,-I,$(INCFLAGS))

cflags:
	@echo ${CFLAGS}

deploy: all
	cd instrument; ./make_sics_tar.tcl

# Use the dependency files we requested gcc to produce for us
DEPS := $(OBJ:.o=.d)
-include $(DEPS)
