dpadd.mk revision 268437
1268437Ssjg# $Id: dpadd.mk,v 1.19 2014/04/05 22:56:54 sjg Exp $
2246149Ssjg#
3246149Ssjg#	@(#) Copyright (c) 2004, Simon J. Gerraty
4246149Ssjg#
5246149Ssjg#	This file is provided in the hope that it will
6246149Ssjg#	be of use.  There is absolutely NO WARRANTY.
7246149Ssjg#	Permission to copy, redistribute or otherwise
8246149Ssjg#	use this file is hereby granted provided that 
9246149Ssjg#	the above copyright notice and this notice are
10246149Ssjg#	left intact. 
11246149Ssjg#      
12246149Ssjg#	Please send copies of changes and bug-fixes to:
13246149Ssjg#	sjg@crufty.net
14246149Ssjg#
15246149Ssjg
16246149Ssjg.if !target(__${.PARSEFILE}__)
17246149Ssjg__${.PARSEFILE}__:
18246149Ssjg
19246149Ssjg# sometimes we play games with .CURDIR etc
20246149Ssjg# _* hold the original values of .*
21246149Ssjg_OBJDIR?= ${.OBJDIR}
22246149Ssjg_CURDIR?= ${.CURDIR}
23246149Ssjg
24246149Ssjg# DPLIBS helps us ensure we keep DPADD and LDADD in sync
25246149SsjgDPLIBS+= ${DPLIBS_LAST}
26246149SsjgDPADD+= ${DPLIBS}
27246149Ssjg.for __lib in ${DPLIBS:T:R}
28246149SsjgLDADD+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
29246149Ssjg.endfor
30246149Ssjg
31246149Ssjg# DPADD can contain things other than libs
32246149Ssjg__dpadd_libs = ${DPADD:M*/lib*}
33246149Ssjg
34246149Ssjg# some libs have dependencies...
35246149Ssjg# DPLIBS_* allows bsd.libnames.mk to flag libs which must be included
36246149Ssjg# in DPADD for a given library.
37246149Ssjg.for __lib in ${__dpadd_libs:@d@${DPLIBS_${d:T:R}}@}
38246149Ssjg.if "${DPADD:M${__lib}}" == ""
39246149SsjgDPADD+= ${__lib}
40246149SsjgLDADD+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
41246149Ssjg.endif
42246149Ssjg.endfor
43246149Ssjg# Last of all... for libc and libgcc
44246149SsjgDPADD+= ${DPADD_LAST}
45246149Ssjg
46246149Ssjg# Convert DPADD into -I and -L options and add them to CPPFLAGS and LDADD
47246149Ssjg# For the -I's convert the path to a relative one.  For separate objdirs
48246149Ssjg# the DPADD paths will be to the obj tree so we need to subst anyway.
49246149Ssjg
50246149Ssjg# If USE_PROFILE is yes, then check for profiled versions of libs
51246149Ssjg# and use them.
52246149Ssjg
53246149SsjgUSE_PROFILE?=no
54246149Ssjg.if defined(LIBDL) && exists(${LIBDL})
55246149Ssjg.if defined(PROG) && (make(${PROG}_p) || ${USE_PROFILE} == yes) && \
56246149Ssjg	defined(LDFLAGS) && ${LDFLAGS:M-export-dynamic}
57246149Ssjg# building profiled version of a prog that needs dlopen to work
58246149SsjgDPLIBS+= ${LIBDL}
59246149Ssjg.endif
60246149Ssjg.endif
61246149Ssjg
62246149Ssjg.if defined(LIBDMALLOC) && exists(${LIBDMALLOC})
63246149Ssjg.if defined(USE_DMALLOC) && ${USE_DMALLOC} != no
64246149Ssjg.if !defined(NO_DMALLOC)
65246149SsjgCPPFLAGS+= -DUSE_DMALLOC
66246149Ssjg.endif
67246149SsjgDPLIBS+= ${LIBDMALLOC}
68246149Ssjg.endif
69246149Ssjg.endif
70246149Ssjg
71268437Ssjg# Order -L's to search ours first.
72246149Ssjg# Avoids picking up old versions already installed.
73246149Ssjg__dpadd_libdirs := ${__dpadd_libs:R:H:S/^/-L/g:O:u:N-L}
74246149SsjgLDADD += ${__dpadd_libdirs:M-L${OBJTOP}/*}
75246149SsjgLDADD += ${__dpadd_libdirs:N-L${OBJTOP}/*}
76246149Ssjg
77246149Ssjg.if ${.CURDIR} == ${SRCTOP}
78246149SsjgRELDIR=.
79246149SsjgRELTOP=.
80246149Ssjg.else
81246149SsjgRELDIR?= ${.CURDIR:S,${SRCTOP}/,,}
82246149Ssjg.if ${RELDIR} == ${.CURDIR}
83246149SsjgRELDIR?= ${.OBJDIR:S,${OBJTOP}/,,}
84246149Ssjg.endif
85246149SsjgRELTOP?= ${RELDIR:C,[^/]+,..,g}
86246149Ssjg.endif
87246149SsjgRELOBJTOP?= ${OBJTOP}
88246149SsjgRELSRCTOP?= ${SRCTOP}
89246149Ssjg
90246149Ssjg.if !make(dpadd)
91246149Ssjg.ifdef LIB
92246149Ssjg# Each lib is its own src_lib, we want to include it in SRC_LIBS
93246149Ssjg# so that the correct INCLUDES_* will be picked up automatically.
94246149SsjgSRC_LIBS+= ${_OBJDIR}/lib${LIB}.a
95246149Ssjg.endif
96246149Ssjg.endif
97246149Ssjg
98246149Ssjg# 
99246149Ssjg# This little bit of magic, assumes that SRC_libfoo will be
100246149Ssjg# set if it cannot be correctly derrived from ${LIBFOO}
101246149Ssjg# Note that SRC_libfoo and INCLUDES_libfoo should be named for the
102268437Ssjg# actual library name not the variable name that might refer to it.
103246149Ssjg# 99% of the time the two are the same, but the DPADD logic
104268437Ssjg# only has the library name available, so stick to that.
105246149Ssjg# 
106246149Ssjg
107246149SsjgSRC_LIBS?=
108246149Ssjg__dpadd_libs += ${SRC_LIBS}
109246149SsjgDPMAGIC_LIBS += ${__dpadd_libs} \
110246149Ssjg	${__dpadd_libs:@d@${DPMAGIC_LIBS_${d:T:R}}@}
111246149Ssjg
112246149Ssjg.for __lib in ${DPMAGIC_LIBS:O:u}
113246149Ssjg# 
114246149Ssjg# if SRC_libfoo is not set, then we assume that the srcdir corresponding
115246149Ssjg# to where we found the library is correct.
116246149Ssjg#
117246149SsjgSRC_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELSRCTOP},}
118246149Ssjg#
119246149Ssjg# This is a no-brainer but just to be complete...
120246149Ssjg#
121246149SsjgOBJ_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELOBJTOP},}
122246149Ssjg#
123246149Ssjg# If INCLUDES_libfoo is not set, then we'll use ${SRC_libfoo}/h if it exists,
124246149Ssjg# else just ${SRC_libfoo}.
125246149Ssjg#
126246149SsjgINCLUDES_${__lib:T:R}?= -I${exists(${SRC_${__lib:T:R}}/h):?${SRC_${__lib:T:R}}/h:${SRC_${__lib:T:R}}}
127246149Ssjg
128246149Ssjg.endfor
129246149Ssjg
130246149Ssjg# Now for the bits we actually need
131246149Ssjg__dpadd_incs=
132246149Ssjg.for __lib in ${__dpadd_libs:u}
133246149Ssjg.if (make(${PROG}_p) || defined(NEED_GPROF)) && exists(${__lib:R}_p.a)
134246149Ssjg__ldadd=-l${__lib:T:R:S,lib,,}
135246149SsjgLDADD := ${LDADD:S,^${__ldadd}$,${__ldadd}_p,g}
136246149Ssjg.endif
137246149Ssjg
138246149Ssjg#
139246149Ssjg# Some libs generate headers, so we potentially need both
140246149Ssjg# the src dir and the obj dir.
141246149Ssjg# If ${INCLUDES_libfoo} contains a word ending in /h, we assume that either
142246149Ssjg# 1. it does not generate headers or
143246149Ssjg# 2. INCLUDES_libfoo will have been set correctly
144246149Ssjg# XXX it gets ugly avoiding duplicates... 
145246149Ssjg# use :? to ensure .for does not prevent correct evaluation
146246149Ssjg#
147246149Ssjg# We take care of duplicate suppression later.
148246149Ssjg__dpadd_incs += ${"${INCLUDES_${__lib:T:R}:M*/h}":? :-I${OBJ_${__lib:T:R}}}
149246149Ssjg__dpadd_incs += ${INCLUDES_${__lib:T:R}}
150246149Ssjg.endfor
151246149Ssjg
152246149Ssjg#
153246149Ssjg# eliminate any duplicates - but don't mess with the order
154246149Ssjg# force evaluation now - to avoid giving make a headache
155246149Ssjg#
156246149Ssjg.for t in CFLAGS CXXFLAGS
157246149Ssjg# avoid duplicates
158246149Ssjg__$t_incs:=${$t:M-I*:O:u}
159246149Ssjg.for i in ${__dpadd_incs}
160246149Ssjg.if "${__$t_incs:M$i}" == ""
161246149Ssjg$t+= $i
162246149Ssjg__$t_incs+= $i
163246149Ssjg.endif
164246149Ssjg.endfor
165246149Ssjg.endfor
166246149Ssjg
167246149Ssjg# This target is used to gather a list of
168246149Ssjg# dir: ${DPADD}
169246149Ssjg# entries
170246149Ssjg.if make(*dpadd*)
171246149Ssjg# allow overrides
172246149Ssjg.-include "dpadd++.mk"
173246149Ssjg
174246149Ssjg.if !target(dpadd)
175246149Ssjgdpadd:	.NOTMAIN
176246149Ssjg.if defined(DPADD) && ${DPADD} != ""
177246149Ssjg	@echo "${RELDIR}: ${DPADD:S,${OBJTOP}/,,}"
178246149Ssjg.endif
179246149Ssjg.endif
180246149Ssjg.endif
181246149Ssjg
182246149Ssjg.ifdef SRC_PATHADD
183246149Ssjg# We don't want to assume that we need to .PATH every element of 
184246149Ssjg# SRC_LIBS, but the Makefile cannot do
185246149Ssjg# .PATH: ${SRC_libfoo}
186246149Ssjg# since the value of SRC_libfoo must be available at the time .PATH:
187246149Ssjg# is read - and we only just worked it out.  
188246149Ssjg# Further, they can't wait until after include of {lib,prog}.mk as 
189246149Ssjg# the .PATH is needed before then.
190246149Ssjg# So we let the Makefile do
191246149Ssjg# SRC_PATHADD+= ${SRC_libfoo}
192246149Ssjg# and we defer the .PATH: until now so that SRC_libfoo will be available.
193246149Ssjg.PATH: ${SRC_PATHADD}
194246149Ssjg.endif
195246149Ssjg
196246149Ssjg.endif
197