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

include ../linux_def
# TODO Should use PSI ../make_gen instead of make_gen_variables
include make_gen_variables
default: all

# PSI rules and variables
EXTRA=nintf.o
PSI_CLEAN_MATRIX = rm -f ../*.o ../psi/*.o; $(MAKE) -C ../matrix $(MFLAGS) clean
PSI_CFLAGS = -I./ $(INC_HDF5) $(INC_TCL8) -DDO_NOT_SELECT_BEFORE_SEND -DHDF5 -DNXXML\
       -DCYGNUS -DNONINTF -std=gnu99 -g $(DFORTIFY)\
	   -Wall -Wextra -Wno-unused
PSI_SLIBS = matrix/libmatrix.a
PSI_LIBS =  \
       $(LIB_TCL8) $(LIB_HDF5) -lpthread \
       -ldl -lz  -lm  -lc $(LIB_MXML) $(LIB_JSON)

../%.o : ../%.c
	cd ..; $(CC) -c $(PSI_CFLAGS) $*.c -o $*.o

# Some files a generated from templates by tjxp
../%.c: ../%.tc
	cd ..; tcl/tjxp $*.tc $*.c

../matrix/libmatrix.a: 
	make -C ../matrix $(MFLAGS) libmatrix.a

# ANSTO rules and variables
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.4.a /usr/lib/libtcl8.5.a /usr/lib/libtcl.so /usr/lib/libtcl8.4.so /usr/lib/libtcl8.5.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.4/tcl.h /usr/lib/tcl8.5/tcl.h))
ifeq (,$(INC_TCL8))
  $(warning INC_TCL8 not found)
else
  INC_TCL8 := -isystem $(dir $(INC_TCL8))
# $(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
  INC_HDF5 := -I$(dir $(INC_HDF5))
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 $(INCFLAGS) $(DEBUGFLAGS) -std=gnu99

SRC = ./../..
SITENAME = _ansto
SITEDIR  = site_ansto
SITELIB  = libansto.a
SITEHARDSUP = hardsup/libhlib.a
HARDSUPDIR = $(SITEDIR)/hardsup
SUBLIBS = libansto.a hardsup/libhlib.a
VPATH = $(SRC)
	TANGOROOT = /usr/local/lib/
	OMNIORBROOT = /usr/local/lib
COREDIR  = $(SRC)

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

TANGOLIBS = \
			/usr/local/lib/libclient.a \
			/usr/local/lib/liblog4tango.a \
			/usr/local/lib/libtango.a \
			/usr/local/lib/libomniDynamic4.a \
			/usr/local/lib/libomniORB4.a \
			/usr/local/lib/libomnithread.a

			
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\
        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 \
        ../psi/tcpdornier.o \
        anstohttp.o \
        hmcontrol_ansto.o\
        lssmonitor.o \
        beamstopaction.o action.o

all: ../matrix/libmatrix.a $(COREOBJ:%=../%) $(EXTRA:%=../%) libansto.a libhardsup
	$(CC) -g -o SICServer  $(COREOBJ:%=../%) $(EXTRA:%=../%) $(SUBLIBS) $(PSI_SLIBS:%=../%) $(PSI_LIBS) $(GHTTP_LIBS)

#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


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

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

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
	rm -f *.a
	rm -f *.o

clean: anstoclean
	$(PSI_CLEAN_MATRIX)

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
