File: ax_llvm.m4

package info (click to toggle)
creduce 2.9~20181016-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 4,360 kB
  • sloc: cpp: 23,977; ansic: 7,062; sh: 4,918; perl: 2,679; makefile: 472; lex: 441
file content (163 lines) | stat: -rw-r--r-- 5,722 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- mode: m4 -*-
#
# Copyright (c) 2012, 2013, 2014, 2015, 2016 The University of Utah
# Copyright (c) 2008 Andy Kitchen <agimbleinthewabe@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved.  This file is offered as-is, without any
# warranty.

###############################################################################

# SYNOPSIS
#
#   AX_LLVM([version],[llvm-libs])
#
# DESCRIPTION
#
#   Test for the existence of LLVM, test that LLVM is at least the specified
#   version, and test that a simple test program can be linked with the
#   libraries described in the llvm-libs argument, i.e.:
#
#     llvm --libs <llvm-libs>
#
#   If the <version> argument is the empty string, then the version check is
#   bypassed.
#
# This file was derived from the LLVM Autoconf macro found in the Autoconf
# Macro Archive: <http://www.gnu.org/software/autoconf-archive/ax_llvm.html>.
# The definition of AX_LLVM in this file is almost completely rewritten from
# the version (serial #12) found in the Archive.
#
# The current file has been updated for modern LLVM (3.3+).

AC_DEFUN([AX_LLVM],
[
  AC_ARG_WITH([llvm],
    AS_HELP_STRING([--with-llvm@<:@=DIR@:>@],
      [use LLVM located in DIR]),
    [with_llvm="$withval"],
    [with_llvm=yes])

  if test "x$with_llvm" = "xno"; then
    AC_MSG_ERROR(
      [--with-llvm=no was given but this package requires LLVM])
  elif test "x$with_llvm" = "xyes"; then
    with_llvm_path="$PATH"
  else
    with_llvm_path="$with_llvm/bin$PATH_SEPARATOR$with_llvm"
  fi

  AC_PATH_PROG([LLVM_CONFIG], [llvm-config], [], [$with_llvm_path])
  if test -z "$LLVM_CONFIG"; then
    AC_MSG_ERROR(
      [LLVM is required but program `llvm-config' cannot be found in $with_llvm_path])
  fi

  if test -n "$1"; then
    AC_MSG_CHECKING([for LLVM version])
    LLVM_VERSION=`$LLVM_CONFIG --version`
    AC_MSG_RESULT([$LLVM_VERSION])
    AX_COMPARE_VERSION([$LLVM_VERSION],[ge],[$1],
      [],
      [
        AC_MSG_ERROR(
          [LLVM version $1 or later is required])
      ])
  fi

  # LLVM post-3.4 supports `llvm-config --system-libs'.
  LLVM_SYSLIBS=`$LLVM_CONFIG --system-libs 2>/dev/null`
  if test $? -ne 0; then
    LLVM_SYSLIBS=""
  fi

  # When setting `LLVM_CPPFLAGS', we weed out command-line options that might
  # be troublesome (e.g., -W/-f options that are not supported by `CXX', or -W
  # options that turn warnings into errors).  We also weed out options that
  # might override choices that *we* want to control (e.g., debug and
  # optimization options).
  #
  # The subparts of the `grep' invocation below remove compiler command-line
  # options of the following forms:
  #   -W...                --- warning options
  #   -w                   --- inhibits all warnings
  #   -pedantic...         --- pedantic warning options
  #   -f...diagnostics...  --- diagnostics reporting options (GCC, Clang)
  #   -f...show...         --- diagnostics reporting options (Clang)
  #   -g...                --- debugging options
  #   -O...                --- optimization options
  # [And for the llvm.org-provided build of Clang 3.8.0 for OS X, weed out:]
  #   -isysroot
  #   /Applications/Xcode.app/...
  #
  # The `tr/sed | grep | xargs' pipeline is intended to be portable.  We use
  # `grep' for matching because writing fancy, portable `sed' expressions is
  # difficult.  For example, some implementations use "\b" to match word
  # boundaries while others use "[[:<:]]" and "[[:>:]]".  The Autoconf
  # documentation says that anchored matches in `sed' are not portable.  Give
  # up; use `grep' instead.  Bonus: better readability!
  
  LLVM_BINDIR=`$LLVM_CONFIG --bindir`
changequote(<<, >>)dnl
  LLVM_CPPFLAGS=`$LLVM_CONFIG --cxxflags | dnl
    tr '\t' ' ' | sed -e 's/  */ /g' | tr ' ' '\n' | dnl
    grep -v -e '^-W' dnl
            -e '^-w$' dnl
            -e '^-pedantic' dnl
            -e '^-f[a-z-]*diagnostics' dnl
            -e '^-f[a-z-]*show' dnl
            -e '^-g' dnl
            -e '^-O$' dnl
            -e '^-O[0-9s]' dnl
            -e '^-isysroot$' dnl
            -e '^/Applications/Xcode\\.app/.*\\.sdk$' | dnl
    xargs`
changequote([, ])dnl
  LLVM_LDFLAGS="`$LLVM_CONFIG --ldflags` $LLVM_SYSLIBS"
  LLVM_LIBS=`$LLVM_CONFIG --libs $2`

  # The output of `llvm-config --ldflags' often contains library directives
  # that must come *after* all the LLVM libraries on the link line: e.g.,
  # "-lpthread -lffi -ldl -lm".  To ensure this, we insert LLVM_LDFLAGS into
  # LIBS, *not* into LDFLAGS.
  #
  # Newer LLVM's (post-3.4) support `llvm-config --system-libs', which will
  # possibly help us to straighten this out in the future.

  AC_REQUIRE([AC_PROG_CXX])
  CPPFLAGS_SAVED="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS $LLVM_CPPFLAGS"
  LDFLAGS_SAVED="$LDFLAGS"
  LDFLAGS="$LDFLAGS"
  # LDFLAGS="$LDFLAGS $LLVM_LDFLAGS" --- see comment above.
  LIBS_SAVED="$LIBS"
  LIBS="$LIBS $LLVM_LIBS $LLVM_LDFLAGS"
  # LIBS="$LIBS $LLVM_LIBS" --- see comment above.

  AC_CACHE_CHECK(can compile with and link with LLVM([$2]),
    ax_cv_llvm,
    [
      AC_LANG_PUSH([C++])
      AC_LINK_IFELSE([
        AC_LANG_PROGRAM(
          [[@%:@include <llvm/IR/LLVMContext.h>
@%:@include <llvm/IR/Module.h>]],
          [[llvm::LLVMContext context;
llvm::Module *M = new llvm::Module("test", context);]])],
        ax_cv_llvm=yes,
        ax_cv_llvm=no)
      AC_LANG_POP([C++])
    ])

  CPPFLAGS="$CPPFLAGS_SAVED"
  LDFLAGS="$LDFLAGS_SAVED"
  LIBS="$LIBS_SAVED"

  if test "$ax_cv_llvm" != "yes"; then
    AC_MSG_FAILURE(
      [cannot compile and link test program with selected LLVM])
  fi
  AC_SUBST(LLVM_BINDIR)
])