#===============================================================================
#
#    makevars
#
#    This file allows users to control some aspects of the build process.
#
#===============================================================================
#####COPYRIGHTBEGIN####
#
# -------------------------------------------
# The contents of this file are subject to the Cygnus eCos Public License
# Version 1.0 (the "License"); you may not use this file except in
# compliance with the License.  You may obtain a copy of the License at
# http://sourceware.cygnus.com/ecos
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the
# License for the specific language governing rights and limitations under
# the License.
# 
# The Original Code is eCos - Embedded Cygnus Operating System, released
# September 30, 1998.
# 
# The Initial Developer of the Original Code is Cygnus.  Portions created
# by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions.  All Rights Reserved.
# -------------------------------------------
#
#####COPYRIGHTEND####
#===============================================================================

#
# Some of the commands used to do builds should generally be specified by
# pkgconf. This is because the commands are target-specific, and it is
# pkgconf that knows about the target architecture and the information held
# in the targets file. Never the less it is desirable to allow users to
# redefine the commands.
#
# First, make automatically defines a number of variables unless it is
# given the -r or --no-builtin-rules arguments. The built-in values are
# unlikely to be correct when doing cross compilation. The following
# checks eliminate any such built-in definitions.
ifeq ($(origin CC),default)
CC :=
endif
ifeq ($(origin CXX),default)
CXX :=
endif
ifeq ($(origin AR),default)
AR :=
endif
ifeq ($(origin LD),default)
LD :=
endif
ifeq ($(origin CP),default)
CP :=
endif
ifeq ($(origin RM),default)
RM :=
endif
ifeq ($(origin TOUCH),default)
TOUCH :=
endif
ifeq ($(origin CAT),default)
CAT :=
endif
ifeq ($(origin TAIL),default)
TAIL :=
endif

#
# At this stage all of the variables identifying commands will be empty
# or undefined strings, or they will have been set on the make command
# line or in environment variables.
#
# For each command that is not currently defined, a default value
# is defined here. Users may make a persistent change to the commands by
# commenting out the default value and filling in an alternative one.
#
ifeq ($(CC),)
CC	:= $(PKGCONF_CC)
#CC	:=
endif
ifeq ($(CXX),)
CXX     := $(PKGCONF_CXX)
#CXX	:=
endif
ifeq ($(AR),)
AR	:= $(PKGCONF_AR)
#AR	:=
endif
ifeq ($(LD),)
LD	:= $(PKGCONF_LD)
#LD	:=
endif
ifeq ($(OBJCOPY),)
OBJCOPY	:= $(PKGCONF_OBJCOPY)
#OBJCOPY :=
endif
ifeq ($(CP),)
CP	:= install -m 0644
#CP	:=
endif
ifeq ($(RM),)
RM	:= rm -f
#RM	:=
endif
ifeq ($(TOUCH),)
TOUCH	:= touch
#TOUCH	:=
endif
ifeq ($(CAT),)
CAT	:= cat
#CAT	:=
endif
ifeq ($(TAIL),)
TAIL	:= tail
#tail	:=
endif

#
# Next deal with the various compiler flags. None of the flags should
# conflict with any built-in variables, so it is sufficient to check
# that a given flag has not been defined on the make command line or via
# an environment variable.
#
# As with the command variables, users can disable the default
# settings by commenting out one line and filling in the next line.
#
# There are five categories of flags:
# 1) architecture-related, i.e. -mthis -mthat -mthe-other
# 2) error and warning related, e.g. -Wall -Werror
# 3) language-related, e.g. -fno-rtti for C++ code
# 4) debug and code generation, e.g. -g -O2
# 5) everything else, e.g. -DCYG_RELEASE_DEBUG
#
# For each category there are four variables. The first one, e.g.
# ARCHFLAGS, is used for all build operations. The second, e.g.
# CARCHFLAGS, is used only when compiling to C code (in addition to
# the generic ARCHFLAGS). The third, e.g. CXXARCHFLAGS is used when
# compiling C++ code and the last, LDARCHFLAGS, is used when linking
# executables (typically test programs).
#

