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