]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
Reorganize some of the exports list generation code. It seems that this
[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 #    $PostgreSQL: pgsql/src/Makefile.shlib,v 1.112 2008/02/26 10:45:24 petere Exp $
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 # 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 # SHLIB_EXPORTS         (optional) Name of file containing list of symbols to
28 #                       export
29 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
30 #
31 # Optional flags when building DLL's (only applicable to win32 and cygwin
32 # platforms).
33 # DLLTOOL_DEFFLAGS      Additional flags when creating the dll .def file
34 # DLLTOOL_LIBFLAGS      Additional flags when creating the lib<module>.a file
35 # DLLWRAP_FLAGS         Additional flags to dllwrap
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 # uninstall-lib         remove the libraries from $(libdir)
46 # clean-lib             delete the static and shared libraries from the build dir
47 # maintainer-clean-lib  delete .def files built for win32
48 #
49 # Since `all-lib' is the first rule in this file you probably want to
50 # have the `all' target before including this file. In the most simple
51 # case it would look like this:
52 #
53 #     all: all-lib
54 #
55 # Similarly, the install rule might look like
56 #
57 #     install: install-lib
58 #
59 # plus any additional things you want to install. Et cetera.
60 #
61 # Got that?  Look at src/interfaces/libpq/Makefile for an example.
62 #
63 # While the linker allows creation of most shared libraries,
64 # -Bsymbolic requires resolution of all symbols, making the
65 # compiler a better choice for shared library creation on ELF platforms.
66 # With the linker, -Bsymbolic requires the crt1.o startup object file.
67 # bjm 2001-02-10
68
69
70 COMPILER = $(CC) $(CFLAGS)
71 LINK.static = $(AR) $(AROPT)
72
73
74
75 ifeq ($(enable_shared), yes)
76
77 # Insert -L from LDFLAGS after any -L already present in SHLIB_LINK
78 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
79
80 # Need a -L-free version of LDFLAGS to use in combination with SHLIB_LINK
81 LDFLAGS_NO_L = $(filter-out -L%, $(LDFLAGS))
82
83 # Default shlib naming convention used by the majority of platforms
84 shlib           = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
85 shlib_major     = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
86 shlib_bare      = lib$(NAME)$(DLSUFFIX)
87
88 # For each platform we support shared libraries on, set shlib to the
89 # name of the library (if default above is not right), set
90 # LINK.shared to the command to link the library,
91 # and adjust SHLIB_LINK if necessary.
92
93 # Try to keep the sections in some kind of order, folks...
94
95 override CFLAGS += $(CFLAGS_SL)
96
97 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
98
99 ifeq ($(PORTNAME), aix)
100   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
101   haslibarule   = yes
102   exports_file          = lib$(NAME).exp
103 endif
104
105 ifeq ($(PORTNAME), darwin)
106   ifneq ($(SO_MAJOR_VERSION), 0)
107     version_link        = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
108   endif
109   ifeq ($(DLTYPE), library)
110     # linkable library
111     DLSUFFIX            = .dylib
112     LINK.shared         = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) $(exported_symbols_list) -multiply_defined suppress
113   else
114     # loadable module (default case)
115     DLSUFFIX            = .so
116     LINK.shared         = $(COMPILER) -bundle -multiply_defined suppress
117   endif
118   shlib                 = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
119   shlib_major           = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
120   BUILD.exports         = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
121   exports_file          = $(SHLIB_EXPORTS:%.txt=%.list)
122   ifneq (,$(exports_file))
123     exported_symbols_list = -exported_symbols_list $(exports_file)
124   endif
125 endif
126
127 ifeq ($(PORTNAME), openbsd)
128   ifdef ELF_SYSTEM
129     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
130     SHLIB_LINK          += -lc
131   else
132     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
133   endif
134 endif
135
136 ifeq ($(PORTNAME), bsdi)
137   ifeq ($(DLSUFFIX), .so)
138     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
139     SHLIB_LINK          += -lc
140   endif
141   ifeq ($(DLSUFFIX), .o)
142     LINK.shared         = shlicc -O $(LDREL)
143   endif
144 endif
145
146 ifeq ($(PORTNAME), freebsd)
147   ifdef ELF_SYSTEM
148     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
149     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
150   else
151     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
152     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
153   endif
154 endif
155
156 ifeq ($(PORTNAME), netbsd)
157   ifdef ELF_SYSTEM
158     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
159   else
160     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
161   endif
162 endif
163
164 ifeq ($(PORTNAME), hpux)
165   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
166   ifeq ($(with_gnu_ld), yes)
167     LINK.shared         = $(CC) $(LDFLAGS_NO_L) -shared -Wl,-h -Wl,$(soname)
168   else
169     # can't use the CC-syntax rpath pattern here
170     rpath =
171     ifeq ($(enable_rpath), yes)
172       LINK.shared       = $(LD) +h $(soname) -b +b '$(rpathdir)'
173     else
174       LINK.shared       = $(LD) +h $(soname) -b
175     endif
176     # On HPUX platforms, gcc is usually configured to search for libraries
177     # in /usr/local/lib, but ld won't do so.  Add an explicit -L switch so
178     # ld can find the same libraries gcc does.  Make sure it goes after any
179     # -L switches provided explicitly.
180     ifeq ($(GCC), yes)
181       SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
182     endif
183   endif
184   # do this last so above filtering doesn't pull out -L switches in LDFLAGS
185   ifeq ($(GCC), yes)
186     SHLIB_LINK          += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
187   endif
188 endif
189
190 ifeq ($(PORTNAME), irix)
191   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
192   LINK.shared           = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
193 endif
194
195 ifeq ($(PORTNAME), linux)
196   LINK.shared           = $(COMPILER) -shared -Wl,-soname,$(soname)
197   BUILD.exports         = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
198   exports_file          = $(SHLIB_EXPORTS:%.txt=%.list)
199   ifneq (,$(exports_file))
200     LINK.shared         += -Wl,--version-script=$(exports_file)
201   endif
202 endif
203
204 ifeq ($(PORTNAME), solaris)
205   ifeq ($(GCC), yes)
206     LINK.shared         = $(COMPILER) -shared # $(COMPILER) needed for -m64
207   else
208 # CFLAGS added for X86_64
209     LINK.shared         = $(CC) -G $(CFLAGS)
210   endif
211   ifeq ($(with_gnu_ld), yes)
212     LINK.shared         += -Wl,-soname,$(soname)
213   else
214     LINK.shared         += -h $(soname)
215   endif
216 endif
217
218 ifeq ($(PORTNAME), sunos4)
219   LINK.shared           = $(LD) -assert pure-text -Bdynamic
220 endif
221  
222 ifeq ($(PORTNAME), osf)
223   LINK.shared           = $(LD) -shared -expect_unresolved '*'
224 endif
225
226 ifeq ($(PORTNAME), sco)
227   ifeq ($(GCC), yes)
228     LINK.shared         = $(CC) -shared
229   else
230     LINK.shared         = $(CC) -G
231     endif
232   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname)
233 endif
234
235 ifeq ($(PORTNAME), svr4)
236   LINK.shared           = $(LD) -G
237 endif
238
239 ifeq ($(PORTNAME), univel)
240   LINK.shared           = $(LD) -G -z text
241 endif
242
243 ifeq ($(PORTNAME), unixware)
244   ifeq ($(GCC), yes)
245     LINK.shared         = $(CC) -shared
246   else
247     LINK.shared         = $(CC) -G
248   endif
249   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname) 
250 endif
251
252 ifeq ($(PORTNAME), cygwin)
253   shlib                 = cyg$(NAME)$(DLSUFFIX)
254   haslibarule   = yes
255 endif
256
257 ifeq ($(PORTNAME), win32)
258   shlib                 = lib$(NAME)$(DLSUFFIX)
259   haslibarule   = yes
260 endif
261
262 ifeq ($(enable_rpath), yes)
263 SHLIB_LINK += $(rpath)
264 endif
265
266 endif # enable_shared
267
268
269
270 ##
271 ## BUILD
272 ##
273
274 .PHONY: all-lib all-static-lib all-shared-lib
275
276 all-lib: all-static-lib all-shared-lib
277
278 all-static-lib: lib$(NAME).a
279
280 all-shared-lib: $(shlib)
281
282 ifndef haslibarule
283 lib$(NAME).a: $(OBJS)
284         $(LINK.static) $@ $^
285         $(RANLIB) $@
286 endif #haslibarule
287
288 ifeq ($(enable_shared), yes)
289
290 ifeq (,$(filter cygwin win32,$(PORTNAME)))
291 ifneq ($(PORTNAME), aix)
292
293 # Normal case
294 $(shlib): $(OBJS)
295         $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
296 # If we're using major and minor versions, then make a symlink to major-version-only.
297 ifneq ($(shlib), $(shlib_major))
298         rm -f $(shlib_major)
299         $(LN_S) $(shlib) $(shlib_major)
300 endif
301 # Make sure we have a link to a name without any version numbers
302 ifneq ($(shlib), $(shlib_bare))
303         rm -f $(shlib_bare)
304         $(LN_S) $(shlib) $(shlib_bare)
305 endif
306
307 # Where possible, restrict the symbols exported by the library to just the
308 # official list, so as to avoid unintentional ABI changes.  On recent Darwin
309 # this also quiets multiply-defined-symbol warnings in programs that use
310 # libpgport along with libpq.
311 ifneq (,$(SHLIB_EXPORTS))
312 ifdef BUILD.exports
313 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
314
315 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
316         $(BUILD.exports)
317 endif
318 endif
319
320 else # PORTNAME == aix
321
322 # AIX case
323 $(shlib) lib$(NAME).a: $(OBJS)
324         $(LINK.static) lib$(NAME).a $^
325         $(RANLIB) lib$(NAME).a
326         $(MKLDEXPORT) lib$(NAME).a >$(exports_file)
327         $(COMPILER) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -o $(shlib) lib$(NAME).a -Wl,-bE:$(exports_file) $(SHLIB_LINK)
328         rm -f lib$(NAME).a
329         $(AR) $(AROPT) lib$(NAME).a $(shlib)
330
331 endif # PORTNAME == aix
332
333 else # PORTNAME == cygwin || PORTNAME == win32
334
335 # Cygwin or Win32 case
336
337 DLL_DEFFILE = lib$(NAME)dll.def
338
339 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
340 # that.  Else we build a temporary one here.
341 ifeq (,$(SHLIB_EXPORTS))
342 exports_file = $(DLL_DEFFILE)
343
344 $(exports_file): $(OBJS)
345         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
346 endif
347
348 $(shlib): $(OBJS) $(DLL_DEFFILE)
349         $(DLLWRAP) $(LDFLAGS_SL) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
350
351 lib$(NAME).a: $(shlib) $(DLL_DEFFILE)
352         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
353
354 endif # PORTNAME == cygwin || PORTNAME == win32
355
356 endif # enable_shared
357
358
359 # We need several not-quite-identical variants of .DEF files to build
360 # DLLs for Windows.  These are made from the single source file
361 # exports.txt.  Since we can't assume that Windows boxes will have
362 # sed, the .DEF files are always built and included in distribution
363 # tarballs.
364
365 ifneq (,$(SHLIB_EXPORTS))
366 all: def-files
367
368 distprep: def-files
369
370 .PHONY: def-files
371
372 def-files: $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
373
374 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
375
376 $(srcdir)/lib$(NAME)dll.def: $(SHLIB_EXPORTS)
377         echo '; DEF file for MS VC++' >$@
378         echo 'LIBRARY LIB$(UC_NAME)' >>$@
379         echo 'EXPORTS' >>$@
380         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
381
382 $(srcdir)/lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
383         echo '; DEF file for MS VC++' >$@
384         echo 'LIBRARY LIB$(UC_NAME)D' >>$@
385         echo 'EXPORTS' >>$@
386         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
387
388 $(srcdir)/blib$(NAME)dll.def: $(SHLIB_EXPORTS)
389         echo '; DEF file for Borland C++ Builder' >$@
390         echo 'LIBRARY BLIB$(UC_NAME)' >>$@
391         echo 'EXPORTS' >>$@
392         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    _\1@ \2/' $< >>$@
393         echo >>$@
394         echo '; Aliases for MS compatible names' >> $@
395         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1= _\1/' $< | sed 's/ *$$//' >>$@
396 endif # SHLIB_EXPORTS
397
398
399 ##
400 ## INSTALL
401 ##
402
403 .PHONY: install-lib install-lib-static install-lib-shared
404 install-lib: install-lib-static install-lib-shared
405
406 install-lib-static: lib$(NAME).a
407         $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/lib$(NAME).a'
408 ifeq ($(PORTNAME), darwin)
409         cd '$(DESTDIR)$(libdir)' && \
410         ranlib lib$(NAME).a
411 endif
412
413 ifeq ($(enable_shared), yes)
414 install-lib-shared: $(shlib)
415 # we don't install $(shlib) on AIX
416 ifneq ($(PORTNAME), aix)
417         $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
418 ifneq ($(PORTNAME), cygwin)
419 ifneq ($(PORTNAME), win32)
420 ifneq ($(shlib), $(shlib_major))
421         cd '$(DESTDIR)$(libdir)' && \
422         rm -f $(shlib_major) && \
423         $(LN_S) $(shlib) $(shlib_major)
424 endif
425 ifneq ($(shlib), $(shlib_bare))
426         cd '$(DESTDIR)$(libdir)' && \
427         rm -f $(shlib_bare) && \
428         $(LN_S) $(shlib) $(shlib_bare)
429 endif
430 endif # not win32
431 endif # not cygwin
432 endif # not aix
433 endif # enable_shared
434
435
436 ##
437 ## UNINSTALL
438 ##
439
440 .PHONY: uninstall-lib
441 uninstall-lib:
442         rm -f '$(DESTDIR)$(libdir)/lib$(NAME).a'
443 ifeq ($(enable_shared), yes)
444         rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
445           '$(DESTDIR)$(libdir)/$(shlib_major)' \
446           '$(DESTDIR)$(libdir)/$(shlib)'
447 endif # enable_shared
448
449
450 ##
451 ## CLEAN
452 ##
453
454 .PHONY: clean-lib
455 clean-lib:
456         rm -f lib$(NAME).a
457 ifeq ($(enable_shared), yes)
458         rm -f $(shlib_bare) $(shlib_major) $(shlib) $(exports_file)
459 endif
460
461 ifneq (,$(SHLIB_EXPORTS))
462 maintainer-clean-lib:
463         rm -f $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
464 endif