#*************************************************************************
# Copyright (c) 2002 The University of Chicago, as Operator of Argonne
#     National Laboratory.
# Copyright (c) 2002 The Regents of the University of California, as
#     Operator of Los Alamos National Laboratory.
# EPICS BASE Versions 3.13.7
# and higher are distributed subject to a Software License Agreement found
# in file LICENSE that is included with this distribution. 
#*************************************************************************
# $Revision-Id$

ifneq ($(strip $(LIBOBJS_$(ARCH_CLASS))),)
LIBOBJS+=$(subst -nil-,,$(LIBOBJS_$(ARCH_CLASS)))
else
ifdef LIBOBJS_DEFAULT
LIBOBJS+=$(LIBOBJS_DEFAULT)
endif
endif

INCREC +=$(RECTYPES) $(MENUS)
INSTALL_PROD = $(PROD:%= $(INSTALL_BIN)/%)
INSTALL_LIBS = $(LIBNAME:%= $(INSTALL_BIN)/%)
INSTALL_MUNCHES = $(MUNCHNAME:%= $(INSTALL_BIN)/%)
INSTALL_LIBS_CXX = $(LIBNAME_CXX:%= $(INSTALL_BIN)/%)
INSTALL_INC = $(INC:%= $(INSTALL_INCLUDE)/%)
INSTALL_OSINCLUDE = $(INSTALL_INCLUDE)/os/vxWorks
INSTALL_OSINC = $(OSINC:%= $(INSTALL_OSINCLUDE)/%)
INSTALL_INCREC = $(INCREC:%= $(INSTALL_INCLUDE)/%)
MANLIST = 1 2 3 4 5 6 7 8 9
INSTALL_MANS = $(foreach n, \
	$(MANLIST),$(MAN$(n):%= $(INSTALL_MAN)/man$(n)/%))
INSTALL_DOCS = $(DOCS:%= $(INSTALL_DOC)/%)
INSTALL_HTMLS = $(HTMLS:%= $(INSTALL_HTML)/$(HTMLS_DIR)/%)
INSTALL_SCRIPTS = $(SCRIPTS:%= $(INSTALL_BIN)/%)
INSTALL_INSTALLS = $(INSTALLS:%= $(INSTALL_BIN)/%)

INSTALL_BPTS = $(BPTS:%= $(INSTALL_DBD)/%)
INSTALL_DBS = $(DBDINSTALL:%= $(INSTALL_DBD)/%)\
              $(RECTYPES:%.h= $(INSTALL_DBD)/%.dbd)\
              $(MENUS:%.h= $(INSTALL_DBD)/%.dbd)

INSTALL_DBDNAME = $(DBDNAME:%= $(INSTALL_DBD)/%)

MAN_DIRECTORY_TARGETS = $(foreach n, $(MANLIST),$(INSTALL_MAN)/man$(n))

#-----------------------------------------------------------------
#	if we are not building base add base includes and dbd dirs
#	(convenience for extensions and applications)
ifneq ($(EPICS_BASE),$(TOP))
ifneq ($(EPICS_BASE),$(INSTALL_LOCATION))
EPICS_INCLUDES += -I$(EPICS_BASE_INCLUDE)/os/$(OS_CLASS) -I$(EPICS_BASE_INCLUDE)
EPICS_DBDFLAGS += -I $(EPICS_BASE)/dbd
endif
endif

all:: install 

build:: inc

rebuild:: clean install

pre_build:: 

build:: pre_build $(MENUS) $(RECTYPES) $(BPTS)\
         $(LIBNAME) $(LIBNAME_CXX) $(MUNCHNAME) $(TARGETS) $(PROD)
ifdef DBDEXPAND
build::	$(DBDNAME)
endif

inc:: $(INSTALL_INC) $(INSTALL_OSINC)

install:: buildInstall 

