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 second 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 if [ -n "$$INHIBIT_SYMLINKS" ]; then :; else \
84 prev=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX; \
85 if [ -n "$$SHLIB_COMPAT" ]; then \
86 for x in $$SHLIB_COMPAT; do \
87 ( $(DEBUG); rm -f $$SHLIB$$x$$SHLIB_SUFFIX; \
88 ln -s $$prev $$SHLIB$$x$$SHLIB_SUFFIX ); \
89 prev=$$SHLIB$$x$$SHLIB_SUFFIX; \
92 if [ -n "$$SHLIB_SOVER" ]; then \
93 ( $(DEBUG); rm -f $$SHLIB$$SHLIB_SUFFIX; \
94 ln -s $$prev $$SHLIB$$SHLIB_SUFFIX ); \
98 LINK_SO_A= SHOBJECTS="lib$(LIBNAME).a $(LIBEXTRAS)"; $(LINK_SO)
99 LINK_SO_O= SHOBJECTS="$(LIBEXTRAS)"; $(LINK_SO)
101 SHOBJECTS=lib$(LIBNAME).o ALL=$$ALLSYMSFLAGS ALLSYMSFLAGS= NOALLSYMSFLAGS=; \
103 ld $(LDFLAGS) -r -o lib$(LIBNAME).o $$ALL lib$(LIBNAME).a $(LIBEXTRAS) ); \
104 $(LINK_SO) && rm -f $(LIBNAME).o
105 LINK_SO_A_UNPACKED= \
106 UNPACKDIR=link_tmp.$$$$; rm -rf $$UNPACKDIR; mkdir $$UNPACKDIR; \
107 (cd $$UNPACKDIR; ar x ../lib$(LIBNAME).a) && cp $(LIBEXTRAS) $$UNPACKDIR && \
108 SHOBJECTS=$$UNPACKDIR/*.o; \
109 $(LINK_SO) && rm -rf $$UNPACKDIR
111 DETECT_GNU_LD=(${CC} -Wl,-V /dev/null 2>&1 | grep '^GNU ld' )>/dev/null
113 DO_GNU=$(CALC_VERSIONS); \
114 SHLIB=lib$(LIBNAME).so \
116 LIBDEPS="$(LIBDEPS) -lc" \
117 ALLSYMSFLAGS='-Wl,--whole-archive' \
118 NOALLSYMSFLAGS='-Wl,--no-whole-archive' \
119 SHAREDFLAGS="-shared -Wl,-Bsymbolic -Wl,-soname=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX" \
123 @ $(DO_GNU); $(LINK_SO_O)
125 @ $(DO_GNU); $(LINK_SO_A)
127 # For Darwin AKA Mac OS/X (dyld)
129 @ $(CALC_VERSIONS); \
130 SHLIB=lib$(LIBNAME) \
131 SHLIB_SUFFIX=.dylib \
132 LIBDEPS="$(LIBDEPS) -lc" \
133 ALLSYMSFLAGS='-all_load' \
135 SHAREDFLAGS="-dynamiclib" \
137 if [ -n "$(LIBVERSION)" ]; then \
138 SHAREDFLAGS="$SHAREDFLAGS -current_version $(LIBVERSION)"; \
140 if [ -n "$$SHLIB_SOVER_NODOT" ]; then \
141 SHAREDFLAGS="$SHAREDFLAGS -compatibility_version $$SHLIB_SOVER_NODOT"; \
145 @ $(CALC_VERSIONS); \
146 SHLIB=lib$(LIBNAME) \
147 SHLIB_SUFFIX=.dylib \
148 LIBDEPS="$(LIBDEPS) -lc" \
149 ALLSYMSFLAGS='-all_load' \
151 SHAREDFLAGS="-dynamiclib" \
153 if [ -n "$(LIBVERSION)" ]; then \
154 SHAREDFLAGS="$SHAREDFLAGS -current_version $(LIBVERSION)"; \
156 if [ -n "$$SHLIB_SOVER_NODOT" ]; then \
157 SHAREDFLAGS="$SHAREDFLAGS -compatibility_version $$SHLIB_SOVER_NODOT"; \
162 @ $(CALC_VERSIONS); \
163 INHIBIT_SYMLINKS=yes; \
164 SHLIB=cyg$(LIBNAME) \
166 LIBDEPS="$(LIBDEPS) -lc" \
167 SHLIB_SOVER=-$(LIBVERSION) \
168 ALLSYMSFLAGS='-Wl,--whole-archive' \
169 NOALLSYMSFLAGS='-Wl,--no-whole-archive' \
170 SHAREDFLAGS="-shared -Wl,-Bsymbolic -Wl,--out-implib,lib$(LIBNAME).dll.a" \
174 @ $(CALC_VERSIONS); \
175 INHIBIT_SYMLINKS=yes; \
176 SHLIB=cyg$(LIBNAME) \
178 LIBDEPS="$(LIBDEPS) -lc" \
180 ALLSYMSFLAGS='-Wl,--whole-archive' \
181 NOALLSYMSFLAGS='-Wl,--no-whole-archive' \
182 SHAREDFLAGS="-shared -Wl,-Bsymbolic -Wl,--out-implib,lib$(LIBNAME).dll.a" \
187 @ if ${DETECT_GNU_LD}; then \
190 SHLIB=lib$(LIBNAME).so \
192 LIBDEPS="$(LIBDEPS) -lc" \
193 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
194 if [ -n "$$SHLIB_HIST" ]; then \
195 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
197 SHLIB_HIST="$(LIBVERSION)"; \
200 ALLSYMSFLAGS='-all' \
201 NOALLSYMSFLAGS='-none' \
202 SHAREDFLAGS="-shared" \
204 if [ -n "$$SHLIB_HIST" ]; then \
205 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
210 @ if ${DETECT_GNU_LD}; then \
213 SHLIB=lib$(LIBNAME).so \
215 LIBDEPS="$(LIBDEPS) -lc" \
216 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
217 if [ -n "$$SHLIB_HIST" ]; then \
218 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
220 SHLIB_HIST="$(LIBVERSION)"; \
223 ALLSYMSFLAGS='-all' \
224 NOALLSYMSFLAGS='-none' \
225 SHAREDFLAGS="-shared" \
227 if [ -n "$$SHLIB_HIST" ]; then \
228 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
233 # The difference between alpha-osf1-shared and tru64-shared is the `-msym'
234 # option passed to the linker.
236 @ if ${DETECT_GNU_LD}; then \
239 SHLIB=lib$(LIBNAME).so \
241 LIBDEPS="$(LIBDEPS) -lc" \
242 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
243 if [ -n "$$SHLIB_HIST" ]; then \
244 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
246 SHLIB_HIST="$(LIBVERSION)"; \
249 ALLSYMSFLAGS='-all' \
250 NOALLSYMSFLAGS='-none' \
251 SHAREDFLAGS="-shared -msym" \
253 if [ -n "$$SHLIB_HIST" ]; then \
254 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
259 @ if ${DETECT_GNU_LD}; then \
262 SHLIB=lib$(LIBNAME).so \
264 LIBDEPS="$(LIBDEPS) -lc" \
265 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
266 if [ -n "$$SHLIB_HIST" ]; then \
267 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
269 SHLIB_HIST="$(LIBVERSION)"; \
272 ALLSYMSFLAGS='-all' \
273 NOALLSYMSFLAGS='-none' \
274 SHAREDFLAGS="-shared -msym" \
276 if [ -n "$$SHLIB_HIST" ]; then \
277 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
282 # The difference between tru64-shared and tru64-shared-rpath is the
283 # -rpath ${LIBRPATH} passed to the linker.
285 @ if ${DETECT_GNU_LD}; then \
288 SHLIB=lib$(LIBNAME).so \
290 LIBDEPS="$(LIBDEPS) -lc" \
291 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
292 if [ -n "$$SHLIB_HIST" ]; then \
293 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
295 SHLIB_HIST="$(LIBVERSION)"; \
298 ALLSYMSFLAGS='-all' \
299 NOALLSYMSFLAGS='-none' \
300 SHAREDFLAGS="-shared -msym -rpath $(LIBRPATH)" \
302 if [ -n "$$SHLIB_HIST" ]; then \
303 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
308 @ if ${DETECT_GNU_LD}; then \
311 SHLIB=lib$(LIBNAME).so \
313 LIBDEPS="$(LIBDEPS) -lc" \
314 SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \
315 if [ -n "$$SHLIB_HIST" ]; then \
316 SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \
318 SHLIB_HIST="$(LIBVERSION)"; \
321 ALLSYMSFLAGS='-all' \
322 NOALLSYMSFLAGS='-none' \
323 SHAREDFLAGS="-shared -msym -rpath $(LIBRPATH)" \
325 if [ -n "$$SHLIB_HIST" ]; then \
326 SHAREDFLAGS="$SHAREDFLAGS -set_version \"$$SHLIB_HIST\""; \
332 @ if ${DETECT_GNU_LD}; then \
336 SHLIB=lib$(LIBNAME).so \
338 LIBDEPS="$(LIBDEPS) -lc" \
339 ALLSYMSFLAGS='-z allextract' \
341 SHAREDFLAGS='-G -dy -z text -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
346 @ if ${DETECT_GNU_LD}; then \
350 SHLIB=lib$(LIBNAME).so \
352 LIBDEPS="$(LIBDEPS) -lc" \
353 ALLSYMSFLAGS='-z allextract' \
355 SHAREDFLAGS='-G -dy -z text -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
360 # OpenServer 5 native compilers used
361 # UnixWare 7 and OpenUNIX 8 native compilers used
363 @ if ${DETECT_GNU_LD}; then \
367 SHLIB=lib$(LIBNAME).so \
369 LIBDEPS="$(LIBDEPS) -lc" \
370 ALLSYMSFLAGS='-z allextract' \
372 SHAREDFLAGS='-G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
377 @ if ${DETECT_GNU_LD}; then \
381 SHLIB=lib$(LIBNAME).so \
383 LIBDEPS="$(LIBDEPS) -lc" \
384 ALLSYMSFLAGS='-z allextract' \
386 SHAREDFLAGS='-G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
389 $(LINK_SO_A_UNPACKED)
392 @ if ${DETECT_GNU_LD}; then \
396 SHLIB=lib$(LIBNAME).so \
398 LIBDEPS="$(LIBDEPS) -lc" \
399 ALLSYMSFLAGS='-all' \
401 SHAREDFLAGS='-shared -Wl,-soname,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
406 @ if ${DETECT_GNU_LD}; then \
410 SHLIB=lib$(LIBNAME).so \
412 LIBDEPS="$(LIBDEPS) -lc" \
413 ALLSYMSFLAGS='-all' \
415 SHAREDFLAGS='-shared -Wl,-soname,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
420 # HP-UX includes the full pathname of libs we depend on, so we would get
421 # ./libcrypto (with ./ as path information) compiled into libssl, hence
422 # we omit the SHLIBDEPS. Applications must be linked with -lssl -lcrypto
424 # The object modules are loaded from lib$i.a using the undocumented -Fl
427 # WARNING: Until DSO is fixed to support a search path, we support SHLIB_PATH
428 # by temporarily specifying "+s"!
431 @ $(CALC_VERSIONS); \
432 SHLIB=lib$(LIBNAME).sl \
434 LIBDEPS="$(LIBDEPS) -lc" \
437 SHAREDFLAGS='+vnocompatwarnings -b -z +s +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
438 SHAREDCMD='/usr/ccs/bin/ld'; \
439 $(LINK_SO_O) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
441 @ $(CALC_VERSIONS); \
442 SHLIB=lib$(LIBNAME).sl \
444 LIBDEPS="$(LIBDEPS) -lc" \
447 SHAREDFLAGS='+vnocompatwarnings -b -z +s +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
448 SHAREDCMD='/usr/ccs/bin/ld'; \
449 $(LINK_SO_A) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
451 # HP-UX includes the full pathname of libs we depend on, so we would get
452 # ./libcrypto (with ./ as path information) compiled into libssl, hence
453 # we omit the SHLIBDEPS. Applications must be linked with -lssl -lcrypto
456 # HP-UX in 64bit mode has "+s" enabled by default; it will search for
457 # shared libraries along LD_LIBRARY_PATH _and_ SHLIB_PATH.
460 @ $(CALC_VERSIONS); \
461 SHLIB=lib$(LIBNAME).sl \
463 LIBDEPS="$(LIBDEPS) -lc" \
464 ALLSYMSFLAGS='+forceload' \
466 SHAREDFLAGS='-b -z +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
467 SHAREDCMD='/usr/ccs/bin/ld'; \
468 $(LINK_SO_O) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
470 @ $(CALC_VERSIONS); \
471 SHLIB=lib$(LIBNAME).sl \
473 LIBDEPS="$(LIBDEPS) -lc" \
474 ALLSYMSFLAGS='+forceload' \
476 SHAREDFLAGS='-b -z +h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX' \
477 SHAREDCMD='/usr/ccs/bin/ld'; \
478 $(LINK_SO_A) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX
481 @ $(CALC_VERSIONS); \
482 SHLIB=lib$(LIBNAME).so \
484 LIBDEPS="$(LIBDEPS) -lc" \
485 ALLSYMSFLAGS='-bnogc' \
487 SHAREDFLAGS='-G -bE:lib$(LIBNAME).exp -bM:SRE' \
491 @ $(CALC_VERSIONS); \
492 SHLIB=lib$(LIBNAME).so \
494 LIBDEPS="$(LIBDEPS) -lc" \
495 ALLSYMSFLAGS='-bnogc' \
497 SHAREDFLAGS='-G -bE:lib$(LIBNAME).exp -bM:SRE' \
502 @ $(CALC_VERSIONS); \
503 SHLIB=lib$(LIBNAME).so \
505 LIBDEPS="$(LIBDEPS) -lc" \
512 @ $(CALC_VERSIONS); \
513 SHLIB=lib$(LIBNAME).so \
515 LIBDEPS="$(LIBDEPS) -lc" \
520 $(LINK_SO_A_UNPACKED)
522 # Targets to build symbolic links when needed
523 symlink.gnu symlink.solaris symlink.svr3 symlink.irix \
524 symlink.aix symlink.reliantunix:
525 @ $(CALC_VERSIONS); \
526 SHLIB=lib$(LIBNAME).so; \
529 @ $(CALC_VERSIONS); \
530 SHLIB=lib$(LIBNAME) \
531 SHLIB_SUFFIX=.dylib; \
533 symlink.hpux32 symlink.hpux64:
534 @ $(CALC_VERSIONS); \
535 SHLIB=lib$(LIBNAME).sl; \
537 # The following lines means those specific architectures do no symlinks
538 symlink.cygwin symlib.alpha-osf1 symlink.tru64 symlink.tru64-rpath:
540 # Compatibility targets
541 link_o.bsd-gcc-shared link_o.linux-shared link_o.gnu-shared: link_o.gnu
542 link_a.bsd-gcc-shared link_a.linux-shared link_a.gnu-shared: link_a.gnu
543 symlink.bsd-gcc-shared symlink.linux-shared symlink.gnu-shared: symlink.gnu
544 link_o.darwin-shared: link_o.darwin
545 link_a.darwin-shared: link_a.darwin
546 symlink.darwin-shared: symlink.darwin
547 link_o.cygwin-shared: link_o.cygwin
548 link_a.cygwin-shared: link_a.cygwin
549 symlink.cygwin-shared: symlink.cygwin
550 link_o.alpha-osf1-shared: link_o.alpha-osf1
551 link_a.alpha-osf1-shared: link_a.alpha-osf1
552 symlink.alpha-osf1-shared: symlink.alpha-osf1
553 link_o.tru64-shared: link_o.tru64
554 link_a.tru64-shared: link_a.tru64
555 symlink.tru64-shared: symlink.tru64
556 link_o.tru64-shared-rpath: link_o.tru64-rpath
557 link_a.tru64-shared-rpath: link_a.tru64-rpath
558 symlink.tru64-shared-rpath: symlink.tru64-rpath
559 link_o.solaris-shared: link_o.solaris
560 link_a.solaris-shared: link_a.solaris
561 symlink.solaris-shared: symlink.solaris
562 link_o.svr3-shared: link_o.svr3
563 link_a.svr3-shared: link_a.svr3
564 symlink.svr3-shared: symlink.svr3
565 link_o.svr5-shared: link_o.svr3
566 link_a.svr5-shared: link_a.svr3
567 symlink.svr5-shared: symlink.svr3
568 link_o.irix-shared: link_o.irix
569 link_a.irix-shared: link_a.irix
570 symlink.irix-shared: symlink.irix
571 link_o.hpux-shared: link_o.hpux32
572 link_a.hpux-shared: link_a.hpux32
573 symlink.hpux-shared: symlink.hpux32
574 link_o.hpux64-shared: link_o.hpux64
575 link_a.hpux64-shared: link_a.hpux64
576 symlink.hpux64-shared: symlink.hpux64
577 link_o.aix-shared: link_o.aix
578 link_a.aix-shared: link_a.aix
579 symlink.aix-shared: symlink.aix
580 link_o.reliantunix-shared: link_o.reliantunix
581 link_a.reliantunix-shared: link_a.reliantunix
582 symlink.reliantunix-shared: symlink.reliantunix