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