xref: /freebsd/contrib/bmake/mk/dpadd.mk (revision b70d2a2aa5003027b422e62435ab5bb9390d543c)
1# $Id: dpadd.mk,v 1.23 2017/02/13 16:46:01 sjg Exp $
2#
3#	@(#) Copyright (c) 2004, Simon J. Gerraty
4#
5#	This file is provided in the hope that it will
6#	be of use.  There is absolutely NO WARRANTY.
7#	Permission to copy, redistribute or otherwise
8#	use this file is hereby granted provided that
9#	the above copyright notice and this notice are
10#	left intact.
11#
12#	Please send copies of changes and bug-fixes to:
13#	sjg@crufty.net
14#
15
16.if !target(__${.PARSEFILE}__)
17__${.PARSEFILE}__:
18
19# sometimes we play games with .CURDIR etc
20# _* hold the original values of .*
21_OBJDIR?= ${.OBJDIR}
22_CURDIR?= ${.CURDIR}
23
24.if ${_CURDIR} == ${SRCTOP}
25RELDIR=.
26RELTOP=.
27.else
28RELDIR?= ${_CURDIR:S,${SRCTOP}/,,}
29.if ${RELDIR} == ${_CURDIR}
30RELDIR?= ${_OBJDIR:S,${OBJTOP}/,,}
31.endif
32RELTOP?= ${RELDIR:C,[^/]+,..,g}
33.endif
34RELOBJTOP?= ${OBJTOP}
35RELSRCTOP?= ${SRCTOP}
36
37# we get included just about everywhere so this is handy...
38# C*DEBUG_XTRA are for defining on cmd line etc
39# so do not use in makefiles.
40.ifdef CFLAGS_DEBUG_XTRA
41CFLAGS_LAST += ${CFLAGS_DEBUG_XTRA}
42.endif
43.ifdef CXXFLAGS_DEBUG_XTRA
44CXXFLAGS_LAST += ${CXXFLAGS_DEBUG_XTRA}
45.endif
46
47.-include <local.dpadd.mk>
48
49# DPLIBS helps us ensure we keep DPADD and LDADD in sync
50DPLIBS+= ${DPLIBS_LAST}
51DPADD+= ${DPLIBS:N-*}
52.for __lib in ${DPLIBS}
53.if "${_lib:M-*}" != ""
54LDADD += ${__lib}
55.else
56LDADD += ${LDADD_${__lib:T:R}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
57.endif
58.endfor
59
60# DPADD can contain things other than libs
61__dpadd_libs := ${DPADD:M*/lib*}
62
63# some libs have dependencies...
64# DPLIBS_* allows bsd.libnames.mk to flag libs which must be included
65# in DPADD for a given library.
66# Gather all such dependencies into __ldadd_all_xtras
67# dups will be dealt with later.
68# Note: libfoo_pic uses DPLIBS_libfoo
69__ldadd_all_xtras=
70.for __lib in ${__dpadd_libs:@d@${DPLIBS_${d:T:R:S,_pic,,}}@}
71__ldadd_all_xtras+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
72.if "${DPADD:M${__lib}}" == ""
73DPADD+= ${__lib}
74.endif
75.endfor
76# Last of all... for libc and libgcc
77DPADD+= ${DPADD_LAST}
78
79# de-dupuplicate __ldadd_all_xtras into __ldadd_xtras
80# in reverse order so that libs end up listed after all that needed them.
81__ldadd_xtras=
82.for __lib in ${__ldadd_all_xtras:[-1..1]}
83.if "${__ldadd_xtras:M${__lib}}" == "" || ${NEED_IMPLICIT_LDADD:tl:Uno} != "no"
84__ldadd_xtras+= ${__lib}
85.endif
86.endfor
87
88.if !empty(__ldadd_xtras)
89# now back to the original order
90__ldadd_xtras:= ${__ldadd_xtras:[-1..1]}
91LDADD+= ${__ldadd_xtras}
92.endif
93
94# Convert DPADD into -I and -L options and add them to CPPFLAGS and LDADD
95# For the -I's convert the path to a relative one.  For separate objdirs
96# the DPADD paths will be to the obj tree so we need to subst anyway.
97
98# update this
99__dpadd_libs := ${DPADD:M*/lib*}
100
101# Order -L's to search ours first.
102# Avoids picking up old versions already installed.
103__dpadd_libdirs := ${__dpadd_libs:R:H:S/^/-L/g:O:u:N-L}
104LDADD += ${__dpadd_libdirs:M-L${OBJTOP}/*}
105LDADD += ${__dpadd_libdirs:N-L${OBJTOP}/*:N-L${HOST_LIBDIR:U/usr/lib}}
106.if defined(HOST_LIBDIR) && ${HOST_LIBDIR} != "/usr/lib"
107LDADD+= -L${HOST_LIBDIR}
108.endif
109
110.if !make(dpadd)
111.ifdef LIB
112# Each lib is its own src_lib, we want to include it in SRC_LIBS
113# so that the correct INCLUDES_* will be picked up automatically.
114SRC_LIBS+= ${_OBJDIR}/lib${LIB}.a
115.endif
116.endif
117
118#
119# This little bit of magic, assumes that SRC_libfoo will be
120# set if it cannot be correctly derrived from ${LIBFOO}
121# Note that SRC_libfoo and INCLUDES_libfoo should be named for the
122# actual library name not the variable name that might refer to it.
123# 99% of the time the two are the same, but the DPADD logic
124# only has the library name available, so stick to that.
125#
126
127SRC_LIBS?=
128__dpadd_libs += ${SRC_LIBS}
129DPMAGIC_LIBS += ${__dpadd_libs} \
130	${__dpadd_libs:@d@${DPMAGIC_LIBS_${d:T:R}}@}
131
132# we skip this for staged libs
133.for __lib in ${DPMAGIC_LIBS:O:u:N${STAGE_OBJTOP:Unot}*/lib/*}
134#
135# if SRC_libfoo is not set, then we assume that the srcdir corresponding
136# to where we found the library is correct.
137#
138SRC_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELSRCTOP},}
139#
140# This is a no-brainer but just to be complete...
141#
142OBJ_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELOBJTOP},}
143#
144# If INCLUDES_libfoo is not set, then we'll use ${SRC_libfoo}/h if it exists,
145# else just ${SRC_libfoo}.
146#
147INCLUDES_${__lib:T:R}?= -I${exists(${SRC_${__lib:T:R}}/h):?${SRC_${__lib:T:R}}/h:${SRC_${__lib:T:R}}}
148
149.endfor
150
151# even for staged libs we sometimes
152# need to allow direct -I to avoid cicular dependencies
153.for __lib in ${DPMAGIC_LIBS:O:u:T:R}
154.if !empty(SRC_${__lib}) && empty(INCLUDES_${__lib})
155# must be a staged lib
156.if exists(${SRC_${__lib}}/h)
157INCLUDES_${__lib} = -I${SRC_${__lib}}/h
158.else
159INCLUDES_${__lib} = -I${SRC_${__lib}}
160.endif
161.endif
162.endfor
163
164# when linking a shared lib, avoid non pic libs
165SHLDADD+= ${LDADD:N-[lL]*}
166.for __lib in ${__dpadd_libs:u}
167.if defined(SHLIB_NAME) && ${LDADD:M-l${__lib:T:R:S,lib,,}} != ""
168.if ${__lib:T:N*_pic.a:N*.so} == "" || exists(${__lib:R}.so)
169SHLDADD+= -l${__lib:T:R:S,lib,,}
170.elif exists(${__lib:R}_pic.a)
171SHLDADD+= -l${__lib:T:R:S,lib,,}_pic
172.else
173.warning ${RELDIR}.${TARGET_SPEC} needs ${__lib:T:R}_pic.a
174SHLDADD+= -l${__lib:T:R:S,lib,,}
175.endif
176SHLDADD+= -L${__lib:H}
177.endif
178.endfor
179
180# Now for the bits we actually need
181__dpadd_incs=
182.for __lib in ${__dpadd_libs:u}
183.if (make(${PROG}_p) || defined(NEED_GPROF)) && exists(${__lib:R}_p.a)
184__ldadd=-l${__lib:T:R:S,lib,,}
185LDADD := ${LDADD:S,^${__ldadd}$,${__ldadd}_p,g}
186.endif
187.endfor
188
189#
190# We take care of duplicate suppression later.
191# don't apply :T:R too early
192__dpadd_incs += ${__dpadd_libs:u:@x@${INCLUDES_${x:T:R}}@}
193__dpadd_incs += ${__dpadd_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_${x:T:R}}@}
194
195__dpadd_last_incs += ${__dpadd_libs:u:@x@${INCLUDES_LAST_${x:T:R}}@}
196__dpadd_last_incs += ${__dpadd_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_LAST_${x:T:R}}@}
197
198.if defined(HOSTPROG) || ${MACHINE} == "host"
199# we want any -I/usr/* last
200__dpadd_last_incs := \
201	${__dpadd_last_incs:N-I/usr/*} \
202	${__dpadd_incs:M-I/usr/*} \
203	${__dpadd_last_incs:M-I/usr/*}
204__dpadd_incs := ${__dpadd_incs:N-I/usr/*}
205.endif
206
207#
208# eliminate any duplicates - but don't mess with the order
209# force evaluation now - to avoid giving make a headache
210#
211.for t in CFLAGS CXXFLAGS
212# avoid duplicates
213__$t_incs:=${$t:M-I*:O:u}
214.for i in ${__dpadd_incs}
215.if "${__$t_incs:M$i}" == ""
216$t+= $i
217__$t_incs+= $i
218.endif
219.endfor
220.endfor
221
222.for t in CFLAGS_LAST CXXFLAGS_LAST
223# avoid duplicates
224__$t_incs:=${$t:M-I*:u}
225.for i in ${__dpadd_last_incs}
226.if "${__$t_incs:M$i}" == ""
227$t+= $i
228__$t_incs+= $i
229.endif
230.endfor
231.endfor
232
233# This target is used to gather a list of
234# dir: ${DPADD}
235# entries
236.if make(*dpadd*)
237.if !target(dpadd)
238dpadd:	.NOTMAIN
239.if defined(DPADD) && ${DPADD} != ""
240	@echo "${RELDIR}: ${DPADD:S,${OBJTOP}/,,}"
241.endif
242.endif
243.endif
244
245.ifdef SRC_PATHADD
246# We don't want to assume that we need to .PATH every element of
247# SRC_LIBS, but the Makefile cannot do
248# .PATH: ${SRC_libfoo}
249# since the value of SRC_libfoo must be available at the time .PATH:
250# is read - and we only just worked it out.
251# Further, they can't wait until after include of {lib,prog}.mk as
252# the .PATH is needed before then.
253# So we let the Makefile do
254# SRC_PATHADD+= ${SRC_libfoo}
255# and we defer the .PATH: until now so that SRC_libfoo will be available.
256.PATH: ${SRC_PATHADD}
257.endif
258
259# after all that, if doing -n we don't care
260.if ${.MAKEFLAGS:Ux:M-n} != ""
261DPADD =
262.elif ${.MAKE.MODE:Mmeta*} != "" && exists(${.MAKE.DEPENDFILE})
263DPADD_CLEAR_DPADD ?= yes
264.if ${DPADD_CLEAR_DPADD} == "yes"
265# save this
266__dpadd_libs := ${__dpadd_libs}
267# we have made what use of it we can of DPADD
268DPADD =
269.endif
270.endif
271
272.endif
273