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

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.

# TCL 8.4 or 8.5
LIB_TCL8 := $(firstword $(wildcard /usr/lib/libtcl8.5.a /usr/lib/libtcl8.5.so /usr/lib/libtcl8.4.a /usr/lib/libtcl8.4.so /usr/lib/libtcl.so))
LIB_TCL8 := $(firstword $(wildcard /usr/lib/libtcl8.5.so /usr/lib/libtcl8.5.so /usr/lib/libtcl8.4.so /usr/lib/libtcl8.4.so /usr/lib/libtcl.so))
ifeq (,$(LIB_TCL8))
  $(warning LIB_TCL8 not found)
else
  XLIB_TCL8 := -L$(dir $(LIB_TCL8)) -l$(subst lib,,$(basename $(notdir $(LIB_TCL8))))
# $(info LIB_TCL8 = $(LIB_TCL8))
endif
#
INC_TCL8 := $(firstword $(wildcard /usr/include/tcl.h /usr/include/tcl8.5/tcl.h /usr/lib/tcl8.4/tcl.h))
ifeq (,$(INC_TCL8))
  $(warning INC_TCL8 not found)
else
  $(info "INC_TCL8 is $(dir $(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 := $(firstword $(wildcard /usr/lib/libhdf5.a /usr/lib/libhdf5.so /usr/local/lib/libhdf5.a))
ifeq (,$(LIB_HDF5))
  $(warning LIB_HDF5 not found)
endif
#
INC_HDF5 := $(firstword $(wildcard /usr/include/hdf5.h /usr/local/include/hdf5.h))
ifeq (,$(INC_HDF5))
  $(warning INC_HDF5 not found)
else
  $(info "INC_HDF5 is $(dir $(INC_HDF5))")
  ifeq ("$(dir $(INC_HDF5))","/usr/include/")
    $(info "HDF5 in /usr/include")
    INC_HDF5 :=
  else
    INC_HDF5 := -I$(dir $(INC_HDF5))
  endif
endif

# JSON-C
LIB_JSON := $(firstword $(wildcard /usr/lib/libjson.a /usr/lib/libjson.so /usr/local/lib/libjson.a))
ifeq (,$(LIB_JSON))
  $(warning LIB_JSON not found)
endif

# MXML
LIB_MXML := $(firstword $(wildcard /usr/lib/libmxml.a /usr/local/lib/libmxml.a))
ifeq (,$(LIB_MXML))
  $(warning LIB_MXML not found)
endif

# GHTTP
LIB_HTTP := $(firstword $(wildcard /usr/lib/libghttp.a /usr/local/lib/libghttp.a))
ifeq (,$(LIB_HTTP))
  $(warning LIB_HTTP not found)
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 libansto.a 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)
