Make test_util GTest-optional to fix benchmark builds with BUILD_TESTING=OFF When Ceres is configured with BUILD_TESTING=OFF and BUILD_BENCHMARKS=ON, benchmarks failed to compile and link due to an unconditional dependency on the test_util library, which requires Googletest (GTest). This commit addresses the issue by introducing the CERES_HAS_GTEST macro and making the test utility library GTest-optional. Fixes: https://github.com/ceres-solver/ceres-solver/issues/1081 Change-Id: I615c37eb6f21d36a4b87b2d24632016944eda3bd
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt index 29c14d1..60200e3 100644 --- a/internal/ceres/CMakeLists.txt +++ b/internal/ceres/CMakeLists.txt
@@ -375,6 +375,7 @@ target_include_directories(test_util PUBLIC ${Ceres_SOURCE_DIR}/internal) target_link_libraries (test_util PUBLIC GTest::gmock ceres_static) + target_compile_definitions(test_util PUBLIC CERES_HAS_GTEST) target_compile_definitions(test_util PRIVATE CERES_TEST_SRCDIR_SUFFIX="../../data") if (BUILD_SHARED_LIBS) @@ -526,7 +527,7 @@ # dependency headers which are not propagated via the ceres targets, # so link them explicitly. target_link_libraries(${BENCHMARK_TARGET} - PRIVATE test_util ceres_static benchmark::benchmark + PRIVATE ceres_static benchmark::benchmark ${CERES_LIBRARY_PRIVATE_DEPENDENCIES}) endmacro()
diff --git a/internal/ceres/bundle_adjustment_test_util.h b/internal/ceres/bundle_adjustment_test_util.h index 57f5ab6..4595665 100644 --- a/internal/ceres/bundle_adjustment_test_util.h +++ b/internal/ceres/bundle_adjustment_test_util.h
@@ -46,8 +46,7 @@ #include "ceres/test_util.h" #include "ceres/types.h" -namespace ceres { -namespace internal { +namespace ceres::internal { const bool kAutomaticOrdering = true; const bool kUserOrdering = false; @@ -62,12 +61,15 @@ ReadData(input_file); BuildProblem(); } + +#ifdef CERES_HAS_GTEST BundleAdjustmentProblem() { const std::string input_file = TestFileAbsolutePath("problem-16-22106-pre.txt"); ReadData(input_file); BuildProblem(); } +#endif ~BundleAdjustmentProblem() { delete[] point_index_; @@ -240,7 +242,8 @@ double* parameters_; }; +#ifdef CERES_HAS_GTEST using BundleAdjustmentTest = SystemTest<BundleAdjustmentProblem>; +#endif -} // namespace internal -} // namespace ceres +} // namespace ceres::internal
diff --git a/internal/ceres/evaluator_test_utils.cc b/internal/ceres/evaluator_test_utils.cc index 904635b..91132ad 100644 --- a/internal/ceres/evaluator_test_utils.cc +++ b/internal/ceres/evaluator_test_utils.cc
@@ -29,6 +29,8 @@ // Author: keir@google.com (Keir Mierle) // sameeragarwal@google.com (Sameer Agarwal) +#ifdef CERES_HAS_GTEST + #include "ceres/evaluator_test_utils.h" #include "ceres/internal/eigen.h" @@ -88,3 +90,5 @@ } } // namespace ceres::internal + +#endif // CERES_HAS_GTEST
diff --git a/internal/ceres/evaluator_test_utils.h b/internal/ceres/evaluator_test_utils.h index e98dfb6..71d72c2 100644 --- a/internal/ceres/evaluator_test_utils.h +++ b/internal/ceres/evaluator_test_utils.h
@@ -31,6 +31,8 @@ // // Test utils used for evaluation testing. +#ifdef CERES_HAS_GTEST + #include "ceres/internal/export.h" namespace ceres::internal { @@ -58,3 +60,5 @@ const double* actual_jacobian); } // namespace ceres::internal + +#endif // CERES_HAS_GTEST
diff --git a/internal/ceres/numeric_diff_test_utils.cc b/internal/ceres/numeric_diff_test_utils.cc index 0aa1778..4c1b2e2 100644 --- a/internal/ceres/numeric_diff_test_utils.cc +++ b/internal/ceres/numeric_diff_test_utils.cc
@@ -29,6 +29,8 @@ // Author: sameeragarwal@google.com (Sameer Agarwal) // tbennun@gmail.com (Tal Ben-Nun) +#ifdef CERES_HAS_GTEST + #include "ceres/numeric_diff_test_utils.h" #include <algorithm> @@ -255,3 +257,5 @@ } } // namespace ceres::internal + +#endif // CERES_HAS_GTEST
diff --git a/internal/ceres/numeric_diff_test_utils.h b/internal/ceres/numeric_diff_test_utils.h index e258ceb..f5f64c7 100644 --- a/internal/ceres/numeric_diff_test_utils.h +++ b/internal/ceres/numeric_diff_test_utils.h
@@ -28,6 +28,8 @@ // // Author: sameeragarwal@google.com (Sameer Agarwal) +#ifdef CERES_HAS_GTEST + #ifndef CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ #define CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ @@ -153,3 +155,5 @@ } // namespace ceres::internal #endif // CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ + +#endif // CERES_HAS_GTEST
diff --git a/internal/ceres/test_util.cc b/internal/ceres/test_util.cc index 9ae661e..1805fcd 100644 --- a/internal/ceres/test_util.cc +++ b/internal/ceres/test_util.cc
@@ -30,6 +30,8 @@ // // Utility functions useful for testing. +#ifdef CERES_HAS_GTEST + #include "ceres/test_util.h" #include <algorithm> @@ -43,6 +45,7 @@ #include "ceres/types.h" #include "gtest/gtest.h" + // This macro is used to inject additional path information specific // to the build system. @@ -139,16 +142,7 @@ return JoinPath(::testing::SrcDir() + CERES_TEST_SRCDIR_SUFFIX, filename); } -std::string ToString(const Solver::Options& options) { - return absl::StrFormat( - "(%s, %s, %s, %s, %d)", - LinearSolverTypeToString(options.linear_solver_type), - SparseLinearAlgebraLibraryTypeToString( - options.sparse_linear_algebra_library_type), - options.linear_solver_ordering ? "USER" : "AUTOMATIC", - PreconditionerTypeToString(options.preconditioner_type), - options.num_threads); -} - } // namespace internal } // namespace ceres + +#endif // CERES_HAS_GTEST
diff --git a/internal/ceres/test_util.h b/internal/ceres/test_util.h index dcac9a6..1283db4 100644 --- a/internal/ceres/test_util.h +++ b/internal/ceres/test_util.h
@@ -28,6 +28,8 @@ // // Author: keir@google.com (Keir Mierle) +#ifdef CERES_HAS_GTEST + #ifndef CERES_INTERNAL_TEST_UTIL_H_ #define CERES_INTERNAL_TEST_UTIL_H_ @@ -75,8 +77,6 @@ // local build/testing environment. CERES_NO_EXPORT std::string TestFileAbsolutePath(const std::string& filename); -CERES_NO_EXPORT std::string ToString(const Solver::Options& options); - // A templated test fixture, that is used for testing Ceres end to end // by computing a solution to the problem for a given solver // configuration and comparing it to a reference solver configuration. @@ -126,9 +126,12 @@ std::vector<double> expected_final_residuals_; }; + } // namespace internal } // namespace ceres #include "ceres/internal/reenable_warnings.h" #endif // CERES_INTERNAL_TEST_UTIL_H_ + +#endif // CERES_HAS_GTEST