# Copyright (c) 1991-2000 Silicon Graphics, Inc.
# This document is licensed under the SGI Free Software B license.
# For details, see
#
# http://oss.sgi.com/projects/FreeB
#
# $Date$ $Revision$
# $Header: //depot/main/tools/include/commondefs#43 $

COMMONRULES=$(ROOT)/usr/include/make/commonrules

# what OS are we running on
OS=$(shell uname)
ifeq ($(subst 64,,$(OS)), IRIX)
OS=IRIX
endif
ifeq (CYGWIN,$(findstring CYGWIN,$(OS)))
OS=Linux
endif

# various source-level directories
TOOLSSRC = $(TOPDIR)/tools
GFXINCLUDESRC = $(TOPDIR)/gfx/include
OGLLIBSRC = $(TOPDIR)/gfx/lib/opengl
GLXLIBSRC = $(TOPDIR)/glx

# SGI Free Software B license
OSSCOPYRIGHT = $(TOPDIR)/doc/release/OSSCOPYRIGHT

# shell-related defs
SHELL	= /bin/sh
AWK	= awk
ifeq ($(OS),IRIX)
ECHO	= /bin/echo
endif
ifeq ($(OS),Linux)
ECHO	= /bin/echo -e
endif
RM	= rm -f
MV	= mv
PERL	= perl
SED	= sed
UNIQ	= uniq

# M4 options - run GNU M4 in compatibility mode
# rdist options - Red Hat 5.2 must have daemon location specified
ifeq ($(OS),IRIX)
M4OPTS	=
endif
ifeq ($(OS),Linux)
M4OPTS	= -G
RDIST_OPTS = -p /usr/sbin/rdistd
endif

# override default gmake CXX with what we have
ifeq ($(OS),IRIX)
CXX	= CC
endif

# what language are we using?  Default to ansi with extended defines
ifeq ($(OS),IRIX)
CVERSION = -xansi
endif
ifeq ($(OS),Linux)
CVERSION= -ansi -include $(ROOT)/usr/include/make/linuxdefs
endif

# WARN is the warning option
ifeq ($(OS),IRIX)
#
# Turn of explicitly some warnings on IRIX.  Specifically:
#	1155:	unrecognized #pragma
#	1174:	parameter/variable "???" was declared but never referenced
#	1201:	trailing comma is nonstandard (last element in struct assnment)
#	1209:	controlling expression is constant
#	1506:	implicit conversion from "???" to "???": rounding, sign
#		extension, or loss of accuracy may result
#	3170:	"???" not marked as intrinsic because it is not declared
#		as a function
#
WARN = -fullwarn \
	-woff 1155,1174,1506,1201,1209,3170
endif
ifeq ($(OS),Linux)
WARN = -Wimplicit \
	-Wreturn-type \
	-Wswitch \
	-Wcomment \
	-Wformat \
	-Wchar-subscripts \
	-Wparentheses \
	-Wpointer-arith \
	-Wcast-qual \
	-Wmissing-prototypes \
	-Wmissing-declarations \
	$(NULL)
endif

# default optimization
ifeq ($(subst y,1,$(GLDEBUG)),1)
OPTIMIZER=-g
else
OPTIMIZER=-O2
ifeq ($(OS),Linux)
WARN += -Wuninitialized
endif
endif

# how to make dependencies
ifeq ($(OS),IRIX)
MKDEPOPT = -MDupdate Makedepend
endif
ifeq ($(OS),Linux)
MKDEPOPT = -MD
endif

override GCOPTS = $(OPTIMIZER) $(MKDEPOPT) $(WARN)
override GCXXOPTS = $(OPTIMIZER) $(MKDEPOPT) $(WARN)

# where is the default include directory?
#  (we don't grab it off $ROOT, to avoid populating $ROOT with system
#  includes.  Yes, I know...  You don't like the idea.
INCLDIR = /usr/include

# where to install include files?
INCLUDE_LOCATION = $(ROOT)/usr/include

