Build src/common files as a library with -fPIC.

Build a third version of libpgcommon.a, with -fPIC and -DFRONTEND,
as commit ea53100d5 did for src/port.  Use that in libpq to avoid
symlinking+rebuilding source files retail.

Also adjust ecpg to use the new src/port and src/common libraries.

Arrange to install these libraries, too, to simplify out-of-tree
builds of shared libraries that need any of these modules.

Discussion: https://postgr.es/m/13022.1538003440@sss.pgh.pa.us
Discussion: https://postgr.es/m/E1g5Y8r-0006vs-QA@gemulon.postgresql.org
This commit is contained in:
Tom Lane 2018-09-28 14:28:19 -04:00
parent f7ab802855
commit 7143b3e821
11 changed files with 68 additions and 111 deletions

View File

@ -534,21 +534,26 @@ libpq_srcdir = $(top_srcdir)/src/interfaces/libpq
libpq_builddir = $(top_builddir)/src/interfaces/libpq
endif
# This macro is for use by libraries linking to libpq. (Because libpgport
# isn't created with the same link flags as libpq, it can't be used.)
# How to link to libpq. (This macro may be used as-is by backend extensions.
# Client-side code should go through libpq_pgport or libpq_pgport_shlib,
# instead.)
libpq = -L$(libpq_builddir) -lpq
# This macro is for use by client executables (not libraries) that use libpq.
# libpq_pgport is for use by client executables (not libraries) that use libpq.
# We force clients to pull symbols from the non-shared libraries libpgport
# and libpgcommon rather than pulling some libpgport symbols from libpq just
# because libpq uses those functions too. This makes applications less
# dependent on changes in libpq's usage of pgport. To do this we link to
# dependent on changes in libpq's usage of pgport (on platforms where we
# don't have symbol export control for libpq). To do this we link to
# pgport before libpq. This does cause duplicate -lpgport's to appear
# on client link lines.
# on client link lines, since that also appears in $(LIBS).
# libpq_pgport_shlib is the same idea, but for use in client shared libraries.
ifdef PGXS
libpq_pgport = -L$(libdir) -lpgcommon -lpgport $(libpq)
libpq_pgport_shlib = -L$(libdir) -lpgcommon_shlib -lpgport_shlib $(libpq)
else
libpq_pgport = -L$(top_builddir)/src/common -lpgcommon -L$(top_builddir)/src/port -lpgport $(libpq)
libpq_pgport_shlib = -L$(top_builddir)/src/common -lpgcommon_shlib -L$(top_builddir)/src/port -lpgport_shlib $(libpq)
endif
# Cygwin seems to need ldap libraries to be mentioned here, too

View File