buildInstall :: build $(INSTALL_INCREC)\
         $(INSTALL_LIBS) $(INSTALL_LIBS_CXX) $(INSTALL_MUNCHES) \
	 $(TARGETS) $(INSTALL_PROD) \
	 $(INSTALL_MANS) $(INSTALL_DOCS) \
         $(INSTALL_HTMLS) \
	 $(INSTALL_SCRIPTS) $(INSTALL_INSTALLS)\
	 $(INSTALL_DBS) $(INSTALL_BPTS) \
	 $(INSTALL_DBDNAME) 

ifdef BIN_INSTALLS
buildInstall :: binInstalls
endif

depends:: $(SRCS.c) $(SRCS.cc)
	$(RM) DEPENDS
ifneq ($(strip $(SRCS.c) $(SRCS.cc)),)
	$(DEPENDS_RULE)
endif

clean::
	@echo "Cleaning"
	@$(RM) *.i *.o *.a *.out $(TARGETS) $(PROD) $(LIBNAME) $(INC) \
	$(DBDINSTALL) $(MENUS) $(RECTYPES) $(BPTS) $(DBDNAME) *.c 
ifdef MUNCH
	@$(RM) $(MUNCHNAME) ctdt.c ctdt.o nm.out
endif

#  The order of the following dependencies is
#  VERY IMPORTANT !!!!

%: %.o
	$(RM) $@
	$(LINK.c) $@ $< $(LDLIBS)

#$(PROD): $(OBJS)
#	$(RM) $@
#	$(LINK.c) $@ $(OBJS)

%.o: %.c
	$(RM) $@
	$(COMPILE.c) $<

%.o: ../%.c
	$(RM) $@
	$(COMPILE.c) $<

%.o: %.cc
	$(RM) $@
	$(COMPILE.cc) $<

%.o: ../%.cc
	$(RM) $@
	$(COMPILE.cc) $<

# C++ munching
%.out : %.o
	@ $(RM) ctct.o ctdt.c nm.out
	$(NM) $<  > nm.out
	$(PERL) $(EPICS_BASE_TOOLS)/munch.pl < nm.out > ctdt.c
	$(COMPILE.c) -traditional ctdt.c
	$(LINK.cc) $@ $< ctdt.o

# C++ munching for VxWorks
$(MUNCHNAME):%.munch : %
	@ $(RM) ctct.o ctdt.c nm.out
	$(NM) $<  > nm.out
	$(PERL) $(EPICS_BASE_TOOLS)/munch.pl < nm.out > ctdt.c
	$(COMPILE.c) -traditional ctdt.c
	$(LINK.cc) $@ $(LDFLAGS) $< ctdt.o

#
# rename the y.tab.h file only if we
# are creating it
#
%.h %.c: ../%.y
	$(RM) $*.c y.tab.c
ifeq ($(findstring -d, $(YACCOPT)),-d)
	$(RM) $*.h y.tab.h
endif
	$(YACC) $(YACCOPT) $<
	$(MV) y.tab.c $*.c
ifeq ($(findstring -d, $(YACCOPT)),-d)
	$(MV) y.tab.h $*.h
endif

%.c: ../%.l
	$(RM) lex.yy.c
	$(LEX) $(LEXOPT) $<
	$(RM) $@
	$(MV) lex.yy.c $@

#state notation language rules
%.c: ../%.st
	@echo "preprocessing $*.st"
	@$(RM) $*.i
	$(CPP) $(CPPFLAGS) $(CPPSNCFLAGS) $< > $*.i
	@echo "converting $*.i"
	@$(RM) $@
	$(SNC) $(TARGET_SNCFLAGS) $(SNCFLAGS) $*.i

%.c: ../%.stt
	@echo "converting $<"
	ln -s $< $*.st
	$(SNC) $(TARGET_SNCFLAGS) $(SNCFLAGS) $*.st
	@$(RM) $*.st
 
# Capfast Rules:
%.db: %.edf
	$(E2DB) $(E2DB_SYSFLAGS) $(E2DB_FLAGS) $<

%.db: ../%.edf
	$(E2DB) $(E2DB_SYSFLAGS) $(E2DB_FLAGS) $<

