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