dpadd.mk revision 246149
1139804Simp# $Id: dpadd.mk,v 1.18 2011/11/10 05:13:37 sjg Exp $
21541Srgrimes#
31541Srgrimes#	@(#) Copyright (c) 2004, Simon J. Gerraty
41541Srgrimes#
51541Srgrimes#	This file is provided in the hope that it will
61541Srgrimes#	be of use.  There is absolutely NO WARRANTY.
71541Srgrimes#	Permission to copy, redistribute or otherwise
81541Srgrimes#	use this file is hereby granted provided that 
91541Srgrimes#	the above copyright notice and this notice are
101541Srgrimes#	left intact. 
111541Srgrimes#      
121541Srgrimes#	Please send copies of changes and bug-fixes to:
131541Srgrimes#	sjg@crufty.net
141541Srgrimes#
151541Srgrimes
161541Srgrimes.if !target(__${.PARSEFILE}__)
171541Srgrimes__${.PARSEFILE}__:
181541Srgrimes
191541Srgrimes# sometimes we play games with .CURDIR etc
201541Srgrimes# _* hold the original values of .*
211541Srgrimes_OBJDIR?= ${.OBJDIR}
221541Srgrimes_CURDIR?= ${.CURDIR}
231541Srgrimes
241541Srgrimes# DPLIBS helps us ensure we keep DPADD and LDADD in sync
251541SrgrimesDPLIBS+= ${DPLIBS_LAST}
261541SrgrimesDPADD+= ${DPLIBS}
271541Srgrimes.for __lib in ${DPLIBS:T:R}
281541SrgrimesLDADD+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
291541Srgrimes.endfor
301541Srgrimes
311541Srgrimes# DPADD can contain things other than libs
321541Srgrimes__dpadd_libs = ${DPADD:M*/lib*}
331541Srgrimes
341541Srgrimes# some libs have dependencies...
351541Srgrimes# DPLIBS_* allows bsd.libnames.mk to flag libs which must be included
361541Srgrimes# in DPADD for a given library.
37116182Sobrien.for __lib in ${__dpadd_libs:@d@${DPLIBS_${d:T:R}}@}
38116182Sobrien.if "${DPADD:M${__lib}}" == ""
39116182SobrienDPADD+= ${__lib}
4013203SwollmanLDADD+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
41101127Srwatson.endif
4213203Swollman.endfor
431541Srgrimes# Last of all... for libc and libgcc
442112SwollmanDPADD+= ${DPADD_LAST}
4569664Speter
4676166Smarkm# Convert DPADD into -I and -L options and add them to CPPFLAGS and LDADD
47101127Srwatson# For the -I's convert the path to a relative one.  For separate objdirs
4889316Salfred# the DPADD paths will be to the obj tree so we need to subst anyway.
491541Srgrimes
501541Srgrimes# If USE_PROFILE is yes, then check for profiled versions of libs
511541Srgrimes# and use them.
521541Srgrimes
531541SrgrimesUSE_PROFILE?=no
541541Srgrimes.if defined(LIBDL) && exists(${LIBDL})
551541Srgrimes.if defined(PROG) && (make(${PROG}_p) || ${USE_PROFILE} == yes) && \
561541Srgrimes	defined(LDFLAGS) && ${LDFLAGS:M-export-dynamic}
571541Srgrimes# building profiled version of a prog that needs dlopen to work
5892751SjeffDPLIBS+= ${LIBDL}
5932011Sbde.endif
60138345Sphk.endif
61138345Sphk
62138345Sphk.if defined(LIBDMALLOC) && exists(${LIBDMALLOC})
631541Srgrimes.if defined(USE_DMALLOC) && ${USE_DMALLOC} != no
6469664Speter.if !defined(NO_DMALLOC)
6569664SpeterCPPFLAGS+= -DUSE_DMALLOC
6692751Sjeff.endif
6769664SpeterDPLIBS+= ${LIBDMALLOC}
6869664Speter.endif
6969664Speter.endif
7069664Speter
7192654Sjeff# Order -L's to seach ours first.
7292654Sjeff# Avoids picking up old versions already installed.
7369664Speter__dpadd_libdirs := ${__dpadd_libs:R:H:S/^/-L/g:O:u:N-L}
7469664SpeterLDADD += ${__dpadd_libdirs:M-L${OBJTOP}/*}
7569664SpeterLDADD += ${__dpadd_libdirs:N-L${OBJTOP}/*}
7669664Speter
7769664Speter.if ${.CURDIR} == ${SRCTOP}
781541SrgrimesRELDIR=.
791541SrgrimesRELTOP=.
801541Srgrimes.else
811541SrgrimesRELDIR?= ${.CURDIR:S,${SRCTOP}/,,}
821541Srgrimes.if ${RELDIR} == ${.CURDIR}
831541SrgrimesRELDIR?= ${.OBJDIR:S,${OBJTOP}/,,}
841541Srgrimes.endif
851541SrgrimesRELTOP?= ${RELDIR:C,[^/]+,..,g}
861541Srgrimes.endif
871541SrgrimesRELOBJTOP?= ${OBJTOP}
881541SrgrimesRELSRCTOP?= ${SRCTOP}
891541Srgrimes
901541Srgrimes.if !make(dpadd)
911541Srgrimes.ifdef LIB
921541Srgrimes# Each lib is its own src_lib, we want to include it in SRC_LIBS
931541Srgrimes# so that the correct INCLUDES_* will be picked up automatically.
941541SrgrimesSRC_LIBS+= ${_OBJDIR}/lib${LIB}.a
951541Srgrimes.endif
961541Srgrimes.endif
971541Srgrimes
981541Srgrimes# 
991541Srgrimes# This little bit of magic, assumes that SRC_libfoo will be
1001541Srgrimes# set if it cannot be correctly derrived from ${LIBFOO}
1011541Srgrimes# Note that SRC_libfoo and INCLUDES_libfoo should be named for the
1021541Srgrimes# actual libary name not the variable name that might refer to it.
1031541Srgrimes# 99% of the time the two are the same, but the DPADD logic
1041541Srgrimes# only has the libary name available, so stick to that.
1051541Srgrimes# 
1061541Srgrimes
1071541SrgrimesSRC_LIBS?=
10883366Sjulian__dpadd_libs += ${SRC_LIBS}
10983366SjulianDPMAGIC_LIBS += ${__dpadd_libs} \
110140714Sjeff	${__dpadd_libs:@d@${DPMAGIC_LIBS_${d:T:R}}@}
111140714Sjeff
1121541Srgrimes.for __lib in ${DPMAGIC_LIBS:O:u}
11391419Sjhb# 
11483366Sjulian# if SRC_libfoo is not set, then we assume that the srcdir corresponding
11542408Seivind# to where we found the library is correct.
11642453Seivind#
11742408SeivindSRC_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELSRCTOP},}
11842453Seivind#
11983366Sjulian# This is a no-brainer but just to be complete...
1201541Srgrimes#
1211541SrgrimesOBJ_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELOBJTOP},}
1221541Srgrimes#
1231541Srgrimes# If INCLUDES_libfoo is not set, then we'll use ${SRC_libfoo}/h if it exists,
1241541Srgrimes# else just ${SRC_libfoo}.
1251541Srgrimes#
126111119SimpINCLUDES_${__lib:T:R}?= -I${exists(${SRC_${__lib:T:R}}/h):?${SRC_${__lib:T:R}}/h:${SRC_${__lib:T:R}}}
1271541Srgrimes
1281541Srgrimes.endfor
12936735Sdfr
1301541Srgrimes# Now for the bits we actually need
1311541Srgrimes__dpadd_incs=
13236735Sdfr.for __lib in ${__dpadd_libs:u}
13320069Sbde.if (make(${PROG}_p) || defined(NEED_GPROF)) && exists(${__lib:R}_p.a)
13420069Sbde__ldadd=-l${__lib:T:R:S,lib,,}
13520069SbdeLDADD := ${LDADD:S,^${__ldadd}$,${__ldadd}_p,g}
13620069Sbde.endif
13720069Sbde
13820069Sbde#
13920069Sbde# Some libs generate headers, so we potentially need both
1401541Srgrimes# the src dir and the obj dir.
14192751Sjeff# If ${INCLUDES_libfoo} contains a word ending in /h, we assume that either
142100613Srwatson# 1. it does not generate headers or
143100613Srwatson# 2. INCLUDES_libfoo will have been set correctly
144100613Srwatson# XXX it gets ugly avoiding duplicates... 
145100613Srwatson# use :? to ensure .for does not prevent correct evaluation
1461541Srgrimes#
1471541Srgrimes# We take care of duplicate suppression later.
1481541Srgrimes__dpadd_incs += ${"${INCLUDES_${__lib:T:R}:M*/h}":? :-I${OBJ_${__lib:T:R}}}
1491541Srgrimes__dpadd_incs += ${INCLUDES_${__lib:T:R}}
1501541Srgrimes.endfor
15197994Sjhb
15297994Sjhb#
15397994Sjhb# eliminate any duplicates - but don't mess with the order
15497994Sjhb# force evaluation now - to avoid giving make a headache
15597994Sjhb#
1561541Srgrimes.for t in CFLAGS CXXFLAGS
1571541Srgrimes# avoid duplicates
1581541Srgrimes__$t_incs:=${$t:M-I*:O:u}
1591541Srgrimes.for i in ${__dpadd_incs}
1601541Srgrimes.if "${__$t_incs:M$i}" == ""
16189306Salfred$t+= $i
16233360Sdyson__$t_incs+= $i
16351649Sphk.endif
16433360Sdyson.endfor
1651541Srgrimes.endfor
166140714Sjeff
1671541Srgrimes# This target is used to gather a list of
16889306Salfred# dir: ${DPADD}
1691541Srgrimes# entries
1701541Srgrimes.if make(*dpadd*)
1711541Srgrimes# allow overrides
1721541Srgrimes.-include "dpadd++.mk"
1731541Srgrimes
1741541Srgrimes.if !target(dpadd)
1751541Srgrimesdpadd:	.NOTMAIN
1761541Srgrimes.if defined(DPADD) && ${DPADD} != ""
177140714Sjeff	@echo "${RELDIR}: ${DPADD:S,${OBJTOP}/,,}"
1781541Srgrimes.endif
1791541Srgrimes.endif
1801541Srgrimes.endif
1811541Srgrimes
1821541Srgrimes.ifdef SRC_PATHADD
183140714Sjeff# We don't want to assume that we need to .PATH every element of 
1841541Srgrimes# SRC_LIBS, but the Makefile cannot do
1851541Srgrimes# .PATH: ${SRC_libfoo}
186140714Sjeff# since the value of SRC_libfoo must be available at the time .PATH:
187140714Sjeff# is read - and we only just worked it out.  
1881541Srgrimes# Further, they can't wait until after include of {lib,prog}.mk as 
1893148Sphk# the .PATH is needed before then.
1903148Sphk# So we let the Makefile do
19192751Sjeff# SRC_PATHADD+= ${SRC_libfoo}
192100613Srwatson# and we defer the .PATH: until now so that SRC_libfoo will be available.
193100613Srwatson.PATH: ${SRC_PATHADD}
194100613Srwatson.endif
195100613Srwatson
1961541Srgrimes.endif
1971541Srgrimes