ARCH = hpux-sl
include $(CDEV)/examples/Makefile.common

SRCROOT     = $(CDEV)/extensions/cdevGenericServer
CDEVSHOBJ   = /usr/user4/mccops/lib/$(TARGETDIR)
APPNAME     = "CDEV Sample Service"

LIBDIR = $(CDEVLIB)
INCDIR = $(SRCROOT)/include
LIBS = 	-L$(LIBDIR) -lSimpleService \
	-L$(CDEVLIB) -lcdev \
	-ly -ll -L/lib -lm 

LINKLIBS = -L$(LIBDIR) -lSimpleService\
	   -ly -ll -L/lib -lm 

CLASS_INCLUDES = -I./ -I$(INCDIR) -I$(CDEV)/extensions/SimpleService
CXXEXTRA = -pta $(EPICSINCLUDES) $(ACE_INCLUDES) $(CLASS_INCLUDES) 

SRCS = SampleService.cc 
	
OBJS = SampleService.o

%   : %.cc
	@rm -f $@
	@echo "=> $(CXX) $< -o $@"
	@$(CXX) $(CXXFLAGS) $(CXXEXTRA) $< -o $@ -L$(ACE_LIBDIR) $(ACE_LIBS)

ifeq ($(SHOBJ), YES)
OUTPUTLIB = SampleService.so
targets: buildingService $(CDEVSHOBJ)/$(CDEVVERSION)/SampleService.so 

else
OUTPUTLIB = libSampleService.a
targets: buildingService $(CDEV)/lib/$(TARGETDIR)/libSampleService.a 

endif

buildingService:
	@echo "BUILDING LIBRARY \"$(OUTPUTLIB)\"..."
		
objClean :
	@echo ""
	@echo "=> Removing old object files prior to building library"
	@rm -rf $(OBJS)
	
$(CDEVSHOBJ)/$(CDEVVERSION)/SampleService.so : $(OBJS) $(SRCS)
	@rm -f $@
	@echo "=> Building shared object $@"
	@echo "   => Pleasuring templates for $@"
	@echo "int main() {return 0; }" >dummy.cc
	@$(CXX) $(CXXFLAGS) $(CXXEXTRA) dummy.cc $(SRCS) -Wl,+s -ptr./ptSampleService -ptr./ptrepository $(LIBS) 
	@rm -rf a.out dummy.*
	@if test -f ./ptSampleService/*.o; \
	then \
		echo "   => Linking $@ from objects and template objects"; \
		$(CXX) -b -o $@ $(OBJS) ptSampleService/*.o $(LINKLIBS); \
	else \
		echo "   => Linking $@ from objects"; \
		$(CXX) -b -o $@ $(OBJS) $(LINKLIBS); \
	fi
	@echo "   => $@ finished"

$(CDEV)/lib/$(TARGETDIR)/libSampleService.a : hail buildingService objClean $(OBJS)
	@rm -f $@
	@echo "=> Building archive library $@"
	@echo "   => Linking $@ from objects"
	@ar ruv libSampleService.a $(OBJS)
	@chmod a+r libSampleService.a
	@echo "   => $@ finished"

clean:
	@echo "=> Sample Service"
	@echo "   => Removing old objects and binaries"
	@rm -rf $(OBJS) $(CDEVSHOBJ)/$(TARGETDIR)/SampleService.so a.out *.o *~ core ptrepository ptSampleService
	@echo "   => Done"
