xref: /freebsd/share/mk/bsd.lib.mk (revision 3884d6f8bd7e13a2b152fedc2d0d4f6de5ad17b8)
1#	from: @(#)bsd.lib.mk	5.26 (Berkeley) 5/2/91
2# $FreeBSD$
3#
4
5.include <bsd.init.mk>
6
7.if defined(LIB_CXX)
8LIB=	${LIB_CXX}
9_LD=	${CXX}
10.else
11_LD=	${CC}
12.endif
13
14LIB_PRIVATE=	${PRIVATELIB:Dprivate}
15# Set up the variables controlling shared libraries.  After this section,
16# SHLIB_NAME will be defined only if we are to create a shared library.
17# SHLIB_LINK will be defined only if we are to create a link to it.
18# INSTALL_PIC_ARCHIVE will be defined only if we are to create a PIC archive.
19.if defined(NO_PIC)
20.undef SHLIB_NAME
21.undef INSTALL_PIC_ARCHIVE
22.else
23.if !defined(SHLIB) && defined(LIB)
24SHLIB=		${LIB}
25.endif
26.if !defined(SHLIB_NAME) && defined(SHLIB) && defined(SHLIB_MAJOR)
27SHLIB_NAME=	lib${LIB_PRIVATE}${SHLIB}.so.${SHLIB_MAJOR}
28.endif
29.if defined(SHLIB_NAME) && !empty(SHLIB_NAME:M*.so.*)
30SHLIB_LINK?=	${SHLIB_NAME:R}
31.endif
32SONAME?=	${SHLIB_NAME}
33.endif
34
35.if defined(CRUNCH_CFLAGS)
36CFLAGS+=	${CRUNCH_CFLAGS}
37.endif
38
39.if ${MK_ASSERT_DEBUG} == "no"
40CFLAGS+= -DNDEBUG
41NO_WERROR=
42.endif
43
44.if defined(DEBUG_FLAGS)
45CFLAGS+= ${DEBUG_FLAGS}
46
47.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
48CTFFLAGS+= -g
49.endif
50.else
51STRIP?=	-s
52.endif
53
54.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
55    empty(DEBUG_FLAGS:M-gdwarf*)
56SHARED_CFLAGS+= -g
57SHARED_CXXFLAGS+= -g
58CTFFLAGS+= -g
59.endif
60
61.include <bsd.libnames.mk>
62
63# prefer .s to a .c, add .po, remove stuff not used in the BSD libraries
64# .So used for PIC object files
65.SUFFIXES:
66.SUFFIXES: .out .o .po .So .S .asm .s .c .cc .cpp .cxx .C .f .y .l .ln
67
68.if !defined(PICFLAG)
69.if ${MACHINE_CPUARCH} == "sparc64"
70PICFLAG=-fPIC
71.else
72PICFLAG=-fpic
73.endif
74.endif
75
76PO_FLAG=-pg
77
78.c.o:
79	${CC} ${STATIC_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
80	${CTFCONVERT_CMD}
81
82.c.po:
83	${CC} ${PO_FLAG} ${STATIC_CFLAGS} ${PO_CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
84	${CTFCONVERT_CMD}
85
86.c.So:
87	${CC} ${PICFLAG} -DPIC ${SHARED_CFLAGS} ${CFLAGS} -c ${.IMPSRC} -o ${.TARGET}
88	${CTFCONVERT_CMD}
89
90.cc.o .C.o .cpp.o .cxx.o:
91	${CXX} ${STATIC_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
92
93.cc.po .C.po .cpp.po .cxx.po:
94	${CXX} ${PO_FLAG} ${STATIC_CXXFLAGS} ${PO_CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
95
96.cc.So .C.So .cpp.So .cxx.So:
97	${CXX} ${PICFLAG} -DPIC ${SHARED_CXXFLAGS} ${CXXFLAGS} -c ${.IMPSRC} -o ${.TARGET}
98
99.f.po:
100	${FC} -pg ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
101	${CTFCONVERT_CMD}
102
103.f.So:
104	${FC} ${PICFLAG} -DPIC ${FFLAGS} -o ${.TARGET} -c ${.IMPSRC}
105	${CTFCONVERT_CMD}
106
107.s.po .s.So:
108	${AS} ${AFLAGS} -o ${.TARGET} ${.IMPSRC}
109	${CTFCONVERT_CMD}
110
111.asm.po:
112	${CC:N${CCACHE_BIN}} -x assembler-with-cpp -DPROF ${PO_CFLAGS} \
113	    ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
114	${CTFCONVERT_CMD}
115
116.asm.So:
117	${CC:N${CCACHE_BIN}} -x assembler-with-cpp ${PICFLAG} -DPIC \
118	    ${CFLAGS} ${ACFLAGS} -c ${.IMPSRC} -o ${.TARGET}
119	${CTFCONVERT_CMD}
120
121.S.po:
122	${CC:N${CCACHE_BIN}} -DPROF ${PO_CFLAGS} ${ACFLAGS} -c ${.IMPSRC} \
123	    -o ${.TARGET}
124	${CTFCONVERT_CMD}
125
126.S.So:
127	${CC:N${CCACHE_BIN}} ${PICFLAG} -DPIC ${CFLAGS} ${ACFLAGS} \
128	    -c ${.IMPSRC} -o ${.TARGET}
129	${CTFCONVERT_CMD}
130
131_LIBDIR:=${LIBDIR}
132_SHLIBDIR:=${SHLIBDIR}
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/(tests/)?lib(32|exec)?(/.*)?%/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(LIB) && !empty(LIB) || defined(SHLIB_NAME)
161OBJS+=		${SRCS:N*.h:R:S/$/.o/}
162CLEANFILES+=	${OBJS} ${STATICOBJS}
163.endif
164
165.if defined(LIB) && !empty(LIB)
166_LIBS=		lib${LIB_PRIVATE}${LIB}.a
167
168lib${LIB_PRIVATE}${LIB}.a: ${OBJS} ${STATICOBJS}
169	@${ECHO} building static ${LIB} library
170	@rm -f ${.TARGET}
171	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${OBJS} ${STATICOBJS} | tsort -q` ${ARADD}
172	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
173.endif
174
175.if !defined(INTERNALLIB)
176
177.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
178_LIBS+=		lib${LIB_PRIVATE}${LIB}_p.a
179POBJS+=		${OBJS:.o=.po} ${STATICOBJS:.o=.po}
180DEPENDOBJS+=	${POBJS}
181CLEANFILES+=	${POBJS}
182
183lib${LIB_PRIVATE}${LIB}_p.a: ${POBJS}
184	@${ECHO} building profiled ${LIB} library
185	@rm -f ${.TARGET}
186	${AR} ${ARFLAGS} ${.TARGET} `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${POBJS} | tsort -q` ${ARADD}
187	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
188.endif
189
190.if defined(SHLIB_NAME) || \
191    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
192SOBJS+=		${OBJS:.o=.So}
193DEPENDOBJS+=	${SOBJS}
194CLEANFILES+=	${SOBJS}
195.endif
196
197.if defined(SHLIB_NAME)
198_LIBS+=		${SHLIB_NAME}
199
200SOLINKOPTS=	-shared -Wl,-x
201.if !defined(ALLOW_SHARED_TEXTREL)
202.if defined(LD_FATAL_WARNINGS) && ${LD_FATAL_WARNINGS} == "no"
203SOLINKOPTS+=	-Wl,--no-fatal-warnings
204.else
205SOLINKOPTS+=	-Wl,--fatal-warnings
206.endif
207SOLINKOPTS+=	-Wl,--warn-shared-textrel
208.endif
209
210.if target(beforelinking)
211beforelinking: ${SOBJS}
212${SHLIB_NAME_FULL}: beforelinking
213.endif
214
215.if defined(SHLIB_LINK)
216# ${_LDSCRIPTROOT} is needed when cross-building
217# and when building 32 bits library shims.
218#
219# ${_LDSCRIPTROOT} is the directory prefix that will be used when generating
220# ld(1) scripts.  The crosstools' ld is configured to lookup libraries in an
221# alternative directory which is called "sysroot", so during buildworld binaries
222# won't be linked against the running system libraries but against the ones of
223# the current source tree.  ${_LDSCRIPTROOT} behavior is twisted because of
224# the location where we store them:
225# - 64 bits libs are located under sysroot, so ${_LDSCRIPTROOT} must be empty
226#   because ld(1) will manage to find them from sysroot;
227# - 32 bits shims are not, so ${_LDSCRIPTROOT} is used to specify their full
228#   path, outside of sysroot.
229# Note that ld(1) scripts are generated both during buildworld and
230# installworld; in the later case ${_LDSCRIPTROOT} must be obviously empty
231# because on the target system, libraries are meant to be looked up from /.
232.if defined(SHLIB_LDSCRIPT) && !empty(SHLIB_LDSCRIPT) && exists(${.CURDIR}/${SHLIB_LDSCRIPT})
233${SHLIB_LINK:R}.ld: ${.CURDIR}/${SHLIB_LDSCRIPT}
234	sed -e 's,@@SHLIB@@,${_LDSCRIPTROOT}${_SHLIBDIR}/${SHLIB_NAME},g' \
235	    -e 's,@@LIBDIR@@,${_LDSCRIPTROOT}${_LIBDIR},g' \
236	    -e 's,/[^ ]*/,,g' \
237	    ${.ALLSRC} > ${.TARGET}
238
239${SHLIB_NAME_FULL}: ${SHLIB_LINK:R}.ld
240CLEANFILES+=	${SHLIB_LINK:R}.ld
241.endif
242CLEANFILES+=	${SHLIB_LINK}
243.endif
244
245${SHLIB_NAME_FULL}: ${SOBJS}
246	@${ECHO} building shared library ${SHLIB_NAME}
247	@rm -f ${SHLIB_NAME} ${SHLIB_LINK}
248.if defined(SHLIB_LINK) && !commands(${SHLIB_LINK:R}.ld)
249	@${INSTALL_SYMLINK} ${SHLIB_NAME} ${SHLIB_LINK}
250.endif
251	${_LD:N${CCACHE_BIN}} ${LDFLAGS} ${SSP_CFLAGS} ${SOLINKOPTS} \
252	    -o ${.TARGET} -Wl,-soname,${SONAME} \
253	    `NM='${NM}' NMFLAGS='${NMFLAGS}' lorder ${SOBJS} | tsort -q` ${LDADD}
254.if ${MK_CTF} != "no"
255	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SOBJS}
256.endif
257
258.if ${MK_DEBUG_FILES} != "no"
259CLEANFILES+=	${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
260${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
261	${OBJCOPY} --strip-debug --add-gnu-debuglink=${SHLIB_NAME}.debug \
262	    ${SHLIB_NAME_FULL} ${.TARGET}
263
264${SHLIB_NAME}.debug: ${SHLIB_NAME_FULL}
265	${OBJCOPY} --only-keep-debug ${SHLIB_NAME_FULL} ${.TARGET}
266.endif
267.endif #defined(SHLIB_NAME)
268
269.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
270_LIBS+=		lib${LIB_PRIVATE}${LIB}_pic.a
271
272lib${LIB_PRIVATE}${LIB}_pic.a: ${SOBJS}
273	@${ECHO} building special pic ${LIB} library
274	@rm -f ${.TARGET}
275	${AR} ${ARFLAGS} ${.TARGET} ${SOBJS} ${ARADD}
276	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
277.endif
278
279.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
280LINTLIB=	llib-l${LIB}.ln
281_LIBS+=		${LINTLIB}
282LINTOBJS+=	${SRCS:M*.c:.c=.ln}
283CLEANFILES+=	${LINTOBJS}
284
285${LINTLIB}: ${LINTOBJS}
286	@${ECHO} building lint library ${.TARGET}
287	@rm -f ${.TARGET}
288	${LINT} ${LINTLIBFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
289.endif
290
291.endif # !defined(INTERNALLIB)
292
293.if defined(_SKIP_BUILD)
294all:
295.else
296.if defined(_LIBS) && !empty(_LIBS)
297all: ${_LIBS}
298CLEANFILES+=	${_LIBS}
299.endif
300
301.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
302all: _manpages
303.endif
304.endif
305
306_EXTRADEPEND:
307.if ${MK_FAST_DEPEND} == "no"
308	@TMP=_depend$$$$; \
309	sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1.So:/' < ${DEPENDFILE} \
310	    > $$TMP; \
311	mv $$TMP ${DEPENDFILE}
312.endif
313.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
314.if defined(DPADD) && !empty(DPADD)
315	echo ${SHLIB_NAME_FULL}: ${DPADD} >> ${DEPENDFILE}
316.endif
317.endif
318
319.if !target(install)
320
321.if defined(PRECIOUSLIB)
322.if !defined(NO_FSCHG)
323SHLINSTALLFLAGS+= -fschg
324.endif
325SHLINSTALLFLAGS+= -S
326.endif
327
328_INSTALLFLAGS:=	${INSTALLFLAGS}
329.for ie in ${INSTALLFLAGS_EDIT}
330_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
331.endfor
332_SHLINSTALLFLAGS:=	${SHLINSTALLFLAGS}
333.for ie in ${INSTALLFLAGS_EDIT}
334_SHLINSTALLFLAGS:=	${_SHLINSTALLFLAGS${ie}}
335.endfor
336
337.if !defined(INTERNALLIB)
338realinstall: _libinstall
339.ORDER: beforeinstall _libinstall
340_libinstall:
341.if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
342	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
343	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}.a ${DESTDIR}${_LIBDIR}/
344.endif
345.if ${MK_PROFILE} != "no" && defined(LIB) && !empty(LIB)
346	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
347	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}_p.a ${DESTDIR}${_LIBDIR}/
348.endif
349.if defined(SHLIB_NAME)
350	${INSTALL} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
351	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
352	    ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}/
353.if ${MK_DEBUG_FILES} != "no"
354.if defined(DEBUGMKDIR)
355	${INSTALL} -T debug -d ${DESTDIR}${DEBUGFILEDIR}/
356.endif
357	${INSTALL} -T debug -o ${LIBOWN} -g ${LIBGRP} -m ${DEBUGMODE} \
358	    ${_INSTALLFLAGS} \
359	    ${SHLIB_NAME}.debug ${DESTDIR}${DEBUGFILEDIR}/
360.endif
361.if defined(SHLIB_LINK)
362.if commands(${SHLIB_LINK:R}.ld)
363	${INSTALL} -S -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
364	    ${_INSTALLFLAGS} ${SHLIB_LINK:R}.ld \
365	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
366.for _SHLIB_LINK_LINK in ${SHLIB_LDSCRIPT_LINKS}
367	${INSTALL_SYMLINK} ${SHLIB_LINK} ${DESTDIR}${_LIBDIR}/${_SHLIB_LINK_LINK}
368.endfor
369.else
370.if ${_SHLIBDIR} == ${_LIBDIR}
371	${INSTALL_SYMLINK} ${SHLIB_NAME} ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
372.else
373	${INSTALL_RSYMLINK} ${DESTDIR}${_SHLIBDIR}/${SHLIB_NAME} \
374	    ${DESTDIR}${_LIBDIR}/${SHLIB_LINK}
375.if exists(${DESTDIR}${_LIBDIR}/${SHLIB_NAME})
376	-chflags noschg ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
377	rm -f ${DESTDIR}${_LIBDIR}/${SHLIB_NAME}
378.endif
379.endif
380.endif # SHLIB_LDSCRIPT
381.endif # SHLIB_LINK
382.endif # SHIB_NAME
383.if defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) && ${MK_TOOLCHAIN} != "no"
384	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
385	    ${_INSTALLFLAGS} lib${LIB}_pic.a ${DESTDIR}${_LIBDIR}/
386.endif
387.if defined(WANT_LINT) && !defined(NO_LINT) && defined(LIB) && !empty(LIB)
388	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
389	    ${_INSTALLFLAGS} ${LINTLIB} ${DESTDIR}${LINTLIBDIR}/
390.endif
391.endif # !defined(INTERNALLIB)
392
393.if !defined(LIBRARIES_ONLY)
394.include <bsd.nls.mk>
395.include <bsd.files.mk>
396.include <bsd.incs.mk>
397.include <bsd.confs.mk>
398.endif
399
400.include <bsd.links.mk>
401
402.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
403realinstall: _maninstall
404.ORDER: beforeinstall _maninstall
405.endif
406
407.endif
408
409.if !target(lint)
410lint: ${SRCS:M*.c}
411	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
412.endif
413
414.if ${MK_MAN} != "no" && !defined(LIBRARIES_ONLY)
415.include <bsd.man.mk>
416.endif
417
418.include <bsd.dep.mk>
419
420.if ${MK_FAST_DEPEND} == "yes" || !exists(${.OBJDIR}/${DEPENDFILE})
421.if defined(LIB) && !empty(LIB)
422.if !exists(${.OBJDIR}/${DEPENDFILE})
423${OBJS} ${STATICOBJS} ${POBJS}: ${SRCS:M*.h}
424.endif
425.for _S in ${SRCS:N*.[hly]}
426${_S:R}.po: ${_S}
427.endfor
428.endif
429.if defined(SHLIB_NAME) || \
430    defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
431.if !exists(${.OBJDIR}/${DEPENDFILE})
432${SOBJS}: ${SRCS:M*.h}
433.endif
434.for _S in ${SRCS:N*.[hly]}
435${_S:R}.So: ${_S}
436.endfor
437.endif
438.endif
439
440.include <bsd.obj.mk>
441
442.include <bsd.sys.mk>
443