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.79 2004/10/08 04:22:59 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
35 # The module Makefile must also include
36 # $(top_builddir)/src/Makefile.global before including this file.
37 # (Makefile.global sets PORTNAME and other needed symbols.)
39 # This makefile provides the following (phony) targets:
41 # all-lib build the static and shared (if applicable) libraries
42 # install-lib install the libraries into $(libdir)
43 # uninstall-lib remove the libraries from $(libdir)
44 # clean-lib delete the static and shared libraries from the build dir
46 # Since `all-lib' is the first rule in this file you probably want to
47 # have the `all' target before including this file. In the most simple
48 # case it would look like this:
52 # Similarly, the install rule might look like
54 # install: install-lib
56 # plus any additional things you want to install. Et cetera.
58 # Got that? Look at src/interfaces/libpq/Makefile for an example.
60 # While the linker allows creation of most shared libraries,
61 # -Bsymbolic requires resolution of all symbols, making the
62 # compiler a better choice for shared library creation on ELF platforms.
63 # With the linker, -Bsymbolic requires the crt1.o startup object file.
67 COMPILER = $(CC) $(CFLAGS)
68 LINK.static = $(AR) $(AROPT)
72 ifeq ($(enable_shared), yes)
74 # Default shlib naming convention used by the majority of platforms
75 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
76 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
77 shlib_bare = lib$(NAME)$(DLSUFFIX)
79 # For each platform we support shared libraries on, set shlib to the
80 # name of the library (if default above is not right), set
81 # LINK.shared to the command to link the library,
82 # and adjust SHLIB_LINK if necessary.
84 # Try to keep the sections in some kind of order, folks...
86 override CFLAGS += $(CFLAGS_SL)
88 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
90 ifeq ($(PORTNAME), aix)
91 shlib = lib$(NAME)$(DLSUFFIX)
95 ifeq ($(PORTNAME), darwin)
96 ifneq ($(SO_MAJOR_VERSION), 0)
97 version_link := -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
99 ifeq ($(DLTYPE), library)
102 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
104 # loadable module (default case)
106 LINK.shared = $(COMPILER) -bundle
108 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
109 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
112 ifeq ($(PORTNAME), openbsd)
114 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
117 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
121 ifeq ($(PORTNAME), bsdi)
122 ifeq ($(DLSUFFIX), .so)
123 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
126 ifeq ($(DLSUFFIX), .o)
127 LINK.shared = shlicc -O $(LDREL)
131 ifeq ($(PORTNAME), freebsd)
133 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
134 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
136 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
137 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
141 ifeq ($(PORTNAME), netbsd)
143 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
145 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
149 ifeq ($(PORTNAME), hpux)
150 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
152 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
154 ifeq ($(with_gnu_ld), yes)
155 LINK.shared = $(CC) $(LDFLAGS) -shared -Wl,-h -Wl,$(soname) -Wl,+b -Wl,$(libdir)
157 LINK.shared = $(LD) +h $(soname) -b +b $(libdir)
161 ifeq ($(PORTNAME), irix)
162 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
163 LINK.shared = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
166 ifeq ($(PORTNAME), linux)
167 LINK.shared = $(COMPILER) -shared -Wl,-soname,$(soname)
170 ifeq ($(PORTNAME), solaris)
172 LINK.shared = $(CC) -shared
174 LINK.shared = $(CC) -G
176 ifeq ($(with_gnu_ld), yes)
177 LINK.shared += -Wl,-soname,$(soname)
179 LINK.shared += -h $(soname)
183 ifeq ($(PORTNAME), sunos4)
184 LINK.shared = $(LD) -assert pure-text -Bdynamic
187 ifeq ($(PORTNAME), osf)
188 LINK.shared = $(LD) -shared -expect_unresolved '*'
191 ifeq ($(PORTNAME), sco)
193 LINK.shared = $(CC) -shared
195 LINK.shared = $(CC) -G
197 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
200 ifeq ($(PORTNAME), svr4)
201 LINK.shared = $(LD) -G
204 ifeq ($(PORTNAME), univel)
205 LINK.shared = $(LD) -G -z text
208 ifeq ($(PORTNAME), unixware)
210 LINK.shared = $(CC) -shared
212 LINK.shared = $(CC) -G
214 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
217 ifeq ($(PORTNAME), cygwin)
218 shlib = $(NAME)$(DLSUFFIX)
221 ifeq ($(PORTNAME), win32)
222 shlib = lib$(NAME)$(DLSUFFIX)
225 ifeq ($(PORTNAME), beos)
226 shlib = lib$(NAME)$(DLSUFFIX)
227 LINK.shared = $(LD) -nostart
228 SHLIB_LINK += -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
231 SHLIB_LINK := $(filter -L%, $(LDFLAGS)) $(SHLIB_LINK)
232 ifeq ($(enable_rpath), yes)
233 SHLIB_LINK += $(rpath)
236 endif # enable_shared
244 .PHONY: all-lib all-static-lib all-shared-lib
246 all-lib: all-static-lib all-shared-lib
248 all-static-lib: lib$(NAME).a
250 all-shared-lib: $(shlib)
252 ifneq ($(PORTNAME), cygwin)
253 ifneq ($(PORTNAME), win32)
259 lib$(NAME).a: $(OBJS)
263 $(LINK.static) $@ `$(LORDER) $^ | tsort`
270 ifeq ($(enable_shared), yes)
272 ifneq ($(PORTNAME), win32)
273 ifneq ($(PORTNAME), cygwin)
274 ifneq ($(PORTNAME), beos)
275 ifneq ($(PORTNAME), aix)
279 $(LINK.shared) $(OBJS) $(SHLIB_LINK) -o $@
280 # If we're using major and minor versions, then make a symlink to major-version-only.
281 ifneq ($(shlib), $(shlib_major))
283 $(LN_S) $(shlib) $(shlib_major)
285 # Make sure we have a link to a name without any version numbers
286 ifneq ($(shlib), $(shlib_bare))
288 $(LN_S) $(shlib) $(shlib_bare)
291 else # PORTNAME == aix
294 $(shlib): lib$(NAME).a
295 $(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
296 $(COMPILER) $(LDFLAGS_SL) -o $@ $< $(LDFLAGS) $(SHLIB_LINK) -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:lib$(NAME)$(EXPSUFF)
298 endif # PORTNAME == aix
300 else # PORTNAME == beos
304 ln -fs $(top_srcdir)/src/backend/postgres _APP_
305 $(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)
307 endif # PORTNAME == beos
309 else # PORTNAME == cygwin
312 $(shlib) lib$(NAME).a: $(OBJS) $(DLLINIT)
313 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
314 $(DLLWRAP) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(DLLINIT) $(SHLIB_LINK)
315 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
317 $(DLLINIT): $(DLLINIT:%.o=%.c)
318 $(MAKE) -C $(@D) $(@F)
320 endif # PORTNAME == cygwin
322 else # PORTNAME == win32
325 $(shlib) lib$(NAME).a: $(OBJS)
326 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
327 $(DLLWRAP) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
328 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
330 endif # PORTNAME == win32
332 endif # enable_shared
339 .PHONY: install-lib install-lib-static install-lib-shared
340 install-lib: install-lib-static install-lib-shared
342 install-lib-static: lib$(NAME).a
343 $(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
344 ifeq ($(PORTNAME), darwin)
345 cd $(DESTDIR)$(libdir) && \
349 ifeq ($(enable_shared), yes)
350 install-lib-shared: $(shlib)
351 $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
352 ifneq ($(PORTNAME), cygwin)
353 ifneq ($(PORTNAME), win32)
354 ifneq ($(shlib), $(shlib_major))
355 cd $(DESTDIR)$(libdir) && \
356 rm -f $(shlib_major) && \
357 $(LN_S) $(shlib) $(shlib_major)
359 ifneq ($(shlib), $(shlib_bare))
360 cd $(DESTDIR)$(libdir) && \
361 rm -f $(shlib_bare) && \
362 $(LN_S) $(shlib) $(shlib_bare)
366 endif # enable_shared
373 .PHONY: uninstall-lib
375 rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
376 ifeq ($(enable_shared), yes)
377 rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
378 $(DESTDIR)$(libdir)/$(shlib_major) \
379 $(DESTDIR)$(libdir)/$(shlib)
380 endif # enable_shared
390 ifeq ($(enable_shared), yes)
391 rm -f $(shlib_bare) $(shlib_major) $(shlib)
393 rm -f lib$(NAME)$(EXPSUFF)
396 ifeq ($(PORTNAME), cygwin)
397 rm -f $(NAME).dll $(NAME).def
400 ifeq ($(PORTNAME), win32)
401 rm -f $(NAME).dll $(NAME).def