lib.mk: always -Bdirect and -lc for shared libs
[unleashed.git] / share / mk / lib.mk
blob0ba9e1e98e567124d37ca87bebd272c3a92e434e
1 # $Id: lib.mk,v 1.61 2017/05/06 17:30:09 sjg Exp $
3 .if !target(__${.PARSEFILE}__)
4 __${.PARSEFILE}__:
6 .include <init.mk>
8 .if ${OBJECT_FMT} == "ELF"
9 NEED_SOLINKS?= yes
10 .endif
12 MAPFILE_VERS?= ${.CURDIR}/mapfile-vers
13 .if exists(${MAPFILE_VERS})
14 SHLIB_MAJOR= 1
15 SHLIB_LDADD+= -M${MAPFILE_VERS}
16 .else
18 SHLIB_VERSION_FILE?= ${.CURDIR}/shlib_version
19 .if !defined(SHLIB_MAJOR) && exists(${SHLIB_VERSION_FILE})
20 SHLIB_MAJOR != . ${SHLIB_VERSION_FILE} ; echo $$major
21 SHLIB_MINOR != . ${SHLIB_VERSION_FILE} ; echo $$minor
22 SHLIB_TEENY != . ${SHLIB_VERSION_FILE} ; echo $$teeny
23 .endif
25 .for x in major minor teeny
26 print-shlib-$x:
27 .if defined(SHLIB_${x:tu}) && ${MK_PIC} != "no"
28 @echo ${SHLIB_${x:tu}}
29 .else
30 @false
31 .endif
32 .endfor
33 .endif
35 SHLIB_FULLVERSION ?= ${${SHLIB_MAJOR} ${SHLIB_MINOR} ${SHLIB_TEENY}:L:ts.}
36 SHLIB_FULLVERSION := ${SHLIB_FULLVERSION}
38 # add additional suffixes not exported.
39 # .po is used for profiling object files.
40 # ${PICO} is used for PIC object files.
41 PICO?= .pico
42 .SUFFIXES: .out .a .ln ${PICO} .po .o .s .S .c .cc .C .m .F .f .r .y .l .cl .p .h
43 .SUFFIXES: .sh .m4 .m
45 CFLAGS+= ${COPTS}
47 # Originally derrived from NetBSD-1.6
49 # Set PICFLAGS to cc flags for producing position-independent code,
50 # if not already set. Includes -DPIC, if required.
52 # Data-driven table using make variables to control how shared libraries
53 # are built for different platforms and object formats.
54 # OBJECT_FMT: currently either "ELF" or "a.out", from <bsd.own.mk>
55 # SHLIB_SOVERSION: version number to be compiled into a shared library
56 # via -soname. Usually ${SHLIB_MAJOR} on ELF.
57 # NetBSD/pmax used to use ${SHLIB_MAJOR}[.${SHLIB_MINOR}
58 # [.${SHLIB_TEENY}]]
59 # SHLIB_SHFLAGS: Flags to tell ${LD} to emit shared library.
60 # with ELF, also set shared-lib version for ld.so.
61 # SHLIB_LDSTARTFILE: support .o file, call C++ file-level constructors
62 # SHLIB_LDENDFILE: support .o file, call C++ file-level destructors
63 # FPICFLAGS: flags for ${FC} to compile .[fF] files to ${PICO} objects.
64 # CPPICFLAGS: flags for ${CPP} to preprocess .[sS] files for ${AS}
65 # CPICFLAGS: flags for ${CC} to compile .[cC] files to ${PICO} objects.
66 # CAPICFLAGS flags for {$CC} to compiling .[Ss] files
67 # (usually just ${CPPPICFLAGS} ${CPICFLAGS})
68 # APICFLAGS: flags for ${AS} to assemble .[sS] to ${PICO} objects.
70 MKPICLIB?= yes
72 # sys.mk can override these
73 LD_X?=-X
74 LD_x?=-x
75 LD_r?=-r
77 LD_shared=-h lib${LIB}.so.${SHLIB_MAJOR} -G -Bdirect
79 SHLIB_LD ?= ${LD}
81 .if !empty(SHLIB_MAJOR)
82 .if ${NEED_SOLINKS} && empty(SHLIB_LINKS)
83 .if ${MK_LINKLIB} != "no"
84 SHLIB_LINKS = lib${LIB}.${LD_solink}
85 .endif
86 .if "${SHLIB_FULLVERSION}" != "${SHLIB_MAJOR}"
87 SHLIB_LINKS += lib${LIB}.${LD_solink}.${SHLIB_MAJOR}
88 .endif
89 .endif
90 .endif
92 LIBTOOL?=libtool
93 LD_shared ?= -Bshareable -Bforcearchive
94 LD_so ?= so.${SHLIB_FULLVERSION}
95 LD_solink ?= so
96 .if empty(LORDER)
97 LD_objs ?= ${OBJS}
98 LD_pobjs ?= ${POBJS}
99 LD_sobjs ?= ${SOBJS}
100 .else
101 LD_objs ?= `${LORDER} ${OBJS} | ${TSORT}`
102 LD_sobjs ?= `${LORDER} ${SOBJS} | ${TSORT}`
103 LD_pobjs ?= `${LORDER} ${POBJS} | ${TSORT}`
104 .endif
105 LD_solib ?= ${LD_sobjs}
106 AR_cq ?= cq
108 # some libs have lots of objects, and scanning all .o, .po and ${PICO} meta files
109 # is a waste of time, this tells meta.autodep.mk to just pick one
110 # (typically ${PICO})
111 # yes, 42 is a random number.
112 .if ${MK_DIRDEPS_BUILD} == "yes" && ${SRCS:Uno:[\#]} > 42
113 OPTIMIZE_OBJECT_META_FILES ?= yes
114 .endif
116 .if ${MK_LIBTOOL} == "yes"
117 # because libtool is so fascist about naming the object files,
118 # we cannot (yet) build profiled libs
119 MK_PROFILE=no
120 _LIBS=lib${LIB}.a
121 .if exists(${.CURDIR}/shlib_version)
122 SHLIB_AGE != . ${.CURDIR}/shlib_version ; echo $$age
123 .endif
124 .else
125 # for the normal .a we do not want to strip symbols
126 .c.o:
127 ${COMPILE.c} ${.IMPSRC}
129 # for the normal .a we do not want to strip symbols
130 ${CXX_SUFFIXES:%=%.o}:
131 ${COMPILE.cc} ${.IMPSRC}
133 .S.o .s.o:
134 @echo ${COMPILE.S} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC}
135 @${COMPILE.S} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC}
137 .if (${LD_X} == "")
138 .c.po:
139 ${COMPILE.c} ${CC_PG} ${PROFFLAGS} ${.IMPSRC} -o ${.TARGET}
141 ${CXX_SUFFIXES:%=%.po}:
142 ${COMPILE.cc} -pg ${.IMPSRC} -o ${.TARGET}
144 .S${PICO} .s${PICO}:
145 ${COMPILE.S} ${PICFLAG} ${CC_PIC} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}
146 .else
147 .c.po:
148 @echo ${COMPILE.c} ${CC_PG} ${PROFFLAGS} ${.IMPSRC} -o ${.TARGET}
149 @${COMPILE.c} ${CC_PG} ${PROFFLAGS} ${.IMPSRC} -o ${.TARGET}.o
150 @${LD} ${LD_X} ${LD_r} ${.TARGET}.o -o ${.TARGET}
151 @rm -f ${.TARGET}.o
153 ${CXX_SUFFIXES:%=%.po}:
154 @echo ${COMPILE.cc} ${CXX_PG} ${PROFFLAGS} ${.IMPSRC} -o ${.TARGET}
155 @${COMPILE.cc} ${CXX_PG} ${.IMPSRC} -o ${.TARGET}.o
156 @${LD} ${LD_X} ${LD_r} ${.TARGET}.o -o ${.TARGET}
157 @rm -f ${.TARGET}.o
159 .S${PICO} .s${PICO}:
160 @echo ${COMPILE.S} ${PICFLAG} ${CC_PIC} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}
161 @${COMPILE.S} ${PICFLAG} ${CC_PIC} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}.o
162 @${LD} ${LD_x} ${LD_r} ${.TARGET}.o -o ${.TARGET}
163 @rm -f ${.TARGET}.o
164 .endif
166 .if (${LD_x} == "")
167 .c${PICO}:
168 ${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
170 ${CXX_SUFFIXES:%=%${PICO}}:
171 ${COMPILE.cc} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
173 .S.po .s.po:
174 ${COMPILE.S} ${PROFFLAGS} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}
175 .else
177 .c${PICO}:
178 @echo ${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
179 @${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}.o
180 @${LD} ${LD_x} ${LD_r} ${.TARGET}.o -o ${.TARGET}
181 @rm -f ${.TARGET}.o
183 ${CXX_SUFFIXES:%=%${PICO}}:
184 @echo ${COMPILE.cc} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
185 @${COMPILE.cc} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}.o
186 @${LD} ${LD_x} ${LD_r} ${.TARGET}.o -o ${.TARGET}
187 @rm -f ${.TARGET}.o
189 .S.po .s.po:
190 @echo ${COMPILE.S} ${PROFFLAGS} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}
191 @${COMPILE.S} ${PROFFLAGS} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}.o
192 @${LD} ${LD_X} ${LD_r} ${.TARGET}.o -o ${.TARGET}
193 @rm -f ${.TARGET}.o
195 .endif
196 .endif
198 .if ${MK_LIBTOOL} != "yes"
200 .if !defined(PICFLAG)
201 PICFLAG=-fpic
202 .endif
204 _LIBS=
206 .if ${MK_ARCHIVE} != "no"
207 _LIBS += lib${LIB}.a
208 .endif
210 .if ${MK_PROFILE} != "no"
211 _LIBS+=lib${LIB}_p.a
212 POBJS+=${OBJS:.o=.po}
213 .endif
215 .if ${MK_PIC} != "no"
216 .if ${MK_PICLIB} == "no"
217 SOLIB ?= lib${LIB}.a
218 .else
219 SOLIB=lib${LIB}_pic.a
220 _LIBS+=${SOLIB}
221 .endif
222 .if !empty(SHLIB_FULLVERSION)
223 _LIBS+=lib${LIB}.${LD_so}
224 .endif
225 .endif
227 # here is where you can define what LIB* are
228 .-include <libnames.mk>
229 .if ${MK_DPADD_MK} == "yes"
230 # lots of cool magic, but might not suit everyone.
231 .include <dpadd.mk>
232 .endif
234 .if empty(LIB)
235 _LIBS=
236 .endif
238 .if !defined(_SKIP_BUILD)
239 realbuild: ${_LIBS}
240 .endif
242 all: _SUBDIRUSE
244 .for s in ${SRCS:N*.h:M*/*}
245 ${.o ${PICO} .po .lo:L:@o@${s:T:R}$o@}: $s
246 .endfor
248 OBJS+= ${SRCS:T:N*.h:R:S/$/.o/g}
249 .NOPATH: ${OBJS}
251 .if ${MK_LIBTOOL} == "yes"
252 .if ${MK_PIC} == "no"
253 LT_STATIC=-static
254 .else
255 LT_STATIC=
256 .endif
257 SHLIB_AGE?=0
259 # .lo's are created as a side effect
260 .s.o .S.o .c.o:
261 ${LIBTOOL} --mode=compile ${CC} ${LT_STATIC} ${CFLAGS} ${CPPFLAGS} ${IMPFLAGS} -c ${.IMPSRC}
263 # can't really do profiled libs with libtool - its too fascist about
264 # naming the output...
265 lib${LIB}.a:: ${OBJS}
266 @rm -f ${.TARGET}
267 ${LIBTOOL} --mode=link ${CC} ${LT_STATIC} -o ${.TARGET:.a=.la} ${OBJS:.o=.lo} -rpath ${SHLIBDIR}:/usr/lib -version-info ${SHLIB_MAJOR}:${SHLIB_MINOR}:${SHLIB_AGE}
268 @ln .libs/${.TARGET} .
270 lib${LIB}.${LD_so}:: lib${LIB}.a
271 @[ -s ${.TARGET}.${SHLIB_AGE} ] || { ln -s .libs/lib${LIB}.${LD_so}* . 2>/dev/null; : }
272 @[ -s ${.TARGET} ] || ln -s ${.TARGET}.${SHLIB_AGE} ${.TARGET}
274 .else # MK_LIBTOOL=yes
276 lib${LIB}.a:: ${OBJS}
277 @echo building standard ${LIB} library
278 @rm -f ${.TARGET}
279 @${AR} ${AR_cq} ${.TARGET} ${LD_objs}
281 POBJS+= ${OBJS:.o=.po}
282 .NOPATH: ${POBJS}
283 lib${LIB}_p.a:: ${POBJS}
284 @echo building profiled ${LIB} library
285 @rm -f ${.TARGET}
286 @${AR} ${AR_cq} ${.TARGET} ${LD_pobjs}
288 SOBJS+= ${OBJS:.o=${PICO}}
289 .NOPATH: ${SOBJS}
290 lib${LIB}_pic.a:: ${SOBJS}
291 @echo building shared object ${LIB} library
292 @rm -f ${.TARGET}
293 @${AR} ${AR_cq} ${.TARGET} ${LD_sobjs}
295 #SHLIB_LDADD?= ${LDADD}
297 lib${LIB}.${LD_so}: ${SOBJS} ${DPADD}
298 @echo building shared ${LIB} library \(version ${SHLIB_FULLVERSION}\)
299 @rm -f ${.TARGET}
300 ${SHLIB_LD} -o ${.TARGET} ${LD_shared} ${LD_solib} ${DLLIB} ${SHLIB_LDADD} -lc
301 .endif
302 .if !empty(SHLIB_LINKS)
303 rm -f ${SHLIB_LINKS}; ${SHLIB_LINKS:O:u:@x@ln -s ${.TARGET} $x;@}
304 .endif
306 .if !target(clean)
307 cleanlib: .PHONY
308 rm -f a.out [Ee]rrs mklog core *.core ${CLEANFILES}
309 rm -f lib${LIB}.a ${OBJS}
310 rm -f lib${LIB}_p.a ${POBJS}
311 rm -f lib${LIB}_pic.a lib${LIB}.so.* ${SOBJS}
312 .if !empty(SHLIB_LINKS)
313 rm -f ${SHLIB_LINKS}
314 .endif
316 clean: _SUBDIRUSE cleanlib
317 cleandir: _SUBDIRUSE cleanlib
318 .else
319 cleandir: _SUBDIRUSE clean
320 .endif
322 .if defined(SRCS) && (!defined(MKDEP) || ${MKDEP} != autodep)
323 afterdepend: .depend
324 @(TMP=/tmp/_depend$$$$; \
325 sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.po \1${PICO} \1.ln:/' \
326 < .depend > $$TMP; \
327 mv $$TMP .depend)
328 .endif
330 .if !target(install)
331 .if !target(beforeinstall)
332 beforeinstall:
333 .endif
335 .if !empty(LIBOWN)
336 LIB_INSTALL_OWN ?= -o ${LIBOWN} -g ${LIBGRP}
337 .endif
339 .include <links.mk>
341 .if !target(realinstall) && !empty(LIB)
342 realinstall: libinstall
343 .endif
344 .if !target(libinstall)
345 libinstall:
346 ${INSTALL} -d ${LIB_INSTALL_OWN} -m 755 ${DESTDIR}${LIBDIR}
347 .if ${MK_ARCHIVE} != "no"
348 ${INSTALL} ${COPY} ${LIB_INSTALL_OWN} -m 600 lib${LIB}.a \
349 ${DESTDIR}${LIBDIR}
350 chmod ${LIBMODE} ${DESTDIR}${LIBDIR}/lib${LIB}.a
351 .endif
352 .if ${MK_PROFILE} != "no"
353 ${INSTALL} ${COPY} ${LIB_INSTALL_OWN} -m 600 \
354 lib${LIB}_p.a ${DESTDIR}${LIBDIR}
355 chmod ${LIBMODE} ${DESTDIR}${LIBDIR}/lib${LIB}_p.a
356 .endif
357 .if ${MK_PIC} != "no"
358 .if ${MK_PICLIB} != "no"
359 ${INSTALL} ${COPY} ${LIB_INSTALL_OWN} -m 600 \
360 lib${LIB}_pic.a ${DESTDIR}${LIBDIR}
361 chmod ${LIBMODE} ${DESTDIR}${LIBDIR}/lib${LIB}_pic.a
362 .endif
363 .if !empty(SHLIB_MAJOR)
364 ${INSTALL} ${COPY} ${LIB_INSTALL_OWN} -m ${LIBMODE} \
365 lib${LIB}.${LD_so} ${DESTDIR}${LIBDIR}
366 .if !empty(SHLIB_LINKS)
367 (cd ${DESTDIR}${LIBDIR} && { rm -f ${SHLIB_LINKS}; ${SHLIB_LINKS:O:u:@x@ln -s lib${LIB}.${LD_so} $x;@} })
368 .endif
369 .endif
370 .endif
371 .if defined(LINKS) && !empty(LINKS)
372 @set ${LINKS}; ${_LINKS_SCRIPT}
373 .endif
374 .endif
376 .if ${MK_MAN} != "no"
377 install: maninstall _SUBDIRUSE
378 maninstall: afterinstall
379 .endif
380 afterinstall: realinstall
381 libinstall: beforeinstall
382 realinstall: beforeinstall
383 .endif
385 .if defined(FILES) || defined(FILESGROUPS)
386 .include <files.mk>
387 .endif
389 .if ${MK_MAN} != "no"
390 .include <man.mk>
391 .endif
393 .if ${MK_NLS} != "no"
394 .include <nls.mk>
395 .endif
397 .include <obj.mk>
398 .include <inc.mk>
399 .include <dep.mk>
400 .include <subdir.mk>
401 .endif
403 # during building we usually need/want to install libs somewhere central
404 # note that we do NOT ch{own,grp} as that would likely fail at this point.
405 # otherwise it is the same as realinstall
406 # Note that we don't need this when using dpadd.mk
407 .libinstall: ${_LIBS}
408 ${INSTALL} -d -m755 ${DESTDIR}${LIBDIR}
409 .for _lib in ${_LIBS:M*.a}
410 ${INSTALL} ${COPY} -m 644 ${_lib} ${DESTDIR}${LIBDIR}
411 .endfor
412 .for _lib in ${_LIBS:M*.${LD_solink}*:O:u}
413 ${INSTALL} ${COPY} -m ${LIBMODE} ${_lib} ${DESTDIR}${LIBDIR}
414 .if !empty(SHLIB_LINKS)
415 (cd ${DESTDIR}${LIBDIR} && { ${SHLIB_LINKS:O:u:@x@ln -sf ${_lib} $x;@}; })
416 .endif
417 .endfor
418 @touch ${.TARGET}
420 .if !empty(LIB)
421 STAGE_LIBDIR?= ${STAGE_OBJTOP}${LIBDIR}
422 stage_libs: ${_LIBS}
423 .endif
425 .include <final.mk>
426 .endif