#
# File:
#    Makefile
#
# Description:
#    Makefile for the CODA 3.0 primary and secondary readout lists 
#    running on an Intel-based controller running Linux
#
#    6/5/2014  D.L. Modified for the mcROL that does not use VME hardware.
#
#  To build for 32-bit systems, do the following:
#
#    make ARCH=i686
#
#  To build for 64-bit systems, do the following:
#
#    make ARCH=x86_64
#
# Uncomment DEBUG line for debugging info ( -g and -Wall )
DEBUG=1
#

ifndef ARCH
  ARCH = i686
endif

ifeq ($(ARCH),i686)
  CFLAGS += -m32
endif

# Plug in your primary readout lists here..
VMEROL		=  mcROL.so
# Add shared library dependencies here.  (jvme, ti, td, ts, sd, ctp are already included)
#ROLLIBS		=  -lcmsgxx -lcmsg -lcmsgRegex -lcodaObject -lcodaChannels -let -levioxx -levio -lexpat
ROLLIBS		=  -lcmsgxx -lcmsg -lcmsgRegex -let -levioxx -levio -lexpat

LINUXVME_LIB	?= ../lib
LINUXVME_INC	?= ../include
LINUXVME_BIN	?= ../bin

# DEFs for compiling primary readout lists
CC					= gcc
AR             = ar
RANLIB         = ranlib
ifdef DEBUG
CFLAGS			+= -Wall -g
else
CFLAGS			+= -O3
endif
CFLAGS			+= -w -I. -I${LINUXVME_INC} -I/usr/include -I${CODA}/common/include\
			  			-L. -L${LINUXVME_LIB} -L${CODA}/Linux-$(ARCH)/lib -DJLAB
#CFLAGS			+= -lrt -lpthread -ljvme -lti -ltd -lts -lsd -lctp $(ROLLIBS)
CFLAGS			+= -lrt -lpthread $(ROLLIBS)
CFLAGS			+= -DLINUX -DDAYTIME=\""`date`"\"
CFLAGS                  += -I../rol_1

ifdef DEBUG
CODA_CFLAGS		+= -Wall -g
endif
CRLFILES		= $(wildcard *.crl)
CFILES		= $(CRLFILES:.crl=.c)
SOBJS			= $(CRLFILES:.crl=.so)

ifeq ($(ARCH),x86_64)
	libdir:=/usr/lib64/gef
else
	libdir:=/usr/lib/gef
endif


all: $(VMEROL) $(SOBJS) 


%.c: %.crl Makefile
	@echo
	@echo "Converting $< to $@"
	@${CCRL} $<

%.so: %.c ../rol_1/conf_output.h ../rol_1/conf_output.c ../rol_1/conf_utils.h Makefile
	@echo
	@echo "Building $@ from $<"
	@echo
	$(CC) -fpic -shared  $(CFLAGS) -DINIT_NAME=$(@:.so=__init) -o $@ $<

clean distclean:
	@rm -f  $(VMEROL) $(SOBJS) $(CFILES) *~ 

.PHONY: all