bsd.lib.mk revision 250992
1#	from: @(#)bsd.lib.mk	5.26 (Berkeley) 5/2/91
2# $FreeBSD: head/share/mk/bsd.lib.mk 250992 2013-05-25 22:17:14Z jlh $
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 by the install target.
298#
299# ${_LDSCRIPTROOT} is the directory prefix that will be used when generating
300# ld(1) scripts.  The crosstools' ld is configured to lookup libraries in an
301# alternative directory which is called "sysroot", so during buildworld binaries
302# won't be linked against the running system libraries but against the ones of
303# the current source tree.  ${_LDSCRIPTROOT} behavior is twisted because of
304# the location where we store them:
305# - 64 bits libs are located under sysroot, so ${_LDSCRIPTROOT} must be empty
306#   because ld(1) will manage to find them from sysroot;
307# - 32 bits shims are not, so ${_LDSCRIPTROOT} is used to specify their full
308#   path, outside of sysroot.
309# Note that ld(1) scripts are generated both during buildworld and
310# installworld; in the later case ${_LDSCRIPTROOT} must be obviously empty
311# because on the target system, libraries are meant to be looked up from /.
312.if defined(SHLIB_LDSCRIPT) && !empty(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
313	sed -e 's,@@SHLIB@@,${_LDSCRIPTROOT}${SHLIBDIR}/${SHLIB_NAME},g' \
314	    -e 's,@@LIBDIR@@,${_LDSCRIPTROOT}${LIBDIR},g' \
315	    ${.CURDIR}/${SHLIB_LDSCRIPT} > lib${LIB}.ld
316	${INSTALL} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
317	    ${_INSTALLFLAGS} lib${LIB}.ld ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
318.else
319.if ${SHLIBDIR} == ${LIBDIR}
320	${INSTALL_SYMLINK} ${SHLIB_NAME} ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
321.else
322	${INSTALL_SYMLINK} ${_SHLIBDIRPREFIX}${SHLIBDIR}/${SHLIB_NAME} \
323	    ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
324.if exists(${DESTDIR}${LIBDIR}/${SHLIB_NAME})
325	-chflags noschg ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
326	rm -f ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
327.endif
328.endif
329.endif # SHLIB_LDSCRIPT
330.endif # SHLIB_LINK
331.endif # SHIB_NAME
332.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
333	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
334	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${LIBDIR}
335.endif
336.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
337	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
338	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}
339.endif
340.endif # !defined(INTERNALLIB)
341
342.if !defined(LIBRARIES_ONLY)
343.include <bsd.nls.mk>
344.include <bsd.files.mk>
345.include <bsd.incs.mk>
346.endif
347
348.include <bsd.links.mk>
349
350.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
351realinstall: _maninstall
352.ORDER: beforeinstall _maninstall
353.endif
354
355.endif
356
357.if !target(lint)
358lint: ${SRCS:M*.c}
359	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
360.endif
361
362.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
363.include <bsd.man.mk>
364.endif
365
366.include <bsd.dep.mk>
367
368.if !exists(${.OBJDIR}/${DEPENDFILE})
369.if defined(LIB) && !empty(LIB)
370${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
371.for _S in ${SRCS:N*.[hly]}
372${_S:R}.po: ${_S}
373.endfor
374.endif
375.if defined(SHLIB_NAME) || \
376    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
377${SOBJS}: ${SRCS:M*.h}
378.for _S in ${SRCS:N*.[hly]}
379${_S:R}.So: ${_S}
380.endfor
381.endif
382.endif
383
384.if !target(clean)
385clean:
386.if defined(CLEANFILES) && !empty(CLEANFILES)
387	rm -f ${CLEANFILES}
388.endif
389.if defined(LIB) && !empty(LIB)
390	rm -f a.out ${OBJS} ${OBJS:S/$/.tmp/} ${STATICOBJS}
391.endif
392.if !defined(INTERNALLIB)
393.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
394	rm -f ${POBJS} ${POBJS:S/$/.tmp/}
395.endif
396.if defined(SHLIB_NAME) || \
397    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
398	rm -f ${SOBJS} ${SOBJS:.So=.so} ${SOBJS:S/$/.tmp/}
399.endif
400.if defined(SHLIB_NAME)
401.if defined(SHLIB_LINK)
402.if defined(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
403	rm -f lib${LIB}.ld
404.endif
405	rm -f ${SHLIB_LINK}
406.endif
407.endif # defined(SHLIB_NAME)
408.if defined(WANT_LINT) && defined(LIB) && !empty(LIB)
409	rm -f ${LINTOBJS}
410.endif
411.endif # !defined(INTERNALLIB)
412.if defined(_LIBS) && !empty(_LIBS)
413	rm -f ${_LIBS}
414.endif
415.if defined(CLEANDIRS) && !empty(CLEANDIRS)
416	rm -rf ${CLEANDIRS}
417.endif
418.if !empty(VERSION_DEF) && !empty(SYMBOL_MAPS)
419	rm -f ${VERSION_MAP}
420.endif
421.endif
422
423.include <bsd.obj.mk>
424
425.include <bsd.sys.mk>
426