#*************************************************************************
# 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. 
#*************************************************************************
#
#	Rules for making things specified in Makefile.Host
#	Some rules for filename-massage are system specific
#	and have "ifdefs" here instead of using definitions
#	from CONFIG.Host.$(ARCH_CLASS)  -  sorry about this,
#	but so far the rules are quite similar on all systems
#	except WIN32 has some specials.
#
#	Maybe there is a way to use indentation to make it
#	easier to read this file?
#
#	Most things may also work if you say e.g.
#		VAR+=ADD
#	even if ADD is not there, but this way "VAR" would
#	be defined in any case, that's why I try to use
#		ifdef ADD
#		VAR+=ADD
#		endif
#
#	-kuk-

#	we are in O.$(ARCH_CLASS), but most sources are one dir above:
#
#	The use of VPATH (no suffix specification) caused everything
#	to break several times.
#	vpath, of course, has the disadvantage that we need explicit rules
#	for scripts or similar os-specific filed which have _no_ suffix...
vpath %.h $(USER_VPATH)   
vpath %.c $(USER_VPATH) ../os/$(ARCH_CLASS) ../os/generic ..
vpath %.cc $(USER_VPATH) ../os/$(ARCH_CLASS) ../os/generic ..
vpath %.cpp $(USER_VPATH) ../os/$(ARCH_CLASS) ../os/generic ..
vpath %.rc $(USER_VPATH) ../os/$(ARCH_CLASS) ../os/generic ..
vpath %.jar $(USER_VPATH) ..

#	check for add-on CFLAGS and CXXFLAGS
#
#	Rules:
#	1) USR_CFLAGS is used
#	2) if there is a special USR_CFLAGS_$(ARCH_CLASS), it's
#	   appended to 1)
#	3) if there is no special defined, but a generic USR_CFLAGS_DEFAULT,
#	   this one is appended
#	4) if you have the special case that your USR_CFLAGS_$(ARCH_CLASS) is
#	   empty but you don't want 3), you have to define it as '-nil-', e.g.:
#		USR_CFLAGS = <defines for all systems>
#		USR_CFLAGS_sun4 = -nil-
#		USR_CFLAGS_DEFAULT = <defines for most systems, not sun4>
#
#	These rules apply to these Makefile-variables:
#	USR_CFLAGS	C flags
#	USR_CXXFLAGS	C++ flags
#	INC		include-files to install
#	LIBSRCS		source files for building library
#	PROD_LIBS	EPICS libs needed by PROD and TESTPROD
#	USR_LIBS	NONEPICS libs needed by PROD and TESTPROD
#	SYS_PROD_LIBS	system libs needed by PROD and TESTPROD
#	PROD		products to build and install
#	SCRIPTS		scripts to install
#
#	Remark:
#	If you define a special INC, e.g. INC_WIN32 = getopt.h,
#	the source (getopt.h) has to be in os/WIN32 (or os/<your ARCH_CLASS>)
#
#	This makes INC_$(ARCH_CLASS) slightly different from OSINC:
#	OSINC = a_file.h
#	means that you have a special  os/$(ARCH_CLASS)/a_file.h
#	for _every_ ARCH_CLASS.
#	If you use INC_$(ARCH_CLASS), you need the special include
#	only for the specified ARCH_CLASS!
#
ifneq ($(strip $(USR_CFLAGS_$(ARCH_CLASS))),)
USR_CFLAGS+=$(subst -nil-,,$(USR_CFLAGS_$(ARCH_CLASS)))
else
ifdef USR_CFLAGS_DEFAULT
USR_CFLAGS+=$(USR_CFLAGS_DEFAULT)
endif
endif

ifneq ($(strip $(USR_CXXFLAGS_$(ARCH_CLASS))),)
USR_CXXFLAGS+=$(subst -nil-,,$(USR_CXXFLAGS_$(ARCH_CLASS)))
else
ifdef USR_CXXFLAGS_DEFAULT
USR_CXXFLAGS+=$(USR_CXXFLAGS_DEFAULT)
endif
endif

