You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

513 lines
16KB

  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. # This file should be included by any Postgres module Makefile that
  13. # wants to build a shared library (if possible for the current
  14. # platform). A static library is also built from the same object
  15. # files. Only one library can be built per makefile.
  16. #
  17. # Before including this file, the module Makefile must define these
  18. # variables:
  19. #
  20. # NAME Name of library to build (no suffix nor "lib" prefix)
  21. # OBJS List of object files to include in library
  22. # SHLIB_LINK Stuff to append to library's link command
  23. # (typically, -L and -l switches for external libraries)
  24. # SHLIB_LINK_INTERNAL -L and -l switches for Postgres-supplied libraries
  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. # Don't use SHLIB_LINK for references to files in the build tree, or the
  30. # wrong things will happen --- use SHLIB_LINK_INTERNAL for those!
  31. #
  32. # When building a shared library, the following version information
  33. # must also be set. It should be omitted when building a dynamically
  34. # loadable module.
  35. #
  36. # SO_MAJOR_VERSION Major version number to use for shared library
  37. # SO_MINOR_VERSION Minor version number to use for shared library
  38. # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
  39. #
  40. # The module Makefile must also include
  41. # $(top_builddir)/src/Makefile.global before including this file.
  42. # (Makefile.global sets PORTNAME and other needed symbols.)
  43. #
  44. # This makefile provides the following (phony) targets:
  45. #
  46. # all-lib build the static and shared (if applicable) libraries
  47. # install-lib install the libraries into $(libdir)
  48. # installdirs-lib create installation directory $(libdir)
  49. # uninstall-lib remove the libraries from $(libdir)
  50. # clean-lib delete the static and shared libraries from the build dir
  51. # maintainer-clean-lib delete .def files built for win32
  52. #
  53. # Typically you would add `all-lib' to the `all' target so that `make all'
  54. # builds the libraries. In the most simple case it would look like this:
  55. #
  56. # all: all-lib
  57. #
  58. # Similarly, the install rule might look like
  59. #
  60. # install: install-lib
  61. #
  62. # plus any additional things you want to install. Et cetera.
  63. #
  64. # Got that? Look at src/interfaces/libpq/Makefile for an example.
  65. #
  66. COMPILER = $(CC) $(CFLAGS)
  67. LINK.static = $(AR) $(AROPT)
  68. LDFLAGS_INTERNAL += $(SHLIB_LINK_INTERNAL)
  69. ifdef SO_MAJOR_VERSION
  70. # Default library naming convention used by the majority of platforms
  71. shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  72. shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
  73. shlib_bare = lib$(NAME)$(DLSUFFIX)
  74. # Testing the soname variable is a reliable way to determine whether a
  75. # linkable library is being built.
  76. soname = $(shlib_major)
  77. pkgconfigdir = $(libdir)/pkgconfig
  78. else
  79. # Naming convention for dynamically loadable modules
  80. shlib = $(NAME)$(DLSUFFIX)
  81. endif
  82. stlib = lib$(NAME).a
  83. ifndef soname
  84. # additional flags for backend modules
  85. SHLIB_LINK += $(BE_DLLLIBS)
  86. endif
  87. # For each platform we support shared libraries on, set shlib to the
  88. # name of the library (if default above is not right), set
  89. # LINK.shared to the command to link the library,
  90. # and adjust SHLIB_LINK if necessary.
  91. # Try to keep the sections in some kind of order, folks...
  92. override CFLAGS += $(CFLAGS_SL)
  93. override CXXFLAGS += $(CFLAGS_SL)
  94. ifdef SO_MAJOR_VERSION
  95. # libraries ought to use this to refer to versioned gettext domain names
  96. override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
  97. endif
  98. ifeq ($(PORTNAME), aix)
  99. ifdef SO_MAJOR_VERSION
  100. shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
  101. endif
  102. haslibarule = yes
  103. # $(exports_file) is also usable as an import file
  104. exports_file = lib$(NAME).exp
  105. endif
  106. ifeq ($(PORTNAME), darwin)
  107. ifdef soname
  108. # linkable library
  109. DLSUFFIX = .dylib
  110. ifneq ($(SO_MAJOR_VERSION), 0)
  111. version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  112. endif
  113. LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
  114. shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
  115. shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
  116. else
  117. # loadable module
  118. DLSUFFIX = .so
  119. LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
  120. endif
  121. BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
  122. exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
  123. ifneq (,$(exports_file))
  124. exported_symbols_list = -exported_symbols_list $(exports_file)
  125. endif
  126. endif
  127. ifeq ($(PORTNAME), openbsd)
  128. LINK.shared = $(COMPILER) -shared
  129. ifdef soname
  130. LINK.shared += -Wl,-x,-soname,$(soname)
  131. endif
  132. BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
  133. exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
  134. ifneq (,$(exports_file))
  135. LINK.shared += -Wl,--version-script=$(exports_file)
  136. endif
  137. SHLIB_LINK += -lc
  138. endif
  139. ifeq ($(PORTNAME), freebsd)
  140. ifdef SO_MAJOR_VERSION
  141. shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
  142. endif
  143. LINK.shared = $(COMPILER) -shared
  144. ifdef soname
  145. LINK.shared += -Wl,-x,-soname,$(soname)
  146. endif
  147. BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
  148. exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
  149. ifneq (,$(exports_file))
  150. LINK.shared += -Wl,--version-script=$(exports_file)
  151. endif
  152. endif
  153. ifeq ($(PORTNAME), netbsd)
  154. LINK.shared = $(COMPILER) -shared
  155. ifdef soname
  156. LINK.shared += -Wl,-x,-soname,$(soname)
  157. endif
  158. BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
  159. exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
  160. ifneq (,$(exports_file))
  161. LINK.shared += -Wl,--version-script=$(exports_file)
  162. endif
  163. endif
  164. ifeq ($(PORTNAME), hpux)
  165. ifdef SO_MAJOR_VERSION
  166. shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
  167. endif
  168. ifeq ($(with_gnu_ld), yes)
  169. LINK.shared = $(CC) -shared -Wl,-Bsymbolic
  170. ifdef soname
  171. LINK.shared += -Wl,-h -Wl,$(soname)
  172. endif
  173. else
  174. LINK.shared = $(LD) -b -Bsymbolic
  175. ifdef soname
  176. LINK.shared += +h $(soname)
  177. endif
  178. # can't use the CC-syntax rpath pattern here, so instead:
  179. rpath =
  180. ifeq ($(enable_rpath), yes)
  181. LINK.shared += +s +b '$(rpathdir)'
  182. endif
  183. # On HPUX platforms, gcc is usually configured to search for libraries
  184. # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
  185. # ld can find the same libraries gcc does. Make sure it goes after any
  186. # -L switches provided explicitly.
  187. ifeq ($(GCC), yes)
  188. SHLIB_LINK += -L/usr/local/lib
  189. endif
  190. endif
  191. # And we need to link with libgcc, too
  192. ifeq ($(GCC), yes)
  193. SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
  194. endif
  195. endif
  196. ifeq ($(PORTNAME), linux)
  197. LINK.shared = $(COMPILER) -shared
  198. ifdef soname
  199. LINK.shared += -Wl,-soname,$(soname)
  200. endif
  201. BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
  202. exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
  203. ifneq (,$(exports_file))
  204. LINK.shared += -Wl,--version-script=$(exports_file)
  205. endif
  206. endif
  207. ifeq ($(PORTNAME), solaris)
  208. ifeq ($(GCC), yes)
  209. LINK.shared = $(COMPILER) -shared -Wl,-Bsymbolic
  210. else
  211. LINK.shared = $(COMPILER) -G -Bsymbolic
  212. endif
  213. ifdef soname
  214. ifeq ($(with_gnu_ld), yes)
  215. LINK.shared += -Wl,-soname,$(soname)
  216. else
  217. LINK.shared += -h $(soname)
  218. endif
  219. endif
  220. endif
  221. ifeq ($(PORTNAME), cygwin)
  222. LINK.shared = $(CC) -shared
  223. ifdef SO_MAJOR_VERSION
  224. shlib = cyg$(NAME)$(DLSUFFIX)
  225. endif
  226. haslibarule = yes
  227. endif
  228. ifeq ($(PORTNAME), win32)
  229. ifdef SO_MAJOR_VERSION
  230. shlib = lib$(NAME)$(DLSUFFIX)
  231. endif
  232. haslibarule = yes
  233. endif
  234. ##
  235. ## BUILD
  236. ##
  237. .PHONY: all-lib all-static-lib all-shared-lib
  238. all-lib: all-shared-lib
  239. ifdef soname
  240. # no static library when building a dynamically loadable module
  241. all-lib: all-static-lib
  242. all-lib: lib$(NAME).pc
  243. endif
  244. all-static-lib: $(stlib)
  245. all-shared-lib: $(shlib)
  246. # In this rule, "touch $@" works around a problem on some platforms wherein
  247. # ranlib updates the library file's mod time with a value calculated to
  248. # seconds precision. If the filesystem has sub-second timestamps, this can
  249. # cause the library file to appear older than its input files, triggering
  250. # parallel-make problems.
  251. ifndef haslibarule
  252. $(stlib): $(OBJS) | $(SHLIB_PREREQS)
  253. rm -f $@
  254. $(LINK.static) $@ $^
  255. $(RANLIB) $@
  256. touch $@
  257. endif #haslibarule
  258. ifeq (,$(filter cygwin win32,$(PORTNAME)))
  259. ifneq ($(PORTNAME), aix)
  260. # Normal case
  261. $(shlib): $(OBJS) | $(SHLIB_PREREQS)
  262. $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
  263. ifdef shlib_major
  264. # If we're using major and minor versions, then make a symlink to major-version-only.
  265. ifneq ($(shlib), $(shlib_major))
  266. rm -f $(shlib_major)
  267. $(LN_S) $(shlib) $(shlib_major)
  268. endif
  269. # Make sure we have a link to a name without any version numbers
  270. ifneq ($(shlib), $(shlib_bare))
  271. rm -f $(shlib_bare)
  272. $(LN_S) $(shlib) $(shlib_bare)
  273. endif
  274. endif # shlib_major
  275. # Where possible, restrict the symbols exported by the library to just the
  276. # official list, so as to avoid unintentional ABI changes. On recent macOS
  277. # this also quiets multiply-defined-symbol warnings in programs that use
  278. # libpgport along with libpq.
  279. ifneq (,$(SHLIB_EXPORTS))
  280. ifdef BUILD.exports
  281. $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
  282. $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
  283. $(BUILD.exports)
  284. endif
  285. endif
  286. else # PORTNAME == aix
  287. # AIX case
  288. # See notes in src/backend/parser/Makefile about the following two rules
  289. $(stlib): $(shlib)
  290. touch $@
  291. $(shlib): $(OBJS) | $(SHLIB_PREREQS)
  292. rm -f $(stlib)
  293. $(LINK.static) $(stlib) $^
  294. $(RANLIB) $(stlib)
  295. $(MKLDEXPORT) $(stlib) $(shlib) >$(exports_file)
  296. $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
  297. rm -f $(stlib)
  298. $(AR) $(AROPT) $(stlib) $(shlib)
  299. endif # PORTNAME == aix
  300. else # PORTNAME == cygwin || PORTNAME == win32
  301. ifeq ($(PORTNAME), cygwin)
  302. # Cygwin case
  303. $(shlib): $(OBJS) | $(SHLIB_PREREQS)
  304. $(CC) $(CFLAGS) -shared -o $@ -Wl,--out-implib=$(stlib) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
  305. # see notes in src/backend/parser/Makefile about use of this type of rule
  306. $(stlib): $(shlib)
  307. touch $@
  308. else
  309. # Win32 case
  310. # See notes in src/backend/parser/Makefile about the following two rules
  311. $(stlib): $(shlib)
  312. touch $@
  313. # XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
  314. # uncleanly, hence -static-libgcc. (Last verified with MinGW-w64 compilers
  315. # from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.) Shared libgcc has better
  316. # support for C++/Java exceptions; while core PostgreSQL does not use them, it
  317. # would be nice to support shared libgcc for the benefit of extensions.
  318. #
  319. # If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
  320. # Else we just use --export-all-symbols.
  321. ifeq (,$(SHLIB_EXPORTS))
  322. $(shlib): $(OBJS) | $(SHLIB_PREREQS)
  323. $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
  324. else
  325. DLL_DEFFILE = lib$(NAME)dll.def
  326. $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
  327. $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
  328. endif
  329. endif # PORTNAME == cygwin
  330. endif # PORTNAME == cygwin || PORTNAME == win32
  331. %.pc: $(MAKEFILE_LIST)
  332. echo 'Name: lib$(NAME)' >$@
  333. echo 'Description: PostgreSQL lib$(NAME) library' >>$@
  334. echo 'Url: http://www.postgresql.org/' >>$@
  335. echo 'Version: $(VERSION)' >>$@
  336. echo 'Requires: ' >>$@
  337. echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
  338. echo 'Cflags: -I$(includedir)' >>$@
  339. echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
  340. # Record -L flags that the user might have passed in to the PostgreSQL
  341. # build to locate third-party libraries (e.g., ldap, ssl). Filter out
  342. # those that point inside the build or source tree. Use sort to
  343. # remove duplicates. Also record the -l flags necessary for static
  344. # linking, but not those already covered by Requires.private.
  345. 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)))' >>$@
  346. # We need several not-quite-identical variants of .DEF files to build
  347. # DLLs for Windows. These are made from the single source file
  348. # exports.txt. Since we can't assume that Windows boxes will have
  349. # sed, the .DEF files are always built and included in distribution
  350. # tarballs.
  351. ifneq (,$(SHLIB_EXPORTS))
  352. distprep: lib$(NAME)dll.def lib$(NAME)ddll.def
  353. UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  354. lib$(NAME)dll.def: $(SHLIB_EXPORTS)
  355. echo '; DEF file for Makefile.shlib (MinGW)' >$@
  356. echo 'LIBRARY LIB$(UC_NAME).dll' >>$@
  357. echo 'EXPORTS' >>$@
  358. sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
  359. lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
  360. echo '; DEF file for Makefile.shlib (MinGW)' >$@
  361. echo 'LIBRARY LIB$(UC_NAME)D.dll' >>$@
  362. echo 'EXPORTS' >>$@
  363. sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
  364. endif # SHLIB_EXPORTS
  365. ##
  366. ## INSTALL
  367. ##
  368. .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
  369. install-lib: install-lib-shared
  370. ifdef soname
  371. install-lib: install-lib-static
  372. install-lib: install-lib-pc
  373. endif
  374. install-lib-pc: lib$(NAME).pc installdirs-lib
  375. $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
  376. install-lib-static: $(stlib) installdirs-lib
  377. $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
  378. ifeq ($(PORTNAME), darwin)
  379. cd '$(DESTDIR)$(libdir)' && \
  380. $(RANLIB) $(stlib)
  381. endif
  382. install-lib-shared: $(shlib) installdirs-lib
  383. ifdef soname
  384. # we don't install $(shlib) on AIX
  385. # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
  386. ifneq ($(PORTNAME), aix)
  387. $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
  388. ifneq ($(PORTNAME), cygwin)
  389. ifneq ($(PORTNAME), win32)
  390. ifneq ($(shlib), $(shlib_major))
  391. cd '$(DESTDIR)$(libdir)' && \
  392. rm -f $(shlib_major) && \
  393. $(LN_S) $(shlib) $(shlib_major)
  394. endif
  395. ifneq ($(shlib), $(shlib_bare))
  396. cd '$(DESTDIR)$(libdir)' && \
  397. rm -f $(shlib_bare) && \
  398. $(LN_S) $(shlib) $(shlib_bare)
  399. endif
  400. endif # not win32
  401. endif # not cygwin
  402. endif # not aix
  403. ifneq (,$(findstring $(PORTNAME),win32 cygwin))
  404. $(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
  405. endif
  406. else # no soname
  407. $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
  408. endif
  409. installdirs-lib:
  410. ifdef soname
  411. $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
  412. else
  413. $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
  414. endif
  415. ##
  416. ## UNINSTALL
  417. ##
  418. .PHONY: uninstall-lib
  419. uninstall-lib:
  420. ifdef soname
  421. rm -f '$(DESTDIR)$(libdir)/$(stlib)'
  422. rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
  423. '$(DESTDIR)$(libdir)/$(shlib_major)' \
  424. '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
  425. '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
  426. else # no soname
  427. rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
  428. endif # no soname
  429. ##
  430. ## CLEAN
  431. ##
  432. .PHONY: clean-lib
  433. clean-lib:
  434. rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
  435. ifneq (,$(SHLIB_EXPORTS))
  436. maintainer-clean-lib:
  437. rm -f lib$(NAME)dll.def lib$(NAME)ddll.def
  438. endif