#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
#

# Standard install rules
# The VAR_SGSBIN and VAR_SGSBIN64 install rules included here are applicable
# to Solaris 10 and earlier releases. For post Solaris 10 releases, the 
# standard ROOTBIN and ROOTBIN64 rules defined in ../Makefile.cmd are used.
# The new rules here are included to allow for easier backporting. Making
# the appropriate updates in ./Makefile.var, will allow the SGS components
# to be installed in /usr/ccs/bin rather than the current /usr/bin.
#
$(VAR_SGSBIN)/%: %
	$(INS.file)

$(VAR_SGSBIN64)/%: %
	$(INS.file)

################################################################################
#
# Linting the Linker Libraries
#
#  Several of the linker's libraries are, in whole or in part, built in two
#  passes, once as Elf32 and once as Elf64 (i.e. with -D_ELF64 defined).  Lint
#  needs to be able to do both passes, but combining the two is problematic for
#  the 2nd pass of lint, as it sees many interfaces as being defined both ways
#  and considers them to be incompatible.  The targets defined here allow for
#  both passes to live independently.  This means that both the lint.out, and
#  the lint library itself get generated separately, to different output files.
#  The lint.out's get combined into a single lint.out report, and the lint
#  libraries get generated with a 32/64 suffix.  The dependents on these lint
#  libraries, then, choose which version they need to use.  Substitutions can
#  be made automatically if the macro's defined in ./Makefile.com are used to
#  specify the dependency, for those libs that need them.
#
# Don't
#
#  Don't use the /*LINTLIBRARY*/ directive in linker libraries, this disables
#  some important checks, including the ability to test format strings from the
#  msg.h files.
#
#  Don't use the `-x' option to lint when linting linker libraries.  This masks
#  all the dead wood in our own header files.  Instead, there has been added to
#  the relevant common directories a file called `lintsup.c' which is used to
#  mask out the headers that we aren't interested in.  This method is used for
#  libraries, like libld, which have their own header files, but is irrelevant
#  to libraries like libldstab which exports no interface of it's own.
#
#  The `lintsup.c' file can also be used, in some cases, to mask out other
#  issues that lint won't otherwise shut up about.
#
# Other Lint Options
#
#  `-m' has been added to the LINTFLAGS.  Warnings about globals that could be
#  static are irrelevant as we use mapfiles to scope down unnecessary globals.
#
#  `-u' is used in the LINTFLAGS for libraries, otherwise lint tends to be very
#  noisy.
#
#  `-x' is avoided for libraries, but is used for executables because all we
#  care about is that what we use is defined, not about declarations in public
#  headers that we don't use.
#
# Relevant variables:
#
# */Makefile.com
#	SRCS=		../common/llib-l<libname>
#	LINTSRCS=	<source files>
#	LDLIBS=		... [$(LDDBG_LIB) $(LD_LIB)]
#	LINTFLAGS=	...
#	LINTFLAGS64=	...
#	CLEANFILES +=	... $(LINTOUTS)
#	CLOBBERFILES +=	... $(LINTLIBS)
#
# Relevant targets:
#
# */Makefile.targ
#	# this file for SGS lint targets.
#	include		$(SRC)/cmd/sgs/Makefile.targ
#
#	lint:		<choose the desired functionality> $(SGSLINTOUT)
#
#		$(LINTLIB32),
#		$(LINTLIB64)	Create an Elf32 or Elf64 lint library from
#				a proto file indicated by the $(SRCS) variable.
#
#		$(LINTOUT32),
#		$(LINTOUT64)	Run lint on the sources indicated by the
#				$(LINTSRCS) variable with respect to Elf32
#				or Elf64.  Dependencies are gathered from
#				the $(LDLIBS) variable.
#
#		$(SGSLINTOUT)	Create a `lint.out' file as the concatination
#				of the lint output from the previous targets.
#				This should be specified *last* in the list.
#
################################################################################

#
# Override the OS's $(LINTOUT) target to avoid confusion.
#
LINTOUT =	$(LINTOUT1)

#
# If LD_LIB, LDDBG_LIB, or CONV_LIB is added to LDLIBS, then the right lint
# library should be picked up automatically.
#
$(LINTOUT32) :=	LD_LIB=$(LD_LIB32)
$(LINTOUT32) :=	LDDBG_LIB=$(LDDBG_LIB32)
$(LINTOUT32) :=	CONV_LIB=$(CONV_LIB32)

$(LINTOUT64) :=	LD_LIB=$(LD_LIB64)
$(LINTOUT64) :=	LDDBG_LIB=$(LDDBG_LIB64)
$(LINTOUT64) :=	CONV_LIB=$(CONV_LIB64)

#
# Force $(LINTLIB) in order to help the $(SGSLINTOUT)
# target produce the same output on successive runs.
#
$(LINTLIB):	FRC