ifneq ($(strip $(USR_CPPFLAGS_$(ARCH_CLASS))),)
USR_CPPFLAGS+=$(subst -nil-,,$(USR_CPPFLAGS_$(ARCH_CLASS)))
else
ifdef USR_CPPFLAGS_DEFAULT
USR_CPPFLAGS+=$(USR_CPPFLAGS_DEFAULT)
endif
endif

ifneq ($(strip $(USR_LDFLAGS_$(ARCH_CLASS))),)
USR_LDFLAGS+=$(subst -nil-,,$(USR_LDFLAGS_$(ARCH_CLASS)))
else
ifdef USR_LDFLAGS_DEFAULT
USR_LDFLAGS+=$(USR_LDFLAGS_DEFAULT)
endif
endif

#	check for special includes:
#
ifneq ($(strip $(INC_$(ARCH_CLASS))),)
#	os-specific includes go to the include/os-directory:
OSINC += $(subst -nil-,,$(INC_$(ARCH_CLASS)))
else
ifdef INC_DEFAULT
INC += $(INC_DEFAULT)
endif
endif

#	concat specific library contents (if defined) to LIBCONTENS
#
ifneq ($(strip $(LIBSRCS_$(ARCH_CLASS))),)
LIBSRCS += $(subst -nil-,,$(LIBSRCS_$(ARCH_CLASS)))
else

ifdef LIBSRCS_DEFAULT
LIBSRCS+=$(LIBSRCS_DEFAULT)
endif

endif

#	adjust object names for library contents
#
ifdef LIBSRCS
LIBOBJS=$(addsuffix $(OBJ), $(basename $(LIBSRCS)))
endif

#
#	concat specific library contents (if defined) to PROD_LIBS
#
ifneq ($(strip $(PROD_LIBS_$(ARCH_CLASS))),)
PROD_LIBS += $(subst -nil-,,$(PROD_LIBS_$(ARCH_CLASS)))

else

ifdef PROD_LIBS_DEFAULT
PROD_LIBS += $(PROD_LIBS_DEFAULT)
endif

endif

#
#	concat specific library contents (if defined) to USR_LIBS
#
ifneq ($(strip $(USR_LIBS_$(ARCH_CLASS))),)
USR_LIBS += $(subst -nil-,,$(USR_LIBS_$(ARCH_CLASS)))

else

ifdef USR_LIBS_DEFAULT
USR_LIBS += $(USR_LIBS_DEFAULT)
endif

endif

#
#	concat specific library contents (if defined) to SYS_PROD_LIBS
#
ifneq ($(strip $(SYS_PROD_LIBS_$(ARCH_CLASS))),)
SYS_PROD_LIBS += $(subst -nil-,,$(SYS_PROD_LIBS_$(ARCH_CLASS)))

else

ifdef SYS_PROD_LIBS_DEFAULT
SYS_PROD_LIBS += $(SYS_PROD_LIBS_DEFAULT)
endif

endif

#
#	concat specific products
#
ifneq ($(strip $(PROD_$(ARCH_CLASS))),)
PROD += $(subst -nil-,,$(PROD_$(ARCH_CLASS)))

else

ifdef PROD_DEFAULT
PROD += $(PROD_DEFAULT)
endif

endif

#
#	concat specific scripts
#
ifneq ($(strip $(SCRIPTS_$(ARCH_CLASS))),)
SCRIPTS += $(subst -nil-,,$(SCRIPTS_$(ARCH_CLASS)))

else

ifdef SCRIPTS_DEFAULT
SCRIPTS += $(SCRIPTS_DEFAULT)
endif

endif

#
#	concat specific resource files
#
ifneq ($(strip $(RCS_$(ARCH_CLASS))),)
RCS += $(subst -nil-,,$(RCS_$(ARCH_CLASS)))
else
ifdef RCS_DEFAULT
RCS += $(RCS_DEFAULT)
endif
endif

