xref: /freebsd/share/mk/bsd.lib.mk (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
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*)
72CFLAGS+= ${DEBUG_FILES_CFLAGS}
73CXXFLAGS+= ${DEBUG_FILES_CFLAGS}
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:${OBJS_SRCS_FILTER:ts:}:S/$/.o/}
172BCOBJS+=	${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.bco/g}
173LLOBJS+=	${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.llo/g}
174CLEANFILES+=	${OBJS} ${BCOBJS} ${LLOBJS} ${STATICOBJS}
175.endif
176
177.if defined(LIB) && !empty(LIB)
178_LIBS=		lib${LIB_PRIVATE}${LIB}.a
179
180lib${LIB_PRIVATE}${LIB}.a: ${OBJS} ${STATICOBJS}
181	@${ECHO} building static ${LIB} library
182	@rm -f ${.TARGET}
183	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' \
184	    ${LORDER} ${OBJS} ${STATICOBJS} | ${TSORT} ${TSORTFLAGS}` ${ARADD}
185	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
186.endif
187
188.if !defined(INTERNALLIB)
189
190.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
191_LIBS+=		lib${LIB_PRIVATE}${LIB}_p.a
192POBJS+=		${OBJS:.o=.po} ${STATICOBJS:.o=.po}
193DEPENDOBJS+=	${POBJS}
194CLEANFILES+=	${POBJS}
195
196lib${LIB_PRIVATE}${LIB}_p.a: ${POBJS}
197	@${ECHO} building profiled ${LIB} library
198	@rm -f ${.TARGET}
199	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' \
200	    ${LORDER} ${POBJS} | ${TSORT} ${TSORTFLAGS}` ${ARADD}
201	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
202.endif
203
204.if defined(LLVM_LINK)
205lib${LIB_PRIVATE}${LIB}.bc: ${BCOBJS}
206	${LLVM_LINK} -o ${.TARGET} ${BCOBJS}
207
208lib${LIB_PRIVATE}${LIB}.ll: ${LLOBJS}
209	${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS}
210
211CLEANFILES+=	lib${LIB_PRIVATE}${LIB}.bc lib${LIB_PRIVATE}${LIB}.ll
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.endif # !defined(INTERNALLIB)
289
290.if defined(_SKIP_BUILD)
291all:
292.else
293.if defined(_LIBS) && !empty(_LIBS)
294all: ${_LIBS}
295.endif
296
297.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
298all: all-man
299.endif
300.endif
301
302CLEANFILES+=	${_LIBS}
303
304_EXTRADEPEND:
305.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
306.if defined(DPADD) && !empty(DPADD)
307	echo ${SHLIB_NAME_FULL}: ${DPADD} >> ${DEPENDFILE}
308.endif
309.endif
310
311.if !target(install)
312
313.if defined(PRECIOUSLIB)
314.if !defined(NO_FSCHG)
315SHLINSTALLFLAGS+= -fschg
316.endif
317.endif
318# Install libraries with -S to avoid risk of modifying in-use libraries when
319# installing to a running system.  It is safe to avoid this for NO_ROOT builds
320# that are only creating an image.
321.if !defined(NO_SAFE_LIBINSTALL) && !defined(NO_ROOT)
322SHLINSTALLFLAGS+= -S
323.endif
324
325_INSTALLFLAGS:=	${INSTALLFLAGS}
326.for ie in ${INSTALLFLAGS_EDIT}
327_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
328.endfor
329_SHLINSTALLFLAGS:=	${SHLINSTALLFLAGS}
330.for ie in ${INSTALLFLAGS_EDIT}
331_SHLINSTALLFLAGS:=	${_SHLINSTALLFLAGS${ie}}
332.endfor
333
334.if !defined(INTERNALLIB)
335realinstall: _libinstall
336.ORDER: beforeinstall _libinstall
337_libinstall:
338.if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
339	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},development} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
340	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}.a ${DESTDIR}${_LIBDIR}/
341.endif
342.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
343	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},profile} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
344	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}_p.a ${DESTDIR}${_LIBDIR}/
345.endif
346.if defined(SHLIB_NAME)
347	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
348	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
349	    ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}/
350.if ${MK_DEBUG_FILES} != "no"
351.if defined(DEBUGMKDIR)
352	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -d ${DESTDIR}${DEBUGFILEDIR}/
353.endif
354	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -o ${LIBOWN} -g ${LIBGRP} -m ${DEBUGMODE} \
355	    ${_INSTALLFLAGS} \
356	    ${SHLIB_NAME}.debug ${DESTDIR}${DEBUGFILEDIR}/
357.endif
358.if defined(SHLIB_LINK)
359.if commands(${SHLIB_LINK:R}.ld)
360	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},development} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
361	    ${_INSTALLFLAGS} ${SHLIB_LINK:R}.ld \
362	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
363.for _SHLIB_LINK_LINK in ${SHLIB_LDSCRIPT_LINKS}
364	${INSTALL_SYMLINK} ${SHLIB_LINK} ${DESTDIR}${_LIBDIR}/${_SHLIB_LINK_LINK}
365.endfor
366.else
367.if ${_SHLIBDIR} == ${_LIBDIR}
368.if ${SHLIB_LINK:Mlib*}
369	${INSTALL_RSYMLINK} ${TAG_ARGS:D${TAG_ARGS},development} ${SHLIB_NAME} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
370.else
371	${INSTALL_RSYMLINK} ${TAG_ARGS} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
372	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
373.endif
374.else
375.if ${SHLIB_LINK:Mlib*}
376	${INSTALL_RSYMLINK} ${TAG_ARGS:D${TAG_ARGS},development} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
377	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
378.else
379	${INSTALL_RSYMLINK} ${TAG_ARGS} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
380	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
381.endif
382.if exists(${DESTDIR}${_LIBDIR}/${SHLIB_NAME})
383	-chflags noschg ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
384	rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
385.endif
386.endif
387.endif # SHLIB_LDSCRIPT
388.endif # SHLIB_LINK
389.endif # SHIB_NAME
390.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
391	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},development} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
392	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${_LIBDIR}/
393.endif
394.endif # !defined(INTERNALLIB)
395
396.if !defined(LIBRARIES_ONLY)
397.include <bsd.nls.mk>
398.include <bsd.files.mk>
399.include <bsd.incs.mk>
400.include <bsd.confs.mk>
401.endif
402
403.include <bsd.links.mk>
404
405.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
406realinstall: maninstall
407.ORDER: beforeinstall maninstall
408.endif
409
410.endif
411
412.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
413.include <bsd.man.mk>
414.endif
415
416.if defined(LIB) && !empty(LIB)
417OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
418.for _S in ${SRCS:N*.[hly]}
419OBJS_DEPEND_GUESS.${_S:${OBJS_SRCS_FILTER:ts:}}.po+=	${_S}
420.endfor
421.endif
422.if defined(SHLIB_NAME) || \
423    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
424.for _S in ${SRCS:N*.[hly]}
425OBJS_DEPEND_GUESS.${_S:${OBJS_SRCS_FILTER:ts:}}.pico+=	${_S}
426.endfor
427.endif
428
429.if defined(HAS_TESTS)
430MAKE+=			MK_MAKE_CHECK_USE_SANDBOX=yes
431SUBDIR_TARGETS+=	check
432TESTS_LD_LIBRARY_PATH+=	${.OBJDIR}
433.endif
434
435.include <bsd.dep.mk>
436.include <bsd.clang-analyze.mk>
437.include <bsd.obj.mk>
438.include <bsd.sys.mk>
439