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.

491 lines
15KB

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