%.edf: ../%.sch
	@if [ ! -f cad.rc -a -r ../cad.rc ] ; then ln -s ../cad.rc ; fi
	$(SCH2EDIF) $(SCH2EDIF_SYSFLAGS) $(SCH2EDIF_FLAGS) $<

# Adl2dl rule
%.dl : ../%.adl
	-$(ADL2DL) $< $@

# Mangen Rule:
%.1:%
	$(MANGEN) -s $<
	$(MV) $(<F).nr $(<F).1

# Mangen Rule:
%.1:../%
	$(MANGEN) -s $<
	$(MV) $(<F).nr $(<F).1

binInstalls:  $(BIN_INSTALLS)
	$(INSTALL) -d -m 555 $^ $(INSTALL_BIN)

$(INSTALL_DBD)/%: % 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(INSTALL_DBD)

$(INSTALL_DBD)/%: ../% 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(INSTALL_DBD)

%Record.h: %Record.dbd
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbToRecordtypeH$(EXE) \
		$(DBDFLAGS)  $<

%Record.h: ../%Record.dbd
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbToRecordtypeH$(EXE) \
		$(DBDFLAGS)  $<

menu%.h: menu%.dbd
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbToMenuH$(EXE) $<

menu%.h: ../menu%.dbd
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbToMenuH$(EXE) $<

bpt%.dbd: bpt%.data
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/makeBpt$(EXE) $<

bpt%.dbd: ../bpt%.data
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/makeBpt$(EXE) $<

# Patch for old applications
ifdef USER_DBDFLAGS
DBDFLAGS = $(USER_DBDFLAGS)
endif

ifdef DBDEXPAND
$(DBDNAME): ../$(DBDEXPAND)
	@echo Expanding dbd
	@$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbExpand$(EXE) $(DBDFLAGS) -o $@ $<
endif

$(LIBNAME): $(LIBOBJS)
	@echo Building library $@
	@$(RM) $@
	$(LINK.c) $@ $(LIBOBJS) $(LDLIBS)

$(LIBNAME_CXX): $(LIBOBJS_CXX)
	@echo Building CXX library $@
	@$(RM) $@
	$(LINK.cc) $@ $(LIBOBJS_CXX) $(LDLIBS)

$(INSTALL_BIN)/%: % 
	@echo "Installing $@"
	@$(INSTALL) -d -m 555 $< $(INSTALL_BIN)

$(INSTALL_BIN)/%: ../% 
	@echo "Installing $@"
	@$(INSTALL) -d -m 555 $< $(INSTALL_BIN)

#
# avoid confusing circular dependency message when
# INSTALL_BIN and EPICS_BASE_BIN are the same (in base)
#
ifneq ($(INSTALL_BIN),$(EPICS_BASE_BIN))
$(INSTALL_BIN)/%: $(EPICS_BASE_BIN)/% 
	@echo "Installing $@ from EPICS base"
	@$(INSTALL) -d -m 555 $< $(INSTALL_BIN)
endif

$(addsuffix /%,$(MAN_DIRECTORY_TARGETS)) : % 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(addsuffix /%,$(MAN_DIRECTORY_TARGETS)) : ../% 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(INSTALL_INCLUDE)/%: % 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(INSTALL_INCLUDE)/%: ../% 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(INSTALL_DOC)/%: % 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(INSTALL_DOC)/%: ../% 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(INSTALL_HTML)/$(HTMLS_DIR)/%: % 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(INSTALL_HTML)/$(HTMLS_DIR)/%: ../% 
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

.PRECIOUS: %.o %.c

.PHONY:: all inc depends build install pre_build clean rebuild buildInstall binInstalls

-include DEPENDS

ifneq (,$(wildcard ../baseLIBOBJS))
$(LIBNAME): ../baseLIBOBJS
endif

ifneq (,$(wildcard ../base.dbd))
$(DBDNAME): ../base.dbd $(RECTYPES:%.h=../%.dbd)
else
$(DBDNAME): $(RECTYPES:%.h=../%.dbd)
endif

#=====> ????  the following line causes a rebuild every time
#=====> $(LIBNAME): ../Makefile.Vx

