summaryrefslogtreecommitdiff
path: root/src/Makefile.shlib
blob: 0d7283ce3eabca2b8846bd31e640a0e85f4deb3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#-------------------------------------------------------------------------
#
# Makefile.shlib
#    Common rules for building shared libraries
#
# Copyright (c) 1998, Regents of the University of California
#
# IDENTIFICATION
#    $PostgreSQL: pgsql/src/Makefile.shlib,v 1.77 2004/07/13 00:06:46 tgl Exp $
#
#-------------------------------------------------------------------------

# This file should be included by any Postgres module Makefile that
# wants to build a shared library (if possible for the current
# platform). A static library is also built from the same object
# files. Only one library can be built per makefile.
#
# Before including this file, the module Makefile must define these
# variables:
#
# NAME                  Name of library to build (no suffix nor "lib" prefix)
# SO_MAJOR_VERSION      Major version number to use for shared library
# SO_MINOR_VERSION      Minor version number to use for shared library
# OBJS                  List of object files to include in library
# SHLIB_LINK            If shared library relies on other libraries,
#                       additional stuff to put in its link command
# (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
#
# Optional flags when building DLL's (only applicable to win32 and cygwin
# platforms).
# DLLTOOL_DEFFLAGS      Additional flags when creating the dll .def file
# DLLTOOL_LIBFLAGS      Additional flags when creating the lib<module>.a file
# DLLWRAP_FLAGS         Additional flags to dllwrap
#
# The module Makefile must also include
# $(top_builddir)/src/Makefile.global before including this file.
# (Makefile.global sets PORTNAME and other needed symbols.)
#
# This makefile provides the following (phony) targets:
#
# all-lib               build the static and shared (if applicable) libraries
# install-lib           install the libraries into $(libdir)
# uninstall-lib         remove the libraries from $(libdir)
# clean-lib             delete the static and shared libraries from the build dir
#
# Since `all-lib' is the first rule in this file you probably want to
# have the `all' target before including this file. In the most simple
# case it would look like this:
#
#     all: all-lib
#
# Similarly, the install rule might look like
#
#     install: install-lib
#
# plus any additional things you want to install. Et cetera.
#
# Got that?  Look at src/interfaces/libpq/Makefile for an example.
#
# While the linker allows creation of most shared libraries,
# -Bsymbolic requires resolution of all symbols, making the
# compiler a better choice for shared library creation on ELF platforms.
# With the linker, -Bsymbolic requires the crt1.o startup object file.
# bjm 2001-02-10


COMPILER = $(CC) $(CFLAGS)
LINK.static = $(AR) $(AROPT)



ifeq ($(enable_shared), yes)

# Default shlib naming convention used by the majority of platforms
shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
shlib_major	= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
shlib_bare	= lib$(NAME)$(DLSUFFIX)

# For each platform we support shared libraries on, set shlib to the
# name of the library (if default above is not right), set
# LINK.shared to the command to link the library,
# and adjust SHLIB_LINK if necessary.

# Try to keep the sections in some kind of order, folks...

override CFLAGS += $(CFLAGS_SL)

soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)

ifeq ($(PORTNAME), aix)
  shlib			= lib$(NAME)$(DLSUFFIX)
#   SHLIB_LINK		+= -lc
endif

ifeq ($(PORTNAME), darwin)
  ifneq ($(SO_MAJOR_VERSION), 0)
    version_link	:= -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  endif
  ifeq ($(DLTYPE), library)
    # linkable library
    DLSUFFIX		:= .dylib
    LINK.shared		= $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
  else
    # loadable module (default case)
    DLSUFFIX		:= .so
    LINK.shared		= $(COMPILER) -bundle
  endif
  shlib			= lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
  shlib_major		= lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
endif

ifeq ($(PORTNAME), openbsd)
  ifdef ELF_SYSTEM
    LINK.shared		= $(COMPILER) -shared -Wl,-x,-soname,$(soname)
    SHLIB_LINK		+= -lc
  else
    LINK.shared		= $(LD) -x -Bshareable -Bforcearchive
  endif
