xref: /openbsd/share/mk/bsd.lib.mk (revision dda28197)
1#	$OpenBSD: bsd.lib.mk,v 1.101 2020/05/26 17:44:23 tb Exp $
2#	$NetBSD: bsd.lib.mk,v 1.67 1996/01/17 20:39:26 mycroft Exp $
3#	@(#)bsd.lib.mk	5.26 (Berkeley) 5/2/91
4
5.include <bsd.own.mk>				# for 'NOPIC' definition
6
7.if exists(${.CURDIR}/../Makefile.inc)
8.include "${.CURDIR}/../Makefile.inc"
9.endif
10
11.if exists(${.CURDIR}/shlib_version)
12.include "${.CURDIR}/shlib_version"
13.if defined(LIB) && defined(LIB${LIB}_VERSION)
14SHLIB_MAJOR=${LIB${LIB}_VERSION:R}
15SHLIB_MINOR=${LIB${LIB}_VERSION:E}
16.else
17SHLIB_MAJOR=${major}
18SHLIB_MINOR=${minor}
19.endif
20.endif
21
22.MAIN: all
23
24# prefer .S to a .c, add .po, remove stuff not used in the BSD libraries.
25# .so used for PIC object files.
26# .do used for distrib "crunchgen" object files
27# .m for objective c files.
28.SUFFIXES:
29.SUFFIXES: .out .o .po .so .do .S .s .c .cc .cpp .C .cxx .f .y .l .m4 .m
30
31.if defined(NOPIE)
32CFLAGS+=	${NOPIE_FLAGS}
33CXXFLAGS+=	${NOPIE_FLAGS}
34AFLAGS+=	${NOPIE_FLAGS}
35.endif
36
37.c.o:
38	@echo "${COMPILE.c} ${.IMPSRC} -o ${.TARGET}"
39	@${COMPILE.c} ${DFLAGS} ${.IMPSRC}  -o ${.TARGET}.o
40	@-mv $@.d $*.d
41	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
42	@rm -f ${.TARGET}.o
43
44.c.po:
45	@echo "${COMPILE.c} -p ${.IMPSRC} -o ${.TARGET}"
46	@${COMPILE.c} ${DFLAGS} -p ${.IMPSRC} -o ${.TARGET}.o
47	@-mv $@.d $*.d
48	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
49	@rm -f ${.TARGET}.o
50
51.c.so:
52	@echo "${COMPILE.c} ${PICFLAG} -DPIC ${.IMPSRC} -o ${.TARGET}"
53	@${COMPILE.c} ${DFLAGS} ${PICFLAG} -DPIC ${.IMPSRC} -o ${.TARGET}.o
54	@-mv $@.d $*.d
55	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
56	@rm -f ${.TARGET}.o
57
58.c.do:
59	@echo "${COMPILE.c} ${DIST_CFLAGS} ${.IMPSRC} -o ${.TARGET}"
60	@${COMPILE.c} ${DFLAGS} ${DIST_CFLAGS} ${.IMPSRC}  -o ${.TARGET}.o
61	@-mv $@.d $*.d
62	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
63	@rm -f ${.TARGET}.o
64
65.cc.o .cpp.o .C.o .cxx.o:
66	@echo "${COMPILE.cc} ${.IMPSRC} -o ${.TARGET}"
67	@${COMPILE.cc} ${DFLAGS} ${.IMPSRC} -o ${.TARGET}.o
68	@-mv $@.d $*.d
69	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
70	@rm -f ${.TARGET}.o
71
72.cc.po .cpp.po .C.po .cxx.po:
73	@echo "${COMPILE.cc} -p ${.IMPSRC} -o ${.TARGET}"
74	@${COMPILE.cc} ${DFLAGS} -p ${.IMPSRC} -o ${.TARGET}.o
75	@-mv $@.d $*.d
76	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
77	@rm -f ${.TARGET}.o
78
79.cc.so .cpp.so .C.so .cxx.so:
80	@echo "${COMPILE.cc} ${PICFLAG} -DPIC ${.IMPSRC} -o ${.TARGET}"
81	@${COMPILE.cc} ${DFLAGS} ${PICFLAG} -DPIC ${.IMPSRC} -o ${.TARGET}.o
82	@-mv $@.d $*.d
83	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
84	@rm -f ${.TARGET}.o
85
86# Fortran 77
87.f.o:
88	@echo "${COMPILE.f} ${.IMPSRC} -o ${.TARGET}"
89	@${COMPILE.f} ${DFLAGS} ${.IMPSRC} -o ${.TARGET}.o
90	@-mv $@.d $*.d
91	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
92	@rm -f ${.TARGET}.o
93
94.f.po:
95	@echo "${COMPILE.f} -p ${.IMPSRC} -o ${.TARGET}"
96	@${COMPILE.f} ${DFLAGS} -p ${.IMPSRC} -o ${.TARGET}.o
97	@-mv $@.d $*.d
98	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
99	@rm -f ${.TARGET}.o
100
101.f.so:
102	@echo "${COMPILE.f} ${PICFLAG} -DPIC ${.IMPSRC} -o ${.TARGET}"
103	@${COMPILE.f} ${DFLAGS} ${PICFLAG} -DPIC ${.IMPSRC} -o ${.TARGET}.o
104	@-mv $@.d $*.d
105	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
106	@rm -f ${.TARGET}.o
107
108.S.o .s.o:
109	@echo "${COMPILE.S} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}"
110	@${COMPILE.S} ${DFLAGS} -MF $@.d ${CFLAGS:M-[IDM]*} ${AINC} \
111	    ${.IMPSRC} -o ${.TARGET}.o
112	@-mv $@.d $*.d
113	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
114	@rm -f ${.TARGET}.o
115
116.S.po .s.po:
117	@echo "${COMPILE.S} -DPROF ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} \
118	    -o ${.TARGET}"
119	@${COMPILE.S} ${DFLAGS} -MF $@.d -DPROF ${CFLAGS:M-[IDM]*} ${AINC} \
120	    ${.IMPSRC} -o ${.TARGET}.o
121	@-mv $@.d $*.d
122	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
123	@rm -f ${.TARGET}.o
124
125.S.so .s.so:
126	@echo "${COMPILE.S} ${PICFLAG} -DSOLIB ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} \
127	    -o ${.TARGET}"
128	@${COMPILE.S} ${DFLAGS} -MF $@.d ${PICFLAG} -DSOLIB ${CFLAGS:M-[IDM]*} \
129	    ${AINC} ${.IMPSRC} -o ${.TARGET}.o
130	@-mv $@.d $*.d
131	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
132	@rm -f ${.TARGET}.o
133
134.S.do .s.do:
135	@echo "${COMPILE.S} ${CFLAGS:M-[ID]*} ${AINC} ${DIST_CFLAGS} \
136	    ${.IMPSRC} -o ${.TARGET}"
137	@${COMPILE.S} ${DFLAGS} -MF $@.d ${CFLAGS:M-[IDM]*} ${AINC} \
138	    ${DIST_CFLAGS} ${.IMPSRC} -o ${.TARGET}.o
139	@-mv $@.d $*.d
140	@${LD} -X -r ${.TARGET}.o -o ${.TARGET}
141	@rm -f ${.TARGET}.o
142
143.if ${WARNINGS:L} == "yes"
144CFLAGS+=	${CDIAGFLAGS}
145CXXFLAGS+=	${CXXDIAGFLAGS}
146.endif
147CFLAGS+=	${COPTS}
148CXXFLAGS+=	${CXXOPTS}
149
150DEBUG?=	-g
151
152_LIBS=lib${LIB}.a
153.if !defined(NOPROFILE)
154_LIBS+=lib${LIB}_p.a
155.endif
156
157.if !defined(NOPIC)
158.if defined(SHLIB_MAJOR) && defined(SHLIB_MINOR)
159FULLSHLIBNAME=lib${LIB}.so.${SHLIB_MAJOR}.${SHLIB_MINOR}
160_LIBS+=${FULLSHLIBNAME}
161.endif
162
163.if defined(LIBREBUILD)
164_LIBS+=${FULLSHLIBNAME}.a
165
166.if exists(${.CURDIR}/Symbols.list)
167SYMBOLSMAP=Symbols.map
168.endif
169
170.endif
171
172.if defined(VERSION_SCRIPT)
173${FULLSHLIBNAME}:	${VERSION_SCRIPT}
174LDADD+=	-Wl,--version-script=${VERSION_SCRIPT}
175.endif
176.endif
177
178all: ${_LIBS} _SUBDIRUSE
179
180BUILDAFTER += ${_LIBS}
181
182OBJS+=	${SRCS:N*.h:R:S/$/.o/}
183DEPS+=	${OBJS:R:S/$/.d/}
184
185_LEXINTM?=${SRCS:M*.l:.l=.c}
186_YACCINTM?=${SRCS:M*.y:.y=.c}
187
188BUILDAFTER += ${OBJS}
189
190lib${LIB}.a: ${OBJS}
191	@echo building standard ${LIB} library
192	@rm -f lib${LIB}.a
193	@${AR} cqD lib${LIB}.a `${LORDER} ${OBJS} | tsort -q`
194	${RANLIB} lib${LIB}.a
195
196POBJS+=	${OBJS:.o=.po}
197BUILDAFTER += ${POBJS}
198
199lib${LIB}_p.a: ${POBJS}
200	@echo building profiled ${LIB} library
201	@rm -f lib${LIB}_p.a
202	@${AR} cqD lib${LIB}_p.a `${LORDER} ${POBJS} | tsort -q`
203	${RANLIB} lib${LIB}_p.a
204
205SOBJS+=	${OBJS:.o=.so}
206BUILDAFTER += ${SOBJS}
207
208# exclude from readelf(1) output for syspatch building
209EXCLUDE_REGEX:=	"(cmll-586|(comparetf|libgcc|unwind-dw)2|		\
210		mul(t|d|s|x)(c|f)3|crt(begin|end)S|			\
211		(div|umod|mod)(d|t)i3|emutls|(add|div|sub)tf3|		\
212		(fixtf|float|extend|trunctf)(d|s)(ftf2|i|itf|f2)|	\
213		floatunsitf|udivmodti4|AMDGPURegAsmNames|clear_cache)"
214
215${FULLSHLIBNAME}: ${SOBJS} ${DPADD}
216	@echo building shared ${LIB} library \(version ${SHLIB_MAJOR}.${SHLIB_MINOR}\)
217	@rm -f ${.TARGET}
218.if defined(SYSPATCH_PATH)
219	${CC} -shared -Wl,-soname,${FULLSHLIBNAME} ${PICFLAG} -o ${.TARGET} \
220	    `readelf -Ws ${SYSPATCH_PATH}${LIBDIR}/${.TARGET} | \
221	    awk '/ FILE/{sub(".*/", "", $$NF); gsub(/\..*/, ".so", $$NF); print $$NF}' | \
222	    egrep -v ${EXCLUDE_REGEX:C/[[:blank:]]//g} | awk '!x[$$0]++'` ${LDADD}
223.else
224	${CC} -shared -Wl,-soname,${FULLSHLIBNAME} ${PICFLAG} -o ${.TARGET} \
225	    `echo ${SOBJS} | tr ' ' '\n' | sort -R` ${LDADD}
226.endif
227
228${FULLSHLIBNAME}.a: ${SOBJS}
229	@echo building shared ${LIB} library \(version ${SHLIB_MAJOR}.${SHLIB_MINOR}\) ar
230	@rm -f ${.TARGET}
231	@echo -Wl,-soname,${FULLSHLIBNAME} ${PICFLAG} ${LDADD} > .ldadd
232	ar cqD ${FULLSHLIBNAME}.a ${SOBJS} .ldadd ${SYMBOLSMAP}
233
234# all .do files...
235DOBJS+=	${OBJS:.o=.do}
236BUILDAFTER += ${DOBJS}
237
238# .do files that we actually need for where this dist lib will be used
239.if defined(DIST_OBJS)
240SELECTED_DOBJS=${DIST_OBJS:.o=.do}
241.else
242SELECTED_DOBJS?=${DOBJS}
243.endif
244
245DIST_LIB?=lib${LIB}_d.a
246${DIST_LIB}: ${SELECTED_DOBJS}
247	@echo building distrib ${DIST_LIB} library from ${SELECTED_DOBJS}
248	@rm -f ${DIST_LIB}
249.if !empty(SELECTED_DOBJS)
250	@${AR} cqD ${DIST_LIB} `${LORDER} ${SELECTED_DOBJS} | tsort -q`
251.else
252	@${AR} cqD ${DIST_LIB}
253.endif
254	${RANLIB} ${DIST_LIB}
255
256.if !target(clean)
257clean: _SUBDIRUSE
258	rm -f a.out [Ee]rrs mklog *.core y.tab.h \
259	    ${_LEXINTM} ${_YACCINTM} ${CLEANFILES}
260	rm -f lib${LIB}.a ${OBJS}
261	rm -f lib${LIB}_g.a ${GOBJS}
262	rm -f lib${LIB}_p.a ${POBJS}
263	rm -f lib${LIB}.so.*.* ${SOBJS} .ldadd
264	rm -f ${DIST_LIB} ${DOBJS}
265.endif
266
267cleandir: _SUBDIRUSE clean
268
269
270.if !target(install)
271.if !target(beforeinstall)
272beforeinstall:
273.endif
274
275realinstall:
276.if !defined(NOLIBSTATIC)
277	${INSTALL} ${INSTALL_COPY} -o ${LIBOWN} -g ${LIBGRP} -m 600 lib${LIB}.a \
278	    ${DESTDIR}${LIBDIR}/lib${LIB}.a
279.if (${INSTALL_COPY} != "-p")
280	${RANLIB} -t ${DESTDIR}${LIBDIR}/lib${LIB}.a
281.endif
282	chmod ${LIBMODE} ${DESTDIR}${LIBDIR}/lib${LIB}.a
283.endif
284.if !defined(NOPROFILE)
285	${INSTALL} ${INSTALL_COPY} -o ${LIBOWN} -g ${LIBGRP} -m 600 \
286	    lib${LIB}_p.a ${DESTDIR}${LIBDIR}
287.if (${INSTALL_COPY} != "-p")
288	${RANLIB} -t ${DESTDIR}${LIBDIR}/lib${LIB}_p.a
289.endif
290	chmod ${LIBMODE} ${DESTDIR}${LIBDIR}/lib${LIB}_p.a
291.endif
292.if !defined(NOPIC) && defined(SHLIB_MAJOR) && defined(SHLIB_MINOR)
293	${INSTALL} ${INSTALL_COPY} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
294	    ${FULLSHLIBNAME} ${DESTDIR}${LIBDIR}
295.if defined(LIBREBUILD)
296.if !defined(DESTDIR)
297	@echo cleaning out old relink libraries to conserve disk space
298	rm -f /usr/share/relink/${LIBDIR}/lib${LIB}.*.a
299.endif
300	${INSTALL} -d -o ${LIBOWN} -g ${LIBGRP} -m 755 \
301	   ${DESTDIR}/usr/share/relink/${LIBDIR}
302	${INSTALL} ${INSTALL_COPY} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
303	    ${FULLSHLIBNAME}.a ${DESTDIR}/usr/share/relink/${LIBDIR}
304.endif
305.endif
306.if defined(LINKS) && !empty(LINKS)
307.  for lnk file in ${LINKS}
308	@l=${DESTDIR}${lnk}; \
309	 t=${DESTDIR}${file}; \
310	 echo $$t -\> $$l; \
311	 rm -f $$t; ln $$l $$t
312.  endfor
313.endif
314
315install: maninstall _SUBDIRUSE
316maninstall: afterinstall
317afterinstall: realinstall
318realinstall: beforeinstall
319.endif
320
321.if !defined(NOMAN)
322.include <bsd.man.mk>
323.endif
324
325.include <bsd.obj.mk>
326.include <bsd.dep.mk>
327.include <bsd.subdir.mk>
328.include <bsd.sys.mk>
329