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