]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
Unlink static libraries before rebuilding them.
[postgresql] / src / Makefile.shlib
1 #-------------------------------------------------------------------------
2 #
3 # Makefile.shlib
4 #    Common rules for building shared libraries
5 #
6 # Copyright (c) 1998, Regents of the University of California
7 #
8 # IDENTIFICATION
9 #    src/Makefile.shlib
10 #
11 #-------------------------------------------------------------------------
12
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.
17 #
18 # Before including this file, the module Makefile must define these
19 # variables:
20 #
21 # NAME                  Name of library to build (no suffix nor "lib" prefix)
22 # OBJS                  List of object files to include in library
23 # SHLIB_LINK            If shared library relies on other libraries,
24 #                       additional stuff to put in its link command
25 # SHLIB_PREREQS         Order-only prerequisites for library build target
26 # SHLIB_EXPORTS         (optional) Name of file containing list of symbols to
27 #                       export, in the format "function_name  number"
28 #
29 # When building a shared library, the following version information
30 # must also be set.  It should be omitted when building a dynamically
31 # loadable module.
32 #
33 # SO_MAJOR_VERSION      Major version number to use for shared library
34 # SO_MINOR_VERSION      Minor version number to use for shared library
35 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
36 #
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.)
40 #
41 # This makefile provides the following (phony) targets:
42 #
43 # all-lib               build the static and shared (if applicable) libraries
44 # install-lib           install the libraries into $(libdir)
45 # installdirs-lib       create installation directory $(libdir)
46 # uninstall-lib         remove the libraries from $(libdir)
47 # clean-lib             delete the static and shared libraries from the build dir
48 # maintainer-clean-lib  delete .def files built for win32
49 #
50 # Since `all-lib' is the first rule in this file you probably want to
51 # have the `all' target before including this file. In the most simple
52 # case it would look like this:
53 #
54 #     all: all-lib
55 #
56 # Similarly, the install rule might look like
57 #
58 #     install: install-lib
59 #
60 # plus any additional things you want to install. Et cetera.
61 #
62 # Got that?  Look at src/interfaces/libpq/Makefile for an example.
63 #
64 # While the linker allows creation of most shared libraries,
65 # -Bsymbolic requires resolution of all symbols, making the
66 # compiler a better choice for shared library creation on ELF platforms.
67 # With the linker, -Bsymbolic requires the crt1.o startup object file.
68 # bjm 2001-02-10
69
70
71 COMPILER = $(CC) $(CFLAGS)
72 LINK.static = $(AR) $(AROPT)
73
74
75
76 ifdef SO_MAJOR_VERSION
77 # Default library naming convention used by the majority of platforms
78 shlib           = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
79 shlib_major     = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
80 shlib_bare      = lib$(NAME)$(DLSUFFIX)
81 # Testing the soname variable is a reliable way to determine whether a
82 # linkable library is being built.
83 soname          = $(shlib_major)
84 pkgconfigdir = $(libdir)/pkgconfig
85 else
86 # Naming convention for dynamically loadable modules
87 shlib           = $(NAME)$(DLSUFFIX)
88 endif
89 stlib           = lib$(NAME).a
90
91 ifndef soname
92 # additional flags for backend modules
93 SHLIB_LINK += $(BE_DLLLIBS)
94 endif
95
96 # For each platform we support shared libraries on, set shlib to the
97 # name of the library (if default above is not right), set
98 # LINK.shared to the command to link the library,
99 # and adjust SHLIB_LINK if necessary.
100
101 # Try to keep the sections in some kind of order, folks...
102
103 override CFLAGS += $(CFLAGS_SL)
104 ifdef SO_MAJOR_VERSION
105 # libraries ought to use this to refer to versioned gettext domain names
106 override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
107 endif
108
109 ifeq ($(PORTNAME), aix)
110   ifdef SO_MAJOR_VERSION
111     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
112   endif
113   haslibarule   = yes
114   exports_file          = lib$(NAME).exp
115 endif
116
117 ifeq ($(PORTNAME), darwin)
118   ifdef soname
119     # linkable library
120     DLSUFFIX            = .dylib
121     ifneq ($(SO_MAJOR_VERSION), 0)
122       version_link      = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
123     endif
124     LINK.shared         = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
125     shlib               = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
126     shlib_major         = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
127   else
128     # loadable module
129     DLSUFFIX            = .so
130     LINK.shared         = $(COMPILER) -bundle -multiply_defined suppress
131   endif
132   BUILD.exports         = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
133   exports_file          = $(SHLIB_EXPORTS:%.txt=%.list)
134   ifneq (,$(exports_file))
135     exported_symbols_list = -exported_symbols_list $(exports_file)
136   endif
137 endif
138
139 ifeq ($(PORTNAME), openbsd)
140   ifdef ELF_SYSTEM
141     LINK.shared         = $(COMPILER) -shared
142     ifdef soname
143       LINK.shared       += -Wl,-x,-soname,$(soname)
144     endif
145     SHLIB_LINK          += -lc
146   else
147     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
148   endif
149 endif
150
151 ifeq ($(PORTNAME), freebsd)
152   ifdef ELF_SYSTEM
153     ifdef SO_MAJOR_VERSION
154       shlib             = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
155     endif
156     LINK.shared         = $(COMPILER) -shared
157     ifdef soname
158       LINK.shared       += -Wl,-x,-soname,$(soname)
159     endif
160   else
161     ifdef SO_MAJOR_VERSION
162       shlib             = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
163     endif
164     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
165   endif
166 endif
167
168 ifeq ($(PORTNAME), netbsd)
169   ifdef ELF_SYSTEM
170     LINK.shared         = $(COMPILER) -shared
171     ifdef soname
172       LINK.shared       += -Wl,-x,-soname,$(soname)
173     endif
174   else
175     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
176   endif
177 endif
178
179 ifeq ($(PORTNAME), hpux)
180   ifdef SO_MAJOR_VERSION
181     shlib                       = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
182   endif
183   ifeq ($(with_gnu_ld), yes)
184     LINK.shared         = $(CC) -shared
185     ifdef soname
186       LINK.shared       += -Wl,-h -Wl,$(soname)
187     endif
188   else
189     LINK.shared         = $(LD) -b
190     ifdef soname
191       LINK.shared       += +h $(soname)
192     endif
193     # can't use the CC-syntax rpath pattern here, so instead:
194     rpath =
195     ifeq ($(enable_rpath), yes)
196       LINK.shared       += +b '$(rpathdir)'
197     endif
198     # On HPUX platforms, gcc is usually configured to search for libraries
199     # in /usr/local/lib, but ld won't do so.  Add an explicit -L switch so
200     # ld can find the same libraries gcc does.  Make sure it goes after any
201     # -L switches provided explicitly.
202     ifeq ($(GCC), yes)
203       SHLIB_LINK        += -L/usr/local/lib
204     endif
205   endif
206   # And we need to link with libgcc, too
207   ifeq ($(GCC), yes)
208     SHLIB_LINK          += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
209   endif
210 endif
211
212 ifeq ($(PORTNAME), linux)
213   LINK.shared           = $(COMPILER) -shared
214   ifdef soname
215     LINK.shared         += -Wl,-soname,$(soname)
216   endif
217   BUILD.exports         = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
218   exports_file          = $(SHLIB_EXPORTS:%.txt=%.list)
219   ifneq (,$(exports_file))
220     LINK.shared         += -Wl,--version-script=$(exports_file)
221   endif
222 endif
223
224 ifeq ($(PORTNAME), solaris)
225   ifeq ($(GCC), yes)
226     LINK.shared         = $(COMPILER) -shared
227   else
228     LINK.shared         = $(COMPILER) -G
229   endif
230   ifdef soname
231     ifeq ($(with_gnu_ld), yes)
232       LINK.shared       += -Wl,-soname,$(soname)
233     else
234       LINK.shared       += -h $(soname)
235     endif
236   endif
237 endif
238
239 ifeq ($(PORTNAME), sco)
240   ifeq ($(GCC), yes)
241     LINK.shared         = $(CC) -shared
242   else
243     LINK.shared         = $(CC) -G
244     endif
245   LINK.shared           += -Wl,-z,text
246   ifdef soname
247     LINK.shared         += -Wl,-h,$(soname)
248   endif
249 endif
250
251 ifeq ($(PORTNAME), unixware)
252   ifeq ($(GCC), yes)
253     LINK.shared         = $(CC) -shared
254   else
255     LINK.shared         = $(CC) -G
256   endif
257   LINK.shared           += -Wl,-z,text
258   ifdef soname
259     LINK.shared         += -Wl,-h,$(soname)
260   endif
261 endif
262
263 ifeq ($(PORTNAME), cygwin)
264   LINK.shared           = $(CC) -shared
265   ifdef SO_MAJOR_VERSION
266     shlib               = cyg$(NAME)$(DLSUFFIX)
267   endif
268   haslibarule   = yes
269 endif
270
271 ifeq ($(PORTNAME), win32)
272   ifdef SO_MAJOR_VERSION
273     shlib               = lib$(NAME)$(DLSUFFIX)
274   endif
275   haslibarule   = yes
276 endif
277
278
279
280 ##
281 ## BUILD
282 ##
283
284 .PHONY: all-lib all-static-lib all-shared-lib
285
286 all-lib: all-shared-lib
287 ifdef soname
288 # no static library when building a dynamically loadable module
289 all-lib: all-static-lib
290 all-lib: lib$(NAME).pc
291 endif
292
293 all-static-lib: $(stlib)
294
295 all-shared-lib: $(shlib)
296
297 ifndef haslibarule
298 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
299         rm -f $@
300         $(LINK.static) $@ $^
301         $(RANLIB) $@
302 endif #haslibarule
303
304
305 ifeq (,$(filter cygwin win32,$(PORTNAME)))
306 ifneq ($(PORTNAME), aix)
307
308 # Normal case
309 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
310         $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
311 ifdef shlib_major
312 # If we're using major and minor versions, then make a symlink to major-version-only.
313 ifneq ($(shlib), $(shlib_major))
314         rm -f $(shlib_major)
315         $(LN_S) $(shlib) $(shlib_major)
316 endif
317 # Make sure we have a link to a name without any version numbers
318 ifneq ($(shlib), $(shlib_bare))
319         rm -f $(shlib_bare)
320         $(LN_S) $(shlib) $(shlib_bare)
321 endif
322 endif # shlib_major
323
324 # Where possible, restrict the symbols exported by the library to just the
325 # official list, so as to avoid unintentional ABI changes.  On recent Darwin
326 # this also quiets multiply-defined-symbol warnings in programs that use
327 # libpgport along with libpq.
328 ifneq (,$(SHLIB_EXPORTS))
329 ifdef BUILD.exports
330 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
331
332 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
333         $(BUILD.exports)
334 endif
335 endif
336
337 else # PORTNAME == aix
338
339 # AIX case
340 $(shlib) $(stlib): $(OBJS) | $(SHLIB_PREREQS)
341         rm -f $(stlib)
342         $(LINK.static) $(stlib) $^
343         $(RANLIB) $(stlib)
344         $(MKLDEXPORT) $(stlib) >$(exports_file)
345         $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
346         rm -f $(stlib)
347         $(AR) $(AROPT) $(stlib) $(shlib)
348
349 endif # PORTNAME == aix
350
351 else # PORTNAME == cygwin || PORTNAME == win32
352
353 ifeq ($(PORTNAME), cygwin)
354
355 # Cygwin case
356
357 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
358         $(CC) $(CFLAGS)  -shared -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
359
360 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
361         rm -f $@
362         $(LINK.static) $@ $^
363         $(RANLIB) $@
364
365 else
366
367 # Win32 case
368
369 # There is no correct way to write a rule that generates two files.
370 # Rules with two targets don't have that meaning, they are merely
371 # shorthand for two otherwise separate rules.  To be safe for parallel
372 # make, we must chain the dependencies like this.  The semicolon is
373 # important, otherwise make will choose some built-in rule.
374
375 $(stlib): $(shlib) ;
376
377 # XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
378 # uncleanly, hence -static-libgcc.  (Last verified with MinGW-w64 compilers
379 # from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.)  Shared libgcc has better
380 # support for C++/Java exceptions; while core PostgreSQL does not use them, it
381 # would be nice to support shared libgcc for the benefit of extensions.
382 #
383 # If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
384 # Else we just use --export-all-symbols.
385 ifeq (,$(SHLIB_EXPORTS))
386 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
387         $(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
388 else
389 DLL_DEFFILE = lib$(NAME)dll.def
390
391 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
392         $(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
393 endif
394
395 endif # PORTNAME == cgywin
396 endif # PORTNAME == cygwin || PORTNAME == win32
397
398
399 %.pc: $(MAKEFILE_LIST)
400         echo 'Name: lib$(NAME)' >$@
401         echo 'Description: PostgreSQL lib$(NAME) library' >>$@
402         echo 'Url: http://www.postgresql.org/' >>$@
403         echo 'Version: $(VERSION)' >>$@
404         echo 'Requires: ' >>$@
405         echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
406         echo 'Cflags: -I$(includedir)' >>$@
407         echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
408 # Record -L flags that the user might have passed in to the PostgreSQL
409 # build to locate third-party libraries (e.g., ldap, ssl).  Filter out
410 # those that point inside the build or source tree.  Use sort to
411 # remove duplicates.  Also record the -l flags necessary for static
412 # linking, but not those already covered by Requires.private.
413         echo 'Libs.private: $(sort $(filter-out -L.% -L$(top_srcdir)/%,$(filter -L%,$(LDFLAGS) $(SHLIB_LINK)))) $(filter-out $(PKG_CONFIG_REQUIRES_PRIVATE:lib%=-l%),$(filter -l%,$(SHLIB_LINK)))' >>$@
414
415
416 # We need several not-quite-identical variants of .DEF files to build
417 # DLLs for Windows.  These are made from the single source file
418 # exports.txt.  Since we can't assume that Windows boxes will have
419 # sed, the .DEF files are always built and included in distribution
420 # tarballs.
421
422 ifneq (,$(SHLIB_EXPORTS))
423 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
424
425 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
426
427 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
428         echo '; DEF file for win32.mak release build and for Makefile.shlib (MinGW)' >$@
429         echo 'LIBRARY LIB$(UC_NAME).dll' >>$@
430         echo 'EXPORTS' >>$@
431         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
432
433 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
434         echo '; DEF file for win32.mak debug build' >$@
435         echo 'LIBRARY LIB$(UC_NAME)D.dll' >>$@
436         echo 'EXPORTS' >>$@
437         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
438
439 blib$(NAME)dll.def: $(SHLIB_EXPORTS)
440         echo '; DEF file for bcc32.mak (Borland C++ Builder)' >$@
441         echo 'LIBRARY BLIB$(UC_NAME)' >>$@
442         echo 'EXPORTS' >>$@
443         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    _\1@ \2/' $< >>$@
444         echo >>$@
445         echo '; Aliases for MS compatible names' >> $@
446         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    \1= _\1/' $< | sed 's/ *$$//' >>$@
447 endif # SHLIB_EXPORTS
448
449
450 ##
451 ## INSTALL
452 ##
453
454 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
455 install-lib: install-lib-shared
456 ifdef soname
457 install-lib: install-lib-static
458 install-lib: install-lib-pc
459 endif
460
461 install-lib-pc: lib$(NAME).pc installdirs-lib
462         $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
463
464 install-lib-static: $(stlib) installdirs-lib
465         $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
466 ifeq ($(PORTNAME), darwin)
467         cd '$(DESTDIR)$(libdir)' && \
468         ranlib $(stlib)
469 endif
470
471 install-lib-shared: $(shlib) installdirs-lib
472 ifdef soname
473 # we don't install $(shlib) on AIX
474 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
475 ifneq ($(PORTNAME), aix)
476         $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
477 ifneq ($(PORTNAME), cygwin)
478 ifneq ($(PORTNAME), win32)
479 ifneq ($(shlib), $(shlib_major))
480         cd '$(DESTDIR)$(libdir)' && \
481         rm -f $(shlib_major) && \
482         $(LN_S) $(shlib) $(shlib_major)
483 endif
484 ifneq ($(shlib), $(shlib_bare))
485         cd '$(DESTDIR)$(libdir)' && \
486         rm -f $(shlib_bare) && \
487         $(LN_S) $(shlib) $(shlib_bare)
488 endif
489 endif # not win32
490 endif # not cygwin
491 endif # not aix
492 ifneq (,$(findstring $(PORTNAME),win32 cygwin))
493         $(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
494 endif
495 else # no soname
496         $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
497 endif
498
499
500 installdirs-lib:
501 ifdef soname
502         $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
503 else
504         $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
505 endif
506
507
508 ##
509 ## UNINSTALL
510 ##
511
512 .PHONY: uninstall-lib
513 uninstall-lib:
514 ifdef soname
515         rm -f '$(DESTDIR)$(libdir)/$(stlib)'
516         rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
517           '$(DESTDIR)$(libdir)/$(shlib_major)' \
518           '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
519           '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
520 else # no soname
521         rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
522 endif # no soname
523
524
525 ##
526 ## CLEAN
527 ##
528
529 .PHONY: clean-lib
530 clean-lib:
531         rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
532
533 ifneq (,$(SHLIB_EXPORTS))
534 maintainer-clean-lib:
535         rm -f lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
536 endif