From ccd1198d727b7bd61e0ec831f3e60d2f9807e69b Mon Sep 17 00:00:00 2001 From: Sameer Agarwal Date: Fri, 13 Mar 2026 08:47:37 -0700 Subject: [PATCH] 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 --- internal/ceres/CMakeLists.txt | 3 ++- internal/ceres/bundle_adjustment_test_util.h | 11 +++++++---- internal/ceres/evaluator_test_utils.cc | 4 ++++ internal/ceres/evaluator_test_utils.h | 4 ++++ internal/ceres/numeric_diff_test_utils.cc | 4 ++++ internal/ceres/numeric_diff_test_utils.h | 4 ++++ internal/ceres/test_util.cc | 16 +++++----------- internal/ceres/test_util.h | 7 +++++-- 8 files changed, 35 insertions(+), 18 deletions(-) diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt index 29c14d1b1..60200e352 100644 --- a/internal/ceres/CMakeLists.txt +++ b/internal/ceres/CMakeLists.txt @@ -375,6 +375,7 @@ if (BUILD_TESTING) 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 @@ macro(add_dependencies_to_benchmark BENCHMARK_TARGET) # 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 57f5ab6ec..459566545 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 @@ class BundleAdjustmentProblem { 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 @@ class BundleAdjustmentProblem { double* parameters_; }; +#ifdef CERES_HAS_GTEST using BundleAdjustmentTest = SystemTest; +#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 904635bef..91132adc3 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 @@ void CompareEvaluations(int expected_num_rows, } } // namespace ceres::internal + +#endif // CERES_HAS_GTEST diff --git a/internal/ceres/evaluator_test_utils.h b/internal/ceres/evaluator_test_utils.h index e98dfb659..71d72c222 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 @@ CERES_NO_EXPORT void CompareEvaluations(int expected_num_rows, 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 0aa17780c..4c1b2e2b3 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 @@ -255,3 +257,5 @@ void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( } } // 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 e258ceba3..f5f64c7a7 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 @@ class CERES_NO_EXPORT RandomizedCostFunction : public SizedCostFunction<1, 1> { } // 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 9ae661e4a..1805fcdf5 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 @@ -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 @@ std::string TestFileAbsolutePath(const std::string& filename) { 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 dcac9a6f2..1283db492 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 @@ CERES_NO_EXPORT void ExpectArraysCloseUptoScale(int n, // 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 @@ class CERES_NO_EXPORT SystemTest : public ::testing::Test { std::vector expected_final_residuals_; }; + } // namespace internal } // namespace ceres #include "ceres/internal/reenable_warnings.h" #endif // CERES_INTERNAL_TEST_UTIL_H_ + +#endif // CERES_HAS_GTEST