1331246Ssjg# $Id: dpadd.mk,v 1.26 2018/02/12 21:54:26 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
63331246Ssjg.if defined(PROG) && ${MK_PROG_LDORDER_MK:Uno} != "no"
64246149Ssjg# some libs have dependencies...
65246149Ssjg# DPLIBS_* allows bsd.libnames.mk to flag libs which must be included
66246149Ssjg# in DPADD for a given library.
67300313Ssjg# Gather all such dependencies into __ldadd_all_xtras
68300313Ssjg# dups will be dealt with later.
69300313Ssjg# Note: libfoo_pic uses DPLIBS_libfoo
70300313Ssjg__ldadd_all_xtras=
71300313Ssjg.for __lib in ${__dpadd_libs:@d@${DPLIBS_${d:T:R:S,_pic,,}}@}
72300313Ssjg__ldadd_all_xtras+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
73246149Ssjg.if "${DPADD:M${__lib}}" == ""
74246149SsjgDPADD+= ${__lib}
75246149Ssjg.endif
76246149Ssjg.endfor
77331246Ssjg.endif
78246149Ssjg# Last of all... for libc and libgcc
79246149SsjgDPADD+= ${DPADD_LAST}
80246149Ssjg
81300313Ssjg# de-dupuplicate __ldadd_all_xtras into __ldadd_xtras
82300313Ssjg# in reverse order so that libs end up listed after all that needed them.
83300313Ssjg__ldadd_xtras=
84300313Ssjg.for __lib in ${__ldadd_all_xtras:[-1..1]}
85300313Ssjg.if "${__ldadd_xtras:M${__lib}}" == "" || ${NEED_IMPLICIT_LDADD:tl:Uno} != "no"
86300313Ssjg__ldadd_xtras+= ${__lib}
87300313Ssjg.endif
88300313Ssjg.endfor
89300313Ssjg
90300313Ssjg.if !empty(__ldadd_xtras)
91300313Ssjg# now back to the original order
92300313Ssjg__ldadd_xtras:= ${__ldadd_xtras:[-1..1]}
93300313SsjgLDADD+= ${__ldadd_xtras}
94300313Ssjg.endif
95300313Ssjg
96246149Ssjg# Convert DPADD into -I and -L options and add them to CPPFLAGS and LDADD
97246149Ssjg# For the -I's convert the path to a relative one.  For separate objdirs
98246149Ssjg# the DPADD paths will be to the obj tree so we need to subst anyway.
99246149Ssjg
100300313Ssjg# update this
101300313Ssjg__dpadd_libs := ${DPADD:M*/lib*}
102246149Ssjg
103268437Ssjg# Order -L's to search ours first.
104246149Ssjg# Avoids picking up old versions already installed.
105301462Ssjg__dpadd_libdirs := ${__dpadd_libs:R:H:S/^/-L/g:O:u:N-L}
106246149SsjgLDADD += ${__dpadd_libdirs:M-L${OBJTOP}/*}
107300313SsjgLDADD += ${__dpadd_libdirs:N-L${OBJTOP}/*:N-L${HOST_LIBDIR:U/usr/lib}}
108300313Ssjg.if defined(HOST_LIBDIR) && ${HOST_LIBDIR} != "/usr/lib"
109300313SsjgLDADD+= -L${HOST_LIBDIR}
110246149Ssjg.endif
111246149Ssjg
112246149Ssjg.if !make(dpadd)
113246149Ssjg.ifdef LIB
114246149Ssjg# Each lib is its own src_lib, we want to include it in SRC_LIBS
115246149Ssjg# so that the correct INCLUDES_* will be picked up automatically.
116246149SsjgSRC_LIBS+= ${_OBJDIR}/lib${LIB}.a
117246149Ssjg.endif
118246149Ssjg.endif
119246149Ssjg
120246149Ssjg# 
121246149Ssjg# This little bit of magic, assumes that SRC_libfoo will be
122246149Ssjg# set if it cannot be correctly derrived from ${LIBFOO}
123246149Ssjg# Note that SRC_libfoo and INCLUDES_libfoo should be named for the
124268437Ssjg# actual library name not the variable name that might refer to it.
125246149Ssjg# 99% of the time the two are the same, but the DPADD logic
126268437Ssjg# only has the library name available, so stick to that.
127246149Ssjg# 
128246149Ssjg
129246149SsjgSRC_LIBS?=
130331246Ssjg# magic_libs includes those we want to link with
131331246Ssjg# as well as those we might look at
132331246Ssjg__dpadd_magic_libs += ${__dpadd_libs} ${SRC_LIBS}
133331246SsjgDPMAGIC_LIBS += ${__dpadd_magic_libs} \
134331246Ssjg	${__dpadd_magic_libs:@d@${DPMAGIC_LIBS_${d:T:R}}@}
135246149Ssjg
136300313Ssjg# we skip this for staged libs
137300313Ssjg.for __lib in ${DPMAGIC_LIBS:O:u:N${STAGE_OBJTOP:Unot}*/lib/*}
138246149Ssjg# 
139246149Ssjg# if SRC_libfoo is not set, then we assume that the srcdir corresponding
140246149Ssjg# to where we found the library is correct.
141246149Ssjg#
142246149SsjgSRC_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELSRCTOP},}
143246149Ssjg#
144246149Ssjg# This is a no-brainer but just to be complete...
145246149Ssjg#
146246149SsjgOBJ_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELOBJTOP},}
147246149Ssjg#
148246149Ssjg# If INCLUDES_libfoo is not set, then we'll use ${SRC_libfoo}/h if it exists,
149246149Ssjg# else just ${SRC_libfoo}.
150246149Ssjg#
151246149SsjgINCLUDES_${__lib:T:R}?= -I${exists(${SRC_${__lib:T:R}}/h):?${SRC_${__lib:T:R}}/h:${SRC_${__lib:T:R}}}
152246149Ssjg
153246149Ssjg.endfor
154246149Ssjg
155300313Ssjg# even for staged libs we sometimes 
156300313Ssjg# need to allow direct -I to avoid cicular dependencies 
157300313Ssjg.for __lib in ${DPMAGIC_LIBS:O:u:T:R}
158300313Ssjg.if !empty(SRC_${__lib}) && empty(INCLUDES_${__lib})
159300313Ssjg# must be a staged lib
160300313Ssjg.if exists(${SRC_${__lib}}/h)
161300313SsjgINCLUDES_${__lib} = -I${SRC_${__lib}}/h
162300313Ssjg.else
163300313SsjgINCLUDES_${__lib} = -I${SRC_${__lib}}
164300313Ssjg.endif
165300313Ssjg.endif
166300313Ssjg.endfor
167300313Ssjg
168300313Ssjg# when linking a shared lib, avoid non pic libs
169300313SsjgSHLDADD+= ${LDADD:N-[lL]*}
170300313Ssjg.for __lib in ${__dpadd_libs:u}
171300313Ssjg.if defined(SHLIB_NAME) && ${LDADD:M-l${__lib:T:R:S,lib,,}} != ""
172300313Ssjg.if ${__lib:T:N*_pic.a:N*.so} == "" || exists(${__lib:R}.so)
173300313SsjgSHLDADD+= -l${__lib:T:R:S,lib,,}
174300313Ssjg.elif exists(${__lib:R}_pic.a)
175300313SsjgSHLDADD+= -l${__lib:T:R:S,lib,,}_pic
176300313Ssjg.else
177300313Ssjg.warning ${RELDIR}.${TARGET_SPEC} needs ${__lib:T:R}_pic.a
178300313SsjgSHLDADD+= -l${__lib:T:R:S,lib,,}
179300313Ssjg.endif
180300313SsjgSHLDADD+= -L${__lib:H}
181300313Ssjg.endif
182300313Ssjg.endfor
183300313Ssjg
184246149Ssjg# Now for the bits we actually need
185246149Ssjg__dpadd_incs=
186246149Ssjg.for __lib in ${__dpadd_libs:u}
187246149Ssjg.if (make(${PROG}_p) || defined(NEED_GPROF)) && exists(${__lib:R}_p.a)
188246149Ssjg__ldadd=-l${__lib:T:R:S,lib,,}
189246149SsjgLDADD := ${LDADD:S,^${__ldadd}$,${__ldadd}_p,g}
190246149Ssjg.endif
191300313Ssjg.endfor
192246149Ssjg
193246149Ssjg#
194246149Ssjg# We take care of duplicate suppression later.
195300313Ssjg# don't apply :T:R too early
196331246Ssjg__dpadd_incs += ${__dpadd_magic_libs:u:@x@${INCLUDES_${x:T:R}}@}
197331246Ssjg__dpadd_incs += ${__dpadd_magic_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_${x:T:R}}@}
198246149Ssjg
199331246Ssjg__dpadd_last_incs += ${__dpadd_magic_libs:u:@x@${INCLUDES_LAST_${x:T:R}}@}
200331246Ssjg__dpadd_last_incs += ${__dpadd_magic_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_LAST_${x:T:R}}@}
201300313Ssjg
202331246Ssjg.if defined(HOSTPROG) || ${MACHINE:Nhost*} == ""
203300313Ssjg# we want any -I/usr/* last
204300313Ssjg__dpadd_last_incs := \
205300313Ssjg	${__dpadd_last_incs:N-I/usr/*} \
206300313Ssjg	${__dpadd_incs:M-I/usr/*} \
207300313Ssjg	${__dpadd_last_incs:M-I/usr/*} 
208300313Ssjg__dpadd_incs := ${__dpadd_incs:N-I/usr/*}
209300313Ssjg.endif
210300313Ssjg
211246149Ssjg#
212246149Ssjg# eliminate any duplicates - but don't mess with the order
213246149Ssjg# force evaluation now - to avoid giving make a headache
214246149Ssjg#
215246149Ssjg.for t in CFLAGS CXXFLAGS
216246149Ssjg# avoid duplicates
217246149Ssjg__$t_incs:=${$t:M-I*:O:u}
218246149Ssjg.for i in ${__dpadd_incs}
219246149Ssjg.if "${__$t_incs:M$i}" == ""
220246149Ssjg$t+= $i
221246149Ssjg__$t_incs+= $i
222246149Ssjg.endif
223246149Ssjg.endfor
224246149Ssjg.endfor
225246149Ssjg
226300313Ssjg.for t in CFLAGS_LAST CXXFLAGS_LAST
227300313Ssjg# avoid duplicates
228300313Ssjg__$t_incs:=${$t:M-I*:u}
229300313Ssjg.for i in ${__dpadd_last_incs}
230300313Ssjg.if "${__$t_incs:M$i}" == ""
231300313Ssjg$t+= $i
232300313Ssjg__$t_incs+= $i
233300313Ssjg.endif
234300313Ssjg.endfor
235300313Ssjg.endfor
236300313Ssjg
237246149Ssjg# This target is used to gather a list of
238246149Ssjg# dir: ${DPADD}
239246149Ssjg# entries
240246149Ssjg.if make(*dpadd*)
241246149Ssjg.if !target(dpadd)
242246149Ssjgdpadd:	.NOTMAIN
243246149Ssjg.if defined(DPADD) && ${DPADD} != ""
244246149Ssjg	@echo "${RELDIR}: ${DPADD:S,${OBJTOP}/,,}"
245246149Ssjg.endif
246246149Ssjg.endif
247246149Ssjg.endif
248246149Ssjg
249246149Ssjg.ifdef SRC_PATHADD
250246149Ssjg# We don't want to assume that we need to .PATH every element of 
251246149Ssjg# SRC_LIBS, but the Makefile cannot do
252246149Ssjg# .PATH: ${SRC_libfoo}
253246149Ssjg# since the value of SRC_libfoo must be available at the time .PATH:
254246149Ssjg# is read - and we only just worked it out.  
255246149Ssjg# Further, they can't wait until after include of {lib,prog}.mk as 
256246149Ssjg# the .PATH is needed before then.
257246149Ssjg# So we let the Makefile do
258246149Ssjg# SRC_PATHADD+= ${SRC_libfoo}
259246149Ssjg# and we defer the .PATH: until now so that SRC_libfoo will be available.
260246149Ssjg.PATH: ${SRC_PATHADD}
261246149Ssjg.endif
262246149Ssjg
263300313Ssjg# after all that, if doing -n we don't care
264300313Ssjg.if ${.MAKEFLAGS:Ux:M-n} != ""
265300313SsjgDPADD =
266300313Ssjg.elif ${.MAKE.MODE:Mmeta*} != "" && exists(${.MAKE.DEPENDFILE})
267300313SsjgDPADD_CLEAR_DPADD ?= yes
268300313Ssjg.if ${DPADD_CLEAR_DPADD} == "yes"
269300313Ssjg# save this
270300313Ssjg__dpadd_libs := ${__dpadd_libs}
271300313Ssjg# we have made what use of it we can of DPADD
272300313SsjgDPADD =
273246149Ssjg.endif
274300313Ssjg.endif
275300313Ssjg
276300313Ssjg.endif
277