]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - share/mk/bsd.lib.mk
MFC r272055:
[FreeBSD/stable/10.git] / share / mk / bsd.lib.mk
1 #       from: @(#)bsd.lib.mk    5.26 (Berkeley) 5/2/91
2 # $FreeBSD$
3 #
4
5 .include <bsd.init.mk>
6
7 # Set up the variables controlling shared libraries.  After this section,
8 # SHLIB_NAME will be defined only if we are to create a shared library.
9 # SHLIB_LINK will be defined only if we are to create a link to it.
10 # INSTALL_PIC_ARCHIVE will be defined only if we are to create a PIC archive.
11 .if defined(NO_PIC)
12 .undef SHLIB_NAME
13 .undef INSTALL_PIC_ARCHIVE
14 .else
15 .if !defined(SHLIB) && defined(LIB)
16 SHLIB=          ${LIB}
17 .endif
18 .if !defined(SHLIB_NAME) && defined(SHLIB) && defined(SHLIB_MAJOR)
19 SHLIB_NAME=     lib${SHLIB}.so.${SHLIB_MAJOR}
20 .endif
21 .if defined(SHLIB_NAME) && !empty(SHLIB_NAME:M*.so.*)
22 SHLIB_LINK?=    ${SHLIB_NAME:R}
23 .endif
24 SONAME?=        ${SHLIB_NAME}
25 .endif
26
27 .if defined(CRUNCH_CFLAGS)
28 CFLAGS+=        ${CRUNCH_CFLAGS}
29 .endif
30
31 .if ${MK_ASSERT_DEBUG} == "no"
32 CFLAGS+= -DNDEBUG
33 NO_WERROR=
34 .endif
35
36 .if defined(DEBUG_FLAGS)
37 CFLAGS+= ${DEBUG_FLAGS}
38
39 .if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
40 CTFFLAGS+= -g
41 .endif
42 .else
43 STRIP?= -s
44 .endif
45
46 .if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
47     empty(DEBUG_FLAGS:M-gdwarf*)
48 CFLAGS+= -g
49 CTFFLAGS+= -g
50 .endif
51
52 .include <bsd.libnames.mk>
53
54 # prefer .s to a .c, add .po, remove stuff not used in the BSD libraries
55 # .So used for PIC object files
56 .SUFFIXES:
57 .SUFFIXES: .out .o .po .So .S .asm .s .c .cc .cpp .cxx .C .f .y .l .ln
58
59 .if !defined(PICFLAG)
60 .if ${MACHINE_CPUARCH} == "sparc64"
61 PICFLAG=-fPIC
62 .else
63 PICFLAG=-fpic
64 .endif
65 .endif
66
67 PO_FLAG=-pg
68
69 .c.o:
70         ${CC} ${STATIC_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
71         ${CTFCONVERT_CMD}
72
73 .c.po:
74         ${CC} ${PO_FLAG} ${STATIC_CFLAGS} ${PO_CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
75         ${CTFCONVERT_CMD}
76
77 .c.So:
78         ${CC} ${PICFLAG} -DPIC ${SHARED_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
79         ${CTFCONVERT_CMD}
80
81 .cc.o .C.o .cpp.o .cxx.o:
82         ${CXX} ${STATIC_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
83
84 .cc.po .C.po .cpp.po .cxx.po:
85         ${CXX} ${PO_FLAG} ${STATIC_CXXFLAGS} ${PO_CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
86
87 .cc.So .C.So .cpp.So .cxx.So:
88         ${CXX} ${PICFLAG} -DPIC ${SHARED_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
89
90 .f.po:
91         ${FC} -pg ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
92         ${CTFCONVERT_CMD}
93
94 .f.So:
95         ${FC} ${PICFLAG} -DPIC ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
96         ${CTFCONVERT_CMD}
97
98 .s.po .s.So:
99         ${AS} ${AFLAGS} -o ${.TARGET} ${.IMPSRC}
100         ${CTFCONVERT_CMD}
101
102 .asm.po:
103         ${CC} -x assembler-with-cpp -DPROF ${PO_CFLAGS} ${ACFLAGS} \
104                 -c ${.IMPSRC} -o ${.TARGET}
105         ${CTFCONVERT_CMD}
106
107 .asm.So:
108         ${CC} -x assembler-with-cpp ${PICFLAG} -DPIC ${CFLAGS} ${ACFLAGS} \
109             -c ${.IMPSRC} -o ${.TARGET}
110         ${CTFCONVERT_CMD}
111
112 .S.po:
113         ${CC} -DPROF ${PO_CFLAGS} ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
114         ${CTFCONVERT_CMD}
115
116 .S.So:
117         ${CC} ${PICFLAG} -DPIC ${CFLAGS} ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
118         ${CTFCONVERT_CMD}
119
120 all: beforebuild .WAIT
121 beforebuild: objwarn
122
123 .if defined(PRIVATELIB)
124 _LIBDIR:=${LIBPRIVATEDIR}
125 _SHLIBDIR:=${LIBPRIVATEDIR}
126 .else
127 _LIBDIR:=${LIBDIR}
128 _SHLIBDIR:=${SHLIBDIR}
129 .endif
130
131 .if defined(SHLIB_NAME)
132 .if ${MK_DEBUG_FILES} != "no"
133 SHLIB_NAME_FULL=${SHLIB_NAME}.full
134 # Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
135 .if ${_SHLIBDIR} == "/boot" ||\
136     ${SHLIBDIR:C%/lib(/.*)?$%/lib%} == "/lib" ||\
137     ${SHLIBDIR:C%/usr/lib(32)?(/.*)?%/usr/lib%} == "/usr/lib"
138 DEBUGFILEDIR=${DEBUGDIR}${_SHLIBDIR}
139 .else
140 DEBUGFILEDIR=${_SHLIBDIR}/.debug
141 DEBUGMKDIR=
142 .endif
143 .else
144 SHLIB_NAME_FULL=${SHLIB_NAME}
145 .endif
146 .endif
147
148 .include <bsd.symver.mk>
149
150 # Allow libraries to specify their own version map or have it
151 # automatically generated (see bsd.symver.mk above).
152 .if ${MK_SYMVER} == "yes" && !empty(VERSION_MAP)
153 ${SHLIB_NAME_FULL}:     ${VERSION_MAP}
154 LDFLAGS+=       -Wl,--version-script=${VERSION_MAP}
155 .endif
156
157 .if defined(USEPRIVATELIB)
158 LDFLAGS+= -L${_SHLIBDIRPREFIX}${LIBPRIVATEDIR} -rpath ${LIBPRIVATEDIR}
159 .endif
160
161 .if defined(LIB) && !empty(LIB) || defined(SHLIB_NAME)
162 OBJS+=          ${SRCS:N*.h:R:S/$/.o/}
163 .endif
164
165 .if defined(LIB) && !empty(LIB)
166 _LIBS=          lib${LIB}.a
167
168 lib${LIB}.a: ${OBJS} ${STATICOBJS}
169         @${ECHO} building static ${LIB} library
170         @rm -f ${.TARGET}
171 .if !defined(NM)
172         @${AR} ${ARFLAGS} ${.TARGET} `lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
173 .else
174         @${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
175 .endif
176         ${RANLIB} ${RANLIBFLAGS} ${.TARGET}
177 .endif
178
179 .if !defined(INTERNALLIB)
180
181 .if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
182 _LIBS+=         lib${LIB}_p.a
183 POBJS+=         ${OBJS:.o=.po} ${STATICOBJS:.o=.po}
184
185 lib${LIB}_p.a: ${POBJS}
186         @${ECHO} building profiled ${LIB} library
187         @rm -f ${.TARGET}
188 .if !defined(NM)
189         @${AR} ${ARFLAGS} ${.TARGET} `lorder ${POBJS} | tsort -q` ${ARADD}
190 .else
191         @${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' lorder ${POBJS} | tsort -q` ${ARADD}
192 .endif
193         ${RANLIB} ${RANLIBFLAGS} ${.TARGET}
194 .endif
195
196 .if defined(SHLIB_NAME) || \
197     defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
198 SOBJS+=         ${OBJS:.o=.So}
199 .endif
200
201 .if defined(SHLIB_NAME)
202 _LIBS+=         ${SHLIB_NAME}
203
204 SOLINKOPTS=     -shared -Wl,-x
205 .if !defined(ALLOW_SHARED_TEXTREL)
206 SOLINKOPTS+=    -Wl,--fatal-warnings -Wl,--warn-shared-textrel
207 .endif
208
209 .if target(beforelinking)
210 beforelinking: ${SOBJS}
211 ${SHLIB_NAME_FULL}: beforelinking
212 .endif
213 ${SHLIB_NAME_FULL}: ${SOBJS}
214         @${ECHO} building shared library ${SHLIB_NAME}
215         @rm -f ${SHLIB_NAME} ${SHLIB_LINK}
216 .if defined(SHLIB_LINK)
217         @${INSTALL_SYMLINK} ${SHLIB_NAME} ${SHLIB_LINK}
218 .endif
219 .if !defined(NM)
220         @${CC} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
221             -o ${.TARGET} -Wl,-soname,${SONAME} \
222             `lorder ${SOBJS} | tsort -q` ${LDADD}
223 .else
224         @${CC} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
225             -o ${.TARGET} -Wl,-soname,${SONAME} \
226             `NM='${NM}' lorder ${SOBJS} | tsort -q` ${LDADD}
227 .endif
228 .if ${MK_CTF} != "no"
229         ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SOBJS}
230 .endif
231
232 .if ${MK_DEBUG_FILES} != "no"
233 CLEANFILES+=    ${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
234 ${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
235         ${OBJCOPY} --strip-debug --add-gnu-debuglink=${SHLIB_NAME}.debug \
236             ${SHLIB_NAME_FULL} ${.TARGET}
237
238 ${SHLIB_NAME}.debug: ${SHLIB_NAME_FULL}
239         ${OBJCOPY} --only-keep-debug ${SHLIB_NAME_FULL} ${.TARGET}
240 .endif
241 .endif #defined(SHLIB_NAME)
242
243 .if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
244 _LIBS+=         lib${LIB}_pic.a
245
246 lib${LIB}_pic.a: ${SOBJS}
247         @${ECHO} building special pic ${LIB} library
248         @rm -f ${.TARGET}
249         @${AR} ${ARFLAGS} ${.TARGET} ${SOBJS} ${ARADD}
250         ${RANLIB} ${RANLIBFLAGS} ${.TARGET}
251 .endif
252
253 .if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
254 LINTLIB=        llib-l${LIB}.ln
255 _LIBS+=         ${LINTLIB}
256 LINTOBJS+=      ${SRCS:M*.c:.c=.ln}
257
258 ${LINTLIB}: ${LINTOBJS}
259         @${ECHO} building lint library ${.TARGET}
260         @rm -f ${.TARGET}
261         ${LINT} ${LINTLIBFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
262 .endif
263
264 .endif # !defined(INTERNALLIB)
265
266 all: ${_LIBS}
267
268 .if ${MK_MAN} != "no"
269 all: _manpages
270 .endif
271
272 _EXTRADEPEND:
273         @TMP=_depend$$$$; \
274         sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1.So:/' < ${DEPENDFILE} \
275             > $$TMP; \
276         mv $$TMP ${DEPENDFILE}
277 .if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
278 .if defined(DPADD) && !empty(DPADD)
279         echo ${SHLIB_NAME}: ${DPADD} >> ${DEPENDFILE}
280 .endif
281 .endif
282
283 .if !target(install)
284
285 .if defined(PRECIOUSLIB)
286 .if !defined(NO_FSCHG)
287 SHLINSTALLFLAGS+= -fschg
288 .endif
289 SHLINSTALLFLAGS+= -S
290 .endif
291
292 _INSTALLFLAGS:= ${INSTALLFLAGS}
293 .for ie in ${INSTALLFLAGS_EDIT}
294 _INSTALLFLAGS:= ${_INSTALLFLAGS${ie}}
295 .endfor
296 _SHLINSTALLFLAGS:=      ${SHLINSTALLFLAGS}
297 .for ie in ${INSTALLFLAGS_EDIT}
298 _SHLINSTALLFLAGS:=      ${_SHLINSTALLFLAGS${ie}}
299 .endfor
300
301 .if !defined(INTERNALLIB)
302 realinstall: _libinstall
303 .ORDER: beforeinstall _libinstall
304 _libinstall:
305 .if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
306         ${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
307             ${_INSTALLFLAGS} lib${LIB}.a ${DESTDIR}${_LIBDIR}
308 .endif
309 .if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
310         ${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
311             ${_INSTALLFLAGS} lib${LIB}_p.a ${DESTDIR}${_LIBDIR}
312 .endif
313 .if defined(SHLIB_NAME)
314         ${INSTALL} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
315             ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
316             ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}
317 .if ${MK_DEBUG_FILES} != "no"
318 .if defined(DEBUGMKDIR)
319         ${INSTALL} -T debug -d ${DESTDIR}${DEBUGFILEDIR}
320 .endif
321         ${INSTALL} -T debug -o ${LIBOWN} -g ${LIBGRP} -m ${DEBUGMODE} \
322             ${_INSTALLFLAGS} \
323             ${SHLIB_NAME}.debug ${DESTDIR}${DEBUGFILEDIR}
324 .endif
325 .if defined(SHLIB_LINK)
326 # ${_SHLIBDIRPREFIX} and ${_LDSCRIPTROOT} are both needed when cross-building
327 # and when building 32 bits library shims.  ${_SHLIBDIRPREFIX} is the directory
328 # prefix where shared objects will be installed by the install target.
329 #
330 # ${_LDSCRIPTROOT} is the directory prefix that will be used when generating
331 # ld(1) scripts.  The crosstools' ld is configured to lookup libraries in an
332 # alternative directory which is called "sysroot", so during buildworld binaries
333 # won't be linked against the running system libraries but against the ones of
334 # the current source tree.  ${_LDSCRIPTROOT} behavior is twisted because of
335 # the location where we store them:
336 # - 64 bits libs are located under sysroot, so ${_LDSCRIPTROOT} must be empty
337 #   because ld(1) will manage to find them from sysroot;
338 # - 32 bits shims are not, so ${_LDSCRIPTROOT} is used to specify their full
339 #   path, outside of sysroot.
340 # Note that ld(1) scripts are generated both during buildworld and
341 # installworld; in the later case ${_LDSCRIPTROOT} must be obviously empty
342 # because on the target system, libraries are meant to be looked up from /.
343 .if defined(SHLIB_LDSCRIPT) && !empty(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
344         sed -e 's,@@SHLIB@@,${_LDSCRIPTROOT}${_SHLIBDIR}/${SHLIB_NAME},g' \
345             -e 's,@@LIBDIR@@,${_LDSCRIPTROOT}${_LIBDIR},g' \
346             ${.CURDIR}/${SHLIB_LDSCRIPT} > ${DESTDIR}${_LIBDIR}/${SHLIB_LINK:R}.ld
347         ${INSTALL} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
348             ${_INSTALLFLAGS} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK:R}.ld \
349             ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
350         rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_LINK:R}.ld
351
352 .else
353 .if ${_SHLIBDIR} == ${_LIBDIR}
354         ${INSTALL_SYMLINK} ${SHLIB_NAME} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
355 .else
356         ${INSTALL_SYMLINK} ${_SHLIBDIRPREFIX}${_SHLIBDIR}/${SHLIB_NAME} \
357             ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
358 .if exists(${DESTDIR}${_LIBDIR}/${SHLIB_NAME})
359         -chflags noschg ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
360         rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
361 .endif
362 .endif
363 .endif # SHLIB_LDSCRIPT
364 .endif # SHLIB_LINK
365 .endif # SHIB_NAME
366 .if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
367         ${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
368             ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${_LIBDIR}
369 .endif
370 .if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
371         ${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
372             ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}
373 .endif
374 .endif # !defined(INTERNALLIB)
375
376 .if !defined(LIBRARIES_ONLY)
377 .include <bsd.nls.mk>
378 .include <bsd.files.mk>
379 .include <bsd.incs.mk>
380 .endif
381
382 .include <bsd.links.mk>
383
384 .if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
385 realinstall: _maninstall
386 .ORDER: beforeinstall _maninstall
387 .endif
388
389 .endif
390
391 .if !target(lint)
392 lint: ${SRCS:M*.c}
393         ${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
394 .endif
395
396 .if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
397 .include <bsd.man.mk>
398 .endif
399
400 .include <bsd.dep.mk>
401
402 .if !exists(${.OBJDIR}/${DEPENDFILE})
403 .if defined(LIB) && !empty(LIB)
404 ${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
405 .for _S in ${SRCS:N*.[hly]}
406 ${_S:R}.po: ${_S}
407 .endfor
408 .endif
409 .if defined(SHLIB_NAME) || \
410     defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
411 ${SOBJS}: ${SRCS:M*.h}
412 .for _S in ${SRCS:N*.[hly]}
413 ${_S:R}.So: ${_S}
414 .endfor
415 .endif
416 .endif
417
418 .if !target(clean)
419 clean:
420 .if defined(CLEANFILES) && !empty(CLEANFILES)
421         rm -f ${CLEANFILES}
422 .endif
423 .if defined(LIB) && !empty(LIB)
424         rm -f a.out ${OBJS} ${OBJS:S/$/.tmp/} ${STATICOBJS}
425 .endif
426 .if !defined(INTERNALLIB)
427 .if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
428         rm -f ${POBJS} ${POBJS:S/$/.tmp/}
429 .endif
430 .if defined(SHLIB_NAME) || \
431     defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
432         rm -f ${SOBJS} ${SOBJS:.So=.so} ${SOBJS:S/$/.tmp/}
433 .endif
434 .if defined(SHLIB_NAME)
435 .if defined(SHLIB_LINK)
436 .if defined(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
437         rm -f lib${LIB}.ld
438 .endif
439         rm -f ${SHLIB_LINK}
440 .endif
441 .endif # defined(SHLIB_NAME)
442 .if defined(WANT_LINT) && defined(LIB) && !empty(LIB)
443         rm -f ${LINTOBJS}
444 .endif
445 .endif # !defined(INTERNALLIB)
446 .if defined(_LIBS) && !empty(_LIBS)
447         rm -f ${_LIBS}
448 .endif
449 .if defined(CLEANDIRS) && !empty(CLEANDIRS)
450         rm -rf ${CLEANDIRS}
451 .endif
452 .if !empty(VERSION_DEF) && !empty(SYMBOL_MAPS)
453         rm -f ${VERSION_MAP}
454 .endif
455 .endif
456
457 .include <bsd.obj.mk>
458
459 .include <bsd.sys.mk>