Fix dependency check for building documentation

Build with documentation fails if the required 'sphinx rtd theme' is
not available. Check if dependency is installed before building with
documentation.

Add Python3 as requirement for building documentation.

Change-Id: I5edc5d7374864990e625a5efb358f5a23b3c50fe
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 203c5d2..055bd0e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -640,9 +640,9 @@
   set(CERES_DOCS_INSTALL_DIR "share/doc/ceres" CACHE STRING
       "Ceres docs install path relative to CMAKE_INSTALL_PREFIX")
 
-  find_package(Sphinx QUIET)
+  find_package(Sphinx)
   if (NOT SPHINX_FOUND)
-    message("-- Failed to find Sphinx, disabling build of documentation.")
+    message("-- Failed to find Sphinx and/or its dependencies, disabling build of documentation.")
     update_cache_variable(BUILD_DOCUMENTATION OFF)
   else()
     # Generate the User's Guide (html).
diff --git a/cmake/FindSphinx.cmake b/cmake/FindSphinx.cmake
index 220108d..2ba3c81 100644
--- a/cmake/FindSphinx.cmake
+++ b/cmake/FindSphinx.cmake
@@ -43,24 +43,30 @@
                /opt/local/bin
              DOC "Sphinx documentation generator")
 
-if (NOT SPHINX_EXECUTABLE)
-  set(_Python_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.6 1.5)
+if (SPHINX_EXECUTABLE)
 
-  foreach (_version ${_Python_VERSIONS})
-    set(_sphinx_NAMES sphinx-build-${_version})
+  find_package(PythonInterp 3)
 
-    find_program(SPHINX_EXECUTABLE
-                 NAMES ${_sphinx_NAMES}
-                 PATHS
-                   /usr/bin
-                   /usr/local/bin
-                   /opt/local/bin
-                 DOC "Sphinx documentation generator")
-  endforeach ()
+  if(PYTHONINTERP_FOUND)
+    # Check for sphinx theme dependency for documentation
+    execute_process(
+      COMMAND ${PYTHON_EXECUTABLE} -m pip show sphinx-rtd-theme
+      RESULT_VARIABLE SPHINX_RTD_THEME
+      OUTPUT_QUIET
+      ERROR_QUIET
+    )
+  endif ()
+
+  if (${SPHINX_RTD_THEME} EQUAL 0)
+    set(SPHINX_RTD_THEME TRUE)
+  else ()
+    set(SPHINX_RTD_THEME FALSE)
+  endif ()
+
 endif ()
 
 include(FindPackageHandleStandardArgs)
 
-find_package_handle_standard_args(Sphinx DEFAULT_MSG SPHINX_EXECUTABLE)
+find_package_handle_standard_args(Sphinx DEFAULT_MSG SPHINX_EXECUTABLE SPHINX_RTD_THEME)
 
 mark_as_advanced(SPHINX_EXECUTABLE)
diff --git a/docs/source/CMakeLists.txt b/docs/source/CMakeLists.txt
index 70bf998..217a519 100644
--- a/docs/source/CMakeLists.txt
+++ b/docs/source/CMakeLists.txt
@@ -9,9 +9,12 @@
         COMPONENT Doc
         PATTERN "${SPHINX_HTML_DIR}/*")
 
+# Find python 3
+find_package(PythonInterp 3 REQUIRED)
+
 # Building using 'make_docs.py' python script
 add_custom_target(ceres_docs ALL
-                  python
+                  ${PYTHON_EXECUTABLE}
                   "${Ceres_SOURCE_DIR}/scripts/make_docs.py"
                   "${Ceres_SOURCE_DIR}"
                   "${Ceres_BINARY_DIR}/docs"