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.94 2005/07/13 02:11:57 momjian 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 # Default shlib naming convention used by the majority of platforms
77 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
78 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
79 shlib_bare = lib$(NAME)$(DLSUFFIX)
81 # For each platform we support shared libraries on, set shlib to the
82 # name of the library (if default above is not right), set
83 # LINK.shared to the command to link the library,
84 # and adjust SHLIB_LINK if necessary.
86 # Try to keep the sections in some kind of order, folks...
88 override CFLAGS += $(CFLAGS_SL)
90 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
92 ifeq ($(PORTNAME), aix)
93 shlib = lib$(NAME)$(DLSUFFIX)
97 ifeq ($(PORTNAME), darwin)
98 ifneq ($(SO_MAJOR_VERSION), 0)
99 version_link := -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
101 ifeq ($(DLTYPE), library)
104 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
106 # loadable module (default case)
108 LINK.shared = $(COMPILER) -bundle
110 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
111 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
114 ifeq ($(PORTNAME), openbsd)
116 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
119 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
123 ifeq ($(PORTNAME), bsdi)
124 ifeq ($(DLSUFFIX), .so)
125 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
128 ifeq ($(DLSUFFIX), .o)
129 LINK.shared = shlicc -O $(LDREL)
133 ifeq ($(PORTNAME), freebsd)
135 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
136 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
138 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
139 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
143 ifeq ($(PORTNAME), netbsd)
145 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
147 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
151 ifeq ($(PORTNAME), hpux)
152 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
154 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
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 += -L/usr/local/lib
176 ifeq ($(PORTNAME), irix)
177 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
178 LINK.shared = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
181 ifeq ($(PORTNAME), linux)
182 LINK.shared = $(COMPILER) -shared -Wl,-soname,$(soname)
185 ifeq ($(PORTNAME), solaris)
187 LINK.shared = $(CC) -shared
189 LINK.shared = $(CC) -G
191 ifeq ($(with_gnu_ld), yes)
192 LINK.shared += -Wl,-soname,$(soname)
194 LINK.shared += -h $(soname)
198 ifeq ($(PORTNAME), sunos4)
199 LINK.shared = $(LD) -assert pure-text -Bdynamic
202 ifeq ($(PORTNAME), osf)
203 LINK.shared = $(LD) -shared -expect_unresolved '*'
206 ifeq ($(PORTNAME), sco)
208 LINK.shared = $(CC) -shared
210 LINK.shared = $(CC) -G
212 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
215 ifeq ($(PORTNAME), svr4)
216 LINK.shared = $(LD) -G
219 ifeq ($(PORTNAME), univel)
220 LINK.shared = $(LD) -G -z text
223 ifeq ($(PORTNAME), unixware)
225 LINK.shared = $(CC) -shared
227 LINK.shared = $(CC) -G
229 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
232 ifeq ($(PORTNAME), cygwin)
233 shlib = $(NAME)$(DLSUFFIX)
234 # needed for /contrib modules, not sure why
235 SHLIB_LINK += $(LIBS)
239 ifeq ($(PORTNAME), win32)
240 shlib = lib$(NAME)$(DLSUFFIX)
244 ifeq ($(PORTNAME), beos)
245 shlib = lib$(NAME)$(DLSUFFIX)
246 LINK.shared = $(LD) -nostart
247 SHLIB_LINK += -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
250 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
251 ifeq ($(enable_rpath), yes)
252 SHLIB_LINK += $(rpath)
255 endif # enable_shared
263 .PHONY: all-lib all-static-lib all-shared-lib
265 all-lib: all-static-lib all-shared-lib
267 all-static-lib: lib$(NAME).a
269 all-shared-lib: $(shlib)
276 lib$(NAME).a: $(OBJS)
280 $(LINK.static) $@ `$(LORDER) $^ | tsort`
285 ifeq ($(enable_shared), yes)
287 ifneq ($(PORTNAME), win32)
288 ifneq ($(PORTNAME), cygwin)
289 ifneq ($(PORTNAME), beos)
290 ifneq ($(PORTNAME), aix)
294 $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
295 # If we're using major and minor versions, then make a symlink to major-version-only.
296 ifneq ($(shlib), $(shlib_major))
298 $(LN_S) $(shlib) $(shlib_major)
300 # Make sure we have a link to a name without any version numbers
301 ifneq ($(shlib), $(shlib_bare))
303 $(LN_S) $(shlib) $(shlib_bare)
306 else # PORTNAME == aix
309 $(shlib): lib$(NAME).a
310 $(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
311 $(COMPILER) $(LDFLAGS_SL) -o $@ $< $(LDFLAGS) $(SHLIB_LINK) -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:lib$(NAME)$(EXPSUFF)
313 endif # PORTNAME == aix
315 else # PORTNAME == beos
319 ln -fs $(top_srcdir)/src/backend/postgres _APP_
320 $(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)
322 endif # PORTNAME == beos
324 else # PORTNAME == cygwin
327 $(shlib) lib$(NAME).a: $(OBJS)
329 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
330 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
331 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
333 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
334 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
337 endif # PORTNAME == cygwin
339 else # PORTNAME == win32
342 $(shlib) lib$(NAME).a: $(OBJS)
344 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
345 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
346 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
348 $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
349 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
352 endif # PORTNAME == win32
354 endif # enable_shared
361 .PHONY: install-lib install-lib-static install-lib-shared
362 install-lib: install-lib-static install-lib-shared
364 install-lib-static: lib$(NAME).a
365 $(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
366 ifeq ($(PORTNAME), darwin)
367 cd $(DESTDIR)$(libdir) && \
371 ifeq ($(enable_shared), yes)
372 install-lib-shared: $(shlib)
373 $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
374 ifneq ($(PORTNAME), cygwin)
375 ifneq ($(PORTNAME), win32)
376 ifneq ($(shlib), $(shlib_major))
377 cd $(DESTDIR)$(libdir) && \
378 rm -f $(shlib_major) && \
379 $(LN_S) $(shlib) $(shlib_major)
381 ifneq ($(shlib), $(shlib_bare))
382 cd $(DESTDIR)$(libdir) && \
383 rm -f $(shlib_bare) && \
384 $(LN_S) $(shlib) $(shlib_bare)
388 endif # enable_shared
395 .PHONY: uninstall-lib
397 rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
398 ifeq ($(enable_shared), yes)
399 rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
400 $(DESTDIR)$(libdir)/$(shlib_major) \
401 $(DESTDIR)$(libdir)/$(shlib)
402 endif # enable_shared
412 ifeq ($(enable_shared), yes)
413 rm -f $(shlib_bare) $(shlib_major) $(shlib)
415 rm -f lib$(NAME)$(EXPSUFF)
418 ifeq ($(PORTNAME), cygwin)
419 rm -f $(NAME).dll $(NAME).def
422 ifeq ($(PORTNAME), win32)
423 rm -f $(NAME).dll $(NAME).def