1 # $Id: dpadd.mk,v 1.26 2018/02/12 21:54:26 sjg Exp $
3 # @(#) Copyright (c) 2004, Simon J. Gerraty
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
12 # Please send copies of changes and bug-fixes to:
16 .if !target(__${.PARSEFILE}__)
19 # sometimes we play games with .CURDIR etc
20 # _* hold the original values of .*
24 .if ${_CURDIR} == ${SRCTOP}
28 RELDIR?= ${_CURDIR:S,${SRCTOP}/,,}
29 .if ${RELDIR} == ${_CURDIR}
30 RELDIR?= ${_OBJDIR:S,${OBJTOP}/,,}
32 RELTOP?= ${RELDIR:C,[^/]+,..,g}
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
41 CFLAGS_LAST += ${CFLAGS_DEBUG_XTRA}
43 .ifdef CXXFLAGS_DEBUG_XTRA
44 CXXFLAGS_LAST += ${CXXFLAGS_DEBUG_XTRA}
47 .-include <local.dpadd.mk>
49 # DPLIBS helps us ensure we keep DPADD and LDADD in sync
50 DPLIBS+= ${DPLIBS_LAST}
52 .for __lib in ${DPLIBS}
53 .if "${_lib:M-*}" != ""
56 LDADD += ${LDADD_${__lib:T:R}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
60 # DPADD can contain things other than libs
61 __dpadd_libs := ${DPADD:M*/lib*}
63 .if defined(PROG) && ${MK_PROG_LDORDER_MK:Uno} != "no"
64 # some libs have dependencies...
65 # DPLIBS_* allows bsd.libnames.mk to flag libs which must be included
66 # in DPADD for a given library.
67 # Gather all such dependencies into __ldadd_all_xtras
68 # dups will be dealt with later.
69 # Note: libfoo_pic uses DPLIBS_libfoo
71 .for __lib in ${__dpadd_libs:@d@${DPLIBS_${d:T:R:S,_pic,,}}@}
72 __ldadd_all_xtras+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
73 .if "${DPADD:M${__lib}}" == ""
78 # Last of all... for libc and libgcc
81 # de-dupuplicate __ldadd_all_xtras into __ldadd_xtras
82 # in reverse order so that libs end up listed after all that needed them.
84 .for __lib in ${__ldadd_all_xtras:[-1..1]}
85 .if "${__ldadd_xtras:M${__lib}}" == "" || ${NEED_IMPLICIT_LDADD:tl:Uno} != "no"
86 __ldadd_xtras+= ${__lib}
90 .if !empty(__ldadd_xtras)
91 # now back to the original order
92 __ldadd_xtras:= ${__ldadd_xtras:[-1..1]}
93 LDADD+= ${__ldadd_xtras}
96 # Convert DPADD into -I and -L options and add them to CPPFLAGS and LDADD
97 # For the -I's convert the path to a relative one. For separate objdirs
98 # the DPADD paths will be to the obj tree so we need to subst anyway.
101 __dpadd_libs := ${DPADD:M*/lib*}
103 # Order -L's to search ours first.
104 # Avoids picking up old versions already installed.
105 __dpadd_libdirs := ${__dpadd_libs:R:H:S/^/-L/g:O:u:N-L}
106 LDADD += ${__dpadd_libdirs:M-L${OBJTOP}/*}
107 LDADD += ${__dpadd_libdirs:N-L${OBJTOP}/*:N-L${HOST_LIBDIR:U/usr/lib}}
108 .if defined(HOST_LIBDIR) && ${HOST_LIBDIR} != "/usr/lib"
109 LDADD+= -L${HOST_LIBDIR}
114 # Each lib is its own src_lib, we want to include it in SRC_LIBS
115 # so that the correct INCLUDES_* will be picked up automatically.
116 SRC_LIBS+= ${_OBJDIR}/lib${LIB}.a
121 # This little bit of magic, assumes that SRC_libfoo will be
122 # set if it cannot be correctly derrived from ${LIBFOO}
123 # Note that SRC_libfoo and INCLUDES_libfoo should be named for the
124 # actual library name not the variable name that might refer to it.
125 # 99% of the time the two are the same, but the DPADD logic
126 # only has the library name available, so stick to that.
130 # magic_libs includes those we want to link with
131 # as well as those we might look at
132 __dpadd_magic_libs += ${__dpadd_libs} ${SRC_LIBS}
133 DPMAGIC_LIBS += ${__dpadd_magic_libs} \
134 ${__dpadd_magic_libs:@d@${DPMAGIC_LIBS_${d:T:R}}@}
136 # we skip this for staged libs
137 .for __lib in ${DPMAGIC_LIBS:O:u:N${STAGE_OBJTOP:Unot}*/lib/*}
139 # if SRC_libfoo is not set, then we assume that the srcdir corresponding
140 # to where we found the library is correct.
142 SRC_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELSRCTOP},}
144 # This is a no-brainer but just to be complete...
146 OBJ_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELOBJTOP},}
148 # If INCLUDES_libfoo is not set, then we'll use ${SRC_libfoo}/h if it exists,
149 # else just ${SRC_libfoo}.
151 INCLUDES_${__lib:T:R}?= -I${exists(${SRC_${__lib:T:R}}/h):?${SRC_${__lib:T:R}}/h:${SRC_${__lib:T:R}}}
155 # even for staged libs we sometimes
156 # need to allow direct -I to avoid cicular dependencies
157 .for __lib in ${DPMAGIC_LIBS:O:u:T:R}
158 .if !empty(SRC_${__lib}) && empty(INCLUDES_${__lib})
159 # must be a staged lib
160 .if exists(${SRC_${__lib}}/h)
161 INCLUDES_${__lib} = -I${SRC_${__lib}}/h
163 INCLUDES_${__lib} = -I${SRC_${__lib}}
168 # when linking a shared lib, avoid non pic libs
169 SHLDADD+= ${LDADD:N-[lL]*}
170 .for __lib in ${__dpadd_libs:u}
171 .if defined(SHLIB_NAME) && ${LDADD:M-l${__lib:T:R:S,lib,,}} != ""
172 .if ${__lib:T:N*_pic.a:N*.so} == "" || exists(${__lib:R}.so)
173 SHLDADD+= -l${__lib:T:R:S,lib,,}
174 .elif exists(${__lib:R}_pic.a)
175 SHLDADD+= -l${__lib:T:R:S,lib,,}_pic
177 .warning ${RELDIR}.${TARGET_SPEC} needs ${__lib:T:R}_pic.a
178 SHLDADD+= -l${__lib:T:R:S,lib,,}
180 SHLDADD+= -L${__lib:H}
184 # Now for the bits we actually need
186 .for __lib in ${__dpadd_libs:u}
187 .if (make(${PROG}_p) || defined(NEED_GPROF)) && exists(${__lib:R}_p.a)
188 __ldadd=-l${__lib:T:R:S,lib,,}
189 LDADD := ${LDADD:S,^${__ldadd}$,${__ldadd}_p,g}
194 # We take care of duplicate suppression later.
195 # don't apply :T:R too early
196 __dpadd_incs += ${__dpadd_magic_libs:u:@x@${INCLUDES_${x:T:R}}@}
197 __dpadd_incs += ${__dpadd_magic_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_${x:T:R}}@}
199 __dpadd_last_incs += ${__dpadd_magic_libs:u:@x@${INCLUDES_LAST_${x:T:R}}@}
200 __dpadd_last_incs += ${__dpadd_magic_libs:O:u:@s@${SRC_LIBS_${s:T:R}:U}@:@x@${INCLUDES_LAST_${x:T:R}}@}
202 .if defined(HOSTPROG) || ${MACHINE:Nhost*} == ""
203 # we want any -I/usr/* last
204 __dpadd_last_incs := \
205 ${__dpadd_last_incs:N-I/usr/*} \
206 ${__dpadd_incs:M-I/usr/*} \
207 ${__dpadd_last_incs:M-I/usr/*}
208 __dpadd_incs := ${__dpadd_incs:N-I/usr/*}
212 # eliminate any duplicates - but don't mess with the order
213 # force evaluation now - to avoid giving make a headache
215 .for t in CFLAGS CXXFLAGS
217 __$t_incs:=${$t:M-I*:O:u}
218 .for i in ${__dpadd_incs}
219 .if "${__$t_incs:M$i}" == ""
226 .for t in CFLAGS_LAST CXXFLAGS_LAST
228 __$t_incs:=${$t:M-I*:u}
229 .for i in ${__dpadd_last_incs}
230 .if "${__$t_incs:M$i}" == ""
237 # This target is used to gather a list of
243 .if defined(DPADD) && ${DPADD} != ""
244 @echo "${RELDIR}: ${DPADD:S,${OBJTOP}/,,}"
250 # We don't want to assume that we need to .PATH every element of
251 # SRC_LIBS, but the Makefile cannot do
252 # .PATH: ${SRC_libfoo}
253 # since the value of SRC_libfoo must be available at the time .PATH:
254 # is read - and we only just worked it out.
255 # Further, they can't wait until after include of {lib,prog}.mk as
256 # the .PATH is needed before then.
257 # So we let the Makefile do
258 # SRC_PATHADD+= ${SRC_libfoo}
259 # and we defer the .PATH: until now so that SRC_libfoo will be available.
260 .PATH: ${SRC_PATHADD}
263 # after all that, if doing -n we don't care
264 .if ${.MAKEFLAGS:Ux:M-n} != ""
266 .elif ${.MAKE.MODE:Mmeta*} != "" && exists(${.MAKE.DEPENDFILE})
267 DPADD_CLEAR_DPADD ?= yes
268 .if ${DPADD_CLEAR_DPADD} == "yes"
270 __dpadd_libs := ${__dpadd_libs}
271 # we have made what use of it we can of DPADD