Skip to content

Instantly share code, notes, and snippets.

@abiiranathan
Created November 3, 2025 15:05
Show Gist options
  • Select an option

  • Save abiiranathan/763b72e4df210268520fa29a3a83b8db to your computer and use it in GitHub Desktop.

Select an option

Save abiiranathan/763b72e4df210268520fa29a3a83b8db to your computer and use it in GitHub Desktop.
Build libpq with Fil-C compiler
#!/usr/bin/env bash
# Exit on error, undefined variables, and pipe failures
set -eup
POSTGRES_VERSION="18.0"
ARCHIVE_NAME="postgresql-${POSTGRES_VERSION}.tar.gz"
EXTRACT_DIR="postgresql-${POSTGRES_VERSION}"
# Skip download if archive already exists
if [ ! -f "${ARCHIVE_NAME}" ]; then
echo "Downloading PostgreSQL ${POSTGRES_VERSION}..."
wget "https://ftp.postgresql.org/pub/source/v${POSTGRES_VERSION}/${ARCHIVE_NAME}"
else
echo "Archive ${ARCHIVE_NAME} already exists, skipping download."
fi
# Skip extraction if directory already exists
if [ ! -d "${EXTRACT_DIR}" ]; then
echo "Extracting ${ARCHIVE_NAME}..."
tar xzf "${ARCHIVE_NAME}"
else
echo "Directory ${EXTRACT_DIR} already exists, skipping extraction."
fi
cd "${EXTRACT_DIR}"
# Build with filcc
./configure --prefix=/opt/fil --without-icu \
--without-readline \
--with-ssl=/opt/fil \
--with-openssl \
CC=/opt/fil/bin/filcc \
CFLAGS_SL="-fPIC -fvisibility=default" \
CXX=/opt/fil/bin/filcpp
make -C src/interfaces/libpq
make -C src/bin/pg_config
make -C src/backend generated-headers
make -C src/include
# Installation steps
sudo make -C src/interfaces/libpq install
sudo make -C src/bin/pg_config install
sudo make -C src/include install
# --- INSTALL THE CMAKE TARGET CONFIGURATION ---
INSTALL_PREFIX="/opt/fil"
CMAKE_CONFIG_DIR="${INSTALL_PREFIX}/lib/cmake/PostgreSQL"
# Create the destination directory for cmake.
sudo mkdir -p "${CMAKE_CONFIG_DIR}"
# Create the PostgreSQLConfig.cmake file content
cat << 'EOF' > postgresql-config.tmp
# This file is manually generated to provide a CMake CONFIG package
# for a PostgreSQL installation built with Autoconf/Make.
# Determine the full path to the installed pg_config utility
find_program(PG_CONFIG_EXE pg_config
PATHS "${PostgreSQL_ROOT}/bin" "${PostgreSQL_ROOT}" NO_DEFAULT_PATH
)
# Fallback search for pg_config if the root path didn't work
if(NOT PG_CONFIG_EXE)
find_program(PG_CONFIG_EXE pg_config)
endif()
if(NOT PG_CONFIG_EXE)
message(FATAL_ERROR "pg_config utility not found. Cannot determine PostgreSQL paths.")
endif()
# Get the required paths and library flags from the pg_config utility
execute_process(COMMAND ${PG_CONFIG_EXE} --includedir_server OUTPUT_VARIABLE PostgreSQL_INCLUDE_DIRS_VAR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PG_CONFIG_EXE} --libdir OUTPUT_VARIABLE PostgreSQL_LIB_DIR_VAR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PG_CONFIG_EXE} --libs_only_L OUTPUT_VARIABLE PostgreSQL_LINK_DIRS_VAR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PG_CONFIG_EXE} --libs_only_l OUTPUT_VARIABLE PostgreSQL_LINK_LIBS_VAR OUTPUT_STRIP_TRAILING_WHITESPACE)
# Remove the '-L' and '-l' prefixes to get clean paths and library names
string(REPLACE "-L" "" PostgreSQL_LINK_DIRS "${PostgreSQL_LINK_DIRS_VAR}")
string(REPLACE "-l" "" PostgreSQL_LINK_LIBS "${PostgreSQL_LINK_LIBS_VAR}")
# Set the variables for the consuming project
set(PostgreSQL_INCLUDE_DIRS "${PostgreSQL_INCLUDE_DIRS_VAR}" CACHE PATH "PostgreSQL include directory")
# Define a modern CMake INTERFACE library target for libpq
# This allows linking with 'PostgreSQL::PostgreSQL'
if(NOT TARGET PostgreSQL::PostgreSQL)
add_library(PostgreSQL::PostgreSQL INTERFACE IMPORTED)
# Set the location of the headers
set_target_properties(PostgreSQL::PostgreSQL PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIRS}"
)
# Set the linking information (library and path)
target_link_options(PostgreSQL::PostgreSQL INTERFACE "${PostgreSQL_LINK_DIRS} ${PostgreSQL_LIB_DIR_VAR}")
target_link_libraries(PostgreSQL::PostgreSQL INTERFACE ${PostgreSQL_LINK_LIBS})
endif()
set(PostgreSQL_FOUND TRUE CACHE INTERNAL "PostgreSQL client library found.")
EOF
# 3. Install the created file
sudo install -m 644 postgresql-config.tmp "${CMAKE_CONFIG_DIR}/PostgreSQLConfig.cmake"
# 4. Clean up the temporary file
rm -f postgresql-config.tmp
echo "PostgreSQL libpq and CMake config installed successfully to ${INSTALL_PREFIX}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment