xref: /freebsd/share/mk/bsd.lib.mk (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
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
39.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
40CTFFLAGS+= -g
41.endif
42.else
43STRIP?=	-s
44.endif
45
46.include <bsd.libnames.mk>
47
48# prefer .s to a .c, add .po, remove stuff not used in the BSD libraries
49# .So used for PIC object files
50.SUFFIXES:
51.SUFFIXES: .out .o .po .So .S .asm .s .c .cc .cpp .cxx .C .f .y .l .ln
52
53.if !defined(PICFLAG)
54.if ${MACHINE_CPUARCH} == "sparc64"
55PICFLAG=-fPIC
56.else
57PICFLAG=-fpic
58.endif
59.endif
60
61PO_FLAG=-pg
62
63.c.o:
64	${CC} ${STATIC_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
65	${CTFCONVERT_CMD}
66
67.c.po:
68	${CC} ${PO_FLAG} ${STATIC_CFLAGS} ${PO_CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
69	${CTFCONVERT_CMD}
70
71.c.So:
72	${CC} ${PICFLAG} -DPIC ${SHARED_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
73	${CTFCONVERT_CMD}
74
75.cc.o .C.o .cpp.o .cxx.o:
76	${CXX} ${STATIC_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
77
78.cc.po .C.po .cpp.po .cxx.po:
79	${CXX} ${PO_FLAG} ${STATIC_CXXFLAGS} ${PO_CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
80
81.cc.So .C.So .cpp.So .cxx.So:
82	${CXX} ${PICFLAG} -DPIC ${SHARED_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
83
84.f.po:
85	${FC} -pg ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
86	${CTFCONVERT_CMD}
87
88.f.So:
89	${FC} ${PICFLAG} -DPIC ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
90	${CTFCONVERT_CMD}
91
92.s.po .s.So:
93	${AS} ${AFLAGS} -o ${.TARGET} ${.IMPSRC}
94	${CTFCONVERT_CMD}
95
96.asm.po:
97	${CC} -x assembler-with-cpp -DPROF ${PO_CFLAGS} ${ACFLAGS} \
98		-c ${.IMPSRC} -o ${.TARGET}
99	${CTFCONVERT_CMD}
100
101.asm.So:
102	${CC} -x assembler-with-cpp ${PICFLAG} -DPIC ${CFLAGS} ${ACFLAGS} \
103	    -c ${.IMPSRC} -o ${.TARGET}
104	${CTFCONVERT_CMD}
105
106.S.po:
107	${CC} -DPROF ${PO_CFLAGS} ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
108	${CTFCONVERT_CMD}
109
110.S.So:
111	${CC} ${PICFLAG} -DPIC ${CFLAGS} ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
112	${CTFCONVERT_CMD}
113
114all: objwarn
115
116.if defined(SHLIB_NAME)
117.if defined(DEBUG_FLAGS)
118SHLIB_NAME_FULL=${SHLIB_NAME}.debug
119.else
120SHLIB_NAME_FULL=${SHLIB_NAME}
121.endif
122.endif
123
124.include <bsd.symver.mk>
125
126# Allow libraries to specify their own version map or have it
127# automatically generated (see bsd.symver.mk above).
128.if ${MK_SYMVER} == "yes" && !empty(VERSION_MAP)
129${SHLIB_NAME_FULL}:	${VERSION_MAP}
130LDFLAGS+=	-Wl,--version-script=${VERSION_MAP}
131.endif
132
133.if defined(LIB) && !empty(LIB) || defined(SHLIB_NAME)
134OBJS+=		${SRCS:N*.h:R:S/$/.o/}
135.endif
136
137.if defined(LIB) && !empty(LIB)
138_LIBS=		lib${LIB}.a
139
140lib${LIB}.a: ${OBJS} ${STATICOBJS}
141	@${ECHO} building static ${LIB} library
142	@rm -f ${.TARGET}
143.if !defined(NM)
144	@${AR} ${ARFLAGS} ${.TARGET} `lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
145.else
146	@${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
147.endif
148	${RANLIB} ${.TARGET}
149.endif
150
151.if !defined(INTERNALLIB)
152
153.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
154_LIBS+=		lib${LIB}_p.a
155POBJS+=		${OBJS:.o=.po} ${STATICOBJS:.o=.po}
156
157lib${LIB}_p.a: ${POBJS}
158	@${ECHO} building profiled ${LIB} library
159	@rm -f ${.TARGET}
160.if !defined(NM)
161	@${AR} ${ARFLAGS} ${.TARGET} `lorder ${POBJS} | tsort -q` ${ARADD}
162.else
163	@${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' lorder ${POBJS} | tsort -q` ${ARADD}
164.endif
165	${RANLIB} ${.TARGET}
166.endif
167
168.if defined(SHLIB_NAME) || \
169    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
170SOBJS+=		${OBJS:.o=.So}
171.endif
172
173.if defined(SHLIB_NAME)
174_LIBS+=		${SHLIB_NAME}
175
176SOLINKOPTS=	-shared -Wl,-x
177.if !defined(ALLOW_SHARED_TEXTREL)
178SOLINKOPTS+=	-Wl,--fatal-warnings -Wl,--warn-shared-textrel
179.endif
180
181.if target(beforelinking)
182beforelinking: ${SOBJS}
183${SHLIB_NAME_FULL}: beforelinking
184.endif
185${SHLIB_NAME_FULL}: ${SOBJS}
186	@${ECHO} building shared library ${SHLIB_NAME}
187	@rm -f ${SHLIB_NAME} ${SHLIB_LINK}
188.if defined(SHLIB_LINK)
189	@${INSTALL_SYMLINK} ${SHLIB_NAME} ${SHLIB_LINK}
190.endif
191.if !defined(NM)
192	@${CC} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
193	    -o ${.TARGET} -Wl,-soname,${SONAME} \
194	    `lorder ${SOBJS} | tsort -q` ${LDADD}
195.else
196	@${CC} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
197	    -o ${.TARGET} -Wl,-soname,${SONAME} \
198	    `NM='${NM}' lorder ${SOBJS} | tsort -q` ${LDADD}
199.endif
200.if ${MK_CTF} != "no"
201	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SOBJS}
202.endif
203
204.if defined(DEBUG_FLAGS)
205CLEANFILES+=	${SHLIB_NAME_FULL} ${SHLIB_NAME}.symbols
206${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${SHLIB_NAME}.symbols
207	${OBJCOPY} --strip-debug --add-gnu-debuglink=${SHLIB_NAME}.symbols \
208	    ${SHLIB_NAME_FULL} ${.TARGET}
209
210${SHLIB_NAME}.symbols: ${SHLIB_NAME_FULL}
211	${OBJCOPY} --only-keep-debug ${SHLIB_NAME_FULL} ${.TARGET}
212.endif
213.endif #defined(SHLIB_NAME)
214
215.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
216_LIBS+=		lib${LIB}_pic.a
217
218lib${LIB}_pic.a: ${SOBJS}
219	@${ECHO} building special pic ${LIB} library
220	@rm -f ${.TARGET}
221	@${AR} ${ARFLAGS} ${.TARGET} ${SOBJS} ${ARADD}
222	${RANLIB} ${.TARGET}
223.endif
224
225.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
226LINTLIB=	llib-l${LIB}.ln
227_LIBS+=		${LINTLIB}
228LINTOBJS+=	${SRCS:M*.c:.c=.ln}
229
230${LINTLIB}: ${LINTOBJS}
231	@${ECHO} building lint library ${.TARGET}
232	@rm -f ${.TARGET}
233	${LINT} ${LINTLIBFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
234.endif
235
236.endif # !defined(INTERNALLIB)
237
238all: ${_LIBS}
239
240.if ${MK_MAN} != "no"
241all: _manpages
242.endif
243
244_EXTRADEPEND:
245	@TMP=_depend$$$$; \
246	sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1.So:/' < ${DEPENDFILE} \
247	    > $$TMP; \
248	mv $$TMP ${DEPENDFILE}
249.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
250.if defined(DPADD) && !empty(DPADD)
251	echo ${SHLIB_NAME}: ${DPADD} >> ${DEPENDFILE}
252.endif
253.endif
254
255.if !target(install)
256
257.if defined(PRECIOUSLIB)
258.if !defined(NO_FSCHG)
259SHLINSTALLFLAGS+= -fschg
260.endif
261SHLINSTALLFLAGS+= -S
262.endif
263
264_INSTALLFLAGS:=	${INSTALLFLAGS}
265.for ie in ${INSTALLFLAGS_EDIT}
266_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
267.endfor
268_SHLINSTALLFLAGS:=	${SHLINSTALLFLAGS}
269.for ie in ${INSTALLFLAGS_EDIT}
270_SHLINSTALLFLAGS:=	${_SHLINSTALLFLAGS${ie}}
271.endfor
272
273.if !defined(INTERNALLIB)
274realinstall: _libinstall
275.ORDER: beforeinstall _libinstall
276_libinstall:
277.if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
278	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
279	    ${_INSTALLFLAGS} lib${LIB}.a ${DESTDIR}${LIBDIR}
280.endif
281.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
282	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
283	    ${_INSTALLFLAGS} lib${LIB}_p.a ${DESTDIR}${LIBDIR}
284.endif
285.if defined(SHLIB_NAME)
286	${INSTALL} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
287	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
288	    ${SHLIB_NAME} ${DESTDIR}${SHLIBDIR}
289.if defined(DEBUG_FLAGS)
290	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
291	    ${_INSTALLFLAGS} \
292	    ${SHLIB_NAME}.symbols ${DESTDIR}${SHLIBDIR}
293.endif
294.if defined(SHLIB_LINK)
295# ${_SHLIBDIRPREFIX} and ${_LDSCRIPTROOT} are both needed when cross-building
296# and when building 32 bits library shims.  ${_SHLIBDIRPREFIX} is the directory
297# prefix where shared objects will be installed.  ${_LDSCRIPTROOT} is the
298# directory prefix that will be used in generated ld(1) scripts.  They cannot
299# be coalesced because of the way ld(1) handles the sysroot prefix (used in the
300# cross-toolchain):
301# - 64 bits libs are located under sysroot, so ${_LDSCRIPTROOT} must be empty.
302# - 32 bits shims are not, so ${_LDSCRIPTROOT} is used to specify their full
303#   path.  Note that ld(1) scripts are generated both during buildworld and
304#   installworld; in the later case ${_LDSCRIPTROOT} must be obviously empty.
305# On the other hand, the use of ${_SHLIBDIRPREFIX} is more consistent since it
306# does not involve the logic of a tool we do not own.
307.if defined(SHLIB_LDSCRIPT) && !empty(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
308	sed -e 's,@@SHLIB@@,${_LDSCRIPTROOT}${SHLIBDIR}/${SHLIB_NAME},g' \
309	    -e 's,@@LIBDIR@@,${_LDSCRIPTROOT}${LIBDIR},g' \
310	    ${.CURDIR}/${SHLIB_LDSCRIPT} > lib${LIB}.ld
311	${INSTALL} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
312	    ${_INSTALLFLAGS} lib${LIB}.ld ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
313.else
314.if ${SHLIBDIR} == ${LIBDIR}
315	${INSTALL_SYMLINK} ${SHLIB_NAME} ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
316.else
317	${INSTALL_SYMLINK} ${_SHLIBDIRPREFIX}${SHLIBDIR}/${SHLIB_NAME} \
318	    ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
319.if exists(${DESTDIR}${LIBDIR}/${SHLIB_NAME})
320	-chflags noschg ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
321	rm -f ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
322.endif
323.endif
324.endif # SHLIB_LDSCRIPT
325.endif # SHLIB_LINK
326.endif # SHIB_NAME
327.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
328	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
329	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${LIBDIR}
330.endif
331.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
332	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
333	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}
334.endif
335.endif # !defined(INTERNALLIB)
336
337.if !defined(LIBRARIES_ONLY)
338.include <bsd.nls.mk>
339.include <bsd.files.mk>
340.include <bsd.incs.mk>
341.endif
342
343.include <bsd.links.mk>
344
345.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
346realinstall: _maninstall
347.ORDER: beforeinstall _maninstall
348.endif
349
350.endif
351
352.if !target(lint)
353lint: ${SRCS:M*.c}
354	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
355.endif
356
357.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
358.include <bsd.man.mk>
359.endif
360
361.include <bsd.dep.mk>
362
363.if !exists(${.OBJDIR}/${DEPENDFILE})
364.if defined(LIB) && !empty(LIB)
365${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
366.for _S in ${SRCS:N*.[hly]}
367${_S:R}.po: ${_S}
368.endfor
369.endif
370.if defined(SHLIB_NAME) || \
371    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
372${SOBJS}: ${SRCS:M*.h}
373.for _S in ${SRCS:N*.[hly]}
374${_S:R}.So: ${_S}
375.endfor
376.endif
377.endif
378
379.if !target(clean)
380clean:
381.if defined(CLEANFILES) && !empty(CLEANFILES)
382	rm -f ${CLEANFILES}
383.endif
384.if defined(LIB) && !empty(LIB)
385	rm -f a.out ${OBJS} ${OBJS:S/$/.tmp/} ${STATICOBJS}
386.endif
387.if !defined(INTERNALLIB)
388.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
389	rm -f ${POBJS} ${POBJS:S/$/.tmp/}
390.endif
391.if defined(SHLIB_NAME) || \
392    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
393	rm -f ${SOBJS} ${SOBJS:.So=.so} ${SOBJS:S/$/.tmp/}
394.endif
395.if defined(SHLIB_NAME)
396.if defined(SHLIB_LINK)
397.if defined(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
398	rm -f lib${LIB}.ld
399.endif
400	rm -f ${SHLIB_LINK}
401.endif
402.endif # defined(SHLIB_NAME)
403.if defined(WANT_LINT) && defined(LIB) && !empty(LIB)
404	rm -f ${LINTOBJS}
405.endif
406.endif # !defined(INTERNALLIB)
407.if defined(_LIBS) && !empty(_LIBS)
408	rm -f ${_LIBS}
409.endif
410.if defined(CLEANDIRS) && !empty(CLEANDIRS)
411	rm -rf ${CLEANDIRS}
412.endif
413.if !empty(VERSION_DEF) && !empty(SYMBOL_MAPS)
414	rm -f ${VERSION_MAP}
415.endif
416.endif
417
418.include <bsd.obj.mk>
419
420.include <bsd.sys.mk>
421