From 47dfd412a19a88aef125b4473337bb68c2dadb6a Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 13 Mar 2017 20:22:10 -0700
Subject: [PATCH 03/16] WIP: Add configure infrastructure to enable LLVM.

---
 configure                  | 97 ++++++++++++++++++++++++++++++++++++++++++++++
 configure.in               | 27 +++++++++++++
 src/Makefile.global.in     |  2 +
 src/backend/Makefile       |  4 ++
 src/include/pg_config.h.in |  3 ++
 5 files changed, 133 insertions(+)

diff --git a/configure b/configure
index a2f9a256b4..fe905e294b 100755
--- a/configure
+++ b/configure
@@ -700,6 +700,9 @@ LDFLAGS_EX
 ELF_SYS
 EGREP
 GREP
+with_llvm
+LLVM_LIBS
+LLVM_CONFIG
 with_zlib
 with_system_tzdata
 with_libxslt
@@ -848,6 +851,7 @@ with_libxml
 with_libxslt
 with_system_tzdata
 with_zlib
+with_llvm
 with_gnu_ld
 enable_largefile
 enable_float4_byval
@@ -1546,6 +1550,7 @@ Optional Packages:
   --with-system-tzdata=DIR
                           use system time zone data in DIR
   --without-zlib          do not use Zlib
+  --with-llvm             build with llvm (JIT) support
   --with-gnu-ld           assume the C compiler uses GNU ld [default=no]
 
 Some influential environment variables:
@@ -6460,6 +6465,98 @@ fi
 
 
 
+
+
+
+# Check whether --with-llvm was given.
+if test "${with_llvm+set}" = set; then :
+  withval=$with_llvm;
+  case $withval in
+    yes)
+
+$as_echo "#define USE_LLVM 1" >>confdefs.h
+
+      ;;
+    no)
+      :
+      ;;
+    *)
+      as_fn_error $? "no argument expected for --with-llvm option" "$LINENO" 5
+      ;;
+  esac
+
+else
+  with_llvm=no
+
+fi
+
+
+
+if test "$with_llvm" = yes ; then
+  for ac_prog in llvm-config
+do
+  # Extract the first word of "$ac_prog", so it can be a program name with args.
+set dummy $ac_prog; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_prog_LLVM_CONFIG+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test -n "$LLVM_CONFIG"; then
+  ac_cv_prog_LLVM_CONFIG="$LLVM_CONFIG" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_LLVM_CONFIG="$ac_prog"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+fi
+fi
+LLVM_CONFIG=$ac_cv_prog_LLVM_CONFIG
+if test -n "$LLVM_CONFIG"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $LLVM_CONFIG" >&5
+$as_echo "$LLVM_CONFIG" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+  test -n "$LLVM_CONFIG" && break
+done
+
+  if test -n "$LLVM_CONFIG"; then
+    for pgac_option in `$LLVM_CONFIG --cflags`; do
+      case $pgac_option in
+        -I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
+      esac
+    done
+    for pgac_option in `$LLVM_CONFIG --ldflags`; do
+      case $pgac_option in
+        -L*) LDFLAGS="$LDFLAGS $pgac_option";;
+      esac
+    done
+    for pgac_option in `$LLVM_CONFIG --libs --system-libs engine`; do
+      case $pgac_option in
+        -l*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
+      esac
+    done
+  fi
+fi
+
+
+
+
 #
 # Elf
 #
diff --git a/configure.in b/configure.in
index e94fba5235..a99da9dff3 100644
--- a/configure.in
+++ b/configure.in
@@ -856,6 +856,33 @@ PGAC_ARG_BOOL(with, zlib, yes,
               [do not use Zlib])
 AC_SUBST(with_zlib)
 
+PGAC_ARG_BOOL(with, llvm, no, [build with llvm (JIT) support],
+              [AC_DEFINE([USE_LLVM], 1, [Define to 1 to build with llvm support. (--with-llvm)])])
+
+if test "$with_llvm" = yes ; then
+  AC_CHECK_PROGS(LLVM_CONFIG, llvm-config)
+  if test -n "$LLVM_CONFIG"; then
+    for pgac_option in `$LLVM_CONFIG --cflags`; do
+      case $pgac_option in
+        -I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
+      esac
+    done
+    for pgac_option in `$LLVM_CONFIG --ldflags`; do
+      case $pgac_option in
+        -L*) LDFLAGS="$LDFLAGS $pgac_option";;
+      esac
+    done
+    for pgac_option in `$LLVM_CONFIG --libs --system-libs engine`; do
+      case $pgac_option in
+        -l*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
+      esac
+    done
+  fi
+fi
+AC_SUBST(LLVM_LIBS)
+AC_SUBST(with_llvm)
+
+
 #
 # Elf
 #
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index e8b3a519cb..ab5862b472 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -186,6 +186,7 @@ with_tcl	= @with_tcl@
 with_openssl	= @with_openssl@
 with_selinux	= @with_selinux@
 with_systemd	= @with_systemd@
+with_llvm	= @with_llvm@
 with_libxml	= @with_libxml@
 with_libxslt	= @with_libxslt@
 with_system_tzdata = @with_system_tzdata@
@@ -270,6 +271,7 @@ LDAP_LIBS_FE = @LDAP_LIBS_FE@
 LDAP_LIBS_BE = @LDAP_LIBS_BE@
 UUID_LIBS = @UUID_LIBS@
 UUID_EXTRA_OBJS = @UUID_EXTRA_OBJS@
+LLVM_LIBS=@LLVM_LIBS@
 LD = @LD@
 with_gnu_ld = @with_gnu_ld@
 
diff --git a/src/backend/Makefile b/src/backend/Makefile
index aab676dbbd..c82ad75bda 100644
--- a/src/backend/Makefile
+++ b/src/backend/Makefile
@@ -45,6 +45,10 @@ LIBS := $(filter-out -lpgport -lpgcommon, $(LIBS)) $(LDAP_LIBS_BE) $(ICU_LIBS)
 # The backend doesn't need everything that's in LIBS, however
 LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))
 
+# Only the backend needs LLVM (if enabled) and it's a big library, so
+# only specify here
+LIBS += $(LLVM_LIBS)
+
 ifeq ($(with_systemd),yes)
 LIBS += -lsystemd
 endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index dcb7a1a320..633c670de9 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -832,6 +832,9 @@
    (--with-libxslt) */
 #undef USE_LIBXSLT
 
+/* Define to 1 to build with llvm support. (--with-llvm) */
+#undef USE_LLVM
+
 /* Define to select named POSIX semaphores. */
 #undef USE_NAMED_POSIX_SEMAPHORES
 
-- 
2.14.1.2.g4274c698f4.dirty