endif

ifeq ($(PORTNAME), bsdi)
  ifeq ($(DLSUFFIX), .so)
    LINK.shared		= $(COMPILER) -shared -Wl,-x,-soname,$(soname)
    SHLIB_LINK		+= -lc
  endif
  ifeq ($(DLSUFFIX), .o)
    LINK.shared		= shlicc -O $(LDREL)
  endif
endif

ifeq ($(PORTNAME), freebsd)
  ifdef ELF_SYSTEM
    shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
    LINK.shared		= $(COMPILER) -shared -Wl,-x,-soname,$(soname)
  else
    shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
    LINK.shared		= $(LD) -x -Bshareable -Bforcearchive
  endif
endif

ifeq ($(PORTNAME), netbsd)
  ifdef ELF_SYSTEM
    LINK.shared		= $(COMPILER) -shared -Wl,-x,-soname,$(soname)
  else
    LINK.shared		= $(LD) -x -Bshareable -Bforcearchive
  endif
endif

ifeq ($(PORTNAME), hpux)
  shlib			= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
  LINK.shared		= $(LD) +h $(soname) -b +b $(libdir)
  ifeq ($(GCC), yes)
    SHLIB_LINK		+= `$(CC) -print-libgcc-file-name`
  endif
endif

ifeq ($(PORTNAME), irix)
  shlib			= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
  LINK.shared		= $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
endif

ifeq ($(PORTNAME), linux)
  LINK.shared		= $(COMPILER) -shared -Wl,-soname,$(soname)
endif

ifeq ($(PORTNAME), solaris)
  ifeq ($(GCC), yes)
    LINK.shared		= $(CC) -shared
  else
    LINK.shared		= $(CC) -G
  endif
  ifeq ($(with_gnu_ld), yes)
    LINK.shared		+= -Wl,-soname,$(soname)
  else
    LINK.shared		+= -h $(soname)
  endif
endif

ifeq ($(PORTNAME), sunos4)
  LINK.shared		= $(LD) -assert pure-text -Bdynamic
endif
 
ifeq ($(PORTNAME), osf)
  LINK.shared		= $(LD) -shared -expect_unresolved '*'
endif

ifeq ($(PORTNAME), sco)
  ifeq ($(GCC), yes)
    LINK.shared		= $(CC) -shared
  else
    LINK.shared		= $(CC) -G
    endif
  LINK.shared		+= -Wl,-z,text -Wl,-h,$(soname)
endif

ifeq ($(PORTNAME), svr4)
  LINK.shared		= $(LD) -G
endif

ifeq ($(PORTNAME), univel)
  LINK.shared		= $(LD) -G -z text
endif

ifeq ($(PORTNAME), unixware)
  ifeq ($(GCC), yes)
    LINK.shared		= $(CC) -shared
  else
    LINK.shared		= $(CC) -G
  endif
  LINK.shared		+= -Wl,-z,text -Wl,-h,$(soname) 
endif

ifeq ($(PORTNAME), cygwin)
  shlib			= $(NAME)$(DLSUFFIX)
endif

ifeq ($(PORTNAME), win32)
  shlib			= lib$(NAME)$(DLSUFFIX)
endif

ifeq ($(PORTNAME), beos)
  shlib			= lib$(NAME)$(DLSUFFIX)
  LINK.shared		= $(LD) -nostart
  SHLIB_LINK		+= -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
endif

SHLIB_LINK := $(filter -L%, $(LDFLAGS)) $(SHLIB_LINK)
ifeq ($(enable_rpath), yes)
SHLIB_LINK += $(rpath)
endif

endif # enable_shared



##
## BUILD
##

.PHONY: all-lib all-static-lib all-shared-lib

all-lib: all-static-lib all-shared-lib

all-static-lib: lib$(NAME).a

all-shared-lib: $(shlib)

ifneq ($(PORTNAME), cygwin)
ifneq ($(PORTNAME), win32)

ifndef LORDER
MK_NO_LORDER := true
endif

