1dnl   This program is free software; you can redistribute it and/or modify
2dnl   it under the terms of the GNU General Public License as published by
3dnl   the Free Software Foundation; either version 2 of the License, or
4dnl   (at your option) any later version.
5dnl
6dnl   This program is distributed in the hope that it will be useful,
7dnl   but WITHOUT ANY WARRANTY; without even the implied warranty of
8dnl   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9dnl   GNU General Public License for more details.
10dnl
11dnl   You should have received a copy of the GNU General Public License
12dnl   along with this program; if not, write to the Free Software
13dnl   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
14dnl   MA 02110-1301, USA.
15
16AC_PREREQ([2.59])
17AC_INIT([libclamavc++],[devel],[https://github.com/Cisco-Talos/clamav/issues])
18AC_CONFIG_AUX_DIR([config])
19AC_CONFIG_MACRO_DIR([m4])
20AC_CONFIG_HEADER([clamavcxx-config.h])
21AC_CANONICAL_TARGET
22AM_INIT_AUTOMAKE([1.11.1 subdir-objects -Wall -Wportability foreign no-define color-tests tar-pax])
23AM_SILENT_RULES([yes])
24
25cxxset=${CXXFLAGS+set}
26AC_PROG_CXX
27AC_C_BIGENDIAN
28m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
29
30if test "$cxxset" != set; then
31# don't use the default -O2 -g because -g bloats the C++ binaries too much
32  CXXFLAGS="-O2"
33fi
34case "$target_os" in
35solaris*)
36  CXXFLAGS="$CXXFLAGS -include llvm/include/llvm/System/Solaris.h"
37  AC_MSG_NOTICE([Building on Solaris])
38  ;;
39esac
40
41m4_include([../../m4/reorganization/libs/openssl.m4])
42AC_SUBST(SSL_CPPFLAGS, [$SSL_CPPFLAGS])
43
44AM_MAINTAINER_MODE
45LT_INIT([disable-static])
46if test "$GXX" != "yes"; then
47    AC_MSG_ERROR([GNU C++ compiler not found, not building LLVM])
48fi
49
50AC_CHECK_GNU_MAKE
51GMAKE="$llvm_cv_gnu_make_command"
52AC_SUBST([GMAKE])
53
54m4_include(m4/llvm-opts.m4)
55if test "x$llvmflagerrmsg" != "x"; then
56    AC_MSG_ERROR([$llvmflagerrmsg])
57fi
58
59dnl Version number check
60if test "x$llvmconfig" != "x"; then
61    AC_MSG_NOTICE([Using external LLVM])
62else
63    packaged_llvm="yes"
64    dnl macro not available in older autotools
65    AC_CONFIG_SUBDIRS([llvm])
66fi
67
68llvmver_prefix=`expr "$llvmver" : '\([[^0-9]]*\)'`
69llvmver_frag=${llvmver#$llvmver_prefix}
70
71llvmver_major=`expr "$llvmver_frag" : '\([[0-9]]*\)'`
72llvmver_frag=${llvmver_frag#*\.}
73llvmver_minor=`expr "$llvmver_frag" : '\([[0-9]]*\)'`
74llvmver_frag=${llvmver_frag#*\.}
75llvmver_patch=`expr "$llvmver_frag" : '\([[0-9]]*\)'`
76
77dnl suffix unused as of LLVM 3.4.1
78llvmver_suffix=
79if test "x$llvmver_patch" = "x"; then
80    llvmver_patch=0
81fi
82
83AC_MSG_CHECKING([for supported LLVM version])
84llvmver_test=${llvmver_major}${llvmver_minor}${llvmver_patch}
85if test "x$packaged_llvm" = "xyes"; then
86    AC_MSG_RESULT([ok ($llvmver)])
87elif test $llvmver_test -lt 290; then
88    AC_MSG_RESULT([no ($llvmver)])
89    AC_MSG_ERROR([LLVM >= 2.9 required, but "$llvmver"($llvmver_test) found])
90elif test $llvmver_test -lt 360; then
91    llvmcomp="jit nativecodegen scalaropts ipo"
92    AC_MSG_RESULT([ok ($llvmver)])
93elif test $llvmver_test -lt 370; then
94    dnl LLVM 3.6.0 removed jit, so we have to use mcjit
95    dnl and we're using InitializeNativeTargetAsmParser, so we need the architecture specific parsers
96    llvmcomp="mcjit nativecodegen scalaropts ipo x86asmparser powerpcasmparser"
97    AC_MSG_RESULT([ok ($llvmver)])
98else
99    AC_MSG_RESULT([no ($llvmver)])
100    AC_MSG_ERROR([LLVM < 3.7 required, but "$llvmver"($llvmver_test) found])
101fi
102
103dnl acquire the required flags to properly link in external LLVM
104if test "x$llvmconfig" != "x"; then
105    AC_SUBST(LLVMCONFIG_CXXFLAGS, [`$llvmconfig --cxxflags`])
106
107    if test "x$llvm_linking" = "xdynamic"; then
108        AC_SUBST(LLVMCONFIG_LDFLAGS, [`$llvmconfig --ldflags`])
109        AC_SUBST(LLVMCONFIG_LIBS, [-lLLVM-$llvmver])
110        AC_SUBST(LLVMCONFIG_LIBFILES, [])
111    else
112        if test $llvmver_test -ge 350; then
113           dnl LLVM 3.5.0 and after splits linker flags into two sets
114           ldflags=`$llvmconfig --ldflags`
115           syslibs=`$llvmconfig --system-libs`
116           AC_SUBST(LLVMCONFIG_LDFLAGS, ["$ldflags $syslibs"])
117        else
118           AC_SUBST(LLVMCONFIG_LDFLAGS, [`$llvmconfig --ldflags`])
119        fi
120        AC_SUBST(LLVMCONFIG_LIBS, [`$llvmconfig --libs $llvmcomp`])
121        AC_SUBST(LLVMCONFIG_LIBFILES, [`$llvmconfig --libfiles $llvmcomp`])
122    fi
123
124    AC_MSG_NOTICE([CXXFLAGS from llvm-config: $LLVMCONFIG_CXXFLAGS])
125    AC_MSG_NOTICE([LDFLAGS from llvm-config: $LLVMCONFIG_LDFLAGS])
126    AC_MSG_NOTICE([LIBS from llvm-config: $LLVMCONFIG_LIBS])
127fi
128dnl patch does not affect clamav source (yet)
129llvmver_int=${llvmver_major}${llvmver_minor}
130AC_SUBST([LLVM_VERSION], [$llvmver_int])
131
132AC_ARG_ENABLE([llvm],AC_HELP_STRING([-enable-llvm],
133				    [Enable 'llvm' JIT/verifier support @<:@default=auto@:>@]),
134				    [enable_llvm=$enableval], [enable_llvm="auto"])
135
136AC_ARG_ENABLE(optimized, AC_HELP_STRING([-enable-optimized],
137	      [Compile with optimizations enabled (default is YES)]),
138	      enable_optimized=$enableval, enable_optimized=default)
139AM_CONDITIONAL(DEBUG_BUILD,[test "x$enable_optimized" = "xno"])
140
141dnl Set configure args for subdir
142if test "$enable_optimized" = "default"; then
143    ac_configure_args="$ac_configure_args --enable-optimized"
144fi
145
146dnl Pretend that GNU make is available for LLVM, we don't use LLVM's buildsystem
147dnl anyway (except for make check)
148ac_configure_args="$ac_configure_args llvm_cv_gnu_make_command=make"
149AC_ARG_ENABLE(all-jit-targets, AC_HELP_STRING([-enable-all-jit-targets],
150	      [Build all the targets that support JIT for testing (default NO)]),
151	      enable_alltargets=$enableval, enable_alltargets=no)
152if test "$enable_alltargets" = "yes"; then
153    new_args="$ac_configure_args --enable-targets=x86,powerpc --enable-bindings=none --enable-libffi=no --without-llvmgcc --without-llvmgxx"
154else
155    new_args="$ac_configure_args --enable-targets=host-only --enable-bindings=none --enable-libffi=no --without-llvmgcc --without-llvmgxx"
156fi
157ac_configure_args=`echo $new_args | sed -e 's/-Werror //g'`
158
159if test "$enable_llvm" = "auto"; then
160    dnl Do some sanity checks, and don't automatically build on platforms
161    dnl that are not supported or have broken compilers.
162    dnl The user can override this by specifying --enable-llvm=yes.
163    AC_MSG_CHECKING([for supported C++ compiler version])
164    gxx_version=`${CXX} -dumpversion` ||
165    AC_MSG_ERROR([Unable to get GNU C++ compiler version])
166    case "${gxx_version}" in
167     [[03]].*)
168	AC_MSG_ERROR([C++ compiler too old (${gxx_version})])
169        ;;
170     4.1.[[1]]*)
171        AC_MSG_ERROR([C++ compiler is buggy])
172        ;;
173     *)
174        AC_MSG_RESULT([ok (${gxx_version})])
175    esac
176
177    AC_MSG_CHECKING([for supported OS])
178    case "$target_cpu" in
179	i?86|amd64|x86_64|powerpc*)
180	    case "$target_os" in
181		darwin*|freebsd*|kfreebsd*|openbsd*|netbsd*|dragonfly*|linux*|solaris*|win32*|mingw*)
182		    AC_MSG_RESULT([ok ($target_cpu-$target_os)])
183		    ;;
184		*)
185                    AC_MSG_ERROR([OS $target_os is not supported, not building LLVM])
186                    ;;
187	    esac
188	    ;;
189	alpha*|arm*)
190	    AC_MSG_ERROR([CPU support is untested, not building LLVM])
191	    ;;
192	*)
193	    AC_MSG_ERROR([Unsupported CPU for JIT: $target_cpu, not building LLVM])
194	    ;;
195    esac
196fi
197
198dnl bb #1971 - doesn't work with older libstdc++ version at link/runtime
199AC_MSG_CHECKING([for working libstdc++])
200AC_LANG_PUSH([C++])
201AC_RUN_IFELSE(
202    [AC_LANG_SOURCE([
203	#include <iostream>
204	int main()
205	{
206	    // this causes an @GLIBCXX_3.4.9 symbol to be linked in -O2 mode
207	    std::cout << "test";
208	    return 0;
209    }
210    ])],[AC_MSG_RESULT([yes])],
211    [AC_MSG_FAILURE([failed])],
212[AC_LINK_IFELSE(
213    [AC_LANG_SOURCE([
214	#include <iostream>
215	int main()
216	{
217	    // this causes an @GLIBCXX_3.4.9 symbol to be linked in -O2 mode
218	    std::cout << "test";
219	    return 0;
220	}
221    ])], [AC_MSG_RESULT([yes])],
222    [AC_MSG_FAILURE([failed])]
223)])
224AC_LANG_POP([C++])
225
226build_x86=no
227build_ppc=no
228case "$target_cpu" in
229    i?86|amd64|x86_64)
230	    build_x86=yes
231	    ;;
232    powerpc*)
233	    build_ppc=yes
234	    ;;
235esac
236
237# FreeBSD is only one which needs something else than -pthread,
238# according to the logic in ClamAV's main configure.in.
239case "$target_os" in
240freebsd[[45]]*)
241    THREAD_LIBS="-pthread -lc_r"
242    ;;
243freebsd*)
244    THREAD_LIBS="-lthr"
245    ;;
246darwin*)
247    # none required
248    THREAD_LIBS=
249    ;;
250
251*)
252    THREAD_LIBS="-pthread"
253esac
254AC_SUBST([THREAD_LIBS])
255
256if test "$enable_alltargets" = "yes"; then
257	build_x86=yes
258	build_ppc=yes
259fi
260if test "$ac_cv_c_bigendian" = "universal"; then
261    AC_MSG_NOTICE([Universal build detected])
262    build_x86=yes
263    build_ppc=yes
264fi
265AC_MSG_NOTICE([Building X86 backend: $build_x86])
266AC_MSG_NOTICE([Building PPC backend: $build_ppc])
267AM_CONDITIONAL(BUILD_X86, [test "$build_x86" = "yes"])
268AM_CONDITIONAL(BUILD_PPC, [test "$build_ppc" = "yes"])
269
270AC_MSG_CHECKING([optional compiler flags])
271CXX_FLAG_CHECK(NO_VARIADIC_MACROS, [-Wno-variadic-macros])
272CXX_FLAG_CHECK(NO_MISSING_FIELD_INITIALIZERS, [-Wno-missing-field-initializers])
273AC_MSG_RESULT([$NO_VARIADIC_MACROS $NO_MISSING_FIELD_INITIALIZERS])
274
275AM_CONDITIONAL(BUILD_EXTERNAL_LLVM, [test -n "$llvmconfig"])
276
277AC_OUTPUT([
278    Makefile
279])
280