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