Configure config.h and include it from the build directory.

- Previously we overwrote the default (empty) config.h in the source
  tree with a configured config.h, generated using the current compile
  options.
- This was undesirable as it could lead to inadvertant commits of the
  generated config.h.

- This patch moves the default config.h to <src>/config/ceres/internal,
  separate from the other headers, thus if Ceres is compiled without
  CMake this directory will now also have to be included.  This
  directory is _not_ added to the CMake include directories for Ceres
  (thus the default config.h is never used when compiling with CMake).
- When using CMake, the generated config.h is now placed in
  <build>/config/ceres/internal, which is in turn added to the include
  directories for Ceres when it is compiled, and the resulting config.h
  is copied to ceres/internal when installed.

Change-Id: Ib1ba45e66e383ade2ebb08603af9165c1df616f2
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9e17604..778682e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -654,11 +654,16 @@
 ENDIF (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
 
 # Configure the Ceres config.h compile options header using the current
-# compile options and put the configured header into the Ceres source tree.
+# compile options and put the configured header into the Ceres build
+# directory.  Note that the ceres/internal subdir in <build>/config where
+# the configured config.h is placed is important, because Ceres will be
+# built against this configured header, it needs to have the same relative
+# include path as it would if it were in the source tree (or installed).
 LIST(REMOVE_DUPLICATES CERES_COMPILE_OPTIONS)
 INCLUDE(CreateCeresConfig)
 CREATE_CERES_CONFIG("${CERES_COMPILE_OPTIONS}"
-  ${CMAKE_SOURCE_DIR}/include/ceres/internal)
+  ${CMAKE_CURRENT_BINARY_DIR}/config/ceres/internal)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/config)
 
 ADD_SUBDIRECTORY(internal/ceres)
 
@@ -684,6 +689,12 @@
 FILE(GLOB CERES_PUBLIC_INTERNAL_HDRS ${CMAKE_SOURCE_DIR}/include/ceres/internal/*.h)
 INSTALL(FILES ${CERES_PUBLIC_INTERNAL_HDRS} DESTINATION include/ceres/internal)
 
+# Also setup installation of Ceres config.h configured with the current
+# build options into the installed headers directory.
+INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/config/ceres/internal/config.h
+        DESTINATION include/ceres/internal)
+
+
 IF (MINIGLOG)
   # Install miniglog header if being used as logging #includes appear in
   # installed public Ceres headers.
diff --git a/cmake/CreateCeresConfig.cmake b/cmake/CreateCeresConfig.cmake
index 43f4d17..23f449b 100644
--- a/cmake/CreateCeresConfig.cmake
+++ b/cmake/CreateCeresConfig.cmake
@@ -56,11 +56,16 @@
 #                                  will be <src>/include/ceres/internal.
 
 FUNCTION(CREATE_CERES_CONFIG CURRENT_CERES_COMPILE_OPTIONS CERES_CONFIG_OUTPUT_DIRECTORY)
-  # Verify that the specified output directory is valid.
-  IF (NOT (EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}" AND
-        IS_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}"))
+  # Create the specified output directory if it does not exist.
+  IF (NOT EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}")
+    MESSAGE(STATUS "Creating configured Ceres config.h output directory: "
+      "${CERES_CONFIG_OUTPUT_DIRECTORY}")
+    FILE(MAKE_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
+  ENDIF()
+  IF (EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}" AND
+      NOT IS_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
     MESSAGE(FATAL_ERROR "Ceres Bug: Specified CERES_CONFIG_OUTPUT_DIRECTORY: "
-      "${CERES_CONFIG_OUTPUT_DIRECTORY} does not exist, or is not a directory.")
+      "${CERES_CONFIG_OUTPUT_DIRECTORY} exists, but is not a directory.")
   ENDIF()
 
   # Read all possible configurable compile options from config.h.in, this avoids
diff --git a/include/ceres/internal/config.h b/config/ceres/internal/config.h
similarity index 100%
rename from include/ceres/internal/config.h
rename to config/ceres/internal/config.h
diff --git a/jni/Android.mk b/jni/Android.mk
index 7027146..23e588f 100644
--- a/jni/Android.mk
+++ b/jni/Android.mk
@@ -79,6 +79,7 @@
 CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../internal
 CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../internal/ceres
 CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../include
+CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../config
 
 # Use the alternate glog implementation if provided by the user.
 ifdef CERES_GLOG_DIR