Skip to content

Commit ea53100

Browse files
committed
Build src/port files as a library with -fPIC, and use that in libpq.
libpq and ecpg need shared-library-friendly versions of assorted src/port/ and src/common/ modules. Up to now, they got those by symlinking the individual source files and compiling them locally. That's baroque, and a pain to maintain, and it results in some amount of duplicated compile work. It might've made sense when only a couple of files were needed, but the list has grown and grown and grown :-( It makes more sense to have the originating directory build a third variant of libpgport.a/libpgcommon.a containing modules built with $(CFLAGS_SL), and just link that into the shared library. Unused files won't get linked, so the end result should be the same. This patch makes a down payment on that idea by having src/port/ build such a library and making libpq use it. If the buildfarm doesn't expose fatal problems with the approach, I'll extend it to the other cases. Discussion: https://postgr.es/m/13022.1538003440@sss.pgh.pa.us
1 parent ce4887b commit ea53100

File tree

4 files changed

+55
-63
lines changed

4 files changed

+55
-63
lines changed

src/interfaces/libpq/.gitignore

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
/exports.list
22
/libpq.rc
33
# .c files that are symlinked in from elsewhere
4-
/chklocale.c
5-
/crypt.c
6-
/erand48.c
7-
/getaddrinfo.c
8-
/getpeereid.c
9-
/inet_aton.c
10-
/inet_net_ntop.c
11-
/noblock.c
12-
/open.c
13-
/system.c
14-
/pgsleep.c
15-
/pg_strong_random.c
16-
/pgstrcasecmp.c
17-
/pqsignal.c
18-
/snprintf.c
19-
/strerror.c
20-
/strlcpy.c
21-
/strnlen.c
22-
/thread.c
23-
/win32error.c
24-
/win32setlocale.c
254
/ip.c
265
/md5.c
276
/base64.c

src/interfaces/libpq/Makefile

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,11 @@ ifneq ($(PORTNAME), win32)
2424
override CFLAGS += $(PTHREAD_CFLAGS)
2525
endif
2626

27-
# Need to recompile any external C files because we need
28-
# all object files to use the same compile flags as libpq; some
29-
# platforms require special flags.
30-
LIBS := $(LIBS:-lpgport=)
31-
3227
# We can't use Makefile variables here because the MSVC build system scrapes
3328
# OBJS from this file.
3429
OBJS= fe-auth.o fe-auth-scram.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
3530
fe-protocol2.o fe-protocol3.o pqexpbuffer.o fe-secure.o \
3631
libpq-events.o
37-
# libpgport C files we always use
38-
OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o \
39-
snprintf.o strerror.o thread.o
40-
# libpgport C files that are needed if identified by configure
41-
OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o strlcpy.o strnlen.o win32error.o win32setlocale.o, $(LIBOBJS))
42-
43-
ifeq ($(enable_strong_random), yes)
44-
OBJS += pg_strong_random.o
45-
else
46-
OBJS += erand48.o
47-
endif
4832

4933
# src/backend/utils/mb
5034
OBJS += encnames.o wchar.o
@@ -62,8 +46,7 @@ override shlib = cyg$(NAME)$(DLSUFFIX)
6246
endif
6347

6448
ifeq ($(PORTNAME), win32)
65-
# pgsleep.o is from libpgport
66-
OBJS += pgsleep.o win32.o libpqrc.o
49+
OBJS += win32.o libpqrc.o
6750

6851
libpqrc.o: libpq.rc
6952
$(WINDRES) -i $< -o $@
@@ -76,11 +59,12 @@ endif
7659

7760
# Add libraries that libpq depends (or might depend) on into the
7861
# shared library link. (The order in which you list them here doesn't
79-
# matter.)
62+
# matter.) Note that we filter out -lpgport from LIBS and instead
63+
# insert -lpgport_shlib, to get port files that are built correctly.
8064
ifneq ($(PORTNAME), win32)
81-
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)
65+
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)
8266
else
83-
SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi32 -lssl -lsocket -lnsl -lresolv -lintl -lm $(PTHREAD_LIBS), $(LIBS)) $(LDAP_LIBS_FE)
67+
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)
8468
endif
8569
ifeq ($(PORTNAME), win32)
8670
SHLIB_LINK += -lshell32 -lws2_32 -lsecur32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32, $(LIBS))
@@ -90,22 +74,19 @@ SHLIB_EXPORTS = exports.txt
9074

9175
all: all-lib
9276

77+
all-lib: | submake-libpgport
78+
9379
# Shared library stuff
9480
include $(top_srcdir)/src/Makefile.shlib
9581
backend_src = $(top_srcdir)/src/backend
9682

9783

98-
# We use several libpgport and backend modules verbatim, but since we need
84+
# We use a few backend modules verbatim, but since we need
9985
# to compile with appropriate options to build a shared lib, we can't
100-
# necessarily use the same object files built for libpgport and the backend.
86+
# use the same object files built for the backend.
10187
# Instead, symlink the source files in here and build our own object files.
102-
# For some libpgport modules, this only happens if configure decides
103-
# the module is needed (see filter hack in OBJS, above).
10488
# When you add a file here, remember to add it in the "clean" target below.
10589

106-
chklocale.c crypt.c erand48.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pg_strong_random.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
107-
rm -f $@ && $(LN_S) $< .
108-
10990
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/%
11091
rm -f $@ && $(LN_S) $< .
11192

@@ -123,6 +104,7 @@ libpq.rc libpq-dist.rc: libpq.rc.in
123104
# installations and is only updated by distprep.)
124105
libpq.rc: $(top_builddir)/src/Makefile.global
125106

107+
# Make dependencies on pg_config_paths.h visible, too.
126108
fe-connect.o: fe-connect.c $(top_builddir)/src/port/pg_config_paths.h
127109
fe-misc.o: fe-misc.c $(top_builddir)/src/port/pg_config_paths.h
128110

@@ -154,8 +136,7 @@ clean distclean: clean-lib
154136
rm -f $(OBJS) pthread.h libpq.rc
155137
# Might be left over from a Win32 client-only build
156138
rm -f pg_config_paths.h
157-
# Remove files we (may have) symlinked in from src/port and other places
158-
rm -f chklocale.c crypt.c erand48.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pg_strong_random.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32error.c win32setlocale.c
139+
# Remove files we (may have) symlinked in from src/common and other places
159140
rm -f ip.c md5.c base64.c link-canary.c scram-common.c sha2.c sha2_openssl.c saslprep.c unicode_norm.c
160141
rm -f encnames.c wchar.c
161142

src/port/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/libpgport.a
2+
/libpgport_shlib.a
23
/libpgport_srv.a
34
/pg_config_paths.h

src/port/Makefile

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
#-------------------------------------------------------------------------
22
#
33
# Makefile
4-
# Makefile for the port-specific subsystem of the backend
4+
# Makefile for src/port
55
#
6-
# These files are used in other directories for portability on systems
7-
# with broken/missing library files, and for common code sharing.
6+
# These files are used by the Postgres backend, and also by frontend
7+
# programs. Primarily, they are meant to provide portability on systems
8+
# with broken/missing library files.
89
#
9-
# This makefile generates two outputs:
10+
# This makefile generates three outputs:
1011
#
1112
# libpgport.a - contains object files with FRONTEND defined,
12-
# for use by client application and libraries
13+
# for use by client applications
14+
#
15+
# libpgport_shlib.a - contains object files with FRONTEND defined,
16+
# built suitably for use in shared libraries; for use
17+
# by libpq and other frontend libraries
1318
#
1419
# libpgport_srv.a - contains object files without FRONTEND defined,
15-
# for use only by the backend binaries
20+
# for use only by the backend
1621
#
1722
# LIBOBJS is set by configure (via Makefile.global) to be the list of object
1823
# files that are conditionally needed as determined by configure's probing.
@@ -40,12 +45,15 @@ ifeq ($(enable_strong_random), yes)
4045
OBJS += pg_strong_random.o
4146
endif
4247

43-
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
48+
# libpgport.a, libpgport_shlib.a, and libpgport_srv.a contain the same files
49+
# foo.o, foo_shlib.o, and foo_srv.o are all built from foo.c
50+
OBJS_SHLIB = $(OBJS:%.o=%_shlib.o)
4451
OBJS_SRV = $(OBJS:%.o=%_srv.o)
4552

46-
all: libpgport.a libpgport_srv.a
53+
all: libpgport.a libpgport_shlib.a libpgport_srv.a
4754

4855
# libpgport is needed by some contrib
56+
# currently we don't install libpgport_shlib.a, maybe we should?
4957
install: all installdirs
5058
$(INSTALL_STLIB) libpgport.a '$(DESTDIR)$(libdir)/libpgport.a'
5159

@@ -59,17 +67,37 @@ libpgport.a: $(OBJS)
5967
rm -f $@
6068
$(AR) $(AROPT) $@ $^
6169

62-
# thread.o needs PTHREAD_CFLAGS (but thread_srv.o does not)
70+
# thread.o and thread_shlib.o need PTHREAD_CFLAGS (but thread_srv.o does not)
6371
thread.o: CFLAGS+=$(PTHREAD_CFLAGS)
72+
thread_shlib.o: CFLAGS+=$(PTHREAD_CFLAGS)
6473

65-
# pg_crc32c_sse42.o and its _srv.o version need CFLAGS_SSE42
74+
# all versions of pg_crc32c_sse42.o need CFLAGS_SSE42
6675
pg_crc32c_sse42.o: CFLAGS+=$(CFLAGS_SSE42)
76+
pg_crc32c_sse42_shlib.o: CFLAGS+=$(CFLAGS_SSE42)
6777
pg_crc32c_sse42_srv.o: CFLAGS+=$(CFLAGS_SSE42)
6878

69-
# pg_crc32c_armv8.o and its _srv.o version need CFLAGS_ARMV8_CRC32C
79+
# all versions of pg_crc32c_armv8.o need CFLAGS_ARMV8_CRC32C
7080
pg_crc32c_armv8.o: CFLAGS+=$(CFLAGS_ARMV8_CRC32C)
81+
pg_crc32c_armv8_shlib.o: CFLAGS+=$(CFLAGS_ARMV8_CRC32C)
7182
pg_crc32c_armv8_srv.o: CFLAGS+=$(CFLAGS_ARMV8_CRC32C)
7283

84+
#
85+
# Shared library versions of object files
86+
#
87+
88+
libpgport_shlib.a: $(OBJS_SHLIB)
89+
rm -f $@
90+
$(AR) $(AROPT) $@ $^
91+
92+
# Because this uses its own compilation rule, it doesn't use the
93+
# dependency tracking logic from Makefile.global. To make sure that
94+
# dependency tracking works anyway for the *_shlib.o files, depend on
95+
# their *.o siblings as well, which do have proper dependencies. It's
96+
# a hack that might fail someday if there is a *_shlib.o without a
97+
# corresponding *.o, but there seems little reason for that.
98+
%_shlib.o: %.c %.o
99+
$(CC) $(CFLAGS) $(CFLAGS_SL) $(CPPFLAGS) -c $< -o $@
100+
73101
#
74102
# Server versions of object files
75103
#
@@ -92,6 +120,8 @@ libpgport_srv.a: $(OBJS_SRV)
92120

93121
path.o: path.c pg_config_paths.h
94122

123+
path_shlib.o: path.c pg_config_paths.h
124+
95125
path_srv.o: path.c pg_config_paths.h
96126

97127
# We create a separate file rather than put these in pg_config.h
@@ -112,4 +142,5 @@ pg_config_paths.h: $(top_builddir)/src/Makefile.global
112142
echo "#define MANDIR \"$(mandir)\"" >>$@
113143

114144
clean distclean maintainer-clean:
115-
rm -f libpgport.a libpgport_srv.a $(OBJS) $(OBJS_SRV) pg_config_paths.h
145+
rm -f libpgport.a libpgport_shlib.a libpgport_srv.a
146+
rm -f $(OBJS) $(OBJS_SHLIB) $(OBJS_SRV) pg_config_paths.h

0 commit comments

Comments
 (0)