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