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