# we would like to use nostdinc on Linux, but unfortunately, it also
# removes include directories with no well-known fixed path (ie. the
# gcc install directory).
ifeq ($(OS),IRIX)
override GCINCS = -nostdinc -I$(ROOT)$(INCLDIR) -I$(INCLDIR)
override GCXXINCS = -nostdinc -I$(ROOT)$(INCLDIR) -I$(INCLDIR)
endif
ifeq ($(OS),Linux)
override GCINCS = -I$(ROOT)$(INCLDIR)
override GCXXINCS = -I$(ROOT)$(INCLDIR) -I$(INCLDIR)
endif

ifeq ($(GLDEBUG),1)
override GCDEFS = -DDEBUG -UNDEBUG
override GCXXDEFS = -DDEBUG -UNDEBUG
else
override GCDEFS = -UDEBUG -DNDEBUG
override GCXXDEFS = -UDEBUG -DNDEBUG
endif

#
# Cc flags, composed of:
#	variable (V*) (set on the command line)
#	local (L*) (set in the makefile)
#	global (G*) (defined in this file, not overrideable)
# in that order.  This is done so that the locally defined includes
# are searched before the globally defined ones.
#
CFLAGS	= $(CVERSION) $(VCFLAGS) $(LCFLAGS) $(GCFLAGS)

#
# Each of these three components is divided into defines (-D's and -U's),
# includes (-I's), and other options.  By segregating the different
# classes of flag to cc, the defines (CDEFS) and includes (CINCS) can be
# easily given to other programs, e.g., lint.
#
# Notes:
#   - The local assignments should be to LCOPTS, LCDEFS, and LCINCS, not to
#     LCFLAGS, although CFLAGS will be correctly set if this is done.
#   - If a program cannot be optimized, it should override the setting of
#     OPTIMIZER with a line such as "OPTIMIZER=" in its make file.
#   - If a program cannot be compiled with ANSI C, its makefile
#     should set CVERSION=-cckr
#
VCFLAGS = $(VCDEFS) $(VCINCS) $(VCOPTS)
LCFLAGS = $(LCDEFS) $(LCINCS) $(LCOPTS)
GCFLAGS = $(GCDEFS) $(GCINCS) $(GCOPTS)

COPTS	= $(VCOPTS) $(LCOPTS) $(GCOPTS)
CDEFS	= $(VCDEFS) $(LCDEFS) $(GCDEFS)
CINCS	= $(VCINCS) $(LCINCS) $(GCINCS)

#
# CXX flags are decomposed using the same hierarchy as C flags.
#
CXXFLAGS  = $(CVERSION) $(VCXXFLAGS) $(LCXXFLAGS) $(GCXXFLAGS)

VCXXFLAGS = $(VCXXDEFS) $(VCXXINCS) $(VCXXOPTS)
LCXXFLAGS = $(LCXXDEFS) $(LCXXINCS) $(LCXXOPTS)
GCXXFLAGS = $(GCXXDEFS) $(GCXXINCS) $(GCXXOPTS)

CXXOPTS   = $(VCXXOPTS) $(LCXXOPTS) $(GCXXOPTS)
CXXDEFS   = $(VCXXDEFS) $(LCXXDEFS) $(GCXXDEFS)
CXXINCS   = $(VCXXINCS) $(LCXXINCS) $(GCXXINCS)

#
# Do something for the linker
#
#
# Library paths (-L's) are part of LDOPTS.
# Libraries (-l's) are part of LDLIBS.
# This is done so that there is a guarantee that all library paths are
# included before the libraries.
#
override GLDOPTS=
override GLDDSOOPTS=
ifeq ($(OS),IRIX)
override GLDOPTS += -L$(ROOT)/usr/lib32
override GLDDSOOPTS += -L$(ROOT)/usr/lib32
endif
ifeq ($(OS),Linux)
override GLDOPTS += -L$(ROOT)/usr/lib -L$(XTOPDIR)/xc/exports/lib
override GLDDSOOPTS += -L$(ROOT)/usr/lib -L$(XTOPDIR)/xc/exports/lib
endif