lib$(NAME).a: $(OBJS)
ifdef MK_NO_LORDER
	$(LINK.static) $@ $^
else
	$(LINK.static) $@ `$(LORDER) $^ | tsort`
endif
	$(RANLIB) $@

endif # not win32
endif # not cygwin

ifeq ($(enable_shared), yes)

ifneq ($(PORTNAME), win32)
ifneq ($(PORTNAME), cygwin)
ifneq ($(PORTNAME), beos)
ifneq ($(PORTNAME), aix)

# Normal case
$(shlib): $(OBJS)
	$(LINK.shared) $(OBJS) $(SHLIB_LINK) -o $@
# If we're using major and minor versions, then make a symlink to major-version-only.
ifneq ($(shlib), $(shlib_major))
	rm -f $(shlib_major)
	$(LN_S) $(shlib) $(shlib_major)
endif
# Make sure we have a link to a name without any version numbers
ifneq ($(shlib), $(shlib_bare))
	rm -f $(shlib_bare)
	$(LN_S) $(shlib) $(shlib_bare)
endif

else # PORTNAME == aix

# AIX case
$(shlib): lib$(NAME).a
	$(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
	$(COMPILER) $(LDFLAGS_SL) -o $@ $< $(LDFLAGS) $(SHLIB_LINK) -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:lib$(NAME)$(EXPSUFF)
	
endif # PORTNAME == aix

else # PORTNAME == beos

# BEOS case
$(shlib): $(OBJS)
	ln -fs $(top_srcdir)/src/backend/postgres _APP_
	$(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)

endif # PORTNAME == beos

else # PORTNAME == cygwin

# Cygwin case
$(shlib) lib$(NAME).a: $(OBJS) $(DLLINIT)
	$(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
	$(DLLWRAP) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(DLLINIT) $(SHLIB_LINK)
	$(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a

$(DLLINIT): $(DLLINIT:%.o=%.c)
	$(MAKE) -C $(@D) $(@F)

endif # PORTNAME == cygwin

else # PORTNAME == win32

# win32 case
$(shlib) lib$(NAME).a: $(OBJS)
	$(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
	$(DLLWRAP) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
	$(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a

endif # PORTNAME == win32

endif # enable_shared


##
## INSTALL
##

.PHONY: install-lib install-lib-static install-lib-shared
install-lib: install-lib-static install-lib-shared

install-lib-static: lib$(NAME).a
	$(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
ifeq ($(PORTNAME), darwin)
	cd $(DESTDIR)$(libdir) && \
	ranlib lib$(NAME).a
endif

ifeq ($(enable_shared), yes)
install-lib-shared: $(shlib)
	$(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
ifneq ($(PORTNAME), cygwin)
ifneq ($(PORTNAME), win32)
ifneq ($(shlib), $(shlib_major))
	cd $(DESTDIR)$(libdir) && \
	rm -f $(shlib_major) && \
	$(LN_S) $(shlib) $(shlib_major)
endif
ifneq ($(shlib), $(shlib_bare))
	cd $(DESTDIR)$(libdir) && \
	rm -f $(shlib_bare) && \
	$(LN_S) $(shlib) $(shlib_bare)
endif
endif # not win32
endif # not cygwin
endif # enable_shared


##
## UNINSTALL
##

.PHONY: uninstall-lib
uninstall-lib:
	rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
ifeq ($(enable_shared), yes)
	rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
	  $(DESTDIR)$(libdir)/$(shlib_major) \
	  $(DESTDIR)$(libdir)/$(shlib)
endif # enable_shared


##
## CLEAN
##

.PHONY: clean-lib
clean-lib:
	rm -f lib$(NAME).a
ifeq ($(enable_shared), yes)
	rm -f $(shlib_bare) $(shlib_major) $(shlib)
ifdef EXPSUFF
	rm -f lib$(NAME)$(EXPSUFF)
endif
endif
ifeq ($(PORTNAME), cygwin)
	rm -f $(NAME).dll $(NAME).def
endif

ifeq ($(PORTNAME), win32)
	rm -f $(NAME).dll $(NAME).def
endif