# HG changeset patch
# User Gene Harvey <gharveymn@gmail.com>
# Date 1559593436 18000
#      Mon Jun 03 15:23:56 2019 -0500
# Branch jitwork
# Node ID ef06683d1c224b57abdaf9e9678286cb3dd075d1
# Parent  30f53f7a7293184f38b832a6494901636f5f2015
fix jit build breaking from -isystem reordering search path

* configure.ac Add additional check if LLVM include directory is already in
system path.

* acinclude.m4 (OCTAVE_CHECK_SEARCH_PATH): New macro.

diff -r 30f53f7a7293 -r ef06683d1c22 configure.ac
--- a/configure.ac	Mon Jun 03 09:04:41 2019 -0700
+++ b/configure.ac	Mon Jun 03 15:23:56 2019 -0500
@@ -1377,25 +1377,39 @@
     save_CPPFLAGS="$CPPFLAGS"
     save_LDFLAGS="$LDFLAGS"
 
+    LLVM_INCLUDE_DIR="$($LLVM_CONFIG --includedir)"
+    AC_MSG_NOTICE([using LLVM_INCLUDE_DIR=$LLVM_INCLUDE_DIR])
+
     dnl Use -isystem if available because we don't want to see warnings in LLVM
-    LLVM_INCLUDE_FLAG=-I
+    LLVM_INCLUDE_FLAG="-I $LLVM_INCLUDE_DIR"
     OCTAVE_CC_FLAG([-isystem .], [
-      LLVM_INCLUDE_FLAG=-isystem
-      AC_MSG_NOTICE([using -isystem for LLVM headers])])
-
-    dnl Use -isystem so we don't get warnings from llvm headers
-    LLVM_CPPFLAGS="$LLVM_INCLUDE_FLAG `$LLVM_CONFIG --includedir`"
-    LLVM_LDFLAGS="-L`$LLVM_CONFIG --libdir`"
-
-    LDFLAGS="$LDFLAGS $LLVM_LDFLAGS"
-    LLVM_SO=LLVM-`$LLVM_CONFIG --version`
+      OCTAVE_CHECK_SEARCH_PATH(["$LLVM_INCLUDE_DIR"],
+        [AC_MSG_NOTICE([-isystem not used because LLVM_INCLUDE_DIR was found in the system search path])],
+        [LLVM_INCLUDE_FLAG="-isystem $LLVM_INCLUDE_DIR"
+         AC_MSG_NOTICE([using -isystem for LLVM headers])])])
+
+    dnl Can't use --cppflags here because earlier versions of LLVM force NDEBUG
+    LLVM_CPPFLAGS="$LLVM_INCLUDE_FLAG -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS"
+    LLVM_LDFLAGS="$($LLVM_CONFIG --ldflags)"
+
+    dnl We can't just use -lLLVM because earlier versions do not supply the
+    dnl library by that name.
+    LLVM_SO="LLVM-$($LLVM_CONFIG --version)"
+
+    dnl Check the guessed library. If it fails fall back to --libs.
+    dnl Don't use --system-libs because earlier versions will link libedit
+    dnl which conflicts with libreadline and will cause a crash at runtime.
     AC_CHECK_LIB([$LLVM_SO], [LLVMBuildAdd], [LLVM_LIBS="-l$LLVM_SO"],
-                 [LLVM_LIBS="`$LLVM_CONFIG --libs` `$LLVM_CONFIG --system-libs`"])
-
-    dnl Define some extra flags that LLVM requires in order to include headers.
-    dnl Ideally we should get these from llvm-config, but llvm-config isn't
-    dnl very helpful.
-    CPPFLAGS="-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS $LLVM_CPPFLAGS $CPPFLAGS"
+                 [LLVM_LIBS="$($LLVM_CONFIG --libs)"])
+
+    CPPFLAGS="$LLVM_CPPFLAGS $CPPFLAGS"
+    LDFLAGS="$LLVM_LDFLAGS $LDFLAGS"
+
+    dnl FIXME: Some of these may show warnings if LLVM is in the system
+    dnl path, but we are building with a different version of LLVM.
+    dnl The warnings don't appear to affect the final build.
+    dnl POTENTIAL FIX: Preappend the headers given to the check macros
+    dnl with $LLVM_INCLUDE_DIR/
     AC_LANG_PUSH(C++)
     AC_CHECK_HEADER([llvm/Support/TargetSelect.h], [warn_llvm=""])
 
diff -r 30f53f7a7293 -r ef06683d1c22 m4/acinclude.m4
--- a/m4/acinclude.m4	Mon Jun 03 09:04:41 2019 -0700
+++ b/m4/acinclude.m4	Mon Jun 03 15:23:56 2019 -0500
@@ -2844,7 +2844,7 @@
 ])
 dnl
 dnl Check llvm::IRBuilder::CreateConstInBoundsGEP1_32 API
-dbl
+dnl
 AC_DEFUN([OCTAVE_LLVM_IRBUILDER_CREATECONSTINBOUNDSGEP1_32_API], [
   AC_CACHE_CHECK([if llvm::IRBuilder::CreateConstInBoundsGEP1_32 requires a type argument],
     [octave_cv_llvm_irbuilder_createconstinboundsgep1_32_requires_type],
@@ -2874,6 +2874,17 @@
   fi
 ])
 dnl
+dnl OCTAVE_CHECK_SEARCH_PATH
+dnl
+dnl Check if the specified path is already in the system search path
+dnl used by $CXX using a regex match. Executes the second argument if
+dnl a match is found. Otherwise, the third argument is executed.
+AC_DEFUN([OCTAVE_CHECK_SEARCH_PATH], [
+  AS_IF([eval "$CXX -E -Wp,-v -xc++ /dev/null 2>&1 | grep -q \"^\\s*[$1][$]\""],
+        ifelse([$2], , , [$2]),
+        ifelse([$3], , , [$3]))
+])
+dnl
 dnl OCTAVE_CHECK_FORTRAN_SYMBOL_AND_CALLING_CONVENTIONS
 dnl
 dnl Set variables related to Fortran symbol names (append underscore,
