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