# options used for DSO's.
ifeq ($(OS),IRIX)
DSONAMEOPT = -soname $(DSONAME)
ifneq ($(DSOEXPORTS),)
DSOEXPORTSOPT = -exports_file $(DSOEXPORTS)
endif
override GLDDSOOPTS += -elf -shared -all $(DSOEXPORTSOPT) $(DSONAMEOPT)
endif

ifeq ($(OS),Linux)
DSONAMEOPT = -soname=$(DSONAME)
ifneq ($(DSOEXPORTS),)
DSOEXPORTSOPT = $(addprefix -u ,$(shell cat $(DSOEXPORTS)))
else
DSOEXPORTSOPT = --whole-archive
endif
override GLDDSOOPTS += -shared $(DSOEXPORTSOPT) $(DSONAMEOPT)
endif

LDDSOOPTS  = $(VLDDSOOPTS) $(LLDDSOOPTS) $(GLDDSOOPTS)
LDDSOLIBS  = $(VLDDSOLIBS) $(LLDDSOLIBS) $(GLDDSOLIBS)
LDDSOFLAGS = $(LDDSOOPTS) $(LDDSOLIBS) $(ENDIAN)

LDOPTS	= $(VLDOPTS) $(LLDOPTS) $(GLDOPTS)
LDLIBS	= $(VLDLIBS) $(LLDLIBS) $(GLDLIBS)
LDFLAGS = $(LDOPTS) $(LDLIBS) $(ENDIAN)

# where to install libraries
ifeq ($(OS),IRIX)
OBJECT_STYLE = N32
LIB_LOCATION = $(ROOT)/usr/lib32
endif
ifeq ($(OS),Linux)
LIB_LOCATION = $(ROOT)/usr/lib
endif

#
# as flags are just like cc flags.
# By default, ASTYLE is the same as CSTYLE, but some may need to make
# the asm more aggressive than 'C' and can set ASTYLE. Users of 'make'
# always get ASTYLE == CSTYLE
#
ASFLAGS = $(VASFLAGS) $(LASFLAGS) $(GASFLAGS)

VASFLAGS = $(VASDEFS) $(VASINCS) $(VASOPTS)
LASFLAGS = $(LASDEFS) $(LASINCS) $(LASOPTS)
GASFLAGS = $(GASDEFS) $(GASINCS) $(GASOPTS)

ASOPTS	= $(VASOPTS) $(LASOPTS) $(GASOPTS)
ASDEFS	= $(VASDEFS) $(LASDEFS) $(GASDEFS)
ASINCS	= $(VASINCS) $(LASINCS) $(GASINCS)

override GASOPTS = $(OPTIMIZER) $(MKDEPOPT)
override GASDEFS = $(GCDEFS)
override GASINCS = $(GCINCS)

#
# the install command to use
#
INSTALL = $(ROOT)/usr/bin/gfxinstall
INSTALLFLAGS = -v -m 0444

#
# Convenient command macros that include the flags macros.
#
# You should always invoke make in makefiles via $(MAKE), as make passes
# all command-line variables through the environment to sub-makes.
#
# Never use just $(CCF), etc. in rules that link executables; LDFLAGS
# needs to be included after your objects in the command line.
#
ASF	= $(AS) $(ASFLAGS)
CXXF	= $(CXX) $(CXXFLAGS)
C++F	= $(CXXF)
CCF	= $(CC) $(CFLAGS)
LDF	= $(LD) $(LDFLAGS)
LEXF	= $(LEX) $(LFLAGS)
YACCF	= $(YACC) $(YFLAGS)

# Targets

COMMONTARGS = clobber clean rmtargets

ALLTARGS = headers headers_install libs libs_install install apps distsi \
	   $(COMMONTARGS)

#
# Rule macros for nonterminal makefiles that iterate over subdirectories,
# making the current target.  Set *SUBDIRS to the relevant list of kids.
#
# Set NOSUBMESG to any value to suppress a warning that subdirectories
# are not present. This is useful with mandefs/rules
#
SUBDIR_MAKERULE= \
	if test ! -d $$d; then \
		if test "$(NOSUBMESG)" = "" ; then \
			echo "SKIPPING $$d: No such directory."; \
		fi \
	else \
		$(ECHO) "\t$(MAKE) -C $$d $${RULE:=$@}"; \
		$(MAKE) -C $$d $${RULE:=$@}; \
	fi