@ -3,17 +3,21 @@
# Makefile
# Makefile for src/common
#
# This makefile generates two outputs:
# These files are used by the Postgres backend, and also by frontend
# programs. These files provide common functionality that isn't directly
# concerned with portability and thus doesn't belong in src/port.
#
# This makefile generates three outputs:
#
# libpgcommon.a - contains object files with FRONTEND defined,
# for use by client applications
#
# libpgcommon_srv.a - contains object files without FRONTEND defined,
# for use only by the backend binaries
# libpgcommon_shlib.a - contains object files with FRONTEND defined,
# built suitably for use in shared libraries; for use
# by frontend libraries
#
# You can also symlink/copy individual source files from this directory,
# to compile with different options. (libpq does that, because it needs
# to use -fPIC on some platforms.)
# libpgcommon_srv.a - contains object files without FRONTEND defined,
# for use only by the backend
#
# IDENTIFICATION
# src/common/Makefile
@ -52,26 +56,48 @@ else
OBJS_COMMON += sha2.o
endif
# A few files are currently only built for frontend, not server
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o file_utils.o restricted_token.o
# foo.o, foo_shlib.o, and foo_srv.o are all built from foo.c
OBJS_SHLIB = $(OBJS_FRONTEND:%.o=%_shlib.o)
OBJS_SRV = $(OBJS_COMMON:%.o=%_srv.o)
all: libpgcommon.a libpgcommon_srv.a
all: libpgcommon.a libpgcommon_shlib.a libpgcommon_srv.a
# libpgcommon is needed by some contrib
install: all installdirs
$(INSTALL_STLIB) libpgcommon.a '$(DESTDIR)$(libdir)/libpgcommon.a'
$(INSTALL_STLIB) libpgcommon_shlib.a '$(DESTDIR)$(libdir)/libpgcommon_shlib.a'
installdirs:
$(MKDIR_P) '$(DESTDIR)$(libdir)'
uninstall:
rm -f '$(DESTDIR)$(libdir)/libpgcommon.a'
rm -f '$(DESTDIR)$(libdir)/libpgcommon_shlib.a'
libpgcommon.a: $(OBJS_FRONTEND)
rm -f $@
$(AR) $(AROPT) $@ $^
#
# Shared library versions of object files
#
libpgcommon_shlib.a: $(OBJS_SHLIB)
rm -f $@
$(AR) $(AROPT) $@ $^
# Because this uses its own compilation rule, it doesn't use the
# dependency tracking logic from Makefile.global. To make sure that
# dependency tracking works anyway for the *_shlib.o files, depend on
# their *.o siblings as well, which do have proper dependencies. It's
# a hack that might fail someday if there is a *_shlib.o without a
# corresponding *.o, but there seems little reason for that.
%_shlib.o: %.c %.o
$(CC) $(CFLAGS) $(CFLAGS_SL) $(CPPFLAGS) -c $< -o $@
#
# Server versions of object files
#
@ -87,16 +113,18 @@ libpgcommon_srv.a: $(OBJS_SRV)
# a hack that might fail someday if there is a *_srv.o without a
# corresponding *.o, but it works for now.
%_srv.o: %.c %.o
$(CC) $(CFLAGS) $(subst -DFRONTEND ,, $(CPPFLAGS)) -c $< -o $@
$(CC) $(CFLAGS) $(subst -DFRONTEND,, $(CPPFLAGS)) -c $< -o $@
# Dependencies of keywords.o need to be managed explicitly to make sure
# that you don't get broken parsing code, even in a non-enable-depend build.
# Note that gram.h isn't required for the frontend version of keywords.o.
# Note that gram.h isn't required for the frontend versions of keywords.o.
$(top_builddir)/src/include/parser/gram.h: $(top_srcdir)/src/backend/parser/gram.y
$(MAKE) -C $(top_builddir)/src/backend $(top_builddir)/src/include/parser/gram.h
keywords.o: $(top_srcdir)/src/include/parser/kwlist.h
keywords_shlib.o: $(top_srcdir)/src/include/parser/kwlist.h
keywords_srv.o: $(top_builddir)/src/include/parser/gram.h $(top_srcdir)/src/include/parser/kwlist.h
clean distclean maintainer-clean:
rm -f libpgcommon.a libpgcommon_srv.a $(OBJS_FRONTEND) $(OBJS_SRV)
rm -f libpgcommon.a libpgcommon_shlib.a libpgcommon_srv.a
rm -f $(OBJS_FRONTEND) $(OBJS_SHLIB) $(OBJS_SRV)

View File

@ -1,7 +1,3 @@
/compatlib.def
/blibecpg_compatdll.def
/exports.list
/snprintf.c
/strerror.c
/strlcpy.c
/strnlen.c

View File

@ -22,17 +22,13 @@ override CPPFLAGS := -I../include -I$(top_srcdir)/src/interfaces/ecpg/include \
-I$(libpq_srcdir) -DFRONTEND $(CPPFLAGS)
override CFLAGS += $(PTHREAD_CFLAGS)
SHLIB_LINK_INTERNAL = -L../ecpglib -lecpg -L../pgtypeslib -lpgtypes $(libpq)
SHLIB_LINK_INTERNAL = -L../ecpglib -lecpg -L../pgtypeslib -lpgtypes $(libpq_pgport_shlib)
SHLIB_LINK = $(filter -lintl -lm, $(LIBS)) $(PTHREAD_LIBS)
SHLIB_PREREQS = submake-ecpglib submake-pgtypeslib
SHLIB_EXPORTS = exports.txt
# Need to recompile any libpgport object files
LIBS := $(filter-out -lpgport, $(LIBS))
OBJS= informix.o snprintf.o strerror.o \
$(filter strlcpy.o strnlen.o, $(LIBOBJS)) $(WIN32RES)
OBJS= informix.o $(WIN32RES)
PKG_CONFIG_REQUIRES_PRIVATE = libecpg libpgtypes
@ -49,9 +45,6 @@ submake-pgtypeslib:
# Shared library stuff
include $(top_srcdir)/src/Makefile.shlib
snprintf.c strerror.c strlcpy.c strnlen.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
install: all installdirs install-lib
installdirs: installdirs-lib
@ -59,6 +52,6 @@ installdirs: installdirs-lib
uninstall: uninstall-lib
clean distclean: clean-lib
rm -f $(OBJS) snprintf.c strerror.c strlcpy.c strnlen.c
rm -f $(OBJS)
maintainer-clean: distclean maintainer-clean-lib

View File

@ -1,12 +1,3 @@
/ecpglib.def
/blibecpgdll.def
/exports.list
/path.c
/pgstrcasecmp.c
/snprintf.c
/strerror.c
/strlcpy.c
/strnlen.c
/thread.c
/win32setlocale.c
/isinf.c

