Update to bmake-20131001 on the vendor branch
[dragonfly.git] / contrib / bmake / mk / dpadd.mk
1 # $Id: dpadd.mk,v 1.18 2011/11/10 05:13:37 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 # DPLIBS helps us ensure we keep DPADD and LDADD in sync
25 DPLIBS+= ${DPLIBS_LAST}
26 DPADD+= ${DPLIBS}
27 .for __lib in ${DPLIBS:T:R}
28 LDADD+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
29 .endfor
30
31 # DPADD can contain things other than libs
32 __dpadd_libs = ${DPADD:M*/lib*}
33
34 # some libs have dependencies...
35 # DPLIBS_* allows bsd.libnames.mk to flag libs which must be included
36 # in DPADD for a given library.
37 .for __lib in ${__dpadd_libs:@d@${DPLIBS_${d:T:R}}@}
38 .if "${DPADD:M${__lib}}" == ""
39 DPADD+= ${__lib}
40 LDADD+= ${LDADD_${__lib}:U${__lib:T:R:S/lib/-l/:C/\.so.*//}}
41 .endif
42 .endfor
43 # Last of all... for libc and libgcc
44 DPADD+= ${DPADD_LAST}
45
46 # Convert DPADD into -I and -L options and add them to CPPFLAGS and LDADD
47 # For the -I's convert the path to a relative one.  For separate objdirs
48 # the DPADD paths will be to the obj tree so we need to subst anyway.
49
50 # If USE_PROFILE is yes, then check for profiled versions of libs
51 # and use them.
52
53 USE_PROFILE?=no
54 .if defined(LIBDL) && exists(${LIBDL})
55 .if defined(PROG) && (make(${PROG}_p) || ${USE_PROFILE} == yes) && \
56         defined(LDFLAGS) && ${LDFLAGS:M-export-dynamic}
57 # building profiled version of a prog that needs dlopen to work
58 DPLIBS+= ${LIBDL}
59 .endif
60 .endif
61
62 .if defined(LIBDMALLOC) && exists(${LIBDMALLOC})
63 .if defined(USE_DMALLOC) && ${USE_DMALLOC} != no
64 .if !defined(NO_DMALLOC)
65 CPPFLAGS+= -DUSE_DMALLOC
66 .endif
67 DPLIBS+= ${LIBDMALLOC}
68 .endif
69 .endif
70
71 # Order -L's to seach ours first.
72 # Avoids picking up old versions already installed.
73 __dpadd_libdirs := ${__dpadd_libs:R:H:S/^/-L/g:O:u:N-L}
74 LDADD += ${__dpadd_libdirs:M-L${OBJTOP}/*}
75 LDADD += ${__dpadd_libdirs:N-L${OBJTOP}/*}
76
77 .if ${.CURDIR} == ${SRCTOP}
78 RELDIR=.
79 RELTOP=.
80 .else
81 RELDIR?= ${.CURDIR:S,${SRCTOP}/,,}
82 .if ${RELDIR} == ${.CURDIR}
83 RELDIR?= ${.OBJDIR:S,${OBJTOP}/,,}
84 .endif
85 RELTOP?= ${RELDIR:C,[^/]+,..,g}
86 .endif
87 RELOBJTOP?= ${OBJTOP}
88 RELSRCTOP?= ${SRCTOP}
89
90 .if !make(dpadd)
91 .ifdef LIB
92 # Each lib is its own src_lib, we want to include it in SRC_LIBS
93 # so that the correct INCLUDES_* will be picked up automatically.
94 SRC_LIBS+= ${_OBJDIR}/lib${LIB}.a
95 .endif
96 .endif
97
98
99 # This little bit of magic, assumes that SRC_libfoo will be
100 # set if it cannot be correctly derrived from ${LIBFOO}
101 # Note that SRC_libfoo and INCLUDES_libfoo should be named for the
102 # actual libary name not the variable name that might refer to it.
103 # 99% of the time the two are the same, but the DPADD logic
104 # only has the libary name available, so stick to that.
105
106
107 SRC_LIBS?=
108 __dpadd_libs += ${SRC_LIBS}
109 DPMAGIC_LIBS += ${__dpadd_libs} \
110         ${__dpadd_libs:@d@${DPMAGIC_LIBS_${d:T:R}}@}
111
112 .for __lib in ${DPMAGIC_LIBS:O:u}
113
114 # if SRC_libfoo is not set, then we assume that the srcdir corresponding
115 # to where we found the library is correct.
116 #
117 SRC_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELSRCTOP},}
118 #
119 # This is a no-brainer but just to be complete...
120 #
121 OBJ_${__lib:T:R} ?= ${__lib:H:S,${OBJTOP},${RELOBJTOP},}
122 #
123 # If INCLUDES_libfoo is not set, then we'll use ${SRC_libfoo}/h if it exists,
124 # else just ${SRC_libfoo}.
125 #
126 INCLUDES_${__lib:T:R}?= -I${exists(${SRC_${__lib:T:R}}/h):?${SRC_${__lib:T:R}}/h:${SRC_${__lib:T:R}}}
127
128 .endfor
129
130 # Now for the bits we actually need
131 __dpadd_incs=
132 .for __lib in ${__dpadd_libs:u}
133 .if (make(${PROG}_p) || defined(NEED_GPROF)) && exists(${__lib:R}_p.a)
134 __ldadd=-l${__lib:T:R:S,lib,,}
135 LDADD := ${LDADD:S,^${__ldadd}$,${__ldadd}_p,g}
136 .endif
137
138 #
139 # Some libs generate headers, so we potentially need both
140 # the src dir and the obj dir.
141 # If ${INCLUDES_libfoo} contains a word ending in /h, we assume that either
142 # 1. it does not generate headers or
143 # 2. INCLUDES_libfoo will have been set correctly
144 # XXX it gets ugly avoiding duplicates... 
145 # use :? to ensure .for does not prevent correct evaluation
146 #
147 # We take care of duplicate suppression later.
148 __dpadd_incs += ${"${INCLUDES_${__lib:T:R}:M*/h}":? :-I${OBJ_${__lib:T:R}}}
149 __dpadd_incs += ${INCLUDES_${__lib:T:R}}
150 .endfor
151
152 #
153 # eliminate any duplicates - but don't mess with the order
154 # force evaluation now - to avoid giving make a headache
155 #
156 .for t in CFLAGS CXXFLAGS
157 # avoid duplicates
158 __$t_incs:=${$t:M-I*:O:u}
159 .for i in ${__dpadd_incs}
160 .if "${__$t_incs:M$i}" == ""
161 $t+= $i
162 __$t_incs+= $i
163 .endif
164 .endfor
165 .endfor
166
167 # This target is used to gather a list of
168 # dir: ${DPADD}
169 # entries
170 .if make(*dpadd*)
171 # allow overrides
172 .-include "dpadd++.mk"
173
174 .if !target(dpadd)
175 dpadd:  .NOTMAIN
176 .if defined(DPADD) && ${DPADD} != ""
177         @echo "${RELDIR}: ${DPADD:S,${OBJTOP}/,,}"
178 .endif
179 .endif
180 .endif
181
182 .ifdef SRC_PATHADD
183 # We don't want to assume that we need to .PATH every element of 
184 # SRC_LIBS, but the Makefile cannot do
185 # .PATH: ${SRC_libfoo}
186 # since the value of SRC_libfoo must be available at the time .PATH:
187 # is read - and we only just worked it out.  
188 # Further, they can't wait until after include of {lib,prog}.mk as 
189 # the .PATH is needed before then.
190 # So we let the Makefile do
191 # SRC_PATHADD+= ${SRC_libfoo}
192 # and we defer the .PATH: until now so that SRC_libfoo will be available.
193 .PATH: ${SRC_PATHADD}
194 .endif
195
196 .endif