]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
Move -L flag around for shared builds:
[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.94 2005/07/13 02:11:57 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 # DLL_DEFFILE           Use pre-existing .def file instead of auto-generating
35 #                       one with all exports in it (win32 only).
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 #
48 # Since `all-lib' is the first rule in this file you probably want to
49 # have the `all' target before including this file. In the most simple
50 # case it would look like this:
51 #
52 #     all: all-lib
53 #
54 # Similarly, the install rule might look like
55 #
56 #     install: install-lib
57 #
58 # plus any additional things you want to install. Et cetera.
59 #
60 # Got that?  Look at src/interfaces/libpq/Makefile for an example.
61 #
62 # While the linker allows creation of most shared libraries,
63 # -Bsymbolic requires resolution of all symbols, making the
64 # compiler a better choice for shared library creation on ELF platforms.
65 # With the linker, -Bsymbolic requires the crt1.o startup object file.
66 # bjm 2001-02-10
67
68
69 COMPILER = $(CC) $(CFLAGS)
70 LINK.static = $(AR) $(AROPT)
71
72
73
74 ifeq ($(enable_shared), yes)
75
76 # Default shlib naming convention used by the majority of platforms
77 shlib           = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
78 shlib_major     = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
79 shlib_bare      = lib$(NAME)$(DLSUFFIX)
80
81 # For each platform we support shared libraries on, set shlib to the
82 # name of the library (if default above is not right), set
83 # LINK.shared to the command to link the library,
84 # and adjust SHLIB_LINK if necessary.
85
86 # Try to keep the sections in some kind of order, folks...
87
88 override CFLAGS += $(CFLAGS_SL)
89
90 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
91
92 ifeq ($(PORTNAME), aix)
93   shlib                 = lib$(NAME)$(DLSUFFIX)
94 #   SHLIB_LINK          += -lc
95 endif
96
97 ifeq ($(PORTNAME), darwin)
98   ifneq ($(SO_MAJOR_VERSION), 0)
99     version_link        := -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
100   endif
101   ifeq ($(DLTYPE), library)
102     # linkable library
103     DLSUFFIX            := .dylib
104     LINK.shared         = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
105   else
106     # loadable module (default case)
107     DLSUFFIX            := .so
108     LINK.shared         = $(COMPILER) -bundle
109   endif
110   shlib                 = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
111   shlib_major           = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
112 endif
113
114 ifeq ($(PORTNAME), openbsd)
115   ifdef ELF_SYSTEM
116     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
117     SHLIB_LINK          += -lc
118   else
119     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
120   endif
121 endif
122
123 ifeq ($(PORTNAME), bsdi)
124   ifeq ($(DLSUFFIX), .so)
125     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
126     SHLIB_LINK          += -lc
127   endif
128   ifeq ($(DLSUFFIX), .o)
129     LINK.shared         = shlicc -O $(LDREL)
130   endif
131 endif
132
133 ifeq ($(PORTNAME), freebsd)
134   ifdef ELF_SYSTEM
135     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
136     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
137   else
138     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
139     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
140   endif
141 endif
142
143 ifeq ($(PORTNAME), netbsd)
144   ifdef ELF_SYSTEM
145     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
146   else
147     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
148   endif
149 endif
150
151 ifeq ($(PORTNAME), hpux)
152   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
153   ifeq ($(GCC), yes)
154     SHLIB_LINK          += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
155   endif
156   ifeq ($(with_gnu_ld), yes)
157     LINK.shared         = $(CC) $(LDFLAGS) -shared -Wl,-h -Wl,$(soname)
158   else
159     # can't use the CC-syntax rpath pattern here
160     rpath =
161     ifeq ($(enable_rpath), yes)
162       LINK.shared       = $(LD) +h $(soname) -b +b $(rpathdir)
163     else
164       LINK.shared       = $(LD) +h $(soname) -b
165     endif
166     # On HPUX platforms, gcc is usually configured to search for libraries
167     # in /usr/local/lib, but ld won't do so.  Add an explicit -L switch so
168     # ld can find the same libraries gcc does.  Make sure it goes after any
169     # -L switches provided explicitly.
170     ifeq ($(GCC), yes)
171       SHLIB_LINK += -L/usr/local/lib
172     endif
173   endif
174 endif
175
176 ifeq ($(PORTNAME), irix)
177   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
178   LINK.shared           = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
179 endif
180
181 ifeq ($(PORTNAME), linux)
182   LINK.shared           = $(COMPILER) -shared -Wl,-soname,$(soname)
183 endif
184
185 ifeq ($(PORTNAME), solaris)
186   ifeq ($(GCC), yes)
187     LINK.shared         = $(CC) -shared
188   else
189     LINK.shared         = $(CC) -G
190   endif
191   ifeq ($(with_gnu_ld), yes)
192     LINK.shared         += -Wl,-soname,$(soname)
193   else
194     LINK.shared         += -h $(soname)
195   endif
196 endif
197
198 ifeq ($(PORTNAME), sunos4)
199   LINK.shared           = $(LD) -assert pure-text -Bdynamic
200 endif
201  
202 ifeq ($(PORTNAME), osf)
203   LINK.shared           = $(LD) -shared -expect_unresolved '*'
204 endif
205
206 ifeq ($(PORTNAME), sco)
207   ifeq ($(GCC), yes)
208     LINK.shared         = $(CC) -shared
209   else
210     LINK.shared         = $(CC) -G
211     endif
212   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname)
213 endif
214
215 ifeq ($(PORTNAME), svr4)
216   LINK.shared           = $(LD) -G
217 endif
218
219 ifeq ($(PORTNAME), univel)
220   LINK.shared           = $(LD) -G -z text
221 endif
222
223 ifeq ($(PORTNAME), unixware)
224   ifeq ($(GCC), yes)
225     LINK.shared         = $(CC) -shared
226   else
227     LINK.shared         = $(CC) -G
228   endif
229   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname) 
230 endif
231
232 ifeq ($(PORTNAME), cygwin)
233   shlib                 = $(NAME)$(DLSUFFIX)
234   # needed for /contrib modules, not sure why
235   SHLIB_LINK            += $(LIBS)
236   haslibarule   = yes
237 endif
238
239 ifeq ($(PORTNAME), win32)
240   shlib                 = lib$(NAME)$(DLSUFFIX)
241   haslibarule   = yes
242 endif
243
244 ifeq ($(PORTNAME), beos)
245   shlib                 = lib$(NAME)$(DLSUFFIX)
246   LINK.shared           = $(LD) -nostart
247   SHLIB_LINK            += -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
248 endif
249
250 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
251 ifeq ($(enable_rpath), yes)
252 SHLIB_LINK += $(rpath)
253 endif
254
255 endif # enable_shared
256
257
258
259 ##
260 ## BUILD
261 ##
262
263 .PHONY: all-lib all-static-lib all-shared-lib
264
265 all-lib: all-static-lib all-shared-lib
266
267 all-static-lib: lib$(NAME).a
268
269 all-shared-lib: $(shlib)
270
271 ifndef LORDER
272 MK_NO_LORDER := true
273 endif
274
275 ifndef haslibarule
276 lib$(NAME).a: $(OBJS)
277 ifdef MK_NO_LORDER
278         $(LINK.static) $@ $^
279 else
280         $(LINK.static) $@ `$(LORDER) $^ | tsort`
281 endif
282         $(RANLIB) $@
283 endif #haslibarule
284
285 ifeq ($(enable_shared), yes)
286
287 ifneq ($(PORTNAME), win32)
288 ifneq ($(PORTNAME), cygwin)
289 ifneq ($(PORTNAME), beos)
290 ifneq ($(PORTNAME), aix)
291
292 # Normal case
293 $(shlib): $(OBJS)
294         $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
295 # If we're using major and minor versions, then make a symlink to major-version-only.
296 ifneq ($(shlib), $(shlib_major))
297         rm -f $(shlib_major)
298         $(LN_S) $(shlib) $(shlib_major)
299 endif
300 # Make sure we have a link to a name without any version numbers
301 ifneq ($(shlib), $(shlib_bare))
302         rm -f $(shlib_bare)
303         $(LN_S) $(shlib) $(shlib_bare)
304 endif
305
306 else # PORTNAME == aix
307
308 # AIX case
309 $(shlib): lib$(NAME).a
310         $(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
311         $(COMPILER) $(LDFLAGS_SL) -o $@ $< $(LDFLAGS) $(SHLIB_LINK) -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:lib$(NAME)$(EXPSUFF)
312         
313 endif # PORTNAME == aix
314
315 else # PORTNAME == beos
316
317 # BEOS case
318 $(shlib): $(OBJS)
319         ln -fs $(top_srcdir)/src/backend/postgres _APP_
320         $(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)
321
322 endif # PORTNAME == beos
323
324 else # PORTNAME == cygwin
325
326 # Cygwin case
327 $(shlib) lib$(NAME).a: $(OBJS)
328 ifndef DLL_DEFFILE
329         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
330         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
331         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
332 else
333         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
334         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
335 endif
336
337 endif # PORTNAME == cygwin
338
339 else # PORTNAME == win32
340
341 # win32 case
342 $(shlib) lib$(NAME).a: $(OBJS)
343 ifndef DLL_DEFFILE
344         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
345         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
346         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
347 else
348         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
349         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
350 endif
351
352 endif # PORTNAME == win32
353
354 endif # enable_shared
355
356
357 ##
358 ## INSTALL
359 ##
360
361 .PHONY: install-lib install-lib-static install-lib-shared
362 install-lib: install-lib-static install-lib-shared
363
364 install-lib-static: lib$(NAME).a
365         $(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
366 ifeq ($(PORTNAME), darwin)
367         cd $(DESTDIR)$(libdir) && \
368         ranlib lib$(NAME).a
369 endif
370
371 ifeq ($(enable_shared), yes)
372 install-lib-shared: $(shlib)
373         $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
374 ifneq ($(PORTNAME), cygwin)
375 ifneq ($(PORTNAME), win32)
376 ifneq ($(shlib), $(shlib_major))
377         cd $(DESTDIR)$(libdir) && \
378         rm -f $(shlib_major) && \
379         $(LN_S) $(shlib) $(shlib_major)
380 endif
381 ifneq ($(shlib), $(shlib_bare))
382         cd $(DESTDIR)$(libdir) && \
383         rm -f $(shlib_bare) && \
384         $(LN_S) $(shlib) $(shlib_bare)
385 endif
386 endif # not win32
387 endif # not cygwin
388 endif # enable_shared
389
390
391 ##
392 ## UNINSTALL
393 ##
394
395 .PHONY: uninstall-lib
396 uninstall-lib:
397         rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
398 ifeq ($(enable_shared), yes)
399         rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
400           $(DESTDIR)$(libdir)/$(shlib_major) \
401           $(DESTDIR)$(libdir)/$(shlib)
402 endif # enable_shared
403
404
405 ##
406 ## CLEAN
407 ##
408
409 .PHONY: clean-lib
410 clean-lib:
411         rm -f lib$(NAME).a
412 ifeq ($(enable_shared), yes)
413         rm -f $(shlib_bare) $(shlib_major) $(shlib)
414 ifdef EXPSUFF
415         rm -f lib$(NAME)$(EXPSUFF)
416 endif
417 endif
418 ifeq ($(PORTNAME), cygwin)
419         rm -f $(NAME).dll $(NAME).def
420 endif
421
422 ifeq ($(PORTNAME), win32)
423         rm -f $(NAME).dll $(NAME).def
424 endif