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