View File

@ -22,20 +22,10 @@ override CPPFLAGS := -I../include -I$(top_srcdir)/src/interfaces/ecpg/include \
-I$(libpq_srcdir) -I$(top_builddir)/src/port -DFRONTEND $(CPPFLAGS)
override CFLAGS += $(PTHREAD_CFLAGS)
# Need to recompile any libpgport object files
LIBS := $(filter-out -lpgport, $(LIBS))
OBJS= execute.o typename.o descriptor.o sqlda.o data.o error.o prepare.o \
memory.o connect.o misc.o $(WIN32RES)
OBJS= execute.o typename.o descriptor.o sqlda.o data.o error.o prepare.o memory.o \
connect.o misc.o path.o pgstrcasecmp.o snprintf.o strerror.o \
$(filter strlcpy.o strnlen.o win32setlocale.o isinf.o, $(LIBOBJS)) \
$(WIN32RES)
# thread.c is needed only for non-WIN32 implementation of path.c
ifneq ($(PORTNAME), win32)
OBJS += thread.o
endif
SHLIB_LINK_INTERNAL = -L../pgtypeslib -lpgtypes $(libpq)
SHLIB_LINK_INTERNAL = -L../pgtypeslib -lpgtypes $(libpq_pgport_shlib)
SHLIB_LINK = $(filter -lintl -lm, $(LIBS)) $(PTHREAD_LIBS)
SHLIB_PREREQS = submake-libpq submake-pgtypeslib
@ -52,16 +42,8 @@ submake-pgtypeslib:
# Shared library stuff
include $(top_srcdir)/src/Makefile.shlib
# We use some port modules verbatim, but since we need to
# compile with appropriate options to build a shared lib, we can't
# necessarily use the same object files as the backend uses. Instead,
# symlink the source files in here and build our own object file.
path.c pgstrcasecmp.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32setlocale.c isinf.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
# Make dependency on pg_config_paths.h visible.
misc.o: misc.c $(top_builddir)/src/port/pg_config_paths.h
path.o: path.c $(top_builddir)/src/port/pg_config_paths.h
$(top_builddir)/src/port/pg_config_paths.h:
$(MAKE) -C $(top_builddir)/src/port pg_config_paths.h
@ -74,6 +56,5 @@ uninstall: uninstall-lib
clean distclean: clean-lib
rm -f $(OBJS)
rm -f path.c pgstrcasecmp.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32setlocale.c isinf.c
maintainer-clean: distclean maintainer-clean-lib

View File

@ -1,10 +1,3 @@
/pgtypeslib.def
/blibpgtypesdll.def
/exports.list
/pgstrcasecmp.c
/rint.c
/snprintf.c
/strerror.c
/strlcpy.c
/string.c
/strnlen.c

View File

@ -22,17 +22,12 @@ override CPPFLAGS := -I../include -I$(top_srcdir)/src/interfaces/ecpg/include \
-DFRONTEND $(CPPFLAGS)
override CFLAGS += $(PTHREAD_CFLAGS)
# Need to recompile any libpgport object files
LIBS := $(filter-out -lpgport, $(LIBS))
SHLIB_LINK_INTERNAL = -lpgcommon_shlib -lpgport_shlib
SHLIB_LINK += $(filter -lintl -lm, $(LIBS))
SHLIB_EXPORTS = exports.txt
OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \
pgstrcasecmp.o snprintf.o strerror.o \
$(filter rint.o strlcpy.o strnlen.o, $(LIBOBJS)) \
string.o \
$(WIN32RES)
all: all-lib
@ -40,17 +35,6 @@ all: all-lib
# Shared library stuff
include $(top_srcdir)/src/Makefile.shlib
# We use some port modules verbatim, but since we need to
# compile with appropriate options to build a shared lib, we can't
# necessarily use the same object files as the backend uses. Instead,
# symlink the source files in here and build our own object file.
pgstrcasecmp.c rint.c snprintf.c strerror.c strlcpy.c strnlen.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
string.c: % : $(top_srcdir)/src/common/%
rm -f $@ && $(LN_S) $< .
install: all installdirs install-lib
installdirs: installdirs-lib
@ -58,6 +42,6 @@ installdirs: installdirs-lib
uninstall: uninstall-lib
clean distclean: clean-lib
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strerror.c strlcpy.c strnlen.c string.c
rm -f $(OBJS)
maintainer-clean: distclean maintainer-clean-lib

View File

@ -1,14 +1,5 @@
/exports.list
/libpq.rc
# .c files that are symlinked in from elsewhere
/ip.c
/md5.c
/base64.c
/link-canary.c
/scram-common.c
/sha2.c
/sha2_openssl.c
/saslprep.c
/unicode_norm.c
/encnames.c
/wchar.c

View File

@ -32,13 +32,9 @@ OBJS= fe-auth.o fe-auth-scram.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-l
# src/backend/utils/mb
OBJS += encnames.o wchar.o
# src/common
OBJS += base64.o ip.o link-canary.o md5.o scram-common.o saslprep.o unicode_norm.o
ifeq ($(with_openssl),yes)
OBJS += fe-secure-openssl.o fe-secure-common.o sha2_openssl.o
else
OBJS += sha2.o
OBJS += fe-secure-openssl.o fe-secure-common.o
endif
ifeq ($(PORTNAME), cygwin)
@ -59,12 +55,14 @@ endif
# Add libraries that libpq depends (or might depend) on into the
# shared library link. (The order in which you list them here doesn't
# matter.) Note that we filter out -lpgport from LIBS and instead
# insert -lpgport_shlib, to get port files that are built correctly.
# matter.) Note that we filter out -lpgcommon and -lpgport from LIBS and
# instead link with -lpgcommon_shlib and -lpgport_shlib, to get object files
# that are built correctly for use in a shlib.
SHLIB_LINK_INTERNAL = -lpgcommon_shlib -lpgport_shlib
ifneq ($(PORTNAME), win32)
SHLIB_LINK += -lpgport_shlib $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi_krb5 -lgss -lgssapi -lssl -lsocket -lnsl -lresolv -lintl -lm, $(LIBS)) $(LDAP_LIBS_FE) $(PTHREAD_LIBS)
SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi_krb5 -lgss -lgssapi -lssl -lsocket -lnsl -lresolv -lintl -lm, $(LIBS)) $(LDAP_LIBS_FE) $(PTHREAD_LIBS)
else
SHLIB_LINK += -lpgport_shlib $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi32 -lssl -lsocket -lnsl -lresolv -lintl -lm $(PTHREAD_LIBS), $(LIBS)) $(LDAP_LIBS_FE)
SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi32 -lssl -lsocket -lnsl -lresolv -lintl -lm $(PTHREAD_LIBS), $(LIBS)) $(LDAP_LIBS_FE)
endif
ifeq ($(PORTNAME), win32)
SHLIB_LINK += -lshell32 -lws2_32 -lsecur32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32, $(LIBS))
@ -87,9 +85,6 @@ backend_src = $(top_srcdir)/src/backend
# Instead, symlink the source files in here and build our own object files.
# When you add a file here, remember to add it in the "clean" target below.
ip.c md5.c base64.c link-canary.c scram-common.c sha2.c sha2_openssl.c saslprep.c unicode_norm.c: % : $(top_srcdir)/src/common/%
rm -f $@ && $(LN_S) $< .
encnames.c wchar.c: % : $(backend_src)/utils/mb/%
rm -f $@ && $(LN_S) $< .
@ -136,8 +131,7 @@ clean distclean: clean-lib
rm -f $(OBJS) pthread.h libpq.rc
# Might be left over from a Win32 client-only build
rm -f pg_config_paths.h
# Remove files we (may have) symlinked in from src/common and other places
rm -f ip.c md5.c base64.c link-canary.c scram-common.c sha2.c sha2_openssl.c saslprep.c unicode_norm.c
# Remove files we (may have) symlinked in from other places
rm -f encnames.c wchar.c
maintainer-clean: distclean maintainer-clean-lib

View File

@ -14,7 +14,7 @@
#
# libpgport_shlib.a - contains object files with FRONTEND defined,
# built suitably for use in shared libraries; for use
# by libpq and other frontend libraries
# by frontend libraries
#
# libpgport_srv.a - contains object files without FRONTEND defined,
# for use only by the backend
@ -53,15 +53,16 @@ OBJS_SRV = $(OBJS:%.o=%_srv.o)
all: libpgport.a libpgport_shlib.a libpgport_srv.a
# libpgport is needed by some contrib
# currently we don't install libpgport_shlib.a, maybe we should?
install: all installdirs
$(INSTALL_STLIB) libpgport.a '$(DESTDIR)$(libdir)/libpgport.a'
$(INSTALL_STLIB) libpgport_shlib.a '$(DESTDIR)$(libdir)/libpgport_shlib.a'
installdirs:
$(MKDIR_P) '$(DESTDIR)$(libdir)'
uninstall:
rm -f '$(DESTDIR)$(libdir)/libpgport.a'
rm -f '$(DESTDIR)$(libdir)/libpgport_shlib.a'
libpgport.a: $(OBJS)
rm -f $@