SUBDIRS_MAKERULE= \
	+@for d in $(SUBDIRS); do $(SUBDIR_MAKERULE); done

#
# One problem with the above rules is that if a make fails, and -k is
# set, the entire for loop might stop. This is because make still can
# (and XPG requires it) exit with an error status if the -k option is set.
# This is an alternate rule for SUBDIRS when it is desired that -k keep
# working on the next SUBDIR if the previous one failed. Its not quite
# as compact a rule as the previous ones.
# A standard usage would be:
#	default install $(COMMONTARGS): $(_FORCE)
#		$(MAKE) __DORULE=$@ $(SUBDIRS)
#	$(SUBDIRS): $(_FORCE)
#		@$(NSUBDIR_MAKERULE)

NSUBDIR_MAKERULE= \
	if test ! -d $@; then \
		if test "$(NOSUBMESG)" = "" ; then \
			echo "SKIPPING $@: No such directory."; \
		fi \
	else \
		echo "\t(cd $@; $(MAKE) $(__DORULE))"; \
		(cd $@; ${MAKE} $(__DORULE)); \
	fi

#
# Convenience file list macros:
#	- Commondefs defines the following lists: SOURCES, enumerating all
#	  source files; OBJECTS, the .o files derived from compilable source;
#	  and DIRT, which lists intermediates and temporary files to be
#	  removed by clean.
#	- The including (parent) makefile may define source file lists for
#	  the standard suffixes: CFILES for .c, ASFILES for .s, YFILES for
#	  .y, etc.  We combine all such lists into SOURCES.  The including
#	  makefile need not define CFILES &c before including commondefs.
#
SOURCES=$(HFILES) $(ASFILES) $(CCFILES) $(C++FILES) $(CXXFILES) $(CFILES) \
	$(EFILES) $(FFILES) $(LFILES) $(PFILES) $(RFILES) $(SHFILES) $(YFILES)

CXXO1=$(CXXFILES:.c++=.o) $(C++FILES:.c++=.o) $(CCFILES:.c++=.o)
CXXO2=$(CXXO1:.cxx=.o)
CXXO3=$(CXXO2:.C=.o)
CXXO4=$(CXXO3:.cc=.o)
CXXOALL=$(CXXO4)
YO1=$(YFILES:.y=.o)
YO2=$(YO1:.yxx=.o)
YO3=$(YO2:.Y=.o)
YOALL=$(YO3)
LO1=$(LFILES:.l=.o)
LO2=$(LO1:.lxx=.o)
LO3=$(LO2:.L=.o)
LOALL=$(LO3)
OBJECTS=$(ASFILES:.s=.o) $(CXXOALL) $(CFILES:.c=.o) $(EFILES:.e=.o) \
	$(FFILES:.f=.o) $(LOALL) $(PFILES:.p=.o) $(RFILES:.r=.o) \
	$(YOALL)

#
# dependency file(s)
#
ifeq ($(OS),IRIX)
MKDEPFILE = Makedepend
endif
ifeq ($(OS),Linux)
MKDEPFILE = $(OBJECTS:.o=.d)
endif

#
# distribution file
#
DISTFILE_EXT = Distfile
DISTFILE_INT = Distfile.int
ifeq ($(OS),IRIX)
DISTFILE = $(DISTFILE_EXT)
else
ifeq ($(OS),Linux)
DISTFILE = $(DISTFILE_EXT)
else
DISTFILE = $(DISTFILE_EXT) $(DISTFILE_INT)
endif
endif

#
# dirt stuff
#
DIRT=$(GDIRT) $(VDIRT) $(LDIRT)
override GDIRT=*.o a.out core lex.yy.[co] y.tab.[cho] ar.tmp.* so_locations

LOCALDEFS	= ./localdefs
LOCALRULES	= ./localrules

-include $(LOCALDEFS)
