2 # Helper makefile to link shared libraries in a portable way.
3 # This is much simpler than libtool, and hopefully not too error-prone.
5 # The following variables need to be set on the command line to build
8 # CC contains the current compiler. This one MUST be defined
10 # LDFLAGS contains flags to be used when the temporary object file is
11 # created. SHARED_LDFLAGS contains flags to be used when the shared
16 # LIBNAME contains just the name of thhe library, without prefix ("lib"
17 # on Unix, "cyg" for certain forms under Cygwin...) or suffix (.a, .so,
18 # .dll, ...). This one MUST have a value when using this makefile.
19 # For example, to build libfoo.so, you need to do the following:
23 # LIBEXTRAS contains extra modules to link together with the library.
24 # For example, if a seond library, say libbar.a needs to be linked into
25 # libfoo.so, you need to do the following:
27 # Note that this MUST be used when using the link_o targets, to hold the
28 # names of all object files that go into the target library.
31 # LIBVERSION contains the current version of the library.
32 # For example, to build libfoo.so.1.2, you need to do the following:
36 # LIBCOMPATVERSIONS contains the compatibility versions (a list) of
37 # the library. They MUST be in decreasing order.
38 # For example, if libfoo.so.1.2.1 is backward compatible with libfoo.so.1.2
39 # and libfoo.so.1, you need to do the following:
40 #LIBCOMPATVERSIONS=1.2 1
41 # Note that on systems that use sonames, the last number will appear as
43 # It's also possible, for systems that support it (Tru64, for example),
44 # to add extra compatibility info with more precision, by adding a second
45 # list of versions, separated from the first with a semicolon, like this:
46 #LIBCOMPATVERSIONS=1.2 1;1.2.0 1.1.2 1.1.1 1.1.0 1.0.0
49 # LIBDEPS contains all the flags necessary to cover all necessary
50 # dependencies to other libraries.
53 #------------------------------------------------------------------------------
54 # The rest is private to this makefile.
60 echo "Trying to use this makefile interactively? Don't."
63 SHLIB_COMPAT=; SHLIB_SOVER=; \
64 if [ -n "$(LIBVERSION)$(LIBCOMPATVERSIONS)" ]; then \
66 for v in `echo "$(LIBVERSION) $(LIBCOMPATVERSIONS)" | cut -d';' -f1`; do \
67 SHLIB_SOVER_NODOT=$$v \
69 if [ -n "$$prev" ]; then \
70 SHLIB_COMPAT="$$SHLIB_COMPAT .$$prev"; \
78 nm -Pg $$SHOBJECTS | grep ' [BDT] ' | cut -f1 -d' ' > lib$(LIBNAME).exp; \
79 $$SHAREDCMD $(SHARED_LDFLAGS) $$SHAREDFLAGS -o $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX \
80 $$ALLSYMSFLAGS $$SHOBJECTS $$NOALLSYMSFLAGS $$LIBDEPS ) && \
81 $(SYMLINK_SO); ( $(DEBUG); rm -f lib$(LIBNAME).exp )
83 prev=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX; \
84 if [ -n "$$SHLIB_COMPAT" ]; then \
85 for x in $$SHLIB_COMPAT; do \
86 ( $(DEBUG); rm -f $$SHLIB$$x$$SHLIB_SUFFIX; \
87 ln -s $$prev $$SHLIB$$x$$SHLIB_SUFFIX ); \
88 prev=$$SHLIB$$x$$SHLIB_SUFFIX; \
91 if [ -n "$$SHLIB_SOVER" ]; then \
92 ( $(DEBUG); rm -f $$SHLIB$$SHLIB_SUFFIX; \
93 ln -s $$prev $$SHLIB$$SHLIB_SUFFIX ); \
96 LINK_SO_A= SHOBJECTS="lib$(LIBNAME).a $(LIBEXTRAS)"; $(LINK_SO)
97 LINK_SO_O= SHOBJECTS="$(LIBEXTRAS)"; $(LINK_SO)
99 SHOBJECTS=lib$(LIBNAME).o ALL=$$ALLSYMSFLAGS ALLSYMSFLAGS= NOALLSYMSFLAGS=; \
101 ld $(LDFLAGS) -r -o lib$(LIBNAME).o $$ALL lib$(LIBNAME).a $(LIBEXTRAS) ); \
102 $(LINK_SO) && rm -f $(LIBNAME).o
103 LINK_SO_A_UNPACKED= \
104 UNPACKDIR=link_tmp.$$$$; rm -rf $$UNPACKDIR; mkdir $$UNPACKDIR; \
105 (cd $$UNPACKDIR; ar x ../lib$(LIBNAME).a) && cp $(LIBEXTRAS) $$UNPACKDIR && \
106 SHOBJECTS=$$UNPACKDIR/*.o; \
107 $(LINK_SO) && rm -rf $$UNPACKDIR
109 DETECT_GNU_LD=${CC} -v 2>&1 | grep '^gcc' >/dev/null 2>&1 && \
110 my_ld=`gcc -print-prog-name=ld 2>&1` && \
111 [ -n "$$my_ld" ] && \
112 $$my_ld -v 2>&1 | grep 'GNU ld' >/dev/null 2>&1
113 DO_GNU=$(CALC_VERSIONS); \
114 SHLIB=lib$(LIBNAME).so \
116 LIBDEPS="$(LIBDEPS) -lc" \
117 NOALLSYMSFLAGS='-Wl,--no-whole-archive' \
118 SHAREDFLAGS="-shared -Wl,-Bsymbolic -Wl,-soname=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX" \
122 @ $(DO_GNU); $(LINK_SO_O)
124 @ $(DO_GNU); $(LINK_SO_A)
126 # For Darwin AKA Mac OS/X (dyld)
128 @ $(CALC_VERSIONS); \
129 SHLIB=lib$(LIBNAME) \
130 SHLIB_SUFFIX=.dylib \
131 LIBDEPS="$(LIBDEPS) -lc" \
132 ALLSYMSFLAGS='-all_load' \
134 SHAREDFLAGS="-dynamiclib" \
136 if [ -n "$(LIBVERSION)" ]; then \
137 SHAREDFLAGS="$SHAREDFLAGS -current_version $(LIBVERSION)"; \
139 if [ -n "$$SHLIB_SOVER_NODOT" ]; then \
140 SHAREDFLAGS="$SHAREDFLAGS -compatibility_version $$SHLIB_SOVER_NODOT"; \
144 @ $(CALC_VERSIONS); \
145 SHLIB=lib$(LIBNAME) \
146 SHLIB_SUFFIX=.dylib \
147 LIBDEPS="$(LIBDEPS) -lc" \
148 ALLSYMSFLAGS='-all_load' \
150 SHAREDFLAGS="-dynamiclib" \
152 if [ -n "$(LIBVERSION)" ]; then \
153 SHAREDFLAGS="$SHAREDFLAGS -current_version $(LIBVERSION)"; \
155 if [ -n "$$SHLIB_SOVER_NODOT" ]; then \
156 SHAREDFLAGS="$SHAREDFLAGS -compatibility_version $$SHLIB_SOVER_NODOT"; \
161 @ SHLIB=cyg$(LIBNAME).dll \
163 LIBDEPS="$(LIBDEPS) -lc" \
165 ALLSYMSFLAGS='-Wl,--whole-archive' \
166 NOALLSYMSFLAGS='-Wl,--no-whole-archive' \
167 SHAREDFLAGS="-shared -Wl,-Bsymbolic -Wl,--out-implib,lib$(LIBNAME).dll.a" \
171 @ SHLIB=cyg$(LIBNAME).dll \
173 LIBDEPS="$(LIBDEPS) -lc" \
175 ALLSYMSFLAGS='-Wl,--whole-archive' \
176 NOALLSYMSFLAGS='-Wl,--no-whole-archive' \
177 SHAREDFLAGS="-shared -Wl,-Bsymbolic -Wl,--out-implib,lib$(LIBNAME).dll.a" \
182 @ if ${DETECT_GNU_LD}; then \
185 SHLIB=lib$(LIBNAME).so \
187 LIBDEPS="$(LIBDEPS) -lc" \
188 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
189 if [ -n "$$SHLIB_HIST" ]; then \
190 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
192 SHLIB_HIST="$(LIBVERSION)"; \
195 ALLSYMSFLAGS='-all' \
196 NOALLSYMSFLAGS='-none' \
197 SHAREDFLAGS="-shared" \
199 if [ -n "$$SHLIB_HIST" ]; then \
200 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
205 @ if ${DETECT_GNU_LD}; then \
208 SHLIB=lib$(LIBNAME).so \
210 LIBDEPS="$(LIBDEPS) -lc" \
211 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
212 if [ -n "$$SHLIB_HIST" ]; then \
213 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
215 SHLIB_HIST="$(LIBVERSION)"; \
218 ALLSYMSFLAGS='-all' \
219 NOALLSYMSFLAGS='-none' \
220 SHAREDFLAGS="-shared" \
222 if [ -n "$$SHLIB_HIST" ]; then \
223 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
228 # The difference between alpha-osf1-shared and tru64-shared is the `-msym'
229 # option passed to the linker.
231 @ if ${DETECT_GNU_LD}; then \
234 SHLIB=lib$(LIBNAME).so \
236 LIBDEPS="$(LIBDEPS) -lc" \
237 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
238 if [ -n "$$SHLIB_HIST" ]; then \
239 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
241 SHLIB_HIST="$(LIBVERSION)"; \
244 ALLSYMSFLAGS='-all' \
245 NOALLSYMSFLAGS='-none' \
246 SHAREDFLAGS="-shared -msym" \
248 if [ -n "$$SHLIB_HIST" ]; then \
249 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
254 @ if ${DETECT_GNU_LD}; then \
257 SHLIB=lib$(LIBNAME).so \
259 LIBDEPS="$(LIBDEPS) -lc" \
260 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
261 if [ -n "$$SHLIB_HIST" ]; then \
262 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
264 SHLIB_HIST="$(LIBVERSION)"; \
267 ALLSYMSFLAGS='-all' \
268 NOALLSYMSFLAGS='-none' \
269 SHAREDFLAGS="-shared -msym" \
271 if [ -n "$$SHLIB_HIST" ]; then \
272 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
277 # The difference between tru64-shared and tru64-shared-rpath is the
278 # -rpath ${LIBRPATH} passed to the linker.
280 @ if ${DETECT_GNU_LD}; then \
283 SHLIB=lib$(LIBNAME).so \
285 LIBDEPS="$(LIBDEPS) -lc" \
286 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
287 if [ -n "$$SHLIB_HIST" ]; then \
288 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
290 SHLIB_HIST="$(LIBVERSION)"; \
293 ALLSYMSFLAGS='-all' \
294 NOALLSYMSFLAGS='-none' \
295 SHAREDFLAGS="-shared -msym -rpath $(LIBRPATH)" \
297 if [ -n "$$SHLIB_HIST" ]; then \
298 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
303 @ if ${DETECT_GNU_LD}; then \
306 SHLIB=lib$(LIBNAME).so \
308 LIBDEPS="$(LIBDEPS) -lc" \
309 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
310 if [ -n "$$SHLIB_HIST" ]; then \
311 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
313 SHLIB_HIST="$(LIBVERSION)"; \
316 ALLSYMSFLAGS='-all' \
317 NOALLSYMSFLAGS='-none' \
318 SHAREDFLAGS="-shared -msym -rpath $(LIBRPATH)" \
320 if [ -n "$$SHLIB_HIST" ]; then \
321 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
327 @ if ${DETECT_GNU_LD}; then \
331 SHLIB=lib$(LIBNAME).so \
333 LIBDEPS="$(LIBDEPS) -lc" \
334 ALLSYMSFLAGS='-z allextract' \
336 SHAREDFLAGS='-G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
341 @ if ${DETECT_GNU_LD}; then \
345 SHLIB=lib$(LIBNAME).so \
347 LIBDEPS="$(LIBDEPS) -lc" \
348 ALLSYMSFLAGS='-z allextract' \
350 SHAREDFLAGS='-G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
355 # OpenServer 5 native compilers used
356 # UnixWare 7 and OpenUNIX 8 native compilers used
358 @ if ${DETECT_GNU_LD}; then \
362 SHLIB=lib$(LIBNAME).so \
364 LIBDEPS="$(LIBDEPS) -lc" \
365 ALLSYMSFLAGS='-z allextract' \
367 SHAREDFLAGS='-G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
372 @ if ${DETECT_GNU_LD}; then \
376 SHLIB=lib$(LIBNAME).so \
378 LIBDEPS="$(LIBDEPS) -lc" \
379 ALLSYMSFLAGS='-z allextract' \
381 SHAREDFLAGS='-G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
384 $(LINK_SO_A_UNPACKED)
387 @ if ${DETECT_GNU_LD}; then \
391 SHLIB=lib$(LIBNAME).so \
393 LIBDEPS="$(LIBDEPS) -lc" \
394 ALLSYMSFLAGS='-all' \
396 SHAREDFLAGS='-shared -Wl,-soname,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
401 @ if ${DETECT_GNU_LD}; then \
405 SHLIB=lib$(LIBNAME).so \
407 LIBDEPS="$(LIBDEPS) -lc" \
408 ALLSYMSFLAGS='-all' \
410 SHAREDFLAGS='-shared -Wl,-soname,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
415 # HP-UX includes the full pathname of libs we depend on, so we would get
416 # ./libcrypto (with ./ as path information) compiled into libssl, hence
417 # we omit the SHLIBDEPS. Applications must be linked with -lssl -lcrypto
419 # The object modules are loaded from lib$i.a using the undocumented -Fl
422 # WARNING: Until DSO is fixed to support a search path, we support SHLIB_PATH
423 # by temporarily specifying "+s"!
426 @ $(CALC_VERSIONS); \
427 SHLIB=lib$(LIBNAME).sl \
429 LIBDEPS="$(LIBDEPS) -lc" \
432 SHAREDFLAGS='+vnocompatwarnings -b -z +s +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
433 SHAREDCMD='/usr/ccs/bin/ld'; \
434 $(LINK_SO_O) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
436 @ $(CALC_VERSIONS); \
437 SHLIB=lib$(LIBNAME).sl \
439 LIBDEPS="$(LIBDEPS) -lc" \
442 SHAREDFLAGS='+vnocompatwarnings -b -z +s +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
443 SHAREDCMD='/usr/ccs/bin/ld'; \
444 $(LINK_SO_A) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
446 # HP-UX includes the full pathname of libs we depend on, so we would get
447 # ./libcrypto (with ./ as path information) compiled into libssl, hence
448 # we omit the SHLIBDEPS. Applications must be linked with -lssl -lcrypto
451 # HP-UX in 64bit mode has "+s" enabled by default; it will search for
452 # shared libraries along LD_LIBRARY_PATH _and_ SHLIB_PATH.
455 @ $(CALC_VERSIONS); \
456 SHLIB=lib$(LIBNAME).sl \
458 LIBDEPS="$(LIBDEPS) -lc" \
459 ALLSYMSFLAGS='+forceload' \
461 SHAREDFLAGS='-b -z +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
462 SHAREDCMD='/usr/ccs/bin/ld'; \
463 $(LINK_SO_O) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
465 @ $(CALC_VERSIONS); \
466 SHLIB=lib$(LIBNAME).sl \
468 LIBDEPS="$(LIBDEPS) -lc" \
469 ALLSYMSFLAGS='+forceload' \
471 SHAREDFLAGS='-b -z +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
472 SHAREDCMD='/usr/ccs/bin/ld'; \
473 $(LINK_SO_A) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
476 @ $(CALC_VERSIONS); \
477 SHLIB=lib$(LIBNAME).so \
479 LIBDEPS="$(LIBDEPS) -lc" \
480 ALLSYMSFLAGS='-bnogc' \
482 SHAREDFLAGS='-G -bE:lib$(LIBNAME).exp -bM:SRE' \
486 @ $(CALC_VERSIONS); \
487 SHLIB=lib$(LIBNAME).so \
489 LIBDEPS="$(LIBDEPS) -lc" \
490 ALLSYMSFLAGS='-bnogc' \
492 SHAREDFLAGS='-G -bE:lib$(LIBNAME).exp -bM:SRE' \
497 @ $(CALC_VERSIONS); \
498 SHLIB=lib$(LIBNAME).so \
500 LIBDEPS="$(LIBDEPS) -lc" \
507 @ $(CALC_VERSIONS); \
508 SHLIB=lib$(LIBNAME).so \
510 LIBDEPS="$(LIBDEPS) -lc" \
515 $(LINK_SO_A_UNPACKED)
517 # Targets to build symbolic links when needed
518 symlink.gnu symlink.solaris symlink.svr3 symlink.irix \
519 symlink.aix symlink.reliantunix:
520 @ $(CALC_VERSIONS); \
521 SHLIB=lib$(LIBNAME).so \
524 @ $(CALC_VERSIONS); \
525 SHLIB=lib$(LIBNAME) \
526 SHLIB_SUFFIX=.dylib \
528 symlink.cygwin symlib.alpha-osf1 symlink.tru64 symlink.tru64-rpath:
529 symlink.hpux32 symlink.hpux64:
530 @ $(CALC_VERSIONS); \
531 SHLIB=lib$(LIBNAME).sl \
534 # Compatibility targets
535 link_o.bsd-gcc-shared link_o.linux-shared link_o.gnu-shared: link_o.gnu
536 link_a.bsd-gcc-shared link_a.linux-shared link_a.gnu-shared: link_a.gnu
537 symlink.bsd-gcc-shared symlink.linux-shared symlink.gnu-shared: symlink.gnu
538 link_o.darwin-shared: link_o.darwin
539 link_a.darwin-shared: link_a.darwin
540 symlink.darwin-shared: symlink.darwin
541 link_o.cygwin-shared: link_o.cygwin
542 link_a.cygwin-shared: link_a.cygwin
543 symlink.cygwin-shared: symlink.cygwin
544 link_o.alpha-osf1-shared: link_o.alpha-osf1
545 link_a.alpha-osf1-shared: link_a.alpha-osf1
546 symlink.alpha-osf1-shared: symlink.alpha-osf1
547 link_o.tru64-shared: link_o.tru64
548 link_a.tru64-shared: link_a.tru64
549 symlink.tru64-shared: symlink.tru64
550 link_o.tru64-shared-rpath: link_o.tru64-rpath
551 link_a.tru64-shared-rpath: link_a.tru64-rpath
552 symlink.tru64-shared-rpath: symlink.tru64-rpath
553 link_o.solaris-shared: link_o.solaris
554 link_a.solaris-shared: link_a.solaris
555 symlink.solaris-shared: symlink.solaris
556 link_o.svr3-shared: link_o.svr3
557 link_a.svr3-shared: link_a.svr3
558 symlink.svr3-shared: symlink.svr3
559 link_o.svr5-shared: link_o.svr3
560 link_a.svr5-shared: link_a.svr3
561 symlink.svr5-shared: symlink.svr3
562 link_o.irix-shared: link_o.irix
563 link_a.irix-shared: link_a.irix
564 symlink.irix-shared: symlink.irix
565 link_o.hpux-shared: link_o.hpux32
566 link_a.hpux-shared: link_a.hpux32
567 symlink.hpux-shared: symlink.hpux32
568 link_o.hpux64-shared: link_o.hpux64
569 link_a.hpux64-shared: link_a.hpux64
570 symlink.hpux64-shared: symlink.hpux64
571 link_o.aix-shared: link_o.aix
572 link_a.aix-shared: link_a.aix
573 symlink.aix-shared: symlink.aix
574 link_o.reliantunix-shared: link_o.reliantunix
575 link_a.reliantunix-shared: link_a.reliantunix
576 symlink.reliantunix-shared: symlink.reliantunix