1 #-------------------------------------------------------------------------
4 # Common rules for building shared libraries
6 # Copyright (c) 1998, Regents of the University of California
9 # $PostgreSQL: pgsql/src/Makefile.shlib,v 1.98 2005/10/28 17:32:22 tgl Exp $
11 #-------------------------------------------------------------------------
13 # This file should be included by any Postgres module Makefile that
14 # wants to build a shared library (if possible for the current
15 # platform). A static library is also built from the same object
16 # files. Only one library can be built per makefile.
18 # Before including this file, the module Makefile must define these
21 # NAME Name of library to build (no suffix nor "lib" prefix)
22 # SO_MAJOR_VERSION Major version number to use for shared library
23 # SO_MINOR_VERSION Minor version number to use for shared library
24 # OBJS List of object files to include in library
25 # SHLIB_LINK If shared library relies on other libraries,
26 # additional stuff to put in its link command
27 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
29 # Optional flags when building DLL's (only applicable to win32 and cygwin
31 # DLLTOOL_DEFFLAGS Additional flags when creating the dll .def file
32 # DLLTOOL_LIBFLAGS Additional flags when creating the lib<module>.a file
33 # DLLWRAP_FLAGS Additional flags to dllwrap
34 # DLL_DEFFILE Use pre-existing .def file instead of auto-generating
35 # one with all exports in it (win32 only).
37 # The module Makefile must also include
38 # $(top_builddir)/src/Makefile.global before including this file.
39 # (Makefile.global sets PORTNAME and other needed symbols.)
41 # This makefile provides the following (phony) targets:
43 # all-lib build the static and shared (if applicable) libraries
44 # install-lib install the libraries into $(libdir)
45 # uninstall-lib remove the libraries from $(libdir)
46 # clean-lib delete the static and shared libraries from the build dir
48 # Since `all-lib' is the first rule in this file you probably want to
49 # have the `all' target before including this file. In the most simple
50 # case it would look like this:
54 # Similarly, the install rule might look like
56 # install: install-lib
58 # plus any additional things you want to install. Et cetera.
60 # Got that? Look at src/interfaces/libpq/Makefile for an example.
62 # While the linker allows creation of most shared libraries,
63 # -Bsymbolic requires resolution of all symbols, making the
64 # compiler a better choice for shared library creation on ELF platforms.
65 # With the linker, -Bsymbolic requires the crt1.o startup object file.
69 COMPILER = $(CC) $(CFLAGS)
70 LINK.static = $(AR) $(AROPT)
74 ifeq ($(enable_shared), yes)
76 # Insert -L from LDFLAGS after any -L already present in SHLIB_LINK
77 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
79 # Default shlib naming convention used by the majority of platforms
80 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
81 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
82 shlib_bare = lib$(NAME)$(DLSUFFIX)
84 # For each platform we support shared libraries on, set shlib to the
85 # name of the library (if default above is not right), set
86 # LINK.shared to the command to link the library,
87 # and adjust SHLIB_LINK if necessary.
89 # Try to keep the sections in some kind of order, folks...
91 override CFLAGS += $(CFLAGS_SL)
93 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
95 ifeq ($(PORTNAME), aix)
96 shlib = lib$(NAME)$(DLSUFFIX)
100 ifeq ($(PORTNAME), darwin)
101 ifneq ($(SO_MAJOR_VERSION), 0)
102 version_link := -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
104 ifeq ($(DLTYPE), library)
107 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
109 # loadable module (default case)
111 LINK.shared = $(COMPILER) -bundle
113 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
114 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
117 ifeq ($(PORTNAME), openbsd)
119 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
122 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
126 ifeq ($(PORTNAME), bsdi)
127 ifeq ($(DLSUFFIX), .so)
128 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
131 ifeq ($(DLSUFFIX), .o)
132 LINK.shared = shlicc -O $(LDREL)
136 ifeq ($(PORTNAME), freebsd)
138 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
139 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
141 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
142 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
146 ifeq ($(PORTNAME), netbsd)
148 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
150 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
154 ifeq ($(PORTNAME), hpux)
155 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
156 ifeq ($(with_gnu_ld), yes)
157 LINK.shared = $(CC) $(LDFLAGS) -shared -Wl,-h -Wl,$(soname)
159 # can't use the CC-syntax rpath pattern here
161 ifeq ($(enable_rpath), yes)
162 LINK.shared = $(LD) +h $(soname) -b +b $(rpathdir)
164 LINK.shared = $(LD) +h $(soname) -b
166 # On HPUX platforms, gcc is usually configured to search for libraries
167 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
168 # ld can find the same libraries gcc does. Make sure it goes after any
169 # -L switches provided explicitly.
171 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
174 # do this last so above filtering doesn't pull out -L switches in LDFLAGS
176 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
180 ifeq ($(PORTNAME), irix)
181 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
182 LINK.shared = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
185 ifeq ($(PORTNAME), linux)
186 LINK.shared = $(COMPILER) -shared -Wl,-soname,$(soname)
189 ifeq ($(PORTNAME), solaris)
191 LINK.shared = $(CC) -shared
193 LINK.shared = $(CC) -G
195 ifeq ($(with_gnu_ld), yes)
196 LINK.shared += -Wl,-soname,$(soname)
198 LINK.shared += -h $(soname)
202 ifeq ($(PORTNAME), sunos4)
203 LINK.shared = $(LD) -assert pure-text -Bdynamic
206 ifeq ($(PORTNAME), osf)
207 LINK.shared = $(LD) -shared -expect_unresolved '*'
210 ifeq ($(PORTNAME), sco)
212 LINK.shared = $(CC) -shared
214 LINK.shared = $(CC) -G
216 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
219 ifeq ($(PORTNAME), svr4)
220 LINK.shared = $(LD) -G
223 ifeq ($(PORTNAME), univel)
224 LINK.shared = $(LD) -G -z text
227 ifeq ($(PORTNAME), unixware)
229 LINK.shared = $(CC) -shared
231 LINK.shared = $(CC) -G
233 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
236 ifeq ($(PORTNAME), cygwin)
237 shlib = cyg$(NAME)$(DLSUFFIX)
241 ifeq ($(PORTNAME), win32)
242 shlib = lib$(NAME)$(DLSUFFIX)
246 ifeq ($(PORTNAME), beos)
247 shlib = lib$(NAME)$(DLSUFFIX)
248 LINK.shared = $(LD) -nostart
249 SHLIB_LINK += -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
252 ifeq ($(enable_rpath), yes)
253 SHLIB_LINK += $(rpath)
256 endif # enable_shared
264 .PHONY: all-lib all-static-lib all-shared-lib
266 all-lib: all-static-lib all-shared-lib
268 all-static-lib: lib$(NAME).a
270 all-shared-lib: $(shlib)
277 lib$(NAME).a: $(OBJS)
281 $(LINK.static) $@ `$(LORDER) $^ | tsort`
286 ifeq ($(enable_shared), yes)
288 ifneq ($(PORTNAME), win32)
289 ifneq ($(PORTNAME), cygwin)
290 ifneq ($(PORTNAME), beos)
291 ifneq ($(PORTNAME), aix)
295 $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
296 # If we're using major and minor versions, then make a symlink to major-version-only.
297 ifneq ($(shlib), $(shlib_major))
299 $(LN_S) $(shlib) $(shlib_major)
301 # Make sure we have a link to a name without any version numbers
302 ifneq ($(shlib), $(shlib_bare))
304 $(LN_S) $(shlib) $(shlib_bare)
307 else # PORTNAME == aix
310 $(shlib): lib$(NAME).a
311 $(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
312 $(COMPILER) $(LDFLAGS) $(LDFLAGS_SL) -o $@ $< -Wl,-bE:lib$(NAME)$(EXPSUFF) $(SHLIB_LINK)
314 endif # PORTNAME == aix
316 else # PORTNAME == beos
320 ln -fs $(top_srcdir)/src/backend/postgres _APP_
321 $(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)
323 endif # PORTNAME == beos
325 else # PORTNAME == cygwin
328 $(shlib) lib$(NAME).a: $(OBJS)
330 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
331 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
332 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
334 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
335 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
338 endif # PORTNAME == cygwin
340 else # PORTNAME == win32
343 $(shlib) lib$(NAME).a: $(OBJS)
345 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
346 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
347 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
349 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
350 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
353 endif # PORTNAME == win32
355 endif # enable_shared
362 .PHONY: install-lib install-lib-static install-lib-shared
363 install-lib: install-lib-static install-lib-shared
365 install-lib-static: lib$(NAME).a
366 $(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
367 ifeq ($(PORTNAME), darwin)
368 cd $(DESTDIR)$(libdir) && \
372 ifeq ($(enable_shared), yes)
373 install-lib-shared: $(shlib)
374 $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
375 ifneq ($(PORTNAME), cygwin)
376 ifneq ($(PORTNAME), win32)
377 ifneq ($(shlib), $(shlib_major))
378 cd $(DESTDIR)$(libdir) && \
379 rm -f $(shlib_major) && \
380 $(LN_S) $(shlib) $(shlib_major)
382 ifneq ($(shlib), $(shlib_bare))
383 cd $(DESTDIR)$(libdir) && \
384 rm -f $(shlib_bare) && \
385 $(LN_S) $(shlib) $(shlib_bare)
389 endif # enable_shared
396 .PHONY: uninstall-lib
398 rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
399 ifeq ($(enable_shared), yes)
400 rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
401 $(DESTDIR)$(libdir)/$(shlib_major) \
402 $(DESTDIR)$(libdir)/$(shlib)
403 endif # enable_shared
413 ifeq ($(enable_shared), yes)
414 rm -f $(shlib_bare) $(shlib_major) $(shlib)
416 rm -f lib$(NAME)$(EXPSUFF)
419 ifeq ($(PORTNAME), cygwin)
420 rm -f $(NAME).dll $(NAME).def
423 ifeq ($(PORTNAME), win32)
424 rm -f $(NAME).dll $(NAME).def