xref: /freebsd/share/mk/bsd.lib.mk (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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 ${SHLIBDIR:M*lib32*}
55TAGS+=	lib32
56.endif
57
58.if defined(NO_ROOT)
59.if !defined(TAGS) || ! ${TAGS:Mpackage=*}
60TAGS+=		package=${PACKAGE:Uruntime}
61.endif
62TAG_ARGS=	-T ${TAGS:[*]:S/ /,/g}
63.endif
64
65.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
66    empty(DEBUG_FLAGS:M-gdwarf*)
67SHARED_CFLAGS+= -g
68SHARED_CXXFLAGS+= -g
69CTFFLAGS+= -g
70.endif
71
72.include <bsd.libnames.mk>
73
74# prefer .s to a .c, add .po, remove stuff not used in the BSD libraries
75# .So used for PIC object files
76.SUFFIXES:
77.SUFFIXES: .out .o .po .So .S .asm .s .c .cc .cpp .cxx .C .f .y .l .ln
78
79.if !defined(PICFLAG)
80.if ${MACHINE_CPUARCH} == "sparc64"
81PICFLAG=-fPIC
82.else
83PICFLAG=-fpic
84.endif
85.endif
86
87PO_FLAG=-pg
88
89.c.o:
90	${CC} ${STATIC_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
91	${CTFCONVERT_CMD}
92
93.c.po:
94	${CC} ${PO_FLAG} ${STATIC_CFLAGS} ${PO_CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
95	${CTFCONVERT_CMD}
96
97.c.So:
98	${CC} ${PICFLAG} -DPIC ${SHARED_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
99	${CTFCONVERT_CMD}
100
101.cc.o .C.o .cpp.o .cxx.o:
102	${CXX} ${STATIC_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
103
104.cc.po .C.po .cpp.po .cxx.po:
105	${CXX} ${PO_FLAG} ${STATIC_CXXFLAGS} ${PO_CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
106
107.cc.So .C.So .cpp.So .cxx.So:
108	${CXX} ${PICFLAG} -DPIC ${SHARED_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
109
110.f.po:
111	${FC} -pg ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
112	${CTFCONVERT_CMD}
113
114.f.So:
115	${FC} ${PICFLAG} -DPIC ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
116	${CTFCONVERT_CMD}
117
118.s.po .s.So:
119	${AS} ${AFLAGS} -o ${.TARGET} ${.IMPSRC}
120	${CTFCONVERT_CMD}
121
122.asm.po:
123	${CC:N${CCACHE_BIN}} -x assembler-with-cpp -DPROF ${PO_CFLAGS} \
124	    ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
125	${CTFCONVERT_CMD}
126
127.asm.So:
128	${CC:N${CCACHE_BIN}} -x assembler-with-cpp ${PICFLAG} -DPIC \
129	    ${CFLAGS} ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
130	${CTFCONVERT_CMD}
131
132.S.po:
133	${CC:N${CCACHE_BIN}} -DPROF ${PO_CFLAGS} ${ACFLAGS} -c ${.IMPSRC} \
134	    -o ${.TARGET}
135	${CTFCONVERT_CMD}
136
137.S.So:
138	${CC:N${CCACHE_BIN}} ${PICFLAG} -DPIC ${CFLAGS} ${ACFLAGS} \
139	    -c ${.IMPSRC} -o ${.TARGET}
140	${CTFCONVERT_CMD}
141
142.if !defined(_SKIP_BUILD)
143all: beforebuild .WAIT
144beforebuild: objwarn
145.endif
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
160DEBUGMKDIR=
161.endif
162.else
163SHLIB_NAME_FULL=${SHLIB_NAME}
164.endif
165.endif
166
167.include <bsd.symver.mk>
168
169# Allow libraries to specify their own version map or have it
170# automatically generated (see bsd.symver.mk above).
171.if ${MK_SYMVER} == "yes" && !empty(VERSION_MAP)
172${SHLIB_NAME_FULL}:	${VERSION_MAP}
173LDFLAGS+=	-Wl,--version-script=${VERSION_MAP}
174.endif
175
176.if defined(LIB) && !empty(LIB) || defined(SHLIB_NAME)
177OBJS+=		${SRCS:N*.h:R:S/$/.o/}
178CLEANFILES+=	${OBJS} ${STATICOBJS}
179.endif
180
181.if defined(LIB) && !empty(LIB)
182_LIBS=		lib${LIB_PRIVATE}${LIB}.a
183
184lib${LIB_PRIVATE}${LIB}.a: ${OBJS} ${STATICOBJS}
185	@${ECHO} building static ${LIB} library
186	@rm -f ${.TARGET}
187	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
188	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
189.endif
190
191.if !defined(INTERNALLIB)
192
193.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
194_LIBS+=		lib${LIB_PRIVATE}${LIB}_p.a
195POBJS+=		${OBJS:.o=.po} ${STATICOBJS:.o=.po}
196DEPENDOBJS+=	${POBJS}
197CLEANFILES+=	${POBJS}
198
199lib${LIB_PRIVATE}${LIB}_p.a: ${POBJS}
200	@${ECHO} building profiled ${LIB} library
201	@rm -f ${.TARGET}
202	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${POBJS} | tsort -q` ${ARADD}
203	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
204.endif
205
206.if defined(SHLIB_NAME) || \
207    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
208SOBJS+=		${OBJS:.o=.So}
209DEPENDOBJS+=	${SOBJS}
210CLEANFILES+=	${SOBJS}
211.endif
212
213.if defined(SHLIB_NAME)
214_LIBS+=		${SHLIB_NAME}
215
216SOLINKOPTS=	-shared -Wl,-x
217.if !defined(ALLOW_SHARED_TEXTREL)
218.if defined(LD_FATAL_WARNINGS) && ${LD_FATAL_WARNINGS} == "no"
219SOLINKOPTS+=	-Wl,--no-fatal-warnings
220.else
221SOLINKOPTS+=	-Wl,--fatal-warnings
222.endif
223SOLINKOPTS+=	-Wl,--warn-shared-textrel
224.endif
225
226.if target(beforelinking)
227beforelinking: ${SOBJS}
228${SHLIB_NAME_FULL}: beforelinking
229.endif
230
231.if defined(SHLIB_LINK)
232# ${_LDSCRIPTROOT} is needed when cross-building
233# and when building 32 bits library shims.
234#
235# ${_LDSCRIPTROOT} is the directory prefix that will be used when generating
236# ld(1) scripts.  The crosstools' ld is configured to lookup libraries in an
237# alternative directory which is called "sysroot", so during buildworld binaries
238# won't be linked against the running system libraries but against the ones of
239# the current source tree.  ${_LDSCRIPTROOT} behavior is twisted because of
240# the location where we store them:
241# - 64 bits libs are located under sysroot, so ${_LDSCRIPTROOT} must be empty
242#   because ld(1) will manage to find them from sysroot;
243# - 32 bits shims are not, so ${_LDSCRIPTROOT} is used to specify their full
244#   path, outside of sysroot.
245# Note that ld(1) scripts are generated both during buildworld and
246# installworld; in the later case ${_LDSCRIPTROOT} must be obviously empty
247# because on the target system, libraries are meant to be looked up from /.
248.if defined(SHLIB_LDSCRIPT) && !empty(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
249${SHLIB_LINK:R}.ld: ${.CURDIR}/${SHLIB_LDSCRIPT}
250	sed -e 's,@@SHLIB@@,${_LDSCRIPTROOT}${_SHLIBDIR}/${SHLIB_NAME},g' \
251	    -e 's,@@LIBDIR@@,${_LDSCRIPTROOT}${_LIBDIR},g' \
252	    -e 's,/[^ ]*/,,g' \
253	    ${.ALLSRC} > ${.TARGET}
254
255${SHLIB_NAME_FULL}: ${SHLIB_LINK:R}.ld
256CLEANFILES+=	${SHLIB_LINK:R}.ld
257.endif
258CLEANFILES+=	${SHLIB_LINK}
259.endif
260
261${SHLIB_NAME_FULL}: ${SOBJS}
262	@${ECHO} building shared library ${SHLIB_NAME}
263	@rm -f ${SHLIB_NAME} ${SHLIB_LINK}
264.if defined(SHLIB_LINK) && !commands(${SHLIB_LINK:R}.ld)
265	@${INSTALL_SYMLINK} ${TAG_ARGS:D${TAG_ARGS},development} ${SHLIB_NAME} ${SHLIB_LINK}
266.endif
267	${_LD:N${CCACHE_BIN}} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
268	    -o ${.TARGET} -Wl,-soname,${SONAME} \
269	    `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${SOBJS} | tsort -q` ${LDADD}
270.if ${MK_CTF} != "no"
271	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SOBJS}
272.endif
273
274.if ${MK_DEBUG_FILES} != "no"
275CLEANFILES+=	${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
276${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
277	${OBJCOPY} --strip-debug --add-gnu-debuglink=${SHLIB_NAME}.debug \
278	    ${SHLIB_NAME_FULL} ${.TARGET}
279
280${SHLIB_NAME}.debug: ${SHLIB_NAME_FULL}
281	${OBJCOPY} --only-keep-debug ${SHLIB_NAME_FULL} ${.TARGET}
282.endif
283.endif #defined(SHLIB_NAME)
284
285.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
286_LIBS+=		lib${LIB_PRIVATE}${LIB}_pic.a
287
288lib${LIB_PRIVATE}${LIB}_pic.a: ${SOBJS}
289	@${ECHO} building special pic ${LIB} library
290	@rm -f ${.TARGET}
291	${AR} ${ARFLAGS} ${.TARGET} ${SOBJS} ${ARADD}
292	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
293.endif
294
295.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
296LINTLIB=	llib-l${LIB}.ln
297_LIBS+=		${LINTLIB}
298LINTOBJS+=	${SRCS:M*.c:.c=.ln}
299CLEANFILES+=	${LINTOBJS}
300
301${LINTLIB}: ${LINTOBJS}
302	@${ECHO} building lint library ${.TARGET}
303	@rm -f ${.TARGET}
304	${LINT} ${LINTLIBFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
305.endif
306
307.endif # !defined(INTERNALLIB)
308
309.if defined(_SKIP_BUILD)
310all:
311.else
312.if defined(_LIBS) && !empty(_LIBS)
313all: ${_LIBS}
314CLEANFILES+=	${_LIBS}
315.endif
316
317.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
318all: _manpages
319.endif
320.endif
321
322_EXTRADEPEND:
323.if ${MK_FAST_DEPEND} == "no"
324	@TMP=_depend$$$$; \
325	sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1.So:/' < ${DEPENDFILE} \
326	    > $$TMP; \
327	mv $$TMP ${DEPENDFILE}
328.endif
329.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
330.if defined(DPADD) && !empty(DPADD)
331	echo ${SHLIB_NAME_FULL}: ${DPADD} >> ${DEPENDFILE}
332.endif
333.endif
334
335.if !target(install)
336
337.if defined(PRECIOUSLIB)
338.if !defined(NO_FSCHG)
339SHLINSTALLFLAGS+= -fschg
340.endif
341SHLINSTALLFLAGS+= -S
342.endif
343
344_INSTALLFLAGS:=	${INSTALLFLAGS}
345.for ie in ${INSTALLFLAGS_EDIT}
346_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
347.endfor
348_SHLINSTALLFLAGS:=	${SHLINSTALLFLAGS}
349.for ie in ${INSTALLFLAGS_EDIT}
350_SHLINSTALLFLAGS:=	${_SHLINSTALLFLAGS${ie}}
351.endfor
352
353.if !defined(INTERNALLIB)
354realinstall: _libinstall
355.ORDER: beforeinstall _libinstall
356_libinstall:
357.if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
358	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},development} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
359	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}.a ${DESTDIR}${_LIBDIR}/
360.endif
361.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
362	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},profile} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
363	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}_p.a ${DESTDIR}${_LIBDIR}/
364.endif
365.if defined(SHLIB_NAME)
366	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
367	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
368	    ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}/
369.if ${MK_DEBUG_FILES} != "no"
370.if defined(DEBUGMKDIR)
371	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -d ${DESTDIR}${DEBUGFILEDIR}/
372.endif
373	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -o ${LIBOWN} -g ${LIBGRP} -m ${DEBUGMODE} \
374	    ${_INSTALLFLAGS} \
375	    ${SHLIB_NAME}.debug ${DESTDIR}${DEBUGFILEDIR}/
376.endif
377.if defined(SHLIB_LINK)
378.if commands(${SHLIB_LINK:R}.ld)
379	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},development} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
380	    ${_INSTALLFLAGS} ${SHLIB_LINK:R}.ld \
381	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
382.for _SHLIB_LINK_LINK in ${SHLIB_LDSCRIPT_LINKS}
383	${INSTALL_SYMLINK} ${SHLIB_LINK} ${DESTDIR}${_LIBDIR}/${_SHLIB_LINK_LINK}
384.endfor
385.else
386.if ${_SHLIBDIR} == ${_LIBDIR}
387.if ${SHLIB_LINK:Mlib*}
388	${INSTALL_RSYMLINK} ${TAG_ARGS:D${TAG_ARGS},development} ${SHLIB_NAME} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
389.else
390	${INSTALL_RSYMLINK} ${TAG_ARGS} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
391	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
392.endif
393.else
394.if ${SHLIB_LINK:Mlib*}
395	${INSTALL_RSYMLINK} ${TAG_ARGS:D${TAG_ARGS},development} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
396	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
397.else
398	${INSTALL_RSYMLINK} ${TAG_ARGS} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
399	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
400.endif
401.if exists(${DESTDIR}${_LIBDIR}/${SHLIB_NAME})
402	-chflags noschg ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
403	rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
404.endif
405.endif
406.endif # SHLIB_LDSCRIPT
407.endif # SHLIB_LINK
408.endif # SHIB_NAME
409.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
410	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},development} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
411	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${_LIBDIR}/
412.endif
413.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
414	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},development} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
415	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}/
416.endif
417.endif # !defined(INTERNALLIB)
418
419.if !defined(LIBRARIES_ONLY)
420.include <bsd.nls.mk>
421.include <bsd.files.mk>
422.include <bsd.incs.mk>
423.include <bsd.confs.mk>
424.endif
425
426.include <bsd.links.mk>
427
428.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
429realinstall: _maninstall
430.ORDER: beforeinstall _maninstall
431.endif
432
433.endif
434
435.if !target(lint)
436lint: ${SRCS:M*.c}
437	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
438.endif
439
440.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
441.include <bsd.man.mk>
442.endif
443
444.include <bsd.dep.mk>
445
446.if ${MK_FAST_DEPEND} == "yes" || !exists(${.OBJDIR}/${DEPENDFILE})
447.if defined(LIB) && !empty(LIB)
448.if !exists(${.OBJDIR}/${DEPENDFILE})
449${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
450.endif
451.for _S in ${SRCS:N*.[hly]}
452${_S:R}.po: ${_S}
453.endfor
454.endif
455.if defined(SHLIB_NAME) || \
456    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
457.if !exists(${.OBJDIR}/${DEPENDFILE})
458${SOBJS}: ${SRCS:M*.h}
459.endif
460.for _S in ${SRCS:N*.[hly]}
461${_S:R}.So: ${_S}
462.endfor
463.endif
464.endif
465
466.include <bsd.obj.mk>
467
468.include <bsd.sys.mk>
469