xref: /freebsd/share/mk/bsd.lib.mk (revision eccfee6ebc28c335f2a2b9db7736dc1961131b04)
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.if !defined(_SKIP_BUILD)
132all: beforebuild .WAIT
133beforebuild: objwarn
134.endif
135
136_LIBDIR:=${LIBDIR}
137_SHLIBDIR:=${SHLIBDIR}
138
139.if defined(SHLIB_NAME)
140.if ${MK_DEBUG_FILES} != "no"
141SHLIB_NAME_FULL=${SHLIB_NAME}.full
142# Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
143.if ${_SHLIBDIR} == "/boot" ||\
144    ${SHLIBDIR:C%/lib(/.*)?$%/lib%} == "/lib" ||\
145    ${SHLIBDIR:C%/usr/(tests/)?lib(32|exec)?(/.*)?%/usr/lib%} == "/usr/lib"
146DEBUGFILEDIR=${DEBUGDIR}${_SHLIBDIR}
147.else
148DEBUGFILEDIR=${_SHLIBDIR}/.debug
149DEBUGMKDIR=
150.endif
151.else
152SHLIB_NAME_FULL=${SHLIB_NAME}
153.endif
154.endif
155
156.include <bsd.symver.mk>
157
158# Allow libraries to specify their own version map or have it
159# automatically generated (see bsd.symver.mk above).
160.if ${MK_SYMVER} == "yes" && !empty(VERSION_MAP)
161${SHLIB_NAME_FULL}:	${VERSION_MAP}
162LDFLAGS+=	-Wl,--version-script=${VERSION_MAP}
163.endif
164
165.if defined(LIB) && !empty(LIB) || defined(SHLIB_NAME)
166OBJS+=		${SRCS:N*.h:R:S/$/.o/}
167CLEANFILES+=	${OBJS} ${STATICOBJS}
168.endif
169
170.if defined(LIB) && !empty(LIB)
171_LIBS=		lib${LIB_PRIVATE}${LIB}.a
172
173lib${LIB_PRIVATE}${LIB}.a: ${OBJS} ${STATICOBJS}
174	@${ECHO} building static ${LIB} library
175	@rm -f ${.TARGET}
176	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
177	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
178.endif
179
180.if !defined(INTERNALLIB)
181
182.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
183_LIBS+=		lib${LIB_PRIVATE}${LIB}_p.a
184POBJS+=		${OBJS:.o=.po} ${STATICOBJS:.o=.po}
185DEPENDOBJS+=	${POBJS}
186CLEANFILES+=	${POBJS}
187
188lib${LIB_PRIVATE}${LIB}_p.a: ${POBJS}
189	@${ECHO} building profiled ${LIB} library
190	@rm -f ${.TARGET}
191	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${POBJS} | tsort -q` ${ARADD}
192	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
193.endif
194
195.if defined(SHLIB_NAME) || \
196    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
197SOBJS+=		${OBJS:.o=.So}
198DEPENDOBJS+=	${SOBJS}
199CLEANFILES+=	${SOBJS}
200.endif
201
202.if defined(SHLIB_NAME)
203_LIBS+=		${SHLIB_NAME}
204
205SOLINKOPTS=	-shared -Wl,-x
206.if !defined(ALLOW_SHARED_TEXTREL)
207.if defined(LD_FATAL_WARNINGS) && ${LD_FATAL_WARNINGS} == "no"
208SOLINKOPTS+=	-Wl,--no-fatal-warnings
209.else
210SOLINKOPTS+=	-Wl,--fatal-warnings
211.endif
212SOLINKOPTS+=	-Wl,--warn-shared-textrel
213.endif
214
215.if target(beforelinking)
216beforelinking: ${SOBJS}
217${SHLIB_NAME_FULL}: beforelinking
218.endif
219
220.if defined(SHLIB_LINK)
221# ${_LDSCRIPTROOT} is needed when cross-building
222# and when building 32 bits library shims.
223#
224# ${_LDSCRIPTROOT} is the directory prefix that will be used when generating
225# ld(1) scripts.  The crosstools' ld is configured to lookup libraries in an
226# alternative directory which is called "sysroot", so during buildworld binaries
227# won't be linked against the running system libraries but against the ones of
228# the current source tree.  ${_LDSCRIPTROOT} behavior is twisted because of
229# the location where we store them:
230# - 64 bits libs are located under sysroot, so ${_LDSCRIPTROOT} must be empty
231#   because ld(1) will manage to find them from sysroot;
232# - 32 bits shims are not, so ${_LDSCRIPTROOT} is used to specify their full
233#   path, outside of sysroot.
234# Note that ld(1) scripts are generated both during buildworld and
235# installworld; in the later case ${_LDSCRIPTROOT} must be obviously empty
236# because on the target system, libraries are meant to be looked up from /.
237.if defined(SHLIB_LDSCRIPT) && !empty(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
238${SHLIB_LINK:R}.ld: ${.CURDIR}/${SHLIB_LDSCRIPT}
239	sed -e 's,@@SHLIB@@,${_LDSCRIPTROOT}${_SHLIBDIR}/${SHLIB_NAME},g' \
240	    -e 's,@@LIBDIR@@,${_LDSCRIPTROOT}${_LIBDIR},g' \
241	    -e 's,/[^ ]*/,,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)
254	@${INSTALL_SYMLINK} ${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} | tsort -q` ${LDADD}
259.if ${MK_CTF} != "no"
260	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SOBJS}
261.endif
262
263.if ${MK_DEBUG_FILES} != "no"
264CLEANFILES+=	${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
265${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
266	${OBJCOPY} --strip-debug --add-gnu-debuglink=${SHLIB_NAME}.debug \
267	    ${SHLIB_NAME_FULL} ${.TARGET}
268
269${SHLIB_NAME}.debug: ${SHLIB_NAME_FULL}
270	${OBJCOPY} --only-keep-debug ${SHLIB_NAME_FULL} ${.TARGET}
271.endif
272.endif #defined(SHLIB_NAME)
273
274.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
275_LIBS+=		lib${LIB_PRIVATE}${LIB}_pic.a
276
277lib${LIB_PRIVATE}${LIB}_pic.a: ${SOBJS}
278	@${ECHO} building special pic ${LIB} library
279	@rm -f ${.TARGET}
280	${AR} ${ARFLAGS} ${.TARGET} ${SOBJS} ${ARADD}
281	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
282.endif
283
284.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
285LINTLIB=	llib-l${LIB}.ln
286_LIBS+=		${LINTLIB}
287LINTOBJS+=	${SRCS:M*.c:.c=.ln}
288CLEANFILES+=	${LINTOBJS}
289
290${LINTLIB}: ${LINTOBJS}
291	@${ECHO} building lint library ${.TARGET}
292	@rm -f ${.TARGET}
293	${LINT} ${LINTLIBFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
294.endif
295
296.endif # !defined(INTERNALLIB)
297
298.if defined(_SKIP_BUILD)
299all:
300.else
301.if defined(_LIBS) && !empty(_LIBS)
302all: ${_LIBS}
303CLEANFILES+=	${_LIBS}
304.endif
305
306.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
307all: _manpages
308.endif
309.endif
310
311_EXTRADEPEND:
312.if ${MK_FAST_DEPEND} == "no"
313	@TMP=_depend$$$$; \
314	sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1.So:/' < ${DEPENDFILE} \
315	    > $$TMP; \
316	mv $$TMP ${DEPENDFILE}
317.endif
318.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
319.if defined(DPADD) && !empty(DPADD)
320	echo ${SHLIB_NAME_FULL}: ${DPADD} >> ${DEPENDFILE}
321.endif
322.endif
323
324.if !target(install)
325
326.if defined(PRECIOUSLIB)
327.if !defined(NO_FSCHG)
328SHLINSTALLFLAGS+= -fschg
329.endif
330SHLINSTALLFLAGS+= -S
331.endif
332
333_INSTALLFLAGS:=	${INSTALLFLAGS}
334.for ie in ${INSTALLFLAGS_EDIT}
335_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
336.endfor
337_SHLINSTALLFLAGS:=	${SHLINSTALLFLAGS}
338.for ie in ${INSTALLFLAGS_EDIT}
339_SHLINSTALLFLAGS:=	${_SHLINSTALLFLAGS${ie}}
340.endfor
341
342.if !defined(INTERNALLIB)
343realinstall: _libinstall
344.ORDER: beforeinstall _libinstall
345_libinstall:
346.if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
347	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
348	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}.a ${DESTDIR}${_LIBDIR}/
349.endif
350.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
351	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
352	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}_p.a ${DESTDIR}${_LIBDIR}/
353.endif
354.if defined(SHLIB_NAME)
355	${INSTALL} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
356	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
357	    ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}/
358.if ${MK_DEBUG_FILES} != "no"
359.if defined(DEBUGMKDIR)
360	${INSTALL} -T debug -d ${DESTDIR}${DEBUGFILEDIR}/
361.endif
362	${INSTALL} -T debug -o ${LIBOWN} -g ${LIBGRP} -m ${DEBUGMODE} \
363	    ${_INSTALLFLAGS} \
364	    ${SHLIB_NAME}.debug ${DESTDIR}${DEBUGFILEDIR}/
365.endif
366.if defined(SHLIB_LINK)
367.if commands(${SHLIB_LINK:R}.ld)
368	${INSTALL} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
369	    ${_INSTALLFLAGS} ${SHLIB_LINK:R}.ld \
370	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
371.for _SHLIB_LINK_LINK in ${SHLIB_LDSCRIPT_LINKS}
372	${INSTALL_SYMLINK} ${SHLIB_LINK} ${DESTDIR}${_LIBDIR}/${_SHLIB_LINK_LINK}
373.endfor
374.else
375.if ${_SHLIBDIR} == ${_LIBDIR}
376	${INSTALL_SYMLINK} ${SHLIB_NAME} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
377.else
378	${INSTALL_RSYMLINK} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
379	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
380.if exists(${DESTDIR}${_LIBDIR}/${SHLIB_NAME})
381	-chflags noschg ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
382	rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
383.endif
384.endif
385.endif # SHLIB_LDSCRIPT
386.endif # SHLIB_LINK
387.endif # SHIB_NAME
388.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
389	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
390	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${_LIBDIR}/
391.endif
392.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
393	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
394	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}/
395.endif
396.endif # !defined(INTERNALLIB)
397
398.if !defined(LIBRARIES_ONLY)
399.include <bsd.nls.mk>
400.include <bsd.files.mk>
401.include <bsd.incs.mk>
402.include <bsd.confs.mk>
403.endif
404
405.include <bsd.links.mk>
406
407.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
408realinstall: _maninstall
409.ORDER: beforeinstall _maninstall
410.endif
411
412.endif
413
414.if !target(lint)
415lint: ${SRCS:M*.c}
416	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
417.endif
418
419.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
420.include <bsd.man.mk>
421.endif
422
423.include <bsd.dep.mk>
424
425.if ${MK_FAST_DEPEND} == "yes" || !exists(${.OBJDIR}/${DEPENDFILE})
426.if defined(LIB) && !empty(LIB)
427.if !exists(${.OBJDIR}/${DEPENDFILE})
428${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
429.endif
430.for _S in ${SRCS:N*.[hly]}
431${_S:R}.po: ${_S}
432.endfor
433.endif
434.if defined(SHLIB_NAME) || \
435    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
436.if !exists(${.OBJDIR}/${DEPENDFILE})
437${SOBJS}: ${SRCS:M*.h}
438.endif
439.for _S in ${SRCS:N*.[hly]}
440${_S:R}.So: ${_S}
441.endfor
442.endif
443.endif
444
445.include <bsd.obj.mk>
446
447.include <bsd.sys.mk>
448