dpadd.mk revision 319884
1319884Ssjg# $Id: dpadd.mk,v 1.23 2017/02/13 16:46:01 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
24300313Ssjg.if ${_CURDIR} == ${SRCTOP}
25300313SsjgRELDIR=.
26300313SsjgRELTOP=.
27300313Ssjg.else
28300313SsjgRELDIR?= ${_CURDIR:S,${SRCTOP}/,,}
29300313Ssjg.if ${RELDIR} == ${_CURDIR}
30300313SsjgRELDIR?= ${_OBJDIR:S,${OBJTOP}/,,}
31300313Ssjg.endif
32300313SsjgRELTOP?= ${RELDIR:C,[^/]+,..,g}
33300313Ssjg.endif
34300313SsjgRELOBJTOP?= ${OBJTOP}
35300313SsjgRELSRCTOP?= ${SRCTOP}
36300313Ssjg
37300313Ssjg# we get included just about everywhere so this is handy...
38300313Ssjg# C*DEBUG_XTRA are for defining on cmd line etc 
39300313Ssjg# so do not use in makefiles.
40300313Ssjg.ifdef CFLAGS_DEBUG_XTRA
41300313SsjgCFLAGS_LAST += ${CFLAGS_DEBUG_XTRA}
42300313Ssjg.endif
43300313Ssjg.ifdef CXXFLAGS_DEBUG_XTRA
44300313SsjgCXXFLAGS_LAST += ${CXXFLAGS_DEBUG_XTRA}
45300313Ssjg.endif
46300313Ssjg
47300313Ssjg.-include <local.dpadd.mk>
48300313Ssjg
49246149Ssjg# DPLIBS helps us ensure we keep DPADD and LDADD in sync
50246149SsjgDPLIBS+= ${DPLIBS_LAST}
51300313SsjgDPADD+= ${DPLIBS:N-*}
52319884Ssjg.for __lib in ${DPLIBS}
53300313Ssjg.if "${_lib:M-*}" != ""
54300313SsjgLDADD += ${__lib}
55300313Ssjg.else
56319884SsjgLDADD += ${LDADD_${__lib:T:R}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
57300313Ssjg.endif
58246149Ssjg.endfor
59246149Ssjg
60246149Ssjg# DPADD can contain things other than libs
61300313Ssjg__dpadd_libs := ${DPADD:M*/lib*}
62246149Ssjg
63246149Ssjg# some libs have dependencies...
64246149Ssjg# DPLIBS_* allows bsd.libnames.mk to flag libs which must be included
65246149Ssjg# in DPADD for a given library.
66300313Ssjg# Gather all such dependencies into __ldadd_all_xtras
67300313Ssjg# dups will be dealt with later.
68300313Ssjg# Note: libfoo_pic uses DPLIBS_libfoo
69300313Ssjg__ldadd_all_xtras=
70300313Ssjg.for __lib in ${__dpadd_libs:@d@${DPLIBS_${d:T:R:S,_pic,,}}@}
71300313Ssjg__ldadd_all_xtras+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
72246149Ssjg.if "${DPADD:M${__lib}}" == ""
73246149SsjgDPADD+= ${__lib}
74246149Ssjg.endif
75246149Ssjg.endfor
76246149Ssjg# Last of all... for libc and libgcc
77246149SsjgDPADD+= ${DPADD_LAST}
78246149Ssjg
79300313Ssjg# de-dupuplicate __ldadd_all_xtras into __ldadd_xtras
80300313Ssjg# in reverse order so that libs end up listed after all that needed them.
81300313Ssjg__ldadd_xtras=
82300313Ssjg.for __lib in ${__ldadd_all_xtras:[-1..1]}
83300313Ssjg.if "${__ldadd_xtras:M${__lib}}" == "" || ${NEED_IMPLICIT_LDADD:tl:Uno} != "no"
84300313Ssjg__ldadd_xtras+= ${__lib}
85300313Ssjg.endif
86300313Ssjg.endfor
87300313Ssjg
88300313Ssjg.if !empty(__ldadd_xtras)
89300313Ssjg# now back to the original order
90300313Ssjg__ldadd_xtras:= ${__ldadd_xtras:[-1..1]}
91300313SsjgLDADD+= ${__ldadd_xtras}
92300313Ssjg.endif
93300313Ssjg
94246149Ssjg# Convert DPADD into -I and -L options and add them to CPPFLAGS and LDADD
95246149Ssjg# For the -I's convert the path to a relative one.  For separate objdirs
96246149Ssjg# the DPADD paths will be to the obj tree so we need to subst anyway.
97246149Ssjg
98300313Ssjg# update this
99300313Ssjg__dpadd_libs := ${DPADD:M*/lib*}
100246149Ssjg
101268437Ssjg# Order -L's to search ours first.
102246149Ssjg# Avoids picking up old versions already installed.
103301462Ssjg__dpadd_libdirs := ${__dpadd_libs:R:H:S/^/-L/g:O:u:N-L}
104246149SsjgLDADD += ${__dpadd_libdirs:M-L${OBJTOP}/*}
105300313SsjgLDADD += ${__dpadd_libdirs:N-L${OBJTOP}/*:N-L${HOST_LIBDIR:U/usr/lib}}
106300313Ssjg.if defined(HOST_LIBDIR) && ${HOST_LIBDIR} != "/usr/lib"
107300313SsjgLDADD+= -L${HOST_LIBDIR}
108246149Ssjg.endif
109246149Ssjg
110246149Ssjg.if !make(dpadd)
111246149Ssjg.ifdef LIB
112246149Ssjg# Each lib is its own src_lib, we want to include it in SRC_LIBS
113246149Ssjg# so that the correct INCLUDES_* will be picked up automatically.
114246149SsjgSRC_LIBS+= ${_OBJDIR}/lib${LIB}.a
115246149Ssjg.endif
116246149Ssjg.endif
117246149Ssjg
118246149Ssjg# 
119246149Ssjg# This little bit of magic, assumes that SRC_libfoo will be
120246149Ssjg# set if it cannot be correctly derrived from ${LIBFOO}
121246149Ssjg# Note that SRC_libfoo and INCLUDES_libfoo should be named for the
122268437Ssjg# actual library name not the variable name that might refer to it.
123246149Ssjg# 99% of the time the two are the same, but the DPADD logic
124268437Ssjg# only has the library name available, so stick to that.
125246149Ssjg# 
126246149Ssjg
127246149SsjgSRC_LIBS?=
128246149Ssjg__dpadd_libs += ${SRC_LIBS}
129246149SsjgDPMAGIC_LIBS += ${__dpadd_libs} \
130246149Ssjg	${__dpadd_libs:@d@${DPMAGIC_LIBS_${d:T:R}}@}
131246149Ssjg
132300313Ssjg# we skip this for staged libs
133300313Ssjg.for __lib in ${DPMAGIC_LIBS:O:u:N${STAGE_OBJTOP:Unot}*/lib/*}
134246149Ssjg# 
135246149Ssjg# if SRC_libfoo is not set, then we assume that the srcdir corresponding
136246149Ssjg# to where we found the library is correct.
137246149Ssjg#
138246149SsjgSRC_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELSRCTOP},}
139246149Ssjg#
140246149Ssjg# This is a no-brainer but just to be complete...
141246149Ssjg#
142246149SsjgOBJ_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELOBJTOP},}
143246149Ssjg#
144246149Ssjg# If INCLUDES_libfoo is not set, then we'll use ${SRC_libfoo}/h if it exists,
145246149Ssjg# else just ${SRC_libfoo}.
146246149Ssjg#
147246149SsjgINCLUDES_${__lib:T:R}?= -I${exists(${SRC_${__lib:T:R}}/h):?${SRC_${__lib:T:R}}/h:${SRC_${__lib:T:R}}}
148246149Ssjg
149246149Ssjg.endfor
150246149Ssjg
151300313Ssjg# even for staged libs we sometimes 
152300313Ssjg# need to allow direct -I to avoid cicular dependencies 
153300313Ssjg.for __lib in ${DPMAGIC_LIBS:O:u:T:R}
154300313Ssjg.if !empty(SRC_${__lib}) && empty(INCLUDES_${__lib})
155300313Ssjg# must be a staged lib
156300313Ssjg.if exists(${SRC_${__lib}}/h)
157300313SsjgINCLUDES_${__lib} = -I${SRC_${__lib}}/h
158300313Ssjg.else
159300313SsjgINCLUDES_${__lib} = -I${SRC_${__lib}}
160300313Ssjg.endif
161300313Ssjg.endif
162300313Ssjg.endfor
163300313Ssjg
164300313Ssjg# when linking a shared lib, avoid non pic libs
165300313SsjgSHLDADD+= ${LDADD:N-[lL]*}
166300313Ssjg.for __lib in ${__dpadd_libs:u}
167300313Ssjg.if defined(SHLIB_NAME) && ${LDADD:M-l${__lib:T:R:S,lib,,}} != ""
168300313Ssjg.if ${__lib:T:N*_pic.a:N*.so} == "" || exists(${__lib:R}.so)
169300313SsjgSHLDADD+= -l${__lib:T:R:S,lib,,}
170300313Ssjg.elif exists(${__lib:R}_pic.a)
171300313SsjgSHLDADD+= -l${__lib:T:R:S,lib,,}_pic
172300313Ssjg.else
173300313Ssjg.warning ${RELDIR}.${TARGET_SPEC} needs ${__lib:T:R}_pic.a
174300313SsjgSHLDADD+= -l${__lib:T:R:S,lib,,}
175300313Ssjg.endif
176300313SsjgSHLDADD+= -L${__lib:H}
177300313Ssjg.endif
178300313Ssjg.endfor
179300313Ssjg
180246149Ssjg# Now for the bits we actually need
181246149Ssjg__dpadd_incs=
182246149Ssjg.for __lib in ${__dpadd_libs:u}
183246149Ssjg.if (make(${PROG}_p) || defined(NEED_GPROF)) && exists(${__lib:R}_p.a)
184246149Ssjg__ldadd=-l${__lib:T:R:S,lib,,}
185246149SsjgLDADD := ${LDADD:S,^${__ldadd}$,${__ldadd}_p,g}
186246149Ssjg.endif
187300313Ssjg.endfor
188246149Ssjg
189246149Ssjg#
190246149Ssjg# We take care of duplicate suppression later.
191300313Ssjg# don't apply :T:R too early
192300313Ssjg__dpadd_incs += ${__dpadd_libs:u:@x@${INCLUDES_${x:T:R}}@}
193300313Ssjg__dpadd_incs += ${__dpadd_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_${x:T:R}}@}
194246149Ssjg
195300313Ssjg__dpadd_last_incs += ${__dpadd_libs:u:@x@${INCLUDES_LAST_${x:T:R}}@}
196300313Ssjg__dpadd_last_incs += ${__dpadd_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_LAST_${x:T:R}}@}
197300313Ssjg
198300313Ssjg.if defined(HOSTPROG) || ${MACHINE} == "host"
199300313Ssjg# we want any -I/usr/* last
200300313Ssjg__dpadd_last_incs := \
201300313Ssjg	${__dpadd_last_incs:N-I/usr/*} \
202300313Ssjg	${__dpadd_incs:M-I/usr/*} \
203300313Ssjg	${__dpadd_last_incs:M-I/usr/*} 
204300313Ssjg__dpadd_incs := ${__dpadd_incs:N-I/usr/*}
205300313Ssjg.endif
206300313Ssjg
207246149Ssjg#
208246149Ssjg# eliminate any duplicates - but don't mess with the order
209246149Ssjg# force evaluation now - to avoid giving make a headache
210246149Ssjg#
211246149Ssjg.for t in CFLAGS CXXFLAGS
212246149Ssjg# avoid duplicates
213246149Ssjg__$t_incs:=${$t:M-I*:O:u}
214246149Ssjg.for i in ${__dpadd_incs}
215246149Ssjg.if "${__$t_incs:M$i}" == ""
216246149Ssjg$t+= $i
217246149Ssjg__$t_incs+= $i
218246149Ssjg.endif
219246149Ssjg.endfor
220246149Ssjg.endfor
221246149Ssjg
222300313Ssjg.for t in CFLAGS_LAST CXXFLAGS_LAST
223300313Ssjg# avoid duplicates
224300313Ssjg__$t_incs:=${$t:M-I*:u}
225300313Ssjg.for i in ${__dpadd_last_incs}
226300313Ssjg.if "${__$t_incs:M$i}" == ""
227300313Ssjg$t+= $i
228300313Ssjg__$t_incs+= $i
229300313Ssjg.endif
230300313Ssjg.endfor
231300313Ssjg.endfor
232300313Ssjg
233246149Ssjg# This target is used to gather a list of
234246149Ssjg# dir: ${DPADD}
235246149Ssjg# entries
236246149Ssjg.if make(*dpadd*)
237246149Ssjg.if !target(dpadd)
238246149Ssjgdpadd:	.NOTMAIN
239246149Ssjg.if defined(DPADD) && ${DPADD} != ""
240246149Ssjg	@echo "${RELDIR}: ${DPADD:S,${OBJTOP}/,,}"
241246149Ssjg.endif
242246149Ssjg.endif
243246149Ssjg.endif
244246149Ssjg
245246149Ssjg.ifdef SRC_PATHADD
246246149Ssjg# We don't want to assume that we need to .PATH every element of 
247246149Ssjg# SRC_LIBS, but the Makefile cannot do
248246149Ssjg# .PATH: ${SRC_libfoo}
249246149Ssjg# since the value of SRC_libfoo must be available at the time .PATH:
250246149Ssjg# is read - and we only just worked it out.  
251246149Ssjg# Further, they can't wait until after include of {lib,prog}.mk as 
252246149Ssjg# the .PATH is needed before then.
253246149Ssjg# So we let the Makefile do
254246149Ssjg# SRC_PATHADD+= ${SRC_libfoo}
255246149Ssjg# and we defer the .PATH: until now so that SRC_libfoo will be available.
256246149Ssjg.PATH: ${SRC_PATHADD}
257246149Ssjg.endif
258246149Ssjg
259300313Ssjg# after all that, if doing -n we don't care
260300313Ssjg.if ${.MAKEFLAGS:Ux:M-n} != ""
261300313SsjgDPADD =
262300313Ssjg.elif ${.MAKE.MODE:Mmeta*} != "" && exists(${.MAKE.DEPENDFILE})
263300313SsjgDPADD_CLEAR_DPADD ?= yes
264300313Ssjg.if ${DPADD_CLEAR_DPADD} == "yes"
265300313Ssjg# save this
266300313Ssjg__dpadd_libs := ${__dpadd_libs}
267300313Ssjg# we have made what use of it we can of DPADD
268300313SsjgDPADD =
269246149Ssjg.endif
270300313Ssjg.endif
271300313Ssjg
272300313Ssjg.endif
273