xref: /freebsd/share/mk/bsd.lib.mk (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1#	from: @(#)bsd.lib.mk	5.26 (Berkeley) 5/2/91
2# $FreeBSD$
3#
4
5.include <bsd.init.mk>
6
7# Set up the variables controlling shared libraries.  After this section,
8# SHLIB_NAME will be defined only if we are to create a shared library.
9# SHLIB_LINK will be defined only if we are to create a link to it.
10# INSTALL_PIC_ARCHIVE will be defined only if we are to create a PIC archive.
11.if defined(NO_PIC)
12.undef SHLIB_NAME
13.undef INSTALL_PIC_ARCHIVE
14.else
15.if !defined(SHLIB) && defined(LIB)
16SHLIB=		${LIB}
17.endif
18.if !defined(SHLIB_NAME) && defined(SHLIB) && defined(SHLIB_MAJOR)
19SHLIB_NAME=	lib${SHLIB}.so.${SHLIB_MAJOR}
20.endif
21.if defined(SHLIB_NAME) && !empty(SHLIB_NAME:M*.so.*)
22SHLIB_LINK?=	${SHLIB_NAME:R}
23.endif
24SONAME?=	${SHLIB_NAME}
25.endif
26
27.if defined(CRUNCH_CFLAGS)
28CFLAGS+=	${CRUNCH_CFLAGS}
29.endif
30
31.if ${MK_ASSERT_DEBUG} == "no"
32CFLAGS+= -DNDEBUG
33NO_WERROR=
34.endif
35
36.if defined(DEBUG_FLAGS)
37CFLAGS+= ${DEBUG_FLAGS}
38.endif
39
40.if !defined(DEBUG_FLAGS)
41STRIP?=	-s
42.endif
43
44.include <bsd.libnames.mk>
45
46# prefer .s to a .c, add .po, remove stuff not used in the BSD libraries
47# .So used for PIC object files
48.SUFFIXES:
49.SUFFIXES: .out .o .po .So .S .asm .s .c .cc .cpp .cxx .m .C .f .y .l .ln
50
51.if !defined(PICFLAG)
52.if ${MACHINE_ARCH} == "sparc64"
53PICFLAG=-fPIC
54.else
55PICFLAG=-fpic
56.endif
57.endif
58
59.if ${CC} == "icc"
60PO_FLAG=-p
61.else
62PO_FLAG=-pg
63.endif
64
65.c.po:
66	${CC} ${PO_FLAG} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
67
68.c.So:
69	${CC} ${PICFLAG} -DPIC ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
70
71.cc.po .C.po .cpp.po .cxx.po:
72	${CXX} ${PO_FLAG} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
73
74.cc.So .C.So .cpp.So .cxx.So:
75	${CXX} ${PICFLAG} -DPIC ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
76
77.f.po:
78	${FC} -pg ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
79
80.f.So:
81	${FC} ${PICFLAG} -DPIC ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
82
83.m.po:
84	${OBJC} ${OBJCFLAGS} -pg -c ${.IMPSRC} -o ${.TARGET}
85
86.m.So:
87	${OBJC} ${PICFLAG} -DPIC ${OBJCFLAGS} -c ${.IMPSRC} -o ${.TARGET}
88
89.s.po .s.So:
90	${AS} ${AFLAGS} -o ${.TARGET} ${.IMPSRC}
91
92.asm.po:
93	${CC} -x assembler-with-cpp -DPROF ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
94
95.asm.So:
96	${CC} -x assembler-with-cpp ${PICFLAG} -DPIC ${CFLAGS} \
97	    -c ${.IMPSRC} -o ${.TARGET}
98
99.S.po:
100	${CC} -DPROF ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
101
102.S.So:
103	${CC} ${PICFLAG} -DPIC ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
104
105all: objwarn
106
107.include <bsd.symver.mk>
108
109# Allow librararies to specify their own version map or have it
110# automatically generated (see bsd.symver.mk above).
111.if ${MK_SYMVER} == "yes" && !empty(VERSION_MAP)
112${SHLIB_NAME}:	${VERSION_MAP}
113LDFLAGS+=	-Wl,--version-script=${VERSION_MAP}
114.endif
115
116.if defined(LIB) && !empty(LIB) || defined(SHLIB_NAME)
117OBJS+=		${SRCS:N*.h:R:S/$/.o/}
118.endif
119
120.if defined(LIB) && !empty(LIB)
121_LIBS=		lib${LIB}.a
122
123lib${LIB}.a: ${OBJS} ${STATICOBJS}
124	@${ECHO} building static ${LIB} library
125	@rm -f ${.TARGET}
126.if !defined(NM)
127	@${AR} cq ${.TARGET} `lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
128.else
129	@${AR} cq ${.TARGET} `NM='${NM}' lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
130.endif
131	${RANLIB} ${.TARGET}
132.endif
133
134.if !defined(INTERNALLIB)
135
136.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
137_LIBS+=		lib${LIB}_p.a
138POBJS+=		${OBJS:.o=.po} ${STATICOBJS:.o=.po}
139
140lib${LIB}_p.a: ${POBJS}
141	@${ECHO} building profiled ${LIB} library
142	@rm -f ${.TARGET}
143.if !defined(NM)
144	@${AR} cq ${.TARGET} `lorder ${POBJS} | tsort -q` ${ARADD}
145.else
146	@${AR} cq ${.TARGET} `NM='${NM}' lorder ${POBJS} | tsort -q` ${ARADD}
147.endif
148	${RANLIB} ${.TARGET}
149.endif
150
151.if defined(SHLIB_NAME) || \
152    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
153SOBJS+=		${OBJS:.o=.So}
154.endif
155
156.if defined(SHLIB_NAME)
157_LIBS+=		${SHLIB_NAME}
158
159${SHLIB_NAME}: ${SOBJS}
160	@${ECHO} building shared library ${SHLIB_NAME}
161	@rm -f ${.TARGET} ${SHLIB_LINK}
162.if defined(SHLIB_LINK)
163	@ln -fs ${.TARGET} ${SHLIB_LINK}
164.endif
165.if !defined(NM)
166	@${CC} ${LDFLAGS} -shared -Wl,-x \
167	    -o ${.TARGET} -Wl,-soname,${SONAME} \
168	    `lorder ${SOBJS} | tsort -q` ${LDADD}
169.else
170	@${CC} ${LDFLAGS} -shared -Wl,-x \
171	    -o ${.TARGET} -Wl,-soname,${SONAME} \
172	    `NM='${NM}' lorder ${SOBJS} | tsort -q` ${LDADD}
173.endif
174.endif
175
176.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
177_LIBS+=		lib${LIB}_pic.a
178
179lib${LIB}_pic.a: ${SOBJS}
180	@${ECHO} building special pic ${LIB} library
181	@rm -f ${.TARGET}
182	@${AR} cq ${.TARGET} ${SOBJS} ${ARADD}
183	${RANLIB} ${.TARGET}
184.endif
185
186.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
187LINTLIB=	llib-l${LIB}.ln
188_LIBS+=		${LINTLIB}
189LINTOBJS+=	${SRCS:M*.c:.c=.ln}
190
191${LINTLIB}: ${LINTOBJS}
192	@${ECHO} building lint library ${.TARGET}
193	@rm -f ${.TARGET}
194	${LINT} ${LINTLIBFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
195.endif
196
197.endif # !defined(INTERNALLIB)
198
199all: ${_LIBS}
200
201.if ${MK_MAN} != "no"
202all: _manpages
203.endif
204
205_EXTRADEPEND:
206	@TMP=_depend$$$$; \
207	sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1.So:/' < ${DEPENDFILE} \
208	    > $$TMP; \
209	mv $$TMP ${DEPENDFILE}
210.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
211.if defined(DPADD) && !empty(DPADD)
212	echo ${SHLIB_NAME}: ${DPADD} >> ${DEPENDFILE}
213.endif
214.endif
215
216.if !target(install)
217
218.if defined(PRECIOUSLIB)
219.if !defined(NO_FSCHG)
220SHLINSTALLFLAGS+= -fschg
221.endif
222SHLINSTALLFLAGS+= -S
223.endif
224
225_INSTALLFLAGS:=	${INSTALLFLAGS}
226.for ie in ${INSTALLFLAGS_EDIT}
227_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
228.endfor
229_SHLINSTALLFLAGS:=	${SHLINSTALLFLAGS}
230.for ie in ${INSTALLFLAGS_EDIT}
231_SHLINSTALLFLAGS:=	${_SHLINSTALLFLAGS${ie}}
232.endfor
233
234.if !defined(INTERNALLIB)
235realinstall: _libinstall
236.ORDER: beforeinstall _libinstall
237_libinstall:
238.if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
239	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
240	    ${_INSTALLFLAGS} lib${LIB}.a ${DESTDIR}${LIBDIR}
241.endif
242.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
243	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
244	    ${_INSTALLFLAGS} lib${LIB}_p.a ${DESTDIR}${LIBDIR}
245.endif
246.if defined(SHLIB_NAME)
247	${INSTALL} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
248	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
249	    ${SHLIB_NAME} ${DESTDIR}${SHLIBDIR}
250.if defined(SHLIB_LINK)
251.if ${SHLIBDIR} == ${LIBDIR}
252	ln -fs ${SHLIB_NAME} ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
253.else
254	ln -fs ${_SHLIBDIRPREFIX}${SHLIBDIR}/${SHLIB_NAME} \
255	    ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
256.if exists(${DESTDIR}${LIBDIR}/${SHLIB_NAME})
257	-chflags noschg ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
258	rm -f ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
259.endif
260.endif
261.endif
262.endif
263.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
264	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
265	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${LIBDIR}
266.endif
267.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
268	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
269	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}
270.endif
271.endif # !defined(INTERNALLIB)
272
273.include <bsd.nls.mk>
274.include <bsd.files.mk>
275.include <bsd.incs.mk>
276.include <bsd.links.mk>
277
278.if ${MK_MAN} != "no"
279realinstall: _maninstall
280.ORDER: beforeinstall _maninstall
281.endif
282
283.endif
284
285.if !target(lint)
286lint: ${SRCS:M*.c}
287	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
288.endif
289
290.if ${MK_MAN} != "no"
291.include <bsd.man.mk>
292.endif
293
294.include <bsd.dep.mk>
295
296.if !exists(${.OBJDIR}/${DEPENDFILE})
297.if defined(LIB) && !empty(LIB)
298${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
299.for _S in ${SRCS:N*.[hly]}
300${_S:R}.po: ${_S}
301.endfor
302.endif
303.if defined(SHLIB_NAME) || \
304    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
305${SOBJS}: ${SRCS:M*.h}
306.for _S in ${SRCS:N*.[hly]}
307${_S:R}.So: ${_S}
308.endfor
309.endif
310.endif
311
312.if !target(clean)
313clean:
314.if defined(CLEANFILES) && !empty(CLEANFILES)
315	rm -f ${CLEANFILES}
316.endif
317.if defined(LIB) && !empty(LIB)
318	rm -f a.out ${OBJS} ${OBJS:S/$/.tmp/} ${STATICOBJS}
319.endif
320.if !defined(INTERNALLIB)
321.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
322	rm -f ${POBJS} ${POBJS:S/$/.tmp/}
323.endif
324.if defined(SHLIB_NAME) || \
325    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
326	rm -f ${SOBJS} ${SOBJS:.So=.so} ${SOBJS:S/$/.tmp/}
327.endif
328.if defined(SHLIB_NAME)
329.if defined(SHLIB_LINK)
330	rm -f ${SHLIB_LINK}
331.endif
332.if defined(LIB) && !empty(LIB)
333	rm -f lib${LIB}.so.* lib${LIB}.so
334.endif
335.endif
336.if defined(WANT_LINT) && defined(LIB) && !empty(LIB)
337	rm -f ${LINTOBJS}
338.endif
339.endif # !defined(INTERNALLIB)
340.if defined(_LIBS) && !empty(_LIBS)
341	rm -f ${_LIBS}
342.endif
343.if defined(CLEANDIRS) && !empty(CLEANDIRS)
344	rm -rf ${CLEANDIRS}
345.endif
346.if !empty(VERSION_DEF) && !empty(SYMBOL_MAPS)
347	rm -f ${VERSION_MAP}
348.endif
349.endif
350
351.include <bsd.obj.mk>
352
353.include <bsd.sys.mk>
354