Subject: [PATCH] Support static linking against LLVM

From: Marcelo Juchem <[email-masked]>
Date: Thu, 10 Aug 2023 14:45:47 -0500

By default, PostgreSQL doesn't explicitly choose whether to link
statically or dynamically against LLVM when LLVM JIT is enabled (e.g.:
`./configure --with-llvm`).

`llvm-config` will choose to dynamically link by default.

In order to statically link, one must pass `--link-static` to
`llvm-config` when listing linker flags (`--ldflags`) and libraries
(`--libs`).

This patch enables custom flags to be passed to `llvm-config` linker
related invocations through the environment variable
`LLVM_CONFIG_LINK_ARGS`.

To statically link against LLVM it suffices, then, to call `configure`
with environment variable `LLVM_CONFIG_LINK_ARGS=--link-static`.
---
 config/llvm.m4 | 5 +++--
 configure      | 6 ++++--
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/config/llvm.m4 b/config/llvm.m4
index 3a75cd8b4d..712bd3de6c 100644
--- a/config/llvm.m4
+++ b/config/llvm.m4
@@ -13,6 +13,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
   AC_REQUIRE([AC_PROG_AWK])
 
   AC_ARG_VAR(LLVM_CONFIG, [path to llvm-config command])
+  AC_ARG_VAR(LLVM_CONFIG_LINK_ARGS, [extra arguments for llvm-config linker related flags])
   PGAC_PATH_PROGS(LLVM_CONFIG, llvm-config llvm-config-7 llvm-config-6.0 llvm-config-5.0 llvm-config-4.0 llvm-config-3.9)
 
   # no point continuing if llvm wasn't found
@@ -52,7 +53,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
     esac
   done
 
-  for pgac_option in `$LLVM_CONFIG --ldflags`; do
+  for pgac_option in `$LLVM_CONFIG --ldflags $LLVM_CONFIG_LINK_ARGS`; do
     case $pgac_option in
       -L*) LDFLAGS="$LDFLAGS $pgac_option";;
     esac
@@ -84,7 +85,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
   # And then get the libraries that need to be linked in for the
   # selected components.  They're large libraries, we only want to
   # link them into the LLVM using shared library.
-  for pgac_option in `$LLVM_CONFIG --libs --system-libs $pgac_components`; do
+  for pgac_option in `$LLVM_CONFIG --libs --system-libs $LLVM_CONFIG_LINK_ARGS $pgac_components`; do
     case $pgac_option in
       -l*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
     esac
diff --git a/configure b/configure
index 86ffccb1ee..974b7f2d4e 100755
--- a/configure
+++ b/configure
@@ -1595,6 +1595,8 @@ Some influential environment variables:
   CXX         C++ compiler command
   CXXFLAGS    C++ compiler flags
   LLVM_CONFIG path to llvm-config command
+  LLVM_CONFIG_LINK_ARGS
+              extra arguments for llvm-config linker related flags
   CLANG       path to clang compiler to generate bitcode
   CPP         C preprocessor
   PKG_CONFIG  path to pkg-config utility
@@ -5200,7 +5202,7 @@ fi
     esac
   done
 
-  for pgac_option in `$LLVM_CONFIG --ldflags`; do
+  for pgac_option in `$LLVM_CONFIG --ldflags $LLVM_CONFIG_LINK_ARGS`; do
     case $pgac_option in
       -L*) LDFLAGS="$LDFLAGS $pgac_option";;
     esac
@@ -5232,7 +5234,7 @@ fi
   # And then get the libraries that need to be linked in for the
   # selected components.  They're large libraries, we only want to
   # link them into the LLVM using shared library.
-  for pgac_option in `$LLVM_CONFIG --libs --system-libs $pgac_components`; do
+  for pgac_option in `$LLVM_CONFIG --libs --system-libs $LLVM_CONFIG_LINK_ARGS $pgac_components`; do
     case $pgac_option in
       -l*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
     esac
-- 
2.40.1

---

From: Marcelo Juchem <[email-masked]>
Date: Fri, 11 Aug 2023 11:30:32 -0500

Andres, Tom, I found your names in the git history for JIT and LLVM.
Any chance one of you could take a look at the patch?

-mj

On Thu, Aug 10, 2023 at 2:45 PM Marcelo Juchem <[email-masked]> wrote:

>    PGAC_PATH_PROGS(LLVM_CONFIG, llvm-config llvm-config-7 llvm-config-6.0
> llvm-config-5.0 llvm-config-4.0 llvm-config-3.9)
>
> +  for pgac_option in `$LLVM_CONFIG --libs --system-libs
> $LLVM_CONFIG_LINK_ARGS $pgac_components`; do
>      case $pgac_option in
> +  for pgac_option in `$LLVM_CONFIG --libs --system-libs
> $LLVM_CONFIG_LINK_ARGS $pgac_components`; do
>      case $pgac_option in