xref: /freebsd/share/mk/bsd.lib.mk (revision f05cddf9)
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(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} > ${DESTDIR}${LIBDIR}/${SHLIB_LINK:R}.ld
334	${INSTALL} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
335	    ${_INSTALLFLAGS} ${DESTDIR}${LIBDIR}/${SHLIB_LINK:R}.ld \
336	    ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
337	rm -f ${DESTDIR}${LIBDIR}/${SHLIB_LINK:R}.ld
338
339.else
340.if ${SHLIBDIR} == ${LIBDIR}
341	${INSTALL_SYMLINK} ${SHLIB_NAME} ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
342.else
343	${INSTALL_SYMLINK} ${_SHLIBDIRPREFIX}${SHLIBDIR}/${SHLIB_NAME} \
344	    ${DESTDIR}${LIBDIR}/${SHLIB_LINK}
345.if exists(${DESTDIR}${LIBDIR}/${SHLIB_NAME})
346	-chflags noschg ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
347	rm -f ${DESTDIR}${LIBDIR}/${SHLIB_NAME}
348.endif
349.endif
350.endif # SHLIB_LDSCRIPT
351.endif # SHLIB_LINK
352.endif # SHIB_NAME
353.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
354	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
355	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${LIBDIR}
356.endif
357.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
358	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
359	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}
360.endif
361.endif # !defined(INTERNALLIB)
362
363.if !defined(LIBRARIES_ONLY)
364.include <bsd.nls.mk>
365.include <bsd.files.mk>
366.include <bsd.incs.mk>
367.endif
368
369.include <bsd.links.mk>
370
371.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
372realinstall: _maninstall
373.ORDER: beforeinstall _maninstall
374.endif
375
376.endif
377
378.if !target(lint)
379lint: ${SRCS:M*.c}
380	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
381.endif
382
383.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
384.include <bsd.man.mk>
385.endif
386
387.include <bsd.dep.mk>
388
389.if !exists(${.OBJDIR}/${DEPENDFILE})
390.if defined(LIB) && !empty(LIB)
391${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
392.for _S in ${SRCS:N*.[hly]}
393${_S:R}.po: ${_S}
394.endfor
395.endif
396.if defined(SHLIB_NAME) || \
397    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
398${SOBJS}: ${SRCS:M*.h}
399.for _S in ${SRCS:N*.[hly]}
400${_S:R}.So: ${_S}
401.endfor
402.endif
403.endif
404
405.if !target(clean)
406clean:
407.if defined(CLEANFILES) && !empty(CLEANFILES)
408	rm -f ${CLEANFILES}
409.endif
410.if defined(LIB) && !empty(LIB)
411	rm -f a.out ${OBJS} ${OBJS:S/$/.tmp/} ${STATICOBJS}
412.endif
413.if !defined(INTERNALLIB)
414.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
415	rm -f ${POBJS} ${POBJS:S/$/.tmp/}
416.endif
417.if defined(SHLIB_NAME) || \
418    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
419	rm -f ${SOBJS} ${SOBJS:.So=.so} ${SOBJS:S/$/.tmp/}
420.endif
421.if defined(SHLIB_NAME)
422.if defined(SHLIB_LINK)
423.if defined(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
424	rm -f lib${LIB}.ld
425.endif
426	rm -f ${SHLIB_LINK}
427.endif
428.endif # defined(SHLIB_NAME)
429.if defined(WANT_LINT) && defined(LIB) && !empty(LIB)
430	rm -f ${LINTOBJS}
431.endif
432.endif # !defined(INTERNALLIB)
433.if defined(_LIBS) && !empty(_LIBS)
434	rm -f ${_LIBS}
435.endif
436.if defined(CLEANDIRS) && !empty(CLEANDIRS)
437	rm -rf ${CLEANDIRS}
438.endif
439.if !empty(VERSION_DEF) && !empty(SYMBOL_MAPS)
440	rm -f ${VERSION_MAP}
441.endif
442.endif
443
444.include <bsd.obj.mk>
445
446.include <bsd.sys.mk>
447