]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
> This lets you do something like:
[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.85 2004/10/15 05:11:00 momjian 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 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
28 #
29 # Optional flags when building DLL's (only applicable to win32 and cygwin
30 # platforms).
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 #
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.)
38 #
39 # This makefile provides the following (phony) targets:
40 #
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
45 #
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:
49 #
50 #     all: all-lib
51 #
52 # Similarly, the install rule might look like
53 #
54 #     install: install-lib
55 #
56 # plus any additional things you want to install. Et cetera.
57 #
58 # Got that?  Look at src/interfaces/libpq/Makefile for an example.
59 #
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.
64 # bjm 2001-02-10
65
66
67 COMPILER = $(CC) $(CFLAGS)
68 LINK.static = $(AR) $(AROPT)
69
70
71
72 ifeq ($(enable_shared), yes)
73
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)
78
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.
83
84 # Try to keep the sections in some kind of order, folks...
85
86 override CFLAGS += $(CFLAGS_SL)
87
88 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
89
90 ifeq ($(PORTNAME), aix)
91   shlib                 = lib$(NAME)$(DLSUFFIX)
92 #   SHLIB_LINK          += -lc
93 endif
94
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)
98   endif
99   ifeq ($(DLTYPE), library)
100     # linkable library
101     DLSUFFIX            := .dylib
102     LINK.shared         = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
103   else
104     # loadable module (default case)
105     DLSUFFIX            := .so
106     LINK.shared         = $(COMPILER) -bundle
107   endif
108   shlib                 = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
109   shlib_major           = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
110 endif
111
112 ifeq ($(PORTNAME), openbsd)
113   ifdef ELF_SYSTEM
114     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
115     SHLIB_LINK          += -lc
116   else
117     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
118   endif
119 endif
120
121 ifeq ($(PORTNAME), bsdi)
122   ifeq ($(DLSUFFIX), .so)
123     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
124     SHLIB_LINK          += -lc
125   endif
126   ifeq ($(DLSUFFIX), .o)
127     LINK.shared         = shlicc -O $(LDREL)
128   endif
129 endif
130
131 ifeq ($(PORTNAME), freebsd)
132   ifdef ELF_SYSTEM
133     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
134     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
135   else
136     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
137     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
138   endif
139 endif
140
141 ifeq ($(PORTNAME), netbsd)
142   ifdef ELF_SYSTEM
143     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
144   else
145     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
146   endif
147 endif
148
149 ifeq ($(PORTNAME), hpux)
150   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
151   ifeq ($(GCC), yes)
152     SHLIB_LINK          += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
153   endif
154   ifeq ($(with_gnu_ld), yes)
155     LINK.shared         = $(CC) $(LDFLAGS) -shared -Wl,-h -Wl,$(soname) -Wl,+b -Wl,$(libdir)
156   else
157     LINK.shared         = $(LD) +h $(soname) -b +b $(libdir)
158   endif
159 endif
160
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)
164 endif
165
166 ifeq ($(PORTNAME), linux)
167   LINK.shared           = $(COMPILER) -shared -Wl,-soname,$(soname)
168 endif
169
170 ifeq ($(PORTNAME), solaris)
171   ifeq ($(GCC), yes)
172     LINK.shared         = $(CC) -shared
173   else
174     LINK.shared         = $(CC) -G
175   endif
176   ifeq ($(with_gnu_ld), yes)
177     LINK.shared         += -Wl,-soname,$(soname)
178   else
179     LINK.shared         += -h $(soname)
180   endif
181 endif
182
183 ifeq ($(PORTNAME), sunos4)
184   LINK.shared           = $(LD) -assert pure-text -Bdynamic
185 endif
186  
187 ifeq ($(PORTNAME), osf)
188   LINK.shared           = $(LD) -shared -expect_unresolved '*'
189 endif
190
191 ifeq ($(PORTNAME), sco)
192   ifeq ($(GCC), yes)
193     LINK.shared         = $(CC) -shared
194   else
195     LINK.shared         = $(CC) -G
196     endif
197   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname)
198 endif
199
200 ifeq ($(PORTNAME), svr4)
201   LINK.shared           = $(LD) -G
202 endif
203
204 ifeq ($(PORTNAME), univel)
205   LINK.shared           = $(LD) -G -z text
206 endif
207
208 ifeq ($(PORTNAME), unixware)
209   ifeq ($(GCC), yes)
210     LINK.shared         = $(CC) -shared
211   else
212     LINK.shared         = $(CC) -G
213   endif
214   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname) 
215 endif
216
217 ifeq ($(PORTNAME), cygwin)
218   shlib                 = $(NAME)$(DLSUFFIX)
219   # needed for /contrib modules, not sure why
220   SHLIB_LINK            += -lpgport
221 endif
222
223 ifeq ($(PORTNAME), win32)
224   shlib                 = lib$(NAME)$(DLSUFFIX)
225 endif
226
227 ifeq ($(PORTNAME), beos)
228   shlib                 = lib$(NAME)$(DLSUFFIX)
229   LINK.shared           = $(LD) -nostart
230   SHLIB_LINK            += -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
231 endif
232
233 SHLIB_LINK := $(filter -L%, $(LDFLAGS)) $(SHLIB_LINK)
234 ifeq ($(enable_rpath), yes)
235 SHLIB_LINK += $(rpath)
236 endif
237
238 endif # enable_shared
239
240
241
242 ##
243 ## BUILD
244 ##
245
246 .PHONY: all-lib all-static-lib all-shared-lib
247
248 all-lib: all-static-lib all-shared-lib
249
250 all-static-lib: $(PTHREAD_H_WIN32) $(top_builddir)/src/port/pg_config_paths.h lib$(NAME).a
251
252 all-shared-lib: $(PTHREAD_H_WIN32) $(top_builddir)/src/port/pg_config_paths.h $(shlib)
253
254 ifndef LORDER
255 MK_NO_LORDER := true
256 endif
257
258 lib$(NAME).a: $(OBJS)
259 ifdef MK_NO_LORDER
260         $(LINK.static) $@ $^
261 else
262         $(LINK.static) $@ `$(LORDER) $^ | tsort`
263 endif
264         $(RANLIB) $@
265
266 ifeq ($(enable_shared), yes)
267
268 ifneq ($(PORTNAME), win32)
269 ifneq ($(PORTNAME), cygwin)
270 ifneq ($(PORTNAME), beos)
271 ifneq ($(PORTNAME), aix)
272
273 # Normal case
274 $(shlib): $(OBJS)
275         $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
276 # If we're using major and minor versions, then make a symlink to major-version-only.
277 ifneq ($(shlib), $(shlib_major))
278         rm -f $(shlib_major)
279         $(LN_S) $(shlib) $(shlib_major)
280 endif
281 # Make sure we have a link to a name without any version numbers
282 ifneq ($(shlib), $(shlib_bare))
283         rm -f $(shlib_bare)
284         $(LN_S) $(shlib) $(shlib_bare)
285 endif
286
287 else # PORTNAME == aix
288
289 # AIX case
290 $(shlib): lib$(NAME).a
291         $(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
292         $(COMPILER) $(LDFLAGS_SL) -o $@ $< $(LDFLAGS) $(SHLIB_LINK) -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:lib$(NAME)$(EXPSUFF)
293         
294 endif # PORTNAME == aix
295
296 else # PORTNAME == beos
297
298 # BEOS case
299 $(shlib): $(OBJS)
300         ln -fs $(top_srcdir)/src/backend/postgres _APP_
301         $(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)
302
303 endif # PORTNAME == beos
304
305 else # PORTNAME == cygwin
306
307 # Cygwin case
308 $(shlib) lib$(NAME).a: $(OBJS)
309         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
310         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
311         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
312
313 endif # PORTNAME == cygwin
314
315 else # PORTNAME == win32
316
317 # win32 case
318 $(shlib) lib$(NAME).a: $(OBJS)
319         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
320         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
321         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
322
323 endif # PORTNAME == win32
324
325 endif # enable_shared
326
327
328 ##
329 ## INSTALL
330 ##
331
332 .PHONY: install-lib install-lib-static install-lib-shared
333 install-lib: install-lib-static install-lib-shared
334
335 install-lib-static: lib$(NAME).a
336         $(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
337 ifeq ($(PORTNAME), darwin)
338         cd $(DESTDIR)$(libdir) && \
339         ranlib lib$(NAME).a
340 endif
341
342 ifeq ($(enable_shared), yes)
343 install-lib-shared: $(shlib)
344         $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
345 ifneq ($(PORTNAME), cygwin)
346 ifneq ($(PORTNAME), win32)
347 ifneq ($(shlib), $(shlib_major))
348         cd $(DESTDIR)$(libdir) && \
349         rm -f $(shlib_major) && \
350         $(LN_S) $(shlib) $(shlib_major)
351 endif
352 ifneq ($(shlib), $(shlib_bare))
353         cd $(DESTDIR)$(libdir) && \
354         rm -f $(shlib_bare) && \
355         $(LN_S) $(shlib) $(shlib_bare)
356 endif
357 endif # not win32
358 endif # not cygwin
359 endif # enable_shared
360
361
362 ##
363 ## UNINSTALL
364 ##
365
366 .PHONY: uninstall-lib
367 uninstall-lib:
368         rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
369 ifeq ($(enable_shared), yes)
370         rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
371           $(DESTDIR)$(libdir)/$(shlib_major) \
372           $(DESTDIR)$(libdir)/$(shlib)
373 endif # enable_shared
374
375
376 ##
377 ## CLEAN
378 ##
379
380 .PHONY: clean-lib
381 clean-lib:
382         rm -f lib$(NAME).a
383 ifeq ($(enable_shared), yes)
384         rm -f $(shlib_bare) $(shlib_major) $(shlib)
385 ifdef EXPSUFF
386         rm -f lib$(NAME)$(EXPSUFF)
387 endif
388 endif
389 ifeq ($(PORTNAME), cygwin)
390         rm -f $(NAME).dll $(NAME).def
391 endif
392
393 ifeq ($(PORTNAME), win32)
394         rm -f $(NAME).dll $(NAME).def
395 endif