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.99 2005/12/03 20:16:31 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 # Need a -L-free version of LDFLAGS to use in combination with SHLIB_LINK
80 LDFLAGS_NO_L := $(filter-out -L%, $(LDFLAGS))
82 # Default shlib naming convention used by the majority of platforms
83 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
84 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
85 shlib_bare = lib$(NAME)$(DLSUFFIX)
87 # For each platform we support shared libraries on, set shlib to the
88 # name of the library (if default above is not right), set
89 # LINK.shared to the command to link the library,
90 # and adjust SHLIB_LINK if necessary.
92 # Try to keep the sections in some kind of order, folks...
94 override CFLAGS += $(CFLAGS_SL)
96 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
98 ifeq ($(PORTNAME), aix)
99 shlib = lib$(NAME)$(DLSUFFIX)
103 ifeq ($(PORTNAME), darwin)
104 ifneq ($(SO_MAJOR_VERSION), 0)
105 version_link := -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
107 ifeq ($(DLTYPE), library)
110 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
112 # loadable module (default case)
114 LINK.shared = $(COMPILER) -bundle
116 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
117 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
120 ifeq ($(PORTNAME), openbsd)
122 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
125 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
129 ifeq ($(PORTNAME), bsdi)
130 ifeq ($(DLSUFFIX), .so)
131 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
134 ifeq ($(DLSUFFIX), .o)
135 LINK.shared = shlicc -O $(LDREL)
139 ifeq ($(PORTNAME), freebsd)
141 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
142 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
144 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
145 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
149 ifeq ($(PORTNAME), netbsd)
151 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
153 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
157 ifeq ($(PORTNAME), hpux)
158 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
159 ifeq ($(with_gnu_ld), yes)
160 LINK.shared = $(CC) $(LDFLAGS_NO_L) -shared -Wl,-h -Wl,$(soname)
162 # can't use the CC-syntax rpath pattern here
164 ifeq ($(enable_rpath), yes)
165 LINK.shared = $(LD) +h $(soname) -b +b $(rpathdir)
167 LINK.shared = $(LD) +h $(soname) -b
169 # On HPUX platforms, gcc is usually configured to search for libraries
170 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
171 # ld can find the same libraries gcc does. Make sure it goes after any
172 # -L switches provided explicitly.
174 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
177 # do this last so above filtering doesn't pull out -L switches in LDFLAGS
179 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
183 ifeq ($(PORTNAME), irix)
184 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
185 LINK.shared = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
188 ifeq ($(PORTNAME), linux)
189 LINK.shared = $(COMPILER) -shared -Wl,-soname,$(soname)
192 ifeq ($(PORTNAME), solaris)
194 LINK.shared = $(CC) -shared
196 LINK.shared = $(CC) -G
198 ifeq ($(with_gnu_ld), yes)
199 LINK.shared += -Wl,-soname,$(soname)
201 LINK.shared += -h $(soname)
205 ifeq ($(PORTNAME), sunos4)
206 LINK.shared = $(LD) -assert pure-text -Bdynamic
209 ifeq ($(PORTNAME), osf)
210 LINK.shared = $(LD) -shared -expect_unresolved '*'
213 ifeq ($(PORTNAME), sco)
215 LINK.shared = $(CC) -shared
217 LINK.shared = $(CC) -G
219 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
222 ifeq ($(PORTNAME), svr4)
223 LINK.shared = $(LD) -G
226 ifeq ($(PORTNAME), univel)
227 LINK.shared = $(LD) -G -z text
230 ifeq ($(PORTNAME), unixware)
232 LINK.shared = $(CC) -shared
234 LINK.shared = $(CC) -G
236 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
239 ifeq ($(PORTNAME), cygwin)
240 shlib = cyg$(NAME)$(DLSUFFIX)
244 ifeq ($(PORTNAME), win32)
245 shlib = lib$(NAME)$(DLSUFFIX)
249 ifeq ($(PORTNAME), beos)
250 shlib = lib$(NAME)$(DLSUFFIX)
251 LINK.shared = $(LD) -nostart
252 SHLIB_LINK += -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
255 ifeq ($(enable_rpath), yes)
256 SHLIB_LINK += $(rpath)
259 endif # enable_shared
267 .PHONY: all-lib all-static-lib all-shared-lib
269 all-lib: all-static-lib all-shared-lib
271 all-static-lib: lib$(NAME).a
273 all-shared-lib: $(shlib)
280 lib$(NAME).a: $(OBJS)
284 $(LINK.static) $@ `$(LORDER) $^ | tsort`
289 ifeq ($(enable_shared), yes)
291 ifneq ($(PORTNAME), win32)
292 ifneq ($(PORTNAME), cygwin)
293 ifneq ($(PORTNAME), beos)
294 ifneq ($(PORTNAME), aix)
298 $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
299 # If we're using major and minor versions, then make a symlink to major-version-only.
300 ifneq ($(shlib), $(shlib_major))
302 $(LN_S) $(shlib) $(shlib_major)
304 # Make sure we have a link to a name without any version numbers
305 ifneq ($(shlib), $(shlib_bare))
307 $(LN_S) $(shlib) $(shlib_bare)
310 else # PORTNAME == aix
313 $(shlib): lib$(NAME).a
314 $(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
315 $(COMPILER) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -o $@ $< -Wl,-bE:lib$(NAME)$(EXPSUFF) $(SHLIB_LINK)
317 endif # PORTNAME == aix
319 else # PORTNAME == beos
323 ln -fs $(top_srcdir)/src/backend/postgres _APP_
324 $(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)
326 endif # PORTNAME == beos
328 else # PORTNAME == cygwin
331 $(shlib) lib$(NAME).a: $(OBJS)
333 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
334 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
335 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
337 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
338 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
341 endif # PORTNAME == cygwin
343 else # PORTNAME == win32
346 $(shlib) lib$(NAME).a: $(OBJS)
348 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
349 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
350 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
352 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
353 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
356 endif # PORTNAME == win32
358 endif # enable_shared
365 .PHONY: install-lib install-lib-static install-lib-shared
366 install-lib: install-lib-static install-lib-shared
368 install-lib-static: lib$(NAME).a
369 $(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
370 ifeq ($(PORTNAME), darwin)
371 cd $(DESTDIR)$(libdir) && \
375 ifeq ($(enable_shared), yes)
376 install-lib-shared: $(shlib)
377 $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
378 ifneq ($(PORTNAME), cygwin)
379 ifneq ($(PORTNAME), win32)
380 ifneq ($(shlib), $(shlib_major))
381 cd $(DESTDIR)$(libdir) && \
382 rm -f $(shlib_major) && \
383 $(LN_S) $(shlib) $(shlib_major)
385 ifneq ($(shlib), $(shlib_bare))
386 cd $(DESTDIR)$(libdir) && \
387 rm -f $(shlib_bare) && \
388 $(LN_S) $(shlib) $(shlib_bare)
392 endif # enable_shared
399 .PHONY: uninstall-lib
401 rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
402 ifeq ($(enable_shared), yes)
403 rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
404 $(DESTDIR)$(libdir)/$(shlib_major) \
405 $(DESTDIR)$(libdir)/$(shlib)
406 endif # enable_shared
416 ifeq ($(enable_shared), yes)
417 rm -f $(shlib_bare) $(shlib_major) $(shlib)
419 rm -f lib$(NAME)$(EXPSUFF)
422 ifeq ($(PORTNAME), cygwin)
423 rm -f $(NAME).dll $(NAME).def
426 ifeq ($(PORTNAME), win32)
427 rm -f $(NAME).dll $(NAME).def