ifeq ($(ARCHFLAGS),)
ARCHFLAGS 	:= $(PKGCONF_ARCHFLAGS)
#ARCHFLAGS	:=
endif
ifeq ($(CARCHFLAGS),)
CARCHFLAGS 	:= $(PKGCONF_CARCHFLAGS)
#CARCHFLAGS	:=
endif
ifeq ($(CXXARCHFLAGS),)
CXXARCHFLAGS 	:= $(PKGCONF_CXXARCHFLAGS)
#CXXARCHFLAGS	:=
endif
ifeq ($(LDARCHFLAGS),)
LDARCHFLAGS 	:= $(PKGCONF_LDARCHFLAGS)
#LDARCHFLAGS	:=
endif
ifeq ($(ERRFLAGS),)
ERRFLAGS 	:= $(PKGCONF_ERRFLAGS)
#ERRFLAGS	:=
endif
ifeq ($(CERRFLAGS),)
CERRFLAGS 	:= $(PKGCONF_CERRFLAGS)
#CERRFLAGS	:=
endif
ifeq ($(CXXERRFLAGS),)
CXXERRFLAGS 	:= $(PKGCONF_CXXERRFLAGS)
#CXXERRFLAGS	:=
endif
ifeq ($(LDERRFLAGS),)
LDERRFLAGS 	:= $(PKGCONF_LDERRFLAGS)
#LDERRFLAGS	:=
endif
ifeq ($(LANGFLAGS),)
LANGFLAGS 	:= $(PKGCONF_LANGFLAGS)
#LANGFLAGS	:=
endif
ifeq ($(CLANGFLAGS),)
CLANGFLAGS 	:= $(PKGCONF_CLANGFLAGS)
#CLANGFLAGS	:=
endif
ifeq ($(CXXLANGFLAGS),)
CXXLANGFLAGS 	:= $(PKGCONF_CXXLANGFLAGS)
#CXXLANGFLAGS	:=
endif
ifeq ($(LDLANGFLAGS),)
LDLANGFLAGS 	:= $(PKGCONF_LDLANGFLAGS)
#LDLANGFLAGS	:=
endif
ifeq ($(DBGFLAGS),)
DBGFLAGS 	:= $(PKGCONF_DBGFLAGS)
#DBGFLAGS	:=
endif
ifeq ($(CDBGFLAGS),)
CDBGFLAGS 	:= $(PKGCONF_CDBGFLAGS)
#CDBGFLAGS	:=
endif
ifeq ($(CXXDBGFLAGS),)
CXXDBGFLAGS 	:= $(PKGCONF_CXXDBGFLAGS)
#CXXDBGFLAGS	:=
endif
ifeq ($(LDDBGFLAGS),)
LDDBGFLAGS 	:= $(PKGCONF_LDDBGFLAGS)
#LDDBGFLAGS	:=
endif
ifeq ($(EXTRAFLAGS),)
EXTRAFLAGS 	:= $(PKGCONF_EXTRAFLAGS)
#EXTRAFLAGS	:=
endif
ifeq ($(CEXTRAFLAGS),)
CEXTRAFLAGS 	:= $(PKGCONF_CEXTRAFLAGS)
#CEXTRAFLAGS	:=
endif
ifeq ($(CXXEXTRAFLAGS),)
CXXEXTRAFLAGS 	:= $(PKGCONF_CXXEXTRAFLAGS)
#CXXEXTRAFLAGS	:=
endif
ifeq ($(LDEXTRAFLAGS),)
LDEXTRAFLAGS 	:= $(PKGCONF_LDEXTRAFLAGS)
#LDEXTRAFLAGS	:=
endif

#
# Now combine all the above flags, so that the build rules are actually legible.
#
CFLAGS 		:= $(ARCHFLAGS)  $(CARCHFLAGS)
CFLAGS 		:= $(CFLAGS) $(ERRFLAGS)   $(CERRFLAGS)
CFLAGS 		:= $(CFLAGS) $(LANGFLAGS)  $(CLANGFLAGS)
CFLAGS 		:= $(CFLAGS) $(DBGFLAGS)   $(CDBGFLAGS)
CFLAGS 		:= $(CFLAGS) $(EXTRAFLAGS) $(CEXTRAFLAGS)
CFLAGS		:= $(strip $(CFLAGS))

CXXFLAGS 	:= $(ARCHFLAGS)  $(CXXARCHFLAGS)
CXXFLAGS 	:= $(CXXFLAGS) $(ERRFLAGS)   $(CXXERRFLAGS)
CXXFLAGS 	:= $(CXXFLAGS) $(LANGFLAGS)  $(CXXLANGFLAGS)
CXXFLAGS 	:= $(CXXFLAGS) $(DBGFLAGS)   $(CXXDBGFLAGS)
CXXFLAGS 	:= $(CXXFLAGS) $(EXTRAFLAGS) $(CXXEXTRAFLAGS)
CXXFLAGS	:= $(strip $(CXXFLAGS))

LDFLAGS 	:= $(ARCHFLAGS)  $(LDARCHFLAGS)
LDFLAGS 	:= $(LDFLAGS) $(ERRFLAGS)   $(LDERRFLAGS)
LDFLAGS 	:= $(LDFLAGS) $(LANGFLAGS)  $(LDLANGFLAGS)
LDFLAGS 	:= $(LDFLAGS) $(DBGFLAGS)   $(LDDBGFLAGS)
LDFLAGS 	:= $(LDFLAGS) $(EXTRAFLAGS) $(LDEXTRAFLAGS)
LDFLAGS		:= $(strip $(LDFLAGS))

#
# Clear the default INCLUDE_PATH. This will be set up in
# makrules.src et al. on the basis of the VPATH environment
# variable.
INCLUDE_PATH :=

#
# Finally, it is possible for users to change the rules that actually
# perform most of the build operations.

$(PACKAGE)_%.o : %.c
	$(CC)  -c $(INCLUDE_PATH) $(CFLAGS) $($(notdir $<)-CFLAGS)  -Wp,-MD,$(@:.o=.tmp) -o $@ $< 
	@echo > $(@:.o=.d)
	@echo $@ ':' $< '\' >> $(@:.o=.d)
	@$(TAIL) +2 $(@:.o=.tmp) >> $(@:.o=.d)
	@$(RM) $(@:.o=.tmp)

$(PACKAGE)_%.o : %.cxx
	$(CXX) -c $(INCLUDE_PATH) $(CXXFLAGS) $($(notdir $<)-CXXFLAGS) -Wp,-MD,$(@:.o=.tmp) -o $@ $< 
	@echo > $(@:.o=.d)
	@echo $@ ':' $< '\' >> $(@:.o=.d)
	@$(TAIL) +2 $(@:.o=.tmp) >> $(@:.o=.d)
	@$(RM) $(@:.o=.tmp)

$(PACKAGE)_%.o : %.S
	$(CXX)  -c $(INCLUDE_PATH) $(CFLAGS) -Wp,-MD,$(@:.o=.tmp) -o $@ $< 
	@echo > $(@:.o=.d)
	@echo $@ ':' $< '\' >> $(@:.o=.d)
	@$(TAIL) +2 $(@:.o=.tmp) >> $(@:.o=.d)
	@$(RM) $(@:.o=.tmp)

$(PREFIX)/lib/libextras.a(%) : %
	$(AR) rcs $(PREFIX)/lib/libextras.a $<
