#*************************************************************************
# 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. 
#*************************************************************************
# $Id$

INCREC +=$(RECTYPES) $(MENUS)
INSTALL_PROD = $(PROD:%= $(INSTALL_BIN)/%)
INSTALL_LIBS = $(LIBNAME:%= $(INSTALL_LIB)/%)
INSTALL_TCLLIBS=$(TCLLIBNAME:%=$(INSTALL_TCLLIB)/%)
INSTALL_TCLINDEX=$(TCLINDEX:%=$(INSTALL_TCLLIB)/%)
INSTALL_INC = $(INC:%= $(INSTALL_INCLUDE)/%)
INSTALL_OSINCLUDE = $(INSTALL_INCLUDE)/os/$(T_A)
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),$(INSTALL_MAN)/man$(n) $(MAN$(n):%= $(INSTALL_MAN)/man$(n)/%))
INSTALL_DOCS = $(DOCS:%= $(INSTALL_DOC)/%)
INSTALL_HTMLS = $(HTMLS:%= $(INSTALL_HTML)/$(HTMLS_DIR)/%)
INSTALL_SCRIPTS = $(SCRIPTS:%= $(INSTALL_BIN)/%)
INSTALL_TEMPLATES_SUBDIR = $(INSTALL_TEMPLATES)/$(TEMPLATES_DIR)
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))

DIRECTORY_TARGETS = $(INSTALL_INCLUDE) $(INSTALL_INCLUDE)/os \
	$(INSTALL_OSINCLUDE) $(INSTALL_DOC) \
	$(INSTALL_TEMPLATES)/$(TEMPLATES_DIR) $(INSTALL_TEMPLATES) \
	$(INSTALL_LOCATION_BIN) $(INSTALL_BIN) $(INSTALL_LIB) \
	$(INSTALL_LOCATION_LIB) $(INSTALL_CONFIG) \
	$(MAN_DIRECTORY_TARGETS) $(INSTALL_MAN)

all:: install

pre_build::

rebuild:: clean install

build:: pre_build $(LIBNAME) $(TARGETS) $(PROD)\
	 $(MENUS) $(RECTYPES) $(BPTS) $(DBDNAME)

inc:: $(INSTALL_INCLUDE) $(INSTALL_INC) $(INSTALL_INCLUDE)/os \
	$(INSTALL_OSINCLUDE) $(INSTALL_OSINC)

install:: inc buildInstall 

buildInstall :: build \
	$(INSTALL_LOCATION_LIB) $(INSTALL_LIB) $(INSTALL_LIBS) \
	$(TARGETS) \
	$(INSTALL_LOCATION_BIN) $(INSTALL_BIN) \
		$(INSTALL_SCRIPTS) $(INSTALL_PROD) \
	$(INSTALL_MAN) $(INSTALL_MANS) $(INSTALL_HTMLS) \
	$(INSTALL_DOC) $(INSTALL_DOCS) $(INSTALL_TEMPLATES) \
	$(INSTALL_TEMPLATES)/$(TEMPLATES_DIR) $(INSTALL_TEMPLATE) \
	$(INSTALL_CONFIG) $(INSTALL_CONFIGS) \
	$(INSTALL_DBD) $(INSTALL_DBS) $(INSTALL_BPTS) \
	$(INSTALL_DBDNAME) $(INSTALL_INCREC) \
	$(INSTALL_TCLLIBS) $(INSTALL_TCLINDEX)

depends:: $(SRCS.c) $(SRCS.cc)
ifdef SRCS
	echo you have a SRCS in your Makefile
	exit 2
endif
ifdef SRCS.c
	$(DEPENDS_RULE.c)
endif
ifdef SRCS.cc
	$(DEPENDS_RULE.cc)
endif

clean::
	@echo "Cleaning"
	@$(RM) *.i *.o *.a $(TARGETS) $(PROD) $(LIBNAME) $(INC)

$(DIRECTORY_TARGETS) :
	@$(MKDIR) $@

$(TARGETS) $(PROD): $(DEPLIBS)

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

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

#$(PROD): $(OBJS)
#	$(RM) $@
#	$(LINK.c) -o $@ ${OBJS} $(LDLIBS)

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

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

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

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

%.c: ../%.y
	$(RM) y.tab.c y.tab.h
	$(YACC) $(YACCOPT) $<
	@if [ -f y.tab.c ]; \
		then \
			echo "$(MV) y.tab.c $*.c"; \
			$(MV) y.tab.c $*.c; \
	fi
	@if [ -f y.tab.h ]; \
		then \
			echo "$(MV) y.tab.h $*.h"; \
			$(MV) y.tab.h $*.h; \
	fi

%.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 $<"
	@$(RM) $@
	$(SNC) $(TARGET_SNCFLAGS) $(SNCFLAGS) $<

# 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 \
	        $(DBDFLAGS)  $<
 
%Record.h: ../%Record.dbd
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbToRecordtypeH \
	        $(DBDFLAGS)  $<
 
menu%.h: menu%.dbd
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbToMenuH $<
 
menu%.h: ../menu%.dbd
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/dbToMenuH $<
 
bpt%.dbd: bpt%.data
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/makeBpt $<
 
bpt%.dbd: ../bpt%.data
	$(RM) $@
	$(EPICS_BASE_HOST_BIN)/makeBpt $<
 
# Patch for old applications
ifdef USER_DBDFLAGS
DBDFLAGS = $(USER_DBDFLAGS)
endif

ifdef DBDEXPAND
$(DBDNAME): ../$(DBDEXPAND)
	@echo expanding dbd
	@$(RM) $@ $@-new
	$(EPICS_BASE_HOST_BIN)/dbExpand $(DBDFLAGS) $< > $@-new
	@$(MV) $@-new $@

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

$(LIBNAME): $(LIBOBJS)
	@echo Building library $@
	$(RM) $@
	$(ARCMD) $(LIBOBJS)
	@if [ ! -z "$(RANLIB)" ] ; then\
		echo $(RANLIB) $@; \
		$(RANLIB) $@; \
	fi

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

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

$(INSTALL_LIB)/%.a: %.a 
	@echo "Installing library $@"
	@$(INSTALL) -d -m 644 $< $(INSTALL_LIB)
	@if [ ! -z "$(RANLIB)" ] ; then\
		$(RANLIB) $(RANLIBFLAGS) $@; \
	fi

$(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_TCLLIB)/$(TCLINDEX): $(INSTALL_TCLLIBS)
	@echo "Updating $@"
	@echo eval auto_mkindex $(INSTALL_TCLLIB) "$(TCLLIBNAME)" | tclsh
 
$(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 template $@"
	@$(INSTALL) -d -m 644 $< $(@D) 

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

.PRECIOUS: %.o %.c

.PHONY:: all inc depends build install pre-build clean rebuild  

-include .DEPENDS

