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
This commit is contained in:
Sameer Agarwal
2026-03-13 08:47:37 -07:00
parent 084b3e3b83
commit ccd1198d72
8 changed files with 35 additions and 18 deletions
+2 -1
View File
@@ -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()
+7 -4
View File
@@ -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<BundleAdjustmentProblem>;
#endif
} // namespace internal
} // namespace ceres
} // namespace ceres::internal
+4
View File
@@ -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
+4
View File
@@ -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
@@ -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 @@ void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
}
} // namespace ceres::internal
#endif // CERES_HAS_GTEST
+4
View File
@@ -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
+5 -11
View File
@@ -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 @@ 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
+5 -2
View File
@@ -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<double> expected_final_residuals_;
};
} // namespace internal
} // namespace ceres
#include "ceres/internal/reenable_warnings.h"
#endif // CERES_INTERNAL_TEST_UTIL_H_
#endif // CERES_HAS_GTEST