Autodetect Homebrew install prefix on OSX.

- Call through to Homebrew on OSX to determine it's install root
  in case it is not /usr/local and add the result to the CMake
  prefix path that is searched for all find_path/library calls.
- This should allow Ceres to compile even when Homebrew is installed
  in a non-standard location.

Change-Id: I230a5e12aef54617567bdfd20c4fd45c5a04a8bf
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6fe1e32..608ce5a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,6 +64,26 @@
   ENDIF (NOT EXISTS ${LOCAL_GIT_DIRECTORY}/hooks/commit-msg)
 ENDIF (EXISTS ${LOCAL_GIT_DIRECTORY})
 
+# On OS X, add the Homebrew prefix to the set of prefixes searched by
+# CMake in find_path & find_library.  This should ensure that we can
+# still build Ceres even if Homebrew is installed in a non-standard
+# location (not /usr/local).
+IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
+  FIND_PROGRAM(HOMEBREW_EXECUTABLE brew)
+  IF (HOMEBREW_EXECUTABLE)
+    # Detected a Homebrew install, query for its install prefix.
+    EXECUTE_PROCESS(COMMAND ${HOMEBREW_EXECUTABLE} --prefix
+      OUTPUT_VARIABLE HOMEBREW_INSTALL_PREFIX
+      OUTPUT_STRIP_TRAILING_WHITESPACE)
+    MESSAGE(STATUS "Detected Homebrew with install prefix: "
+      "${HOMEBREW_INSTALL_PREFIX}, adding to CMake search paths.")
+  ENDIF()
+  # Note that Eigen is stored in <XXX>/include/eigen3/Eigen/XXX.h
+  # as such, the default prefix path on it's own is insufficient.
+  LIST(APPEND CMAKE_PREFIX_PATH "${HOMEBREW_INSTALL_PREFIX}"
+    "${HOMEBREW_INSTALL_PREFIX}/include/eigen3")
+ENDIF()
+
 # Make CMake aware of the cmake folder for local FindXXX scripts,
 # append rather than set in case the user has passed their own
 # additional paths via -D.