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