Move most of suitesparse/cxsparse ifdef code to their headers

Main purpose of this is to make implementation files free from
endless ifdef blocks every time this libraries are needed to be
included. This would hopefully prevent compilation errors in
the future caused by missing ifdef around header include.

This also includes some stubs added to suitesparse/cxsparse
headers to make code even more free from ifdefs.

Change-Id: Ic8554e7df31d8c4751583fe004b99e71b3c9087b
diff --git a/internal/ceres/covariance_impl.cc b/internal/ceres/covariance_impl.cc
index ca5be49..61feb6b 100644
--- a/internal/ceres/covariance_impl.cc
+++ b/internal/ceres/covariance_impl.cc
@@ -49,10 +49,6 @@
 #include "ceres/wall_time.h"
 #include "glog/logging.h"
 
-#ifndef CERES_NO_SUITESPARSE
-#  include "SuiteSparseQR.hpp"
-#endif
-
 namespace ceres {
 namespace internal {
 namespace {
@@ -607,15 +603,8 @@
   const int num_cols = jacobian.num_cols;
   const int num_nonzeros = jacobian.values.size();
 
-  // UF_long is deprecated but SuiteSparse_long is only available in
-  // newer versions of SuiteSparse.
-#if (SUITESPARSE_VERSION < 4002)
-  vector<UF_long> transpose_rows(num_cols + 1, 0);
-  vector<UF_long> transpose_cols(num_nonzeros, 0);
-#else
   vector<SuiteSparse_long> transpose_rows(num_cols + 1, 0);
   vector<SuiteSparse_long> transpose_cols(num_nonzeros, 0);
-#endif
 
   vector<double> transpose_values(num_nonzeros, 0);
 
diff --git a/internal/ceres/cxsparse.h b/internal/ceres/cxsparse.h
index 6004301..cd87908 100644
--- a/internal/ceres/cxsparse.h
+++ b/internal/ceres/cxsparse.h
@@ -125,6 +125,11 @@
 }  // namespace internal
 }  // namespace ceres
 
+#else  // CERES_NO_CXSPARSE
+
+class CXSparse {};
+typedef void cs_dis;
+
 #endif  // CERES_NO_CXSPARSE
 
 #endif  // CERES_INTERNAL_CXSPARSE_H_
diff --git a/internal/ceres/schur_complement_solver.cc b/internal/ceres/schur_complement_solver.cc
index c21d3b5..b192aa1 100644
--- a/internal/ceres/schur_complement_solver.cc
+++ b/internal/ceres/schur_complement_solver.cc
@@ -33,16 +33,13 @@
 #include <set>
 #include <vector>
 
-#ifndef CERES_NO_CXSPARSE
-#include "cs.h"
-#endif  // CERES_NO_CXSPARSE
-
 #include "Eigen/Dense"
 #include "ceres/block_random_access_dense_matrix.h"
 #include "ceres/block_random_access_matrix.h"
 #include "ceres/block_random_access_sparse_matrix.h"
 #include "ceres/block_sparse_matrix.h"
 #include "ceres/block_structure.h"
+#include "ceres/cxsparse.h"
 #include "ceres/detect_structure.h"
 #include "ceres/internal/eigen.h"
 #include "ceres/internal/port.h"
@@ -153,14 +150,9 @@
 
 SparseSchurComplementSolver::SparseSchurComplementSolver(
     const LinearSolver::Options& options)
-    : SchurComplementSolver(options) {
-#ifndef CERES_NO_SUITESPARSE
-  factor_ = NULL;
-#endif  // CERES_NO_SUITESPARSE
-
-#ifndef CERES_NO_CXSPARSE
-  cxsparse_factor_ = NULL;
-#endif  // CERES_NO_CXSPARSE
+    : SchurComplementSolver(options),
+      factor_(NULL),
+      cxsparse_factor_(NULL) {
 }
 
 SparseSchurComplementSolver::~SparseSchurComplementSolver() {
diff --git a/internal/ceres/schur_complement_solver.h b/internal/ceres/schur_complement_solver.h
index 9525e37..b5a1c74 100644
--- a/internal/ceres/schur_complement_solver.h
+++ b/internal/ceres/schur_complement_solver.h
@@ -167,18 +167,14 @@
   // Size of the blocks in the Schur complement.
   vector<int> blocks_;
 
-#ifndef CERES_NO_SUITESPARSE
   SuiteSparse ss_;
   // Symbolic factorization of the reduced linear system. Precomputed
   // once and reused in subsequent calls.
   cholmod_factor* factor_;
-#endif  // CERES_NO_SUITESPARSE
 
-#ifndef CERES_NO_CXSPARSE
   CXSparse cxsparse_;
   // Cached factorization
   cs_dis* cxsparse_factor_;
-#endif  // CERES_NO_CXSPARSE
   CERES_DISALLOW_COPY_AND_ASSIGN(SparseSchurComplementSolver);
 };
 
diff --git a/internal/ceres/sparse_normal_cholesky_solver.cc b/internal/ceres/sparse_normal_cholesky_solver.cc
index cfb0f17..f1a5237 100644
--- a/internal/ceres/sparse_normal_cholesky_solver.cc
+++ b/internal/ceres/sparse_normal_cholesky_solver.cc
@@ -36,11 +36,8 @@
 #include <cstring>
 #include <ctime>
 
-#ifndef CERES_NO_CXSPARSE
-#include "cs.h"
-#endif
-
 #include "ceres/compressed_row_sparse_matrix.h"
+#include "ceres/cxsparse.h"
 #include "ceres/internal/eigen.h"
 #include "ceres/internal/scoped_ptr.h"
 #include "ceres/linear_solver.h"
@@ -54,14 +51,9 @@
 
 SparseNormalCholeskySolver::SparseNormalCholeskySolver(
     const LinearSolver::Options& options)
-    : options_(options) {
-#ifndef CERES_NO_SUITESPARSE
-  factor_ = NULL;
-#endif
-
-#ifndef CERES_NO_CXSPARSE
-  cxsparse_factor_ = NULL;
-#endif  // CERES_NO_CXSPARSE
+    : factor_(NULL),
+      cxsparse_factor_(NULL),
+      options_(options) {
 }
 
 SparseNormalCholeskySolver::~SparseNormalCholeskySolver() {
diff --git a/internal/ceres/sparse_normal_cholesky_solver.h b/internal/ceres/sparse_normal_cholesky_solver.h
index ebb32e6..61111b4 100644
--- a/internal/ceres/sparse_normal_cholesky_solver.h
+++ b/internal/ceres/sparse_normal_cholesky_solver.h
@@ -73,17 +73,13 @@
       const LinearSolver::PerSolveOptions& options,
       double* x);
 
-#ifndef CERES_NO_SUITESPARSE
   SuiteSparse ss_;
   // Cached factorization
   cholmod_factor* factor_;
-#endif  // CERES_NO_SUITESPARSE
 
-#ifndef CERES_NO_CXSPARSE
   CXSparse cxsparse_;
   // Cached factorization
   cs_dis* cxsparse_factor_;
-#endif  // CERES_NO_CXSPARSE
 
   const LinearSolver::Options options_;
   CERES_DISALLOW_COPY_AND_ASSIGN(SparseNormalCholeskySolver);
diff --git a/internal/ceres/suitesparse.h b/internal/ceres/suitesparse.h
index 8a5b0a8..c177292 100644
--- a/internal/ceres/suitesparse.h
+++ b/internal/ceres/suitesparse.h
@@ -43,6 +43,7 @@
 #include "ceres/internal/port.h"
 #include "cholmod.h"
 #include "glog/logging.h"
+#include "SuiteSparseQR.hpp"
 
 // Before SuiteSparse version 4.2.0, cholmod_camd was only enabled
 // if SuiteSparse was compiled with Metis support. This makes
@@ -58,6 +59,12 @@
 #define CERES_NO_CAMD
 #endif
 
+// UF_long is deprecated but SuiteSparse_long is only available in
+// newer versions of SuiteSparse.
+#if (SUITESPARSE_VERSION < 4002)
+typedef UF_long SuiteSparse_long;
+#endif
+
 namespace ceres {
 namespace internal {
 
@@ -261,6 +268,11 @@
 }  // namespace internal
 }  // namespace ceres
 
+#else // CERES_NO_SUITESPARSE
+
+class SuiteSparse {};
+typedef void cholmod_factor;
+
 #endif  // CERES_NO_SUITESPARSE
 
 #endif  // CERES_INTERNAL_SUITESPARSE_H_
diff --git a/internal/ceres/visibility.cc b/internal/ceres/visibility.cc
index fcd793c..acfa45b 100644
--- a/internal/ceres/visibility.cc
+++ b/internal/ceres/visibility.cc
@@ -153,4 +153,4 @@
 }  // namespace internal
 }  // namespace ceres
 
-#endif
+#endif  // CERES_NO_SUITESPARSE