1# config/llvm.m4
2
3# PGAC_LLVM_SUPPORT
4# -----------------
5#
6# Look for the LLVM installation, check that it's new enough, set the
7# corresponding LLVM_{CFLAGS,CXXFLAGS,BINPATH} and LDFLAGS
8# variables. Also verify that CLANG is available, to transform C
9# into bitcode.
10#
11AC_DEFUN([PGAC_LLVM_SUPPORT],
12[
13  AC_REQUIRE([AC_PROG_AWK])
14
15  AC_ARG_VAR(LLVM_CONFIG, [path to llvm-config command])
16  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)
17
18  # no point continuing if llvm wasn't found
19  if test -z "$LLVM_CONFIG"; then
20    AC_MSG_ERROR([llvm-config not found, but required when compiling --with-llvm, specify with LLVM_CONFIG=])
21  fi
22  # check if detected $LLVM_CONFIG is executable
23  pgac_llvm_version="$($LLVM_CONFIG --version 2> /dev/null || echo no)"
24  if test "x$pgac_llvm_version" = "xno"; then
25    AC_MSG_ERROR([$LLVM_CONFIG does not work])
26  fi
27  # and whether the version is supported
28  if echo $pgac_llvm_version | $AWK -F '.' '{ if ([$]1 >= 4 || ([$]1 == 3 && [$]2 >= 9)) exit 1; else exit 0;}';then
29    AC_MSG_ERROR([$LLVM_CONFIG version is $pgac_llvm_version but at least 3.9 is required])
30  fi
31
32  # need clang to create some bitcode files
33  AC_ARG_VAR(CLANG, [path to clang compiler to generate bitcode])
34  PGAC_PATH_PROGS(CLANG, clang clang-7 clang-6.0 clang-5.0 clang-4.0 clang-3.9)
35  if test -z "$CLANG"; then
36    AC_MSG_ERROR([clang not found, but required when compiling --with-llvm, specify with CLANG=])
37  fi
38  # make sure clang is executable
39  if test "x$($CLANG --version 2> /dev/null || echo no)" = "xno"; then
40    AC_MSG_ERROR([$CLANG does not work])
41  fi
42  # Could check clang version, but it doesn't seem that
43  # important. Systems with a new enough LLVM version are usually
44  # going to have a decent clang version too. It's also not entirely
45  # clear what the minimum version is.
46
47  # Collect compiler flags necessary to build the LLVM dependent
48  # shared library.
49  for pgac_option in `$LLVM_CONFIG --cppflags`; do
50    case $pgac_option in
51      -I*|-D*) LLVM_CPPFLAGS="$pgac_option $LLVM_CPPFLAGS";;
52    esac
53  done
54
55  for pgac_option in `$LLVM_CONFIG --ldflags`; do
56    case $pgac_option in
57      -L*) LDFLAGS="$LDFLAGS $pgac_option";;
58    esac
59  done
60
61  # ABI influencing options, standard influencing options
62  for pgac_option in `$LLVM_CONFIG --cxxflags`; do
63    case $pgac_option in
64      -fno-rtti*) LLVM_CXXFLAGS="$LLVM_CXXFLAGS $pgac_option";;
65      -std=*) LLVM_CXXFLAGS="$LLVM_CXXFLAGS $pgac_option";;
66    esac
67  done
68
69  # Look for components we're interested in, collect necessary
70  # libs. As some components are optional, we can't just list all of
71  # them as it'd raise an error.
72  pgac_components='';
73  for pgac_component in `$LLVM_CONFIG --components`; do
74    case $pgac_component in
75      engine) pgac_components="$pgac_components $pgac_component";;
76      debuginfodwarf) pgac_components="$pgac_components $pgac_component";;
77      orcjit) pgac_components="$pgac_components $pgac_component";;
78      passes) pgac_components="$pgac_components $pgac_component";;
79      native) pgac_components="$pgac_components $pgac_component";;
80      perfjitevents) pgac_components="$pgac_components $pgac_component";;
81    esac
82  done;
83
84  # And then get the libraries that need to be linked in for the
85  # selected components.  They're large libraries, we only want to
86  # link them into the LLVM using shared library.
87  for pgac_option in `$LLVM_CONFIG --libs --system-libs $pgac_components`; do
88    case $pgac_option in
89      -l*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
90    esac
91  done
92
93  LLVM_BINPATH=`$LLVM_CONFIG --bindir`
94
95dnl LLVM_CONFIG, CLANG are already output via AC_ARG_VAR
96  AC_SUBST(LLVM_LIBS)
97  AC_SUBST(LLVM_CPPFLAGS)
98  AC_SUBST(LLVM_CFLAGS)
99  AC_SUBST(LLVM_CXXFLAGS)
100  AC_SUBST(LLVM_BINPATH)
101
102])# PGAC_LLVM_SUPPORT
103
104
105# PGAC_CHECK_LLVM_FUNCTIONS
106# -------------------------
107#
108# Check presence of some optional LLVM functions.
109# (This shouldn't happen until we're ready to run AC_CHECK_DECLS tests;
110# because PGAC_LLVM_SUPPORT runs very early, it's not an appropriate place.)
111#
112AC_DEFUN([PGAC_CHECK_LLVM_FUNCTIONS],
113[
114  # Check which functionality is present
115  SAVE_CPPFLAGS="$CPPFLAGS"
116  CPPFLAGS="$CPPFLAGS $LLVM_CPPFLAGS"
117  AC_CHECK_DECLS([LLVMOrcGetSymbolAddressIn], [], [], [[#include <llvm-c/OrcBindings.h>]])
118  AC_CHECK_DECLS([LLVMGetHostCPUName, LLVMGetHostCPUFeatures], [], [], [[#include <llvm-c/TargetMachine.h>]])
119  AC_CHECK_DECLS([LLVMCreateGDBRegistrationListener, LLVMCreatePerfJITEventListener], [], [], [[#include <llvm-c/ExecutionEngine.h>]])
120  CPPFLAGS="$SAVE_CPPFLAGS"
121])# PGAC_CHECK_LLVM_FUNCTIONS
122