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