1# -*- mode: autoconf -*-
2#
3# AX_CHECK_CL
4#
5# Check for an OpenCL implementation.  If CL is found, the required
6# include, compiler and linker flags are included in the output
7# variables "CL_CPPFLAGS", "CL_CFLAGS" and "CL_LIBS", respectively.
8# If no usable CL implementation is found, "no_cl" is set to "yes".
9#
10# If the header "CL/OpenCL.h" is found, "HAVE_CL_OPENCL_H" is defined.  If the header
11# "OpenCL/OpenCL.h" is found, HAVE_OPENCL_OPENCL_H is defined.  These preprocessor
12# definitions may not be mutually exclusive.
13#
14# Based on AX_CHECK_GL, version: 2.4 author: Braden McDaniel
15# <braden@endoframe.com>
16#
17# Updated 2013-07-18 by Roy Stogner <roystgnr@ices.utexas.edu>
18#
19# This program is free software; you can redistribute it and/or modify
20# it under the terms of the GNU General Public License as published by
21# the Free Software Foundation; either version 2, or (at your option)
22# any later version.
23#
24# This program is distributed in the hope that it will be useful,
25# but WITHOUT ANY WARRANTY; without even the implied warranty of
26# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27# GNU General Public License for more details.
28#
29# You should have received a copy of the GNU General Public License
30# along with this program; if not, write to the Free Software
31# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32# 02110-1301, USA.
33#
34# As a special exception, the you may copy, distribute and modify the
35# configure scripts that are the output of Autoconf when processing
36# the Macro.  You need not follow the terms of the GNU General Public
37# License when using or distributing such scripts.
38#
39AC_DEFUN([AX_CHECK_CL],
40[AC_REQUIRE([AC_CANONICAL_HOST])dnl
41AC_REQUIRE([AC_PROG_SED])dnl
42AC_REQUIRE([ACX_PTHREAD])dnl
43
44AC_ARG_ENABLE([opencl],
45    [AC_HELP_STRING([--disable-opencl],
46                    [do not use OpenCL])],
47    [enable_opencl=$enableval],
48    [enable_opencl='yes'])
49
50if test "$enable_opencl" = 'yes'; then
51  AC_LANG_PUSH([$1])
52  AX_LANG_COMPILER_MS
53  AS_IF([test X$ax_compiler_ms = Xno],
54        [CL_CFLAGS="${PTHREAD_CFLAGS}"; CL_LIBS="${PTHREAD_LIBS} -lm"])
55
56  ax_save_CPPFLAGS=$CPPFLAGS
57  CPPFLAGS="$CL_CFLAGS $CPPFLAGS"
58  if test "x${OPENCL_DIR}" != "x" -a -d "${OPENCL_DIR}/include"; then
59    OPENCL_INCLUDE="${OPENCL_DIR}/include"
60  fi
61  if test "x${OPENCL_INCLUDE}" != x; then
62    CL_CPPFLAGS="-I$OPENCL_INCLUDE"
63    CPPFLAGS="$CPPFLAGS $CL_CPPFLAGS"
64  else
65    CL_CPPFLAGS=""
66  fi
67
68  if test "x${OPENCL_DIR}" != "x" -a -d "${OPENCL_DIR}/lib"; then
69    OPENCL_LIBDIR="${OPENCL_DIR}/lib"
70  fi
71  if test "x${OPENCL_LIB}" != "x" -a -d "${OPENCL_LIB}"; then
72    OPENCL_LIBDIR="${OPENCL_LIB}"
73  fi
74  if test "x${OPENCL_LIBDIR}" != x; then
75    CL_LDFLAGS="-L$OPENCL_LIBDIR"
76  else
77    CL_LDFLAGS=""
78  fi
79
80  AC_CHECK_HEADERS([CL/cl.h OpenCL/cl.h])
81  CPPFLAGS=$ax_save_CPPFLAGS
82
83  AC_CHECK_HEADERS([windows.h])
84
85  m4_define([AX_CHECK_CL_PROGRAM],
86            [AC_LANG_PROGRAM([[
87  # if defined(HAVE_WINDOWS_H) && defined(_WIN32)
88  #   include <windows.h>
89  # endif
90  # ifdef HAVE_CL_CL_H
91  #   include <CL/cl.h>
92  # elif defined(HAVE_OPENCL_CL_H)
93  #   include <OpenCL/cl.h>
94  # else
95  #   error no CL.h
96  # endif]],
97                             [[clCreateContextFromType(0,0,0,0,0)]])])
98
99  AC_CACHE_CHECK([for OpenCL library], [ax_cv_check_cl_libcl],
100  [ax_cv_check_cl_libcl=no
101  case $host_cpu in
102    x86_64) ax_check_cl_libdir=lib64 ;;
103    *)      ax_check_cl_libdir=lib ;;
104  esac
105  ax_save_CPPFLAGS=$CPPFLAGS
106  CPPFLAGS="$CL_CFLAGS $CL_CPPFLAGS $CPPFLAGS"
107  ax_save_LIBS=$LIBS
108  ax_save_LDFLAGS=$LDFLAGS
109  LIBS=""
110  LDFLAGS="$CL_LDFLAGS"
111  ax_check_libs="-lOpenCL -lCL -lclparser"
112  for ax_lib in $ax_check_libs; do
113    AS_IF([test X$ax_compiler_ms = Xyes],
114          [ax_try_lib=`echo $ax_lib | $SED -e 's/^-l//' -e 's/$/.lib/'`],
115          [ax_try_lib=$ax_lib])
116    LIBS="$ax_try_lib $CL_LIBS $ax_save_LIBS"
117    AC_LINK_IFELSE([AX_CHECK_CL_PROGRAM],
118                   [ax_cv_check_cl_libcl=$ax_try_lib; break],
119                   [ax_check_cl_nvidia_flags="-L/usr/$ax_check_cl_libdir/nvidia" LIBS="$ax_try_lib $ax_check_cl_nvidia_flags $CL_LIBS $ax_save_LIBS"
120                   AC_LINK_IFELSE([AX_CHECK_CL_PROGRAM],
121                                  [ax_cv_check_cl_libcl="$ax_try_lib $ax_check_cl_nvidia_flags"; break],
122                                  [ax_check_cl_dylib_flag='-framework OpenCL' LIBS="$ax_check_cl_dylib_flag"
123                                  AC_LINK_IFELSE([AX_CHECK_CL_PROGRAM],
124                                                 [ax_cv_check_cl_libcl="$ax_check_cl_dylib_flag"; break])])])
125  done
126
127  AS_IF([test "X$ax_cv_check_cl_libcl" = Xno -a X$no_x = Xyes],
128        [LIBS='-framework OpenCL'
129        AC_LINK_IFELSE([AX_CHECK_CL_PROGRAM],
130                       [ax_cv_check_cl_libcl=$LIBS])])
131
132  LIBS=$ax_save_LIBS
133  LDFLAGS=$ax_save_LDFLAGS
134  CPPFLAGS=$ax_save_CPPFLAGS])
135
136  AS_IF([test "X$ax_cv_check_cl_libcl" = Xno],
137        [no_cl=yes; CL_CFLAGS=""; CL_LIBS=""],
138        [CL_LIBS="$ax_cv_check_cl_libcl $CL_LIBS"; AC_DEFINE([_OPENCL], [1],
139      [Define this for the OpenCL Accelerator])])
140  AC_LANG_POP([$1])
141fi
142
143AC_SUBST([CL_CPPFLAGS])
144AC_SUBST([CL_CFLAGS])
145AC_SUBST([CL_LDFLAGS])
146AC_SUBST([CL_LIBS])
147])dnl
148