$(LINTLIB32):	$(SRCS)
	$(LINT.c) -o $(LIBNAME32) $(SRCS)

$(LINTLIB64):	$(SRCS)
	$(LINT.c) -D_ELF64 -o $(LIBNAME64) $(SRCS)

$(LINTOUT32):	$(LINTSRCS) $(LINTSRCS32)
	$(LINT.c) $(LINTSRCS) $(LINTSRCS32) $(LDLIBS) > $(LINTOUT32) 2>&1

$(LINTOUT64):	$(LINTSRCS) $(LINTSRCS64)
	$(LINT.c) -D_ELF64 $(LINTSRCS) $(LINTSRCS64) \
	    $(LDLIBS) > $(LINTOUT64) 2>&1

#
# A couple of macros used in the SGSLINTOUT rule below
#
#	LINT_HDR - Use sgs/tools/lint_hdr.pl to generate lint output headers
#	LINT_TEE - Use tee to write output to stdout and also capture it
#		in the SGSLINT output file.
#
# An additional complexity: We produce headers for all the lint
# output so that we can tell what came from where when we look
# at the resulting file. We also cat these headers to stdout so that
# the user of make will see them. However, we don't want the headers
# to go to stdout if there is only one of LINTOUT32 and LINTOUT64. To
# the interactive user, the headers are only interesting as a way to
# separate the two ELF classes. We only bother with this for the
# non-DYNLIB and non-RTLD case, because at the current time, both of these
# cases always have both 32 and 64-bit ELFCLASS support.
#
LINT_HDR=	perl $(SGSTOOLS)/lint_hdr.pl
LINT_TEE=	tee -a $(SGSLINTOUT)

$(SGSLINTOUT): FRC
	@ rm -f $(SGSLINTOUT)
	@ if [ -r $(LINTOUT1) ]; then			\
		$(LINT_HDR) $(LINTLIB) | $(LINT_TEE);	\
		cat $(LINTOUT1) | $(LINT_TEE);	\
	fi
	@ if [ -r $(LINTOUT32) ]; then			\
		if [ -n "$(DYNLIB)" ] ; then		\
			$(LINT_HDR) $(DYNLIB) 32 | $(LINT_TEE); \
		elif [ -n "$(RTLD)" ] ; then		\
			$(LINT_HDR) $(RTLD) 32 | $(LINT_TEE); \
		else \
			if [ -r $(LINTOUT64) ]; then \
				$(LINT_HDR) $(PROG) 32 | $(LINT_TEE);	\
			else \
				$(LINT_HDR) $(PROG) 32 >> $(SGSLINTOUT); \
			fi; \
		fi; \
		cat $(LINTOUT32) | $(LINT_TEE);	\
	fi
	@ if [ -r $(LINTOUT64) ]; then			\
		if [ -n "$(DYNLIB)" ] ; then		\
			if [ $(DYNLIB) = "libld.so.2" ] ; then \
				$(LINT_HDR) libld.so.3 64 | $(LINT_TEE); \
			else \
				$(LINT_HDR) $(DYNLIB) 64 | $(LINT_TEE); \
			fi; \
		elif [ -n "$(RTLD)" ] ; then		\
			$(LINT_HDR) $(RTLD) 64 | $(LINT_TEE); \
		else \
			if [ -r $(LINTOUT32) ]; then \
				$(LINT_HDR) $(PROG) 64 | $(LINT_TEE); \
			else \
				$(LINT_HDR) $(PROG) 64 >> $(SGSLINTOUT); \
			fi; \
		fi; \
		cat $(LINTOUT64) | $(LINT_TEE);	\
	fi
	@ rm -f $(LINTOUT1) $(LINTOUT32) $(LINTOUT64)

#
# For those that install the lint library source file.
#
$(ROOTLIBDIR)/$(LINTLIBSRC): ../common/$(LINTLIBSRC)
	$(INS.file) ../common/$(LINTLIBSRC)

$(ROOTFS_LIBDIR)/$(LIBLINKS): $(ROOTFS_LIBDIR)/$(LIBLINKS)$(VERS)
	$(INS.liblink)

$(ROOTFS_LIBDIR64)/$(LIBLINKS): $(ROOTFS_LIBDIR64)/$(LIBLINKS)$(VERS)
	$(INS.liblink64)

$(ROOTFS_LIBDIR)/$(LIBLINKSCCC): $(ROOTFS_LIBDIR)/$(LIBLINKSCCC)$(VERS)
	$(INS.liblinkccc)

$(ROOTFS_LIBDIR64)/$(LIBLINKSCCC): $(ROOTFS_LIBDIR64)/$(LIBLINKSCCC)$(VERS)
	$(INS.liblinkccc64)

FRC: