Search code examples
c++cpostgresqlpostgresql-12

How to configure pg_config/pgxs/make to pick up CPPFLAGS and CFLAGS in the Makefile to build Postgres C/C++ extensions?


I would like to add compiler flags to build my Postgres C/C++ extension. I have tried standard Makefile practices but pg_config does not pick up any of the compiler flags I add.

The Makefile is as follows:

  1 # the extensions name
  2 EXTENSION     = extension_one
  3 DATA          = $(wildcard *--*.sql)            # script files to install
  4 TESTS         = $(wildcard test/sql/*.sql)      # use appropriate testfiles
  5 
  6 CFLAGS   = -std=c99
  7 CPPFLAGS = -std=c++17
  8 
  9 # find the sql and expected directories under test
 10 # load plpgsql into test db
 11 # load extension into test db
 12 # dbname
 13 REGRESS_OPTS  = --inputdir=test         \
 14                 --load-extension=extension_one \
 15                 --load-language=plpgsql
 16 REGRESS       = $(patsubst test/sql/%.sql,%,$(TESTS))
 17 OBJS          = $(patsubst %.c,%.o,$(wildcard src/*.c)) # object files
 18 # final shared library to be build from multiple source files (OBJS)
 19 MODULE_big    = $(EXTENSION)
 20 
 21 
 22 # postgres build stuff
 23 PG_CONFIG = pg_config
 24 PGXS := $(shell $(PG_CONFIG) --pgxs)
 25 include $(PGXS)

The pg_config environment variables are set as follows:

>pg_config
BINDIR = /usr/lib/postgresql/12/bin
DOCDIR = /usr/share/doc/postgresql-doc-12
HTMLDIR = /usr/share/doc/postgresql-doc-12
INCLUDEDIR = /usr/include/postgresql
PKGINCLUDEDIR = /usr/include/postgresql
INCLUDEDIR-SERVER = /usr/include/postgresql/12/server
LIBDIR = /usr/lib/x86_64-linux-gnu
PKGLIBDIR = /usr/lib/postgresql/12/lib
LOCALEDIR = /usr/share/locale
MANDIR = /usr/share/postgresql/12/man
SHAREDIR = /usr/share/postgresql/12
SYSCONFDIR = /etc/postgresql-common
PGXS = /usr/lib/postgresql/12/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--build=x86_64-linux-gnu' '--prefix=/usr' '--includedir=/usr/include' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--sysconfdir=/etc' '--localstatedir=/var' '--disable-silent-rules' '--libdir=/usr/lib/x86_64-linux-gnu' '--libexecdir=/usr/lib/x86_64-linux-gnu' '--disable-maintainer-mode' '--disable-dependency-tracking' '--with-icu' '--with-tcl' '--with-perl' '--with-python' '--with-pam' '--with-openssl' '--with-libxml' '--with-libxslt' 'PYTHON=/usr/bin/python3' '--mandir=/usr/share/postgresql/12/man' '--docdir=/usr/share/doc/postgresql-doc-12' '--sysconfdir=/etc/postgresql-common' '--datarootdir=/usr/share/' '--datadir=/usr/share/postgresql/12' '--bindir=/usr/lib/postgresql/12/bin' '--libdir=/usr/lib/x86_64-linux-gnu/' '--libexecdir=/usr/lib/postgresql/' '--includedir=/usr/include/postgresql/' '--with-extra-version= (Ubuntu 12.4-0ubuntu0.20.04.1)' '--enable-nls' '--enable-integer-datetimes' '--enable-thread-safety' '--enable-tap-tests' '--enable-debug' '--enable-dtrace' '--disable-rpath' '--with-uuid=e2fs' '--with-gnu-ld' '--with-pgport=5432' '--with-system-tzdata=/usr/share/zoneinfo' '--with-llvm' 'LLVM_CONFIG=/usr/bin/llvm-config-10' 'CLANG=/usr/bin/clang-10' '--with-systemd' '--with-selinux' 'MKDIR_P=/bin/mkdir -p' 'TAR=/bin/tar' 'CFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now' '--with-gssapi' '--with-ldap' '--with-includes=/usr/include/mit-krb5' '--with-libs=/usr/lib/mit-krb5' '--with-libs=/usr/lib/x86_64-linux-gnu/mit-krb5' 'build_alias=x86_64-linux-gnu' 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2' 'CXXFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security'
CC = gcc
CPPFLAGS = -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2 -I/usr/include/mit-krb5
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer
CFLAGS_SL = -fPIC
LDFLAGS = -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -L/usr/lib/llvm-10/lib -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgcommon -lpgport -lpthread -lselinux -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -ledit -lrt -lcrypt -ldl -lm 
VERSION = PostgreSQL 12.4 (Ubuntu 12.4-0ubuntu0.20.04.1)

Since pg_config displays "CFLAGS" and "CPPFLAGS" and the official documentation (https://www.postgresql.org/docs/12/libpq-build.html) says to use these flags that is what I have tried, but when I run make it does not pick them up. Note: I have tried both "=" as normal, and "+=" as it suggests in the docs linked above. And nothing.

When I run make I get:

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer -fPIC -I. -I./ -I/usr/include/postgresql/12/server -I/usr/include/postgresql/internal  -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/mit-krb5  -c -o src/extension_one.o src/extension_one.c

Note: I asked this question in the "Database Administrator" stack exchange, but now realize it might be more appropriate to ask it here.

Note: Here is a nice example of a Postgresql C extention ready to go if you need an example to work with: Postgres_c_extension_demo


Solution

  • I'm not a DB guy, but have had my share of compiling days with Gentoo. And this caught my curiosity. So here are my 2 cents:

    Long story short, you should use PG_CFLAGS or PG_CPPFLAGS(only if working with c++), in your Makefile

    How I found out?

    $ pg_config --help
    
    pg_config provides information about the installed version of PostgreSQL.
    

    Keyword being provides info about the installed version. So your extra CFLAGS will not show up there.

    However I noticed the Makefile pulls the output of pg_config --pgxs to extend the Makefile. The output of that in my distro is: /usr/lib64/pgsql/pgxs/src/makefiles/pgxs.mk

    So I opened that in my editor and here is a snippet:

    #   PG_CPPFLAGS -- will be prepended to CPPFLAGS
    #   PG_CFLAGS -- will be appended to CFLAGS
    #   PG_CXXFLAGS -- will be appended to CXXFLAGS
    #   PG_LDFLAGS -- will be prepended to LDFLAGS