#	adjust executables
ifdef TESTPROD
TESTPROD := $(addsuffix $(EXE), $(TESTPROD))
endif

#	adjust executables
ifdef PROD
PROD := $(addsuffix $(EXE), $(PROD))
endif

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

#---------------------------------------------------------------
#	----------------------------------------------------
#	create names (lists) for installed things
#	----------------------------------------------------

INCREC +=$(RECTYPES) $(MENUS)

INSTALL_PROD= $(PROD:%= $(INSTALL_BIN)/%)
INSTALL_LIBS= $(LIBNAME:%=$(INSTALL_LIB)/%)
INSTALL_SHRLIBS= $(SHRLIBNAME:%=$(INSTALL_SHRLIB)/%)
INSTALL_DLL_LINK_LIBS=$(DLL_LINK_LIBNAME:%=$(INSTALL_LIB)/%)
INSTALL_TCLLIBS=$(TCLLIBNAME:%=$(INSTALL_TCLLIB)/%)
INSTALL_TCLINDEX=$(TCLINDEX:%=$(INSTALL_TCLLIB)/%)

INSTALL_INC= $(INC:%=$(INSTALL_INCLUDE)/%)
INSTALL_OSINCLUDE=$(INSTALL_INCLUDE)/os/$(ARCH_CLASS)
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)/%)
ifdef TEMPLATES_DIR
INSTALL_TEMPLATES_SUBDIR = $(INSTALL_TEMPLATES)/$(TEMPLATES_DIR)
else
INSTALL_TEMPLATES_SUBDIR = $(INSTALL_TEMPLATES)
endif
INSTALL_TEMPLATE = $(TEMPLATES:%= $(INSTALL_TEMPLATES_SUBDIR)/%)
INSTALL_CONFIGS = $(CONFIGS:%= $(INSTALL_CONFIG)/%)
 
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))

#---------------------------------------------------------------
ifneq ($(strip $(SHARED_LIBRARIES_$(ARCH_CLASS))),)
SHARED_LIBRARIES+=$(subst -nil-,,$(SHARED_LIBRARIES_$(ARCH_CLASS)))
else
ifdef SHARED_LIBRARIES_DEFAULT
SHARED_LIBRARIES_+=$(SHARED_LIBRARIES_DEFAULT)
endif
endif

#---------------------------------------------------------------
# always use c++ linker
ifneq ($(strip $(CPLUSPLUS)),)
LINK.c = $(LINK.cc)
endif # CPLUSPLUS

#---------------------------------------------------------------
# Version number for base shared libraries (and win32 products)
ifeq ($(EPICS_BASE),$(TOP))
SHRLIB_VERSION = $(EPICS_VERSION).$(EPICS_REVISION)
PROD_VERSION = $(EPICS_VERSION).$(EPICS_REVISION)
endif # EPICS_BASE

#---------------------------------------------------------------
#	Libraries
#
#	if there are no objects LIBOBJS to include
#	in this library (may be for e.g. base/src/libCompat
#	on some archs), don't define (and build) any library!
ifdef LIBRARY
ifdef LIBOBJS
LIBTARGETS += $(LIBNAME) $(INSTALL_LIBS)

LIBNAME_RCS=$(foreach lib, $(basename $(LIBRARY)), $(RCS) $($(lib)_RCS) $($(lib)_RCS_$(ARCH_CLASS)))
LIBNAME_RESS=$(addsuffix $(RES), $(basename $(LIBNAME_RCS)))

#  check if shared libraries requested
ifeq ($(strip $(SHARED_LIBRARIES)),YES)

CFLAGS +=  $($(ANSI)_SHRLIB_CFLAGS_YES)
CXXFLAGS +=  $($(CPLUSPLUS)_SHRLIB_CFLAGS_YES)
SHRLIB_LDFLAGS = $($(CPLUSPLUS)_SHRLIB_LDFLAGS_YES)

