]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
COPY: use pg_plan_query() instead of planner()
[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) is also usable as an import file
115   exports_file          = lib$(NAME).exp
116 endif
117
118 ifeq ($(PORTNAME), darwin)
119   ifdef soname
120     # linkable library
121     DLSUFFIX            = .dylib
122     ifneq ($(SO_MAJOR_VERSION), 0)
123       version_link      = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
124     endif
125     LINK.shared         = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
126     shlib               = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
127     shlib_major         = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
128   else
129     # loadable module
130     DLSUFFIX            = .so
131     LINK.shared         = $(COMPILER) -bundle -multiply_defined suppress -Wl,-undefined,dynamic_lookup
132   endif
133   BUILD.exports         = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
134   exports_file          = $(SHLIB_EXPORTS:%.txt=%.list)
135   ifneq (,$(exports_file))
136     exported_symbols_list = -exported_symbols_list $(exports_file)
137   endif
138 endif
139
140 ifeq ($(PORTNAME), openbsd)
141   ifdef ELF_SYSTEM
142     LINK.shared         = $(COMPILER) -shared
143     ifdef soname
144       LINK.shared       += -Wl,-x,-soname,$(soname)
145     endif
146     SHLIB_LINK          += -lc
147   else
148     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
149   endif
150 endif
151
152 ifeq ($(PORTNAME), freebsd)
153   ifdef ELF_SYSTEM
154     ifdef SO_MAJOR_VERSION
155       shlib             = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
156     endif
157     LINK.shared         = $(COMPILER) -shared
158     ifdef soname
159       LINK.shared       += -Wl,-x,-soname,$(soname)
160     endif
161   else
162     ifdef SO_MAJOR_VERSION
163       shlib             = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
164     endif
165     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
166   endif
167 endif
168
169 ifeq ($(PORTNAME), netbsd)
170   ifdef ELF_SYSTEM
171     LINK.shared         = $(COMPILER) -shared
172     ifdef soname
173       LINK.shared       += -Wl,-x,-soname,$(soname)
174     endif
175   else
176     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
177   endif
178 endif
179
180 ifeq ($(PORTNAME), hpux)
181   ifdef SO_MAJOR_VERSION
182     shlib                       = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
183   endif
184   ifeq ($(with_gnu_ld), yes)
185     LINK.shared         = $(CC) -shared
186     ifdef soname
187       LINK.shared       += -Wl,-h -Wl,$(soname)
188     endif
189   else
190     LINK.shared         = $(LD) -b
191     ifdef soname
192       LINK.shared       += +h $(soname)
193     endif
194     # can't use the CC-syntax rpath pattern here, so instead:
195     rpath =
196     ifeq ($(enable_rpath), yes)
197       LINK.shared       += +b '$(rpathdir)'
198     endif
199     # On HPUX platforms, gcc is usually configured to search for libraries
200     # in /usr/local/lib, but ld won't do so.  Add an explicit -L switch so
201     # ld can find the same libraries gcc does.  Make sure it goes after any
202     # -L switches provided explicitly.
203     ifeq ($(GCC), yes)
204       SHLIB_LINK        += -L/usr/local/lib
205     endif
206   endif
207   # And we need to link with libgcc, too
208   ifeq ($(GCC), yes)
209     SHLIB_LINK          += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
210   endif
211 endif
212
213 ifeq ($(PORTNAME), linux)
214   LINK.shared           = $(COMPILER) -shared
215   ifdef soname
216     LINK.shared         += -Wl,-soname,$(soname)
217   endif
218   BUILD.exports         = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
219   exports_file          = $(SHLIB_EXPORTS:%.txt=%.list)
220   ifneq (,$(exports_file))
221     LINK.shared         += -Wl,--version-script=$(exports_file)
222   endif
223 endif
224
225 ifeq ($(PORTNAME), solaris)
226   ifeq ($(GCC), yes)
227     LINK.shared         = $(COMPILER) -shared
228   else
229     LINK.shared         = $(COMPILER) -G
230   endif
231   ifdef soname
232     ifeq ($(with_gnu_ld), yes)
233       LINK.shared       += -Wl,-soname,$(soname)
234     else
235       LINK.shared       += -h $(soname)
236     endif
237   endif
238 endif
239
240 ifeq ($(PORTNAME), sco)
241   ifeq ($(GCC), yes)
242     LINK.shared         = $(CC) -shared
243   else
244     LINK.shared         = $(CC) -G
245     endif
246   LINK.shared           += -Wl,-z,text
247   ifdef soname
248     LINK.shared         += -Wl,-h,$(soname)
249   endif
250 endif
251
252 ifeq ($(PORTNAME), unixware)
253   ifeq ($(GCC), yes)
254     LINK.shared         = $(CC) -shared
255   else
256     LINK.shared         = $(CC) -G
257   endif
258   LINK.shared           += -Wl,-z,text
259   ifdef soname
260     LINK.shared         += -Wl,-h,$(soname)
261   endif
262 endif
263
264 ifeq ($(PORTNAME), cygwin)
265   LINK.shared           = $(CC) -shared
266   ifdef SO_MAJOR_VERSION
267     shlib               = cyg$(NAME)$(DLSUFFIX)
268   endif
269   haslibarule   = yes
270 endif
271
272 ifeq ($(PORTNAME), win32)
273   ifdef SO_MAJOR_VERSION
274     shlib               = lib$(NAME)$(DLSUFFIX)
275   endif
276   haslibarule   = yes
277 endif
278
279
280
281 ##
282 ## BUILD
283 ##
284
285 .PHONY: all-lib all-static-lib all-shared-lib
286
287 all-lib: all-shared-lib
288 ifdef soname
289 # no static library when building a dynamically loadable module
290 all-lib: all-static-lib
291 all-lib: lib$(NAME).pc
292 endif
293
294 all-static-lib: $(stlib)
295
296 all-shared-lib: $(shlib)
297
298 ifndef haslibarule
299 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
300         rm -f $@
301         $(LINK.static) $@ $^
302         $(RANLIB) $@
303 endif #haslibarule
304
305
306 ifeq (,$(filter cygwin win32,$(PORTNAME)))
307 ifneq ($(PORTNAME), aix)
308
309 # Normal case
310 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
311         $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
312 ifdef shlib_major
313 # If we're using major and minor versions, then make a symlink to major-version-only.
314 ifneq ($(shlib), $(shlib_major))
315         rm -f $(shlib_major)
316         $(LN_S) $(shlib) $(shlib_major)
317 endif
318 # Make sure we have a link to a name without any version numbers
319 ifneq ($(shlib), $(shlib_bare))
320         rm -f $(shlib_bare)
321         $(LN_S) $(shlib) $(shlib_bare)
322 endif
323 endif # shlib_major
324
325 # Where possible, restrict the symbols exported by the library to just the
326 # official list, so as to avoid unintentional ABI changes.  On recent Darwin
327 # this also quiets multiply-defined-symbol warnings in programs that use
328 # libpgport along with libpq.
329 ifneq (,$(SHLIB_EXPORTS))
330 ifdef BUILD.exports
331 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
332
333 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
334         $(BUILD.exports)
335 endif
336 endif
337
338 else # PORTNAME == aix
339
340 # AIX case
341 $(shlib) $(stlib): $(OBJS) | $(SHLIB_PREREQS)
342         rm -f $(stlib)
343         $(LINK.static) $(stlib) $^
344         $(RANLIB) $(stlib)
345         $(MKLDEXPORT) $(stlib) $(shlib) >$(exports_file)
346         $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
347         rm -f $(stlib)
348         $(AR) $(AROPT) $(stlib) $(shlib)
349
350 endif # PORTNAME == aix
351
352 else # PORTNAME == cygwin || PORTNAME == win32
353
354 ifeq ($(PORTNAME), cygwin)
355
356 # Cygwin case
357
358 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
359         $(CC) $(CFLAGS)  -shared -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
360
361 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
362         rm -f $@
363         $(LINK.static) $@ $^
364         $(RANLIB) $@
365
366 else
367
368 # Win32 case
369
370 # There is no correct way to write a rule that generates two files.
371 # Rules with two targets don't have that meaning, they are merely
372 # shorthand for two otherwise separate rules.  To be safe for parallel
373 # make, we must chain the dependencies like this.  The semicolon is
374 # important, otherwise make will choose some built-in rule.
375
376 $(stlib): $(shlib) ;
377
378 # XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
379 # uncleanly, hence -static-libgcc.  (Last verified with MinGW-w64 compilers
380 # from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.)  Shared libgcc has better
381 # support for C++/Java exceptions; while core PostgreSQL does not use them, it
382 # would be nice to support shared libgcc for the benefit of extensions.
383 #
384 # If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
385 # Else we just use --export-all-symbols.
386 ifeq (,$(SHLIB_EXPORTS))
387 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
388         $(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
389 else
390 DLL_DEFFILE = lib$(NAME)dll.def
391
392 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
393         $(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
394 endif
395
396 endif # PORTNAME == cgywin
397 endif # PORTNAME == cygwin || PORTNAME == win32
398
399
400 %.pc: $(MAKEFILE_LIST)
401         echo 'Name: lib$(NAME)' >$@
402         echo 'Description: PostgreSQL lib$(NAME) library' >>$@
403         echo 'Url: http://www.postgresql.org/' >>$@
404         echo 'Version: $(VERSION)' >>$@
405         echo 'Requires: ' >>$@
406         echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
407         echo 'Cflags: -I$(includedir)' >>$@
408         echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
409 # Record -L flags that the user might have passed in to the PostgreSQL
410 # build to locate third-party libraries (e.g., ldap, ssl).  Filter out
411 # those that point inside the build or source tree.  Use sort to
412 # remove duplicates.  Also record the -l flags necessary for static
413 # linking, but not those already covered by Requires.private.
414         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)))' >>$@
415
416
417 # We need several not-quite-identical variants of .DEF files to build
418 # DLLs for Windows.  These are made from the single source file
419 # exports.txt.  Since we can't assume that Windows boxes will have
420 # sed, the .DEF files are always built and included in distribution
421 # tarballs.
422
423 ifneq (,$(SHLIB_EXPORTS))
424 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
425
426 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
427
428 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
429         echo '; DEF file for win32.mak release build and for Makefile.shlib (MinGW)' >$@
430         echo 'LIBRARY LIB$(UC_NAME).dll' >>$@
431         echo 'EXPORTS' >>$@
432         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
433
434 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
435         echo '; DEF file for win32.mak debug build' >$@
436         echo 'LIBRARY LIB$(UC_NAME)D.dll' >>$@
437         echo 'EXPORTS' >>$@
438         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
439
440 blib$(NAME)dll.def: $(SHLIB_EXPORTS)
441         echo '; DEF file for bcc32.mak (Borland C++ Builder)' >$@
442         echo 'LIBRARY BLIB$(UC_NAME)' >>$@
443         echo 'EXPORTS' >>$@
444         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    _\1@ \2/' $< >>$@
445         echo >>$@
446         echo '; Aliases for MS compatible names' >> $@
447         sed -e '/^#/d' -e 's/^\(.*[     ]\)\([0-9][0-9]*\)/    \1= _\1/' $< | sed 's/ *$$//' >>$@
448 endif # SHLIB_EXPORTS
449
450
451 ##
452 ## INSTALL
453 ##
454
455 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
456 install-lib: install-lib-shared
457 ifdef soname
458 install-lib: install-lib-static
459 install-lib: install-lib-pc
460 endif
461
462 install-lib-pc: lib$(NAME).pc installdirs-lib
463         $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
464
465 install-lib-static: $(stlib) installdirs-lib
466         $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
467 ifeq ($(PORTNAME), darwin)
468         cd '$(DESTDIR)$(libdir)' && \
469         ranlib $(stlib)
470 endif
471
472 install-lib-shared: $(shlib) installdirs-lib
473 ifdef soname
474 # we don't install $(shlib) on AIX
475 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
476 ifneq ($(PORTNAME), aix)
477         $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
478 ifneq ($(PORTNAME), cygwin)
479 ifneq ($(PORTNAME), win32)
480 ifneq ($(shlib), $(shlib_major))
481         cd '$(DESTDIR)$(libdir)' && \
482         rm -f $(shlib_major) && \
483         $(LN_S) $(shlib) $(shlib_major)
484 endif
485 ifneq ($(shlib), $(shlib_bare))
486         cd '$(DESTDIR)$(libdir)' && \
487         rm -f $(shlib_bare) && \
488         $(LN_S) $(shlib) $(shlib_bare)
489 endif
490 endif # not win32
491 endif # not cygwin
492 endif # not aix
493 ifneq (,$(findstring $(PORTNAME),win32 cygwin))
494         $(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
495 endif
496 else # no soname
497         $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
498 endif
499
500
501 installdirs-lib:
502 ifdef soname
503         $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
504 else
505         $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
506 endif
507
508
509 ##
510 ## UNINSTALL
511 ##
512
513 .PHONY: uninstall-lib
514 uninstall-lib:
515 ifdef soname
516         rm -f '$(DESTDIR)$(libdir)/$(stlib)'
517         rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
518           '$(DESTDIR)$(libdir)/$(shlib_major)' \
519           '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
520           '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
521 else # no soname
522         rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
523 endif # no soname
524
525
526 ##
527 ## CLEAN
528 ##
529
530 .PHONY: clean-lib
531 clean-lib:
532         rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
533
534 ifneq (,$(SHLIB_EXPORTS))
535 maintainer-clean-lib:
536         rm -f lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
537 endif