Fix detection of deprecated Bessel function names on MSVC.

- MSVC deprecated the standard POSIX names for Bessel functions in
  favour of underscore prefixed versions.
- Previously we were checking for the presence of the newer underscore
  functions via a macro, which fails as the functions are not defined
  as macros.
- Now we check via a check_cxx_source_compiles() if the newer underscore
  prefixed versions exist when compiling on MSVC and use a new Ceres
  configuration #define to switch on their presence in jet.h

Change-Id: I430880bde2981d12f4d03dbc94d903b9842e887e
diff --git a/CMakeLists.txt b/CMakeLists.txt
index eda2aec..d1d7043 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -659,6 +659,14 @@
 
   # Tuple sizes of 10 are used by Gtest.
   add_definitions("-D_VARIADIC_MAX=10")
+
+  include(CheckIfUnderscorePrefixedBesselFunctionsExist)
+  check_if_underscore_prefixed_bessel_functions_exist(
+    HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
+  if (HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
+    list(APPEND CERES_COMPILE_OPTIONS
+      CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
+  endif()
 endif (MSVC)
 
 if (UNIX)
diff --git a/cmake/CheckIfUnderscorePrefixedBesselFunctionsExist.cmake b/cmake/CheckIfUnderscorePrefixedBesselFunctionsExist.cmake
new file mode 100644
index 0000000..a05721c
--- /dev/null
+++ b/cmake/CheckIfUnderscorePrefixedBesselFunctionsExist.cmake
@@ -0,0 +1,54 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2017 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+#   this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright notice,
+#   this list of conditions and the following disclaimer in the documentation
+#   and/or other materials provided with the distribution.
+# * Neither the name of Google Inc. nor the names of its contributors may be
+#   used to endorse or promote products derived from this software without
+#   specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# Author: alexs.mac@gmail.com (Alex Stewart)
+
+# Microsoft deprecated the POSIX Bessel functions: j[0,1,n]() in favour
+# of _j[0,1,n](), it appears since at least MSVC 2005 [1].  This function
+# checks if the underscore prefixed versions of the Bessel functions are
+# defined, and sets ${HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS_VAR} to
+# TRUE if they do.
+#
+# [1] https://msdn.microsoft.com/en-us/library/ms235384(v=vs.100).aspx
+function(check_if_underscore_prefixed_bessel_functions_exist
+    HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS_VAR)
+  include(CheckCXXSourceCompiles)
+  check_cxx_source_compiles(
+    "#include <math.h>
+     int main(int argc, char * argv[]) {
+       double result;
+       result = _j0(1.2345);
+       result = _j1(1.2345);
+       result = _jn(2, 1.2345);
+       return 0;
+     }"
+     HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
+   set(${HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS_VAR}
+     ${HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS}
+     PARENT_SCOPE)
+endfunction()
diff --git a/cmake/config.h.in b/cmake/config.h.in
index a98fb56..0bf0f8f 100644
--- a/cmake/config.h.in
+++ b/cmake/config.h.in
@@ -88,4 +88,9 @@
 // If defined, Ceres was built as a shared library.
 @CERES_USING_SHARED_LIBRARY@
 
+// If defined, Ceres was compiled with a version MSVC >= 2005 which
+// deprecated the standard POSIX names for bessel functions, replacing them
+// with underscore prefixed versions (e.g. j0() -> _j0()).
+@CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS@
+
 #endif  // CERES_PUBLIC_INTERNAL_CONFIG_H_
diff --git a/include/ceres/jet.h b/include/ceres/jet.h
index a0505a2..2bb8b85 100644
--- a/include/ceres/jet.h
+++ b/include/ceres/jet.h
@@ -541,21 +541,21 @@
 // function errors in client code (the specific warning is suppressed when
 // Ceres itself is built).
 inline double BesselJ0(double x) {
-#if defined(_MSC_VER) && defined(_j0)
+#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
   return _j0(x);
 #else
   return j0(x);
 #endif
 }
 inline double BesselJ1(double x) {
-#if defined(_MSC_VER) && defined(_j1)
+#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
   return _j1(x);
 #else
   return j1(x);
 #endif
 }
 inline double BesselJn(int n, double x) {
-#if defined(_MSC_VER) && defined(_jn)
+#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
   return _jn(n, x);
 #else
   return jn(n, x);