PROD_VERSION =$(SHRLIB_VERSION)
# always use c++ linker
SHRLIB_LINKER = $(CXX)
LIBTARGETS += $(SHRLIBNAME) $(INSTALL_SHRLIBS) $(INSTALL_DLL_LINK_LIBS)

endif # SHARED_LIBRARIES=YES

endif # LIBOBJS
endif # LIBRARY

#---------------------------------------------------------------
all::	install

build:: inc

build:: $(LIBTARGETS) $(PROD) $(TESTPROD) $(INSTALLS) \
	$(MENUS) $(RECTYPES) $(BPTS)
ifdef DBDEXPAND
build::	$(DBDNAME)
endif

inc:: $(INSTALL_INC) $(INSTALL_OSINC)

rebuild:: clean install

install:: buildInstall 

buildInstall :: build $(TARGETS) \
	$(INSTALL_SCRIPTS) $(INSTALL_PROD) \
	$(INSTALL_MANS) \
	$(INSTALL_DOCS) \
	$(INSTALL_HTMLS) \
	$(INSTALL_TEMPLATE) \
	$(INSTALL_CONFIGS) \
	$(INSTALL_DBS) $(INSTALL_BPTS) \
	$(INSTALL_DBDNAME) $(INSTALL_INCREC) \
	$(INSTALL_TCLLIBS) $(INSTALL_TCLINDEX)

clean::
	@echo "Cleaning"
	@$(RM) *.i *$(OBJ) *.a $(PROD) $(TESTPROD) $(LIBNAME) $(SHRLIBNAME) $(INC) \
	*$(RES) $(TARGETS) *.dbd $(MENUS) $(RECTYPES) $(BPTS)
ifdef DBDEXPAND
	@$(RM) $(DBDNAME)
endif

#---------------------------------------------------------------
#	Products
#

PROD += $(TESTPROD)
ifdef PROD

COND_PROD_SRCS=$(foreach prod, $(basename $(PROD)), $($(prod)_SRCS))
COND_PROD_RCS=$(foreach prod, $(basename $(PROD)), $($(prod)_RCS) $($(prod)_RCS_$(ARCH_CLASS)))

ifdef PRODNAME

ifneq ($(strip $(PRODNAME_SRCS_$(ARCH_CLASS))),)
PRODNAME_SRCS += $(subst -nil-,,$(PRODNAME_SRCS_$(ARCH_CLASS)))
else
ifdef PRODNAME_SRCS_DEFAULT
PRODNAME_SRCS += $(PRODNAME_SRCS_DEFAULT)
endif
endif

ifneq ($(strip $(PRODNAME_OBJS_$(ARCH_CLASS))),)
PRODNAME_OBJS += $(subst -nil-,,$(PRODNAME_OBJS_$(ARCH_CLASS)))
else
ifdef PRODNAME_OBJS_DEFAULT
PRODNAME_OBJS += $(PRODNAME_OBJS_DEFAULT)
endif
endif

ifneq ($(strip $(PRODNAME_RCS_$(ARCH_CLASS))),)
PRODNAME_RCS += $(subst -nil-,,$(PRODNAME_RCS_$(ARCH_CLASS)))
else
ifdef PRODNAME_RCS_DEFAULT
PRODNAME_RCS += $(PRODNAME_RCS_DEFAULT)
endif
endif

$(PRODNAME): $(PROD_DEPLIBS) $(PRODNAME_DEPLIBS)

ifdef PRODNAME_SRCS

ifeq ($(findstring cc,$(suffix $(PRODNAME_SRCS))),cc)
PRODNAME_LINKER = $(LINK.cc)
else
PRODNAME_LINKER = $(LINK.c)
endif

PRODNAME_OBJS+=$(addsuffix $(OBJ), $(basename $(PRODNAME_SRCS)))
PRODNAME_RESS+=$(addsuffix $(RES), $(basename $(PRODNAME_RCS)))

ifdef BORLANDC
$(PRODNAME): $(PRODNAME_OBJS) $(PRODNAME_RESS)
	@$(RM) $@
	$(PRODNAME_LINKER) $(PRODNAME_OBJS) , $@ ,,$(LINKLIBS) $(subst /,\\,$(LDLIBS)),, $(PRODNAME_RESS)

else
$(PRODNAME): $(PRODNAME_OBJS) $(PRODNAME_RESS)
	@$(RM) $@
	$(PRODNAME_LINKER) $(PRODNAME_OBJS) $(PRODNAME_RESS) $(LDLIBS)
	$(MT_EXE_COMMAND)

endif
endif  # ifdef PRODNAME_SRCS

else # PRODNAME not defined

# We have to use the product's true dependancies and
# call make again to determine if product should be rebuilt

ifneq ($(strip $(SRCS) $(COND_PROD_SRCS)),)
PROD_OBJS=$(addsuffix $(OBJ), $(basename $(SRCS) $(COND_PROD_SRCS)))

PROD_RESS=$(addsuffix $(RES), $(basename $(RCS)  $(COND_PROD_RCS)))

PROD_MAKE_COMMAND=$(MAKE) $@\
	PRODNAME="$@"\
	PRODNAME_SRCS="$(SRCS) $($(basename $@)_SRCS)"\
	PRODNAME_SRCS_DEFAULT="$($(basename $@)_SRCS_DEFAULT)"\
	PRODNAME_SRCS_$(ARCH_CLASS)="$($(basename $@)_SRCS_$(ARCH_CLASS))"\
	PRODNAME_RCS="$(RCS) $($(basename $@)_RCS)"\
	PRODNAME_RCS_DEFAULT="$($(basename $@)_RCS_DEFAULT)"\
	PRODNAME_RCS_$(ARCH_CLASS)="$($(basename $@)_RCS_$(ARCH_CLASS))"\
	PRODNAME_LIBS="$($(basename $@)_LIBS)"

$(PROD): $(SRCS) $(PROD_OBJS) $(COND_PROD_SRCS) $(PROD_RESS) $(PROD_DEPLIBS) $(COND_PROD_DEPLIBS)
	@$(PROD_MAKE_COMMAND)

endif
endif  #ifdef PRODNAME

endif  #ifdef PROD

#---------------------------------------------------------------
#     Java classes and packages
#

ifdef JAVA
DIRECTORY_TARGETS += $(INSTALL_JAVA)
ifdef PACKAGE
DIRECTORY_TARGETS += $(INSTALL_JAVA)/$(PACKAGE)
endif
endif

vpath %.class $(INSTALL_JAVA)/$(PACKAGE)

CLASSES += $(subst .java,.class,$(JAVA))
TESTCLASSES += $(subst .java,.class,$(TESTJAVA))
INSTALL_CLASSES =$(CLASSES:%=$(INSTALL_JAVA)/$(PACKAGE)/%)
INSTALL_JAR =$(JAR:%=$(INSTALL_JAVA)/%)

ifeq ($(strip $(JAVADOC)),YES)
DIRECTORY_TARGETS += $(INSTALL_HTML)
INSTALL_JAVADOC = $(CLASSES:%.class=$(INSTALL_HTML)/%.html)
ifdef PACKAGE
DIRECTORY_TARGETS += $(INSTALL_HTML)/$(PACKAGE)
INSTALL_JAVADOC = $(CLASSES:%.class=$(INSTALL_HTML)/$(PACKAGE)/$(PACKAGE)/%.html)
endif
endif

JAR_OPTIONS = cvf
ifdef MANIFEST
JAR_OPTIONS = cvmf
endif
#JAR_DEPFILES +=  $(wildcard $(JAR_INPUT) $(addsuffix /*,$(JAR_INPUT)))
JAR_DEPFILES +=  $(JAR_INPUT)

$(DIRECTORY_TARGETS) :
	$(MKDIR) $@

build:: $(TESTCLASSES) $(JAR)

buildInstall :: $(DIRECTORY_TARGETS) $(INSTALL_CLASSES) $(INSTALL_JAR) $(INSTALL_JAVADOC)

clean::
	@$(RM) $(TESTCLASSES) $(JAR) $(INSTALL_CLASSES) $(INSTALL_JAVADOC)

%.class:%.java
	@echo Creating java class file $@
	$(RM) $@
	$(JAVACCMD) $<

$(INSTALL_JAVA)/$(PACKAGE)/%.class:%.java
	@echo Creating java class file $@
	@$(RM) $@
	$(JAVACCMD) -d $(INSTALL_JAVA) $<

$(INSTALL_JAVADOC):$(JAVA)
	@echo Creating javadoc html files $@
	@$(RM) $@
	$(JAVADOCCMD) -d $(INSTALL_HTML)/$(PACKAGE) $(PACKAGE) 

$(JAR):%.jar:  $(JAR_DEPFILES)
	@echo Creating java jar file $@
	@$(RM) $@
	$(JARCMD)

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

$(PACKAGE)_%.h:$(INSTALL_JAVA)/$(PACKAGE)/%.class
	$(JAVAHCMD) $(PACKAGE).$*

.PRECIOUS: $(INSTALL_CLASSES) $(INSTALL_JAVADOC)

#---------------------------------------------------------------
#---------------------------------------------------------------
#	Generic Rules for 'simple' targets that
#	can be generated from a single source with same basename.
#
#	The usual two rules .c* -> $(OBJ) and then  $(OBJ) -> $(EXE)
#	do not work because the $(OBJ)->$(EXE) rule wouldn't
#	know if the original source was C or C++.
#
#	Hint: The $(subst...) construct removes the .c or .cc
#	      as well as the '../' from the filename and adds $(OBJ):
#	      e.g.  $< = '../abc.c'   ->  'abc.o'
#
#  The order of the following rules is
#  VERY IMPORTANT !!!!

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

ifdef BORLANDC
%$(EXE): %.c
	@$(RM) $@
	$(COMPILE.c)  $<
	$(LINK.c) $(subst ../,,$(basename $<))$(OBJ) , $@ ,,$(LINKLIBS)  $(subst /,\\,$(LDLIBS))

%$(EXE): %.cc
	@$(RM) $@
	$(COMPILE.cc)  $<
	$(LINK.cc) $(subst ../,,$(basename $<))$(OBJ) , $@ ,,$(LINKLIBS)  $(subst /,\\,$(LDLIBS))

%$(EXE): %.cpp
	@$(RM) $@
	$(COMPILE.cc)  $<
	$(LINK.cc) $(subst ../,,$(basename $<))$(OBJ) , $@ ,,$(LINKLIBS)  $(subst /,\\,$(LDLIBS))

%$(EXE): %.C
	@$(RM) $@
	$(COMPILE.cc)  $<
	$(LINK.cc) $(subst ../,,$(basename $<))$(OBJ) , $@ ,,$(LINKLIBS)  $(subst /,\\,$(LDLIBS))

else

%$(EXE): %.c
	@$(RM) $@
	$(COMPILE.c)  $<
	$(LINK.c) $(subst ../,,$(basename $<))$(OBJ) $(LDLIBS)

%$(EXE): %.cc
	@$(RM) $@
	$(COMPILE.cc)  $<
	$(LINK.cc) $(subst ../,,$(basename $<))$(OBJ) $(LDLIBS)

%$(EXE): %.cpp
	@$(RM) $@
	$(COMPILE.cc)  $<
	$(LINK.cc) $(subst ../,,$(basename $<))$(OBJ) $(LDLIBS)

%$(EXE): %.C
	@$(RM) $@
	$(COMPILE.cc)  $<
	$(LINK.cc) $(subst ../,,$(basename $<))$(OBJ) $(LDLIBS)

endif

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

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

%$(OBJ): %.cpp
	@$(RM) $@
	$(COMPILE.cc)  $<

%$(OBJ): %.C
	@$(RM) $@
	$(COMPILE.cc)  $<

# WIN95/NT resource compiler
%$(RES): %.rc
	@$(RM) $@
	$(RCCMD)

#
# 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 rule
%.c: ../%.st
	@echo "preprocessing $*.st"
	@$(RM) $*.i
	$(CPP) $(CPPFLAGS) $< $*.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 $(DEPSCHS)
	@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

$(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 $@ $<

ifneq (,$(wildcard ../base.dbd))
$(DBDNAME): ../base.dbd
endif
endif

ifdef BORLANDC
$(DLL_LINK_LIBNAME) $(SHRLIBNAME): $(LIBOBJS) $(LIBNAME_RESS)
	$(LINK.shrlib) $(LIBOBJS) , $@ ,,$(LINKLIBS) $(subst /,\\,$(DLL_LDLIBS)), $(DLL_DEF_FLAG), $(LIBNAME_RESS)

else
$(DLL_LINK_LIBNAME) $(SHRLIBNAME): $(LIBOBJS) $(LIBNAME_RESS)
	$(LINK.shrlib) $(LIBOBJS) $(DLL_LDLIBS) $(LIBNAME_RESS)
	$(MT_DLL_COMMAND)

endif
ifdef BORLANDC
# rule for lib (archive of object files) creation
$(LIBNAME): $(LIBOBJS)
	$(RM) $@
	$(ARCMD) $(foreach lib,$(LIBOBJS),+$(lib))

else
# rule for lib (archive of object files) creation
$(LIBNAME): $(LIBOBJS)
	$(RM) $@
	$(ARCMD) $(LIBOBJS)
ifdef RANLIB
	$(RANLIB) $@;
endif # RANLIB

endif

$(INSTALL_BIN)/%: ../os/$(ARCH_CLASS)/%
	@echo "Installing os-specific script $@"
	@$(INSTALL_PRODUCT) -d -m 555 $< $(INSTALL_BIN)

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

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

$(INSTALL_LIB)/%.a: %.a
	@echo "Installing library $@"
	@$(INSTALL_LIBRARY) -d -m 644 $< $(INSTALL_LIB)
ifdef RANLIB
	$(RANLIB) $(RANLIBFLAGS) $@
endif # RANLIB

$(INSTALL_LIB)/%.lib: %.lib
	@echo "Installing library $@"
	@$(INSTALL_LIBRARY) -d -m 644 $< $(INSTALL_LIB)

$(INSTALL_SHRLIB)/lib%: lib%
	@echo "Installing library $@"
	@$(INSTALL_LIBRARY) -d -m 555 $< $(INSTALL_SHRLIB)
ifdef SHRLIB_VERSION
	$(RM) $(subst .$(SHRLIB_VERSION),,$@)
	ln -s $< $(subst .$(SHRLIB_VERSION),,$@)
endif # SHRLIB_VERSION

$(INSTALL_TCLLIB)/%: %
	@echo "Installing Tcl library $@"
	@$(INSTALL) -d -m 555 $< $(INSTALL_TCLLIB)
 
$(INSTALL_TCLLIB)/%: ../%
	@echo "Installing Tcl library $@"
	@$(INSTALL) -d -m 555 $< $(INSTALL_TCLLIB)
 
$(INSTALL_CONFIG)/%: %
	@echo "Installing config file $@"
	@$(INSTALL) -d -m 644 $< $(INSTALL_CONFIG)

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

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

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

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

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

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

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

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

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

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

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

.PRECIOUS: %.o %.c

-include DEPENDS

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

#	EOF RULES.Host
