xref: /freebsd/share/mk/bsd.lib.mk (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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)
210.if defined(LD_FATAL_WARNINGS) && ${LD_FATAL_WARNINGS} == "no"
211SOLINKOPTS+=	-Wl,--no-fatal-warnings
212.else
213SOLINKOPTS+=	-Wl,--fatal-warnings
214.endif
215SOLINKOPTS+=	-Wl,--warn-shared-textrel
216.endif
217
218.if target(beforelinking)
219beforelinking: ${SOBJS}
220${SHLIB_NAME_FULL}: beforelinking
221.endif
222${SHLIB_NAME_FULL}: ${SOBJS}
223	@${ECHO} building shared library ${SHLIB_NAME}
224	@rm -f ${SHLIB_NAME} ${SHLIB_LINK}
225.if defined(SHLIB_LINK)
226	@${INSTALL_SYMLINK} ${SHLIB_NAME} ${SHLIB_LINK}
227.endif
228.if !defined(NM)
229	@${CC} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
230	    -o ${.TARGET} -Wl,-soname,${SONAME} \
231	    `lorder ${SOBJS} | tsort -q` ${LDADD}
232.else
233	@${CC} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
234	    -o ${.TARGET} -Wl,-soname,${SONAME} \
235	    `NM='${NM}' lorder ${SOBJS} | tsort -q` ${LDADD}
236.endif
237.if ${MK_CTF} != "no"
238	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SOBJS}
239.endif
240
241.if ${MK_DEBUG_FILES} != "no"
242CLEANFILES+=	${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
243${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
244	${OBJCOPY} --strip-debug --add-gnu-debuglink=${SHLIB_NAME}.debug \
245	    ${SHLIB_NAME_FULL} ${.TARGET}
246
247${SHLIB_NAME}.debug: ${SHLIB_NAME_FULL}
248	${OBJCOPY} --only-keep-debug ${SHLIB_NAME_FULL} ${.TARGET}
249.endif
250.endif #defined(SHLIB_NAME)
251
252.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
253_LIBS+=		lib${LIB}_pic.a
254
255lib${LIB}_pic.a: ${SOBJS}
256	@${ECHO} building special pic ${LIB} library
257	@rm -f ${.TARGET}
258	@${AR} ${ARFLAGS} ${.TARGET} ${SOBJS} ${ARADD}
259	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
260.endif
261
262.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
263LINTLIB=	llib-l${LIB}.ln
264_LIBS+=		${LINTLIB}
265LINTOBJS+=	${SRCS:M*.c:.c=.ln}
266NOPATH_FILES+=	${LINTOBJS}
267
268${LINTLIB}: ${LINTOBJS}
269	@${ECHO} building lint library ${.TARGET}
270	@rm -f ${.TARGET}
271	${LINT} ${LINTLIBFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
272.endif
273
274.endif # !defined(INTERNALLIB)
275
276all: ${_LIBS}
277
278.if ${MK_MAN} != "no"
279all: _manpages
280.endif
281
282_EXTRADEPEND:
283	@TMP=_depend$$$$; \
284	sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1.So:/' < ${DEPENDFILE} \
285	    > $$TMP; \
286	mv $$TMP ${DEPENDFILE}
287.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
288.if defined(DPADD) && !empty(DPADD)
289	echo ${SHLIB_NAME}: ${DPADD} >> ${DEPENDFILE}
290.endif
291.endif
292
293.if !target(install)
294
295.if defined(PRECIOUSLIB)
296.if !defined(NO_FSCHG)
297SHLINSTALLFLAGS+= -fschg
298.endif
299SHLINSTALLFLAGS+= -S
300.endif
301
302_INSTALLFLAGS:=	${INSTALLFLAGS}
303.for ie in ${INSTALLFLAGS_EDIT}
304_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
305.endfor
306_SHLINSTALLFLAGS:=	${SHLINSTALLFLAGS}
307.for ie in ${INSTALLFLAGS_EDIT}
308_SHLINSTALLFLAGS:=	${_SHLINSTALLFLAGS${ie}}
309.endfor
310
311.if !defined(INTERNALLIB)
312realinstall: _libinstall
313.ORDER: beforeinstall _libinstall
314_libinstall:
315.if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no" && !defined(PRIVATELIB)
316	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
317	    ${_INSTALLFLAGS} lib${LIB}.a ${DESTDIR}${_LIBDIR}
318.endif
319.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB) && !defined(PRIVATELIB)
320	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
321	    ${_INSTALLFLAGS} lib${LIB}_p.a ${DESTDIR}${_LIBDIR}
322.endif
323.if defined(SHLIB_NAME)
324	${INSTALL} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
325	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
326	    ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}
327.if ${MK_DEBUG_FILES} != "no"
328.if defined(DEBUGMKDIR)
329	${INSTALL} -T debug -d ${DESTDIR}${DEBUGFILEDIR}
330.endif
331	${INSTALL} -T debug -o ${LIBOWN} -g ${LIBGRP} -m ${DEBUGMODE} \
332	    ${_INSTALLFLAGS} \
333	    ${SHLIB_NAME}.debug ${DESTDIR}${DEBUGFILEDIR}
334.endif
335.if defined(SHLIB_LINK) && !defined(PRIVATELIB)
336# ${_SHLIBDIRPREFIX} and ${_LDSCRIPTROOT} are both needed when cross-building
337# and when building 32 bits library shims.  ${_SHLIBDIRPREFIX} is the directory
338# prefix where shared objects will be installed by the install target.
339#
340# ${_LDSCRIPTROOT} is the directory prefix that will be used when generating
341# ld(1) scripts.  The crosstools' ld is configured to lookup libraries in an
342# alternative directory which is called "sysroot", so during buildworld binaries
343# won't be linked against the running system libraries but against the ones of
344# the current source tree.  ${_LDSCRIPTROOT} behavior is twisted because of
345# the location where we store them:
346# - 64 bits libs are located under sysroot, so ${_LDSCRIPTROOT} must be empty
347#   because ld(1) will manage to find them from sysroot;
348# - 32 bits shims are not, so ${_LDSCRIPTROOT} is used to specify their full
349#   path, outside of sysroot.
350# Note that ld(1) scripts are generated both during buildworld and
351# installworld; in the later case ${_LDSCRIPTROOT} must be obviously empty
352# because on the target system, libraries are meant to be looked up from /.
353.if defined(SHLIB_LDSCRIPT) && !empty(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
354	sed -e 's,@@SHLIB@@,${_LDSCRIPTROOT}${_SHLIBDIR}/${SHLIB_NAME},g' \
355	    -e 's,@@LIBDIR@@,${_LDSCRIPTROOT}${_LIBDIR},g' \
356	    ${.CURDIR}/${SHLIB_LDSCRIPT} > ${DESTDIR}${_LIBDIR}/${SHLIB_LINK:R}.ld
357	${INSTALL} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
358	    ${_INSTALLFLAGS} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK:R}.ld \
359	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
360	rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_LINK:R}.ld
361
362.else
363.if ${_SHLIBDIR} == ${_LIBDIR}
364	${INSTALL_SYMLINK} ${SHLIB_NAME} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
365.else
366	${INSTALL_SYMLINK} ${_SHLIBDIRPREFIX}${_SHLIBDIR}/${SHLIB_NAME} \
367	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
368.if exists(${DESTDIR}${_LIBDIR}/${SHLIB_NAME})
369	-chflags noschg ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
370	rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
371.endif
372.endif
373.endif # SHLIB_LDSCRIPT
374.endif # SHLIB_LINK
375.endif # SHIB_NAME
376.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no" && !defined(PRIVATELIB)
377	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
378	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${_LIBDIR}
379.endif
380.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
381	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
382	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}
383.endif
384.endif # !defined(INTERNALLIB)
385
386.if !defined(LIBRARIES_ONLY)
387.include <bsd.nls.mk>
388.include <bsd.files.mk>
389.include <bsd.incs.mk>
390.endif
391
392.include <bsd.links.mk>
393
394.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
395realinstall: _maninstall
396.ORDER: beforeinstall _maninstall
397.endif
398
399.endif
400
401.if !target(lint)
402lint: ${SRCS:M*.c}
403	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
404.endif
405
406.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
407.include <bsd.man.mk>
408.endif
409
410.include <bsd.dep.mk>
411
412.if !exists(${.OBJDIR}/${DEPENDFILE})
413.if defined(LIB) && !empty(LIB)
414${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
415.for _S in ${SRCS:N*.[hly]}
416${_S:R}.po: ${_S}
417.endfor
418.endif
419.if defined(SHLIB_NAME) || \
420    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
421${SOBJS}: ${SRCS:M*.h}
422.for _S in ${SRCS:N*.[hly]}
423${_S:R}.So: ${_S}
424.endfor
425.endif
426.endif
427
428.if !target(clean)
429clean:
430.if defined(CLEANFILES) && !empty(CLEANFILES)
431	rm -f ${CLEANFILES}
432.endif
433.if defined(LIB) && !empty(LIB)
434	rm -f a.out ${OBJS} ${OBJS:S/$/.tmp/} ${STATICOBJS}
435.endif
436.if !defined(INTERNALLIB)
437.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
438	rm -f ${POBJS} ${POBJS:S/$/.tmp/}
439.endif
440.if defined(SHLIB_NAME) || \
441    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
442	rm -f ${SOBJS} ${SOBJS:.So=.so} ${SOBJS:S/$/.tmp/}
443.endif
444.if defined(SHLIB_NAME)
445.if defined(SHLIB_LINK)
446.if defined(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
447	rm -f lib${LIB}.ld
448.endif
449	rm -f ${SHLIB_LINK}
450.endif
451.endif # defined(SHLIB_NAME)
452.if defined(WANT_LINT) && defined(LIB) && !empty(LIB)
453	rm -f ${LINTOBJS}
454.endif
455.endif # !defined(INTERNALLIB)
456.if defined(_LIBS) && !empty(_LIBS)
457	rm -f ${_LIBS}
458.endif
459.if defined(CLEANDIRS) && !empty(CLEANDIRS)
460	rm -rf ${CLEANDIRS}
461.endif
462.if !empty(VERSION_DEF) && !empty(SYMBOL_MAPS)
463	rm -f ${VERSION_MAP}
464.endif
465.endif
466
467.if !empty(_LIBS)
468NOPATH_FILES+=	${_LIBS}
469.endif
470
471.include <bsd.obj.mk>
472
473.include <bsd.sys.mk>
474