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
11 # LDFLAGS contains flags to be used when temporary object files (when building
12 # shared libraries) are created, or when an application is linked.
13 # SHARED_LDFLAGS contains flags to be used when the shared library is created.
15 SHARED_LDFLAGS=$(SHARED_LDFLAG)
18 # SHARED_RCFLAGS are flags used with windres, i.e. when build for Cygwin
20 SHARED_RCFLAGS=$(SHARED_RCFLAG)
24 # LIBNAME contains just the name of the library, without prefix ("lib"
25 # on Unix, "cyg" for certain forms under Cygwin...) or suffix (.a, .so,
26 # .dll, ...). This one MUST have a value when using this makefile to
27 # build shared libraries.
28 # For example, to build libfoo.so, you need to do the following:
32 # STLIBNAME contains the path of the static library to build the shared
33 # library from, for example:
37 # On most Unix platforms, SHLIBNAME contains the path of the short name of
38 # the shared library to build, for example
40 # On Windows POSIX layers (cygwin and mingw), SHLIBNAME contains the import
41 # library name for the shared library to be built, for example:
42 #SHLIBNAME=libfoo.dll.a
44 # SHLIBNAME_FULL contains the path of the full name of the shared library to
46 #SHLIBNAME_FULL=libfoo.so.1.2
47 # When building DSOs, SHLIBNAME_FULL contains path of the full DSO name, for
49 #SHLIBNAME_FULL=dir/dso.so
52 # SHLIBVERSION contains the current version of the shared library (not to
53 # be confused with the project version)
57 # NOTE: to build shared libraries, LIBNAME, STLIBNAME, SHLIBNAME and
58 # SHLIBNAME_FULL MUST have values when using this makefile, and in some
59 # cases, SHLIBVERSION as well. To build DSOs, SHLIBNAME_FULL MUST have
60 # a value, the rest can be left alone.
63 # APPNAME contains just the name of the application, without suffix (""
64 # on Unix, ".exe" on Windows, ...). This one MUST have a value when using
65 # this makefile to build applications.
66 # For example, to build foo, you need to do the following:
70 # SRCDIR is the top directory of the source tree.
73 # OBJECTS contains all the object files to link together into the application.
74 # This must contain at least one object file.
78 # LIBEXTRAS contains extra modules to link together with the library.
79 # For example, if a second library, say libbar.a needs to be linked into
80 # libfoo.so, you need to do the following:
82 # Note that this MUST be used when using the link_dso targets, to hold the
83 # names of all object files that go into the target shared object.
86 # LIBDEPS contains all the flags necessary to cover all necessary
87 # dependencies to other libraries.
90 #------------------------------------------------------------------------------
91 # The rest is private to this makefile.
97 echo "Trying to use this makefile interactively? Don't."
101 LIBDEPS="$${LIBDEPS:-$(LIBDEPS)}"; \
102 LDCMD="$${LDCMD:-$(CC)}"; LDFLAGS="$${LDFLAGS:-$(CFLAGS) $(LDFLAGS)}"; \
103 LIBPATH=`for x in $$LIBDEPS; do echo $$x; done | sed -e 's/^ *-L//;t' -e d | uniq`; \
104 LIBPATH=`echo $$LIBPATH | sed -e 's/ /:/g'`; \
105 echo LD_LIBRARY_PATH=$$LIBPATH:$$LD_LIBRARY_PATH \
106 $${LDCMD} $${LDFLAGS} -o $${APPNAME:=$(APPNAME)} $(OBJECTS) $${LIBDEPS}; \
107 LD_LIBRARY_PATH=$$LIBPATH:$$LD_LIBRARY_PATH \
108 $${LDCMD} $${LDFLAGS} -o $${APPNAME:=$(APPNAME)} $(OBJECTS) $${LIBDEPS} )
112 LIBDEPS="$${LIBDEPS:-$(LIBDEPS)}"; \
113 SHAREDCMD="$${SHAREDCMD:-$(CC)}"; \
114 SHAREDFLAGS="$${SHAREDFLAGS:-$(CFLAGS) $(SHARED_LDFLAGS)}"; \
115 LIBPATH=`for x in $$LIBDEPS; do echo $$x; done | sed -e 's/^ *-L//;t' -e d | uniq`; \
116 LIBPATH=`echo $$LIBPATH | sed -e 's/ /:/g'`; \
117 echo LD_LIBRARY_PATH=$$LIBPATH:$$LD_LIBRARY_PATH \
118 $${SHAREDCMD} $${SHAREDFLAGS} \
119 -o $(SHLIBNAME_FULL) \
120 $$ALLSYMSFLAGS $$SHOBJECTS $$NOALLSYMSFLAGS $$LIBDEPS; \
121 LD_LIBRARY_PATH=$$LIBPATH:$$LD_LIBRARY_PATH \
122 $${SHAREDCMD} $${SHAREDFLAGS} \
123 -o $(SHLIBNAME_FULL) \
124 $$ALLSYMSFLAGS $$SHOBJECTS $$NOALLSYMSFLAGS $$LIBDEPS \
128 if [ -n "$$INHIBIT_SYMLINKS" ]; then :; else \
129 if [ -n "$(SHLIBNAME_FULL)" -a -n "$(SHLIBNAME)" -a \
130 "$(SHLIBNAME_FULL)" != "$(SHLIBNAME)" ]; then \
132 rm -f $(SHLIBNAME); \
133 ln -s $(SHLIBNAME_FULL) $(SHLIBNAME) ); \
137 LINK_SO_SHLIB= SHOBJECTS="$(STLIBNAME) $(LIBEXTRAS)"; $(LINK_SO)
138 LINK_SO_DSO= INHIBIT_SYMLINKS=yes; SHOBJECTS="$(LIBEXTRAS)"; $(LINK_SO)
140 LINK_SO_SHLIB_VIA_O= \
141 SHOBJECTS=$(STLIBNAME).o; \
142 ALL=$$ALLSYMSFLAGS; ALLSYMSFLAGS=; NOALLSYMSFLAGS=; \
143 ( echo ld $(LDFLAGS) -r -o $$SHOBJECTS $$ALL $(STLIBNAME) $(LIBEXTRAS); \
144 ld $(LDFLAGS) -r -o $$SHOBJECTS $$ALL $(STLIBNAME) $(LIBEXTRAS) ); \
145 $(LINK_SO) && ( echo rm -f $$SHOBJECTS; rm -f $$SHOBJECTS )
147 LINK_SO_SHLIB_UNPACKED= \
148 UNPACKDIR=link_tmp.$$$$; rm -rf $$UNPACKDIR; mkdir $$UNPACKDIR; \
149 (cd $$UNPACKDIR; ar x ../$(STLIBNAME)) && \
150 ([ -z "$(LIBEXTRAS)" ] || cp $(LIBEXTRAS) $$UNPACKDIR) && \
151 SHOBJECTS=$$UNPACKDIR/*.o; \
152 $(LINK_SO) && rm -rf $$UNPACKDIR
154 DETECT_GNU_LD=($(CC) -Wl,-V /dev/null 2>&1 | grep '^GNU ld' )>/dev/null
157 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-Bsymbolic -Wl,-soname=$(SHLIBNAME_FULL)"
161 ALLSYMSFLAGS='-Wl,--whole-archive'; \
162 NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \
164 DO_GNU_APP=LDFLAGS="$(CFLAGS) $(LDFLAGS)"
166 #This is rather special. It's a special target with which one can link
167 #applications without bothering with any features that have anything to
168 #do with shared libraries, for example when linking against static
169 #libraries. It's mostly here to avoid a lot of conditionals everywhere
175 @ $(DO_GNU_DSO); $(LINK_SO_DSO)
177 @ $(DO_GNU_SO); $(LINK_SO_SHLIB)
179 @ $(DO_GNU_APP); $(LINK_APP)
181 link_shlib.linux-shared:
182 @$(PERL) $(SRCDIR)/util/mkdef.pl $(LIBNAME) linux >$(LIBNAME).map; \
184 ALLSYMSFLAGS='-Wl,--whole-archive,--version-script=$(LIBNAME).map'; \
188 @if $(DETECT_GNU_LD); then $(DO_GNU_DSO); else \
192 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -nostdlib"; \
195 @if $(DETECT_GNU_LD); then $(DO_GNU_SO); else \
197 ALLSYMSFLAGS="-Wl,-Bforcearchive"; \
199 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -nostdlib"; \
202 @if $(DETECT_GNU_LD); then $(DO_GNU_APP); else \
203 LDFLAGS="$(CFLAGS) $(LDFLAGS)"; \
206 # For Darwin AKA Mac OS/X (dyld)
207 # Originally link_dso.darwin produced .so, because it was hard-coded
208 # in dso_dlfcn module. At later point dso_dlfcn switched to .dylib
209 # extension in order to allow for run-time linking with vendor-
210 # supplied shared libraries such as libz, so that link_dso.darwin had
211 # to be harmonized with it. This caused minor controversy, because
212 # it was believed that dlopen can't be used to dynamically load
213 # .dylib-s, only so called bundle modules (ones linked with -bundle
214 # flag). The belief seems to be originating from pre-10.4 release,
215 # where dlfcn functionality was emulated by dlcompat add-on. In
216 # 10.4 dlopen was rewritten as native part of dyld and is documented
217 # to be capable of loading both dynamic libraries and bundles. In
218 # order to provide compatibility with pre-10.4 dlopen, modules are
219 # linked with -bundle flag, which makes .dylib extension misleading.
220 # It works, because dlopen is [and always was] extension-agnostic.
221 # Alternative to this heuristic approach is to develop specific
222 # MacOS X dso module relying on whichever "native" dyld interface.
226 SHAREDFLAGS="$(CFLAGS) `echo $(SHARED_LDFLAGS) | sed s/dynamiclib/bundle/`"; \
229 @ ALLSYMSFLAGS='-all_load'; \
231 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -current_version $(SHLIBVERSION) -compatibility_version $(SHLIBVERSION) -install_name $(INSTALLTOP)/$(LIBDIR)/$(SHLIBNAME_FULL)"; \
233 link_app.darwin: # is there run-path on darwin?
239 base=-Wl,--enable-auto-image-base; \
240 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared $$base -Wl,-Bsymbolic"; \
243 @ INHIBIT_SYMLINKS=yes; \
244 echo "$(PERL) $(SRCDIR)/util/mkrc.pl $(SHLIBNAME_FULL) |" \
245 "$(RC) $(SHARED_RCFLAGS) -o rc.o"; \
246 $(PERL) $(SRCDIR)/util/mkrc.pl $(SHLIBNAME_FULL) | \
247 $(RC) $(SHARED_RCFLAGS) -o rc.o; \
248 ALLSYMSFLAGS='-Wl,--whole-archive'; \
249 NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \
250 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,--enable-auto-image-base -Wl,-Bsymbolic -Wl,--out-implib,$(SHLIBNAME) rc.o"; \
251 $(LINK_SO_SHLIB) || exit 1; \
256 # link_dso.mingw-shared and link_app.mingw-shared are mapped to the
257 # corresponding cygwin targets, as they do the exact same thing.
259 @ INHIBIT_SYMLINKS=yes; \
260 base=; [ $(LIBNAME) = "crypto" -a -n "$(FIPSCANLIB)" ] && base=-Wl,--image-base,0x63000000; \
261 $(PERL) $(SRCDIR)/util/mkdef.pl 32 $(LIBNAME) \
262 | sed -e 's|^\(LIBRARY *\)$(LIBNAME)32|\1$(SHLIBNAME_FULL)|' \
264 echo "$(PERL) $(SRCDIR)/util/mkrc.pl $(SHLIBNAME_FULL) |" \
265 "$(RC) $(SHARED_RCFLAGS) -o rc.o"; \
266 $(PERL) $(SRCDIR)/util/mkrc.pl $(SHLIBNAME_FULL) | \
267 $(RC) $(SHARED_RCFLAGS) -o rc.o; \
268 ALLSYMSFLAGS='-Wl,--whole-archive'; \
269 NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \
270 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared $$base -Wl,-Bsymbolic -Wl,--out-implib,$(SHLIBNAME) $(LIBNAME).def rc.o"; \
271 $(LINK_SO_SHLIB) || exit 1; \
272 rm $(LIBNAME).def rc.o
275 @ if $(DETECT_GNU_LD); then \
280 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-B,symbolic"; \
283 link_shlib.alpha-osf1:
284 @ if $(DETECT_GNU_LD); then \
287 ALLSYMSFLAGS='-all'; \
288 NOALLSYMSFLAGS='-none'; \
289 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-B,symbolic -set_version $(SHLIBVERSION)"; \
293 @if $(DETECT_GNU_LD); then \
296 LDFLAGS="$(CFLAGS) $(LDFLAGS)"; \
301 @ if $(DETECT_GNU_LD); then \
306 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -h $(SHLIBNAME_FULL) -Wl,-Bsymbolic"; \
310 @ if $(DETECT_GNU_LD); then \
313 $(PERL) $(SRCDIR)/util/mkdef.pl $(LIBNAME) linux >$(LIBNAME).map; \
314 ALLSYMSFLAGS="-Wl,-z,allextract,-M,$(LIBNAME).map"; \
315 NOALLSYMSFLAGS="-Wl,-z,defaultextract"; \
316 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -h $(SHLIBNAME_FULL) -Wl,-Bsymbolic"; \
320 @ if $(DETECT_GNU_LD); then \
323 LDFLAGS="$(CFLAGS) $(LDFLAGS)"; \
327 # OpenServer 5 native compilers used
329 @ if $(DETECT_GNU_LD); then \
334 SHAREDFLAGS="$(CFLAGS) -G -h $(SHLIBNAME_FULL)"; \
338 @ if $(DETECT_GNU_LD); then \
343 SHAREDFLAGS="$(CFLAGS) -G -h $(SHLIBNAME_FULL)"; \
345 $(LINK_SO_SHLIB_UNPACKED)
347 @$(DETECT_GNU_LD) && $(DO_GNU_APP); \
350 # UnixWare 7 and OpenUNIX 8 native compilers used
352 @ if $(DETECT_GNU_LD); then \
356 ($(CC) -v 2>&1 | grep gcc) > /dev/null && SHARE_FLAG='-shared'; \
359 SHAREDFLAGS="$(CFLAGS) $${SHARE_FLAG} -h $(SHLIBNAME_FULL)"; \
363 @ if $(DETECT_GNU_LD); then \
367 ($(CC) -v 2>&1 | grep gcc) > /dev/null && SHARE_FLAG='-shared'; \
370 SHAREDFLAGS="$(CFLAGS) $${SHARE_FLAG} -h $(SHLIBNAME_FULL)"; \
372 $(LINK_SO_SHLIB_UNPACKED)
374 @$(DETECT_GNU_LD) && $(DO_GNU_APP); \
378 @ if $(DETECT_GNU_LD); then \
383 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-soname,$(SHLIBNAME_FULL),-B,symbolic"; \
387 @ if $(DETECT_GNU_LD); then \
391 ($(CC) -v 2>&1 | grep gcc) > /dev/null && MINUSWL="-Wl,"; \
392 ALLSYMSFLAGS="$${MINUSWL}-all"; \
393 NOALLSYMSFLAGS="$${MINUSWL}-none"; \
394 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-soname,$(SHLIBNAME_FULL),-B,symbolic"; \
398 @LDFLAGS="$(CFLAGS) $(LDFLAGS)"; \
401 # 32-bit PA-RISC HP-UX embeds the -L pathname of libs we link with, so
402 # we compensate for it with +cdp ../: and +cdp ./:. Yes, these rewrite
403 # rules imply that we can only link one level down in catalog structure,
404 # but that's what takes place for the moment of this writing. +cdp option
405 # was introduced in HP-UX 11.x and applies in 32-bit PA-RISC link
406 # editor context only [it's simply ignored in other cases, which are all
410 @if $(DETECT_GNU_LD); then $(DO_GNU_DSO); else \
413 expr $(PLATFORM) : 'hpux64' > /dev/null && ALLSYMSFLAGS='-Wl,+forceload'; \
414 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-B,symbolic,+vnocompatwarnings,-z,+s,+h,$(SHLIBNAME_FULL),+cdp,../:,+cdp,./:"; \
416 rm -f $(SHLIBNAME_FULL) || :; \
417 $(LINK_SO_DSO) && chmod a=rx $(SHLIBNAME_FULL)
419 @if $(DETECT_GNU_LD); then $(DO_GNU_SO); else \
420 ALLSYMSFLAGS='-Wl,-Fl'; \
422 expr $(PLATFORM) : 'hpux64' > /dev/null && ALLSYMSFLAGS='-Wl,+forceload'; \
423 SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-B,symbolic,+vnocompatwarnings,-z,+s,+h,$(SHLIBNAME_FULL),+cdp,../:,+cdp,./:"; \
425 rm -f $(SHLIBNAME_FULL) || :; \
426 $(LINK_SO_SHLIB) && chmod a=rx $(SHLIBNAME_FULL)
428 @if $(DETECT_GNU_LD); then $(DO_GNU_APP); else \
429 LDFLAGS="$(CFLAGS) $(LDFLAGS) -Wl,+s,+cdp,../:,+cdp,./:"; \
434 @OBJECT_MODE=`expr "x$(SHARED_LDFLAGS)" : 'x\-[a-z]*\(64\)'` || :; \
435 OBJECT_MODE=$${OBJECT_MODE:-32}; export OBJECT_MODE; \
438 SHAREDFLAGS='$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-bexpall,-bnolibpath,-bM:SRE'; \
439 rm -f $(SHLIBNAME_FULL) 2>&1 > /dev/null ; \
442 @ OBJECT_MODE=`expr "x$(SHARED_LDFLAGS)" : 'x\-[a-z]*\(64\)'` || : ; \
443 OBJECT_MODE=$${OBJECT_MODE:-32}; export OBJECT_MODE; \
444 ALLSYMSFLAGS='-bnogc'; \
446 SHAREDFLAGS='$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-bexpall,-bnolibpath,-bM:SRE'; \
447 rm -f $(SHLIBNAME_FULL) 2>&1 > /dev/null ; \
448 $(LINK_SO_SHLIB_VIA_O)
450 LDFLAGS="$(CFLAGS) -Wl,-bsvr4 $(LDFLAGS)"; \
454 # Targets to build symbolic links when needed
455 symlink.gnu symlink.solaris symlink.svr3 symlink.svr5 symlink.irix \
462 # The following lines means those specific architectures do no symlinks
463 symlink.cygwin symlink.alpha-osf1 symlink.tru64 symlink.tru64-rpath:
465 # Compatibility targets
466 link_dso.bsd-gcc-shared link_dso.linux-shared link_dso.gnu-shared: link_dso.gnu
467 link_shlib.bsd-gcc-shared: link_shlib.linux-shared
468 link_shlib.gnu-shared: link_shlib.gnu
469 link_app.bsd-gcc-shared link_app.linux-shared link_app.gnu-shared: link_app.gnu
470 symlink.bsd-gcc-shared symlink.bsd-shared symlink.linux-shared symlink.gnu-shared: symlink.gnu
471 link_dso.bsd-shared: link_dso.bsd
472 link_shlib.bsd-shared: link_shlib.bsd
473 link_app.bsd-shared: link_app.bsd
474 link_dso.darwin-shared: link_dso.darwin
475 link_shlib.darwin-shared: link_shlib.darwin
476 link_app.darwin-shared: link_app.darwin
477 symlink.darwin-shared: symlink.darwin
478 link_dso.cygwin-shared: link_dso.cygwin
479 link_shlib.cygwin-shared: link_shlib.cygwin
480 link_app.cygwin-shared: link_app.cygwin
481 symlink.cygwin-shared: symlink.cygwin
482 link_dso.mingw-shared: link_dso.cygwin
483 link_shlib.mingw-shared: link_shlib.mingw
484 link_app.mingw-shared: link_app.cygwin
485 symlink.mingw-shared: symlink.cygwin
486 link_dso.alpha-osf1-shared: link_dso.alpha-osf1
487 link_shlib.alpha-osf1-shared: link_shlib.alpha-osf1
488 link_app.alpha-osf1-shared: link_app.alpha-osf1
489 symlink.alpha-osf1-shared: symlink.alpha-osf1
490 link_dso.tru64-shared: link_dso.tru64
491 link_shlib.tru64-shared: link_shlib.tru64
492 link_app.tru64-shared: link_app.tru64
493 symlink.tru64-shared: symlink.tru64
494 link_dso.tru64-shared-rpath: link_dso.tru64-rpath
495 link_shlib.tru64-shared-rpath: link_shlib.tru64-rpath
496 link_app.tru64-shared-rpath: link_app.tru64-rpath
497 symlink.tru64-shared-rpath: symlink.tru64-rpath
498 link_dso.solaris-shared: link_dso.solaris
499 link_shlib.solaris-shared: link_shlib.solaris
500 link_app.solaris-shared: link_app.solaris
501 symlink.solaris-shared: symlink.solaris
502 link_dso.svr3-shared: link_dso.svr3
503 link_shlib.svr3-shared: link_shlib.svr3
504 link_app.svr3-shared: link_app.svr3
505 symlink.svr3-shared: symlink.svr3
506 link_dso.svr5-shared: link_dso.svr5
507 link_shlib.svr5-shared: link_shlib.svr5
508 link_app.svr5-shared: link_app.svr5
509 symlink.svr5-shared: symlink.svr5
510 link_dso.irix-shared: link_dso.irix
511 link_shlib.irix-shared: link_shlib.irix
512 link_app.irix-shared: link_app.irix
513 symlink.irix-shared: symlink.irix
514 link_dso.hpux-shared: link_dso.hpux
515 link_shlib.hpux-shared: link_shlib.hpux
516 link_app.hpux-shared: link_app.hpux
517 symlink.hpux-shared: symlink.hpux
518 link_dso.aix-shared: link_dso.aix
519 link_shlib.aix-shared: link_shlib.aix
520 link_app.aix-shared: link_app.aix
521 symlink.aix-shared: symlink.aix