Improve CMake scoping, CUDA defaults, and examples Avoid leaking top-level build settings when Ceres is embedded via FetchContent or add_subdirectory. Output directories, debug postfixes, PIC, and the default CMAKE_BUILD_TYPE now apply only when Ceres is the top-level project (and CMAKE_BUILD_TYPE is only defaulted for single-config generators). export(PACKAGE) now uses PROJECT_NAME instead of CMAKE_PROJECT_NAME. Respect user-supplied CMAKE_CUDA_ARCHITECTURES instead of clearing it unconditionally, declare WITH_EIGENSPARSE before WITH_EIGENMETIS, move EIGEN_MPL2_ONLY and SuiteSparse/METIS version defines onto the ceres target, and make CERES_TEST and add_dependencies_to_benchmark functions instead of macros. In examples, move absl::time from simple_bundle_adjuster to bundle_adjuster, drop unused absl::log_initialize from curve_fitting_c, and replace directory-wide include_directories in examples/slam with per-target include directories. In docs/source/CMakeLists.txt, drop -a from the Sphinx build so incremental rebuilds work and remove the no-op PATTERN clause from install(DIRECTORY). Change-Id: Ifd65b419313b1b34e1415f143676739706277b97
diff --git a/CMakeLists.txt b/CMakeLists.txt index f3774e7..ee0d561 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -81,17 +81,19 @@ VERSION ${CERES_VERSION} ) -# Always build position-independent code (PIC), even when building Ceres as a -# static library so that shared libraries can link against it, not just -# executables (PIC does not apply on Windows). Global variable can be overridden -# by the user whereas target properties can be not. -set(CMAKE_POSITION_INDEPENDENT_CODE ON) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Ceres_BINARY_DIR}/bin) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${Ceres_BINARY_DIR}/lib) -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${Ceres_BINARY_DIR}/lib) -# Set postfixes for generated libraries based on buildtype. -set(CMAKE_RELEASE_POSTFIX "") -set(CMAKE_DEBUG_POSTFIX "-debug") +if (PROJECT_IS_TOP_LEVEL) + # Always build position-independent code (PIC), even when building Ceres as a + # static library so that shared libraries can link against it, not just + # executables (PIC does not apply on Windows). Global variable can be + # overridden by the user whereas target properties cannot. + set(CMAKE_POSITION_INDEPENDENT_CODE ON) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Ceres_BINARY_DIR}/bin) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${Ceres_BINARY_DIR}/lib) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${Ceres_BINARY_DIR}/lib) + # Set postfixes for generated libraries based on buildtype. + set(CMAKE_RELEASE_POSTFIX "") + set(CMAKE_DEBUG_POSTFIX "-debug") +endif() # Configure CMake checks and helper includes. include(AddCompileFlagsIfSupported) @@ -130,11 +132,10 @@ "WITH_CUDA" OFF) option(WITH_CUSTOM_BLAS "Use handcoded BLAS routines (usually faster) instead of Eigen." ON) -cmake_dependent_option(WITH_EIGENMETIS "Enable Eigen METIS support." ON - "NOT DEFINED WITH_EIGENSPARSE OR WITH_EIGENSPARSE" OFF) -# The controlling option is declared later and defaults to ON. option(WITH_EIGENSPARSE "Enable Eigen as a sparse linear algebra library." ON) +cmake_dependent_option(WITH_EIGENMETIS "Enable Eigen METIS support." ON + "WITH_EIGENSPARSE" OFF) # Apple considers the BLAS dsyrk_ symbol a private API and rejects iOS apps # that contain it. cmake_dependent_option(WITH_LAPACK @@ -387,38 +388,38 @@ if (_Ceres_FEATURE_EIGEN_SPARSE) list(APPEND CERES_COMPILE_OPTIONS CERES_USE_EIGEN_SPARSE) -else() - add_definitions(-DEIGEN_MPL2_ONLY) endif() if (_Ceres_FEATURE_CUDA) set(CUDAToolkit_DEPENDENCY "find_dependency(CUDAToolkit ${CUDAToolkit_VERSION})") enable_language(CUDA) - set(CMAKE_CUDA_ARCHITECTURES "") - if (CUDAToolkit_VERSION VERSION_LESS "13.0") - # Support Maxwell GPUs. - list(APPEND CMAKE_CUDA_ARCHITECTURES "50") - if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "8.0") - # Support Pascal GPUs. - list(APPEND CMAKE_CUDA_ARCHITECTURES "60") + if (NOT DEFINED CMAKE_CUDA_ARCHITECTURES) + set(CMAKE_CUDA_ARCHITECTURES "") + if (CUDAToolkit_VERSION VERSION_LESS "13.0") + # Support Maxwell GPUs. + list(APPEND CMAKE_CUDA_ARCHITECTURES "50") + if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "8.0") + # Support Pascal GPUs. + list(APPEND CMAKE_CUDA_ARCHITECTURES "60") + endif() + if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "9.0") + # Support Volta GPUs. + list(APPEND CMAKE_CUDA_ARCHITECTURES "70") + endif() endif() - if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "9.0") - # Support Volta GPUs. - list(APPEND CMAKE_CUDA_ARCHITECTURES "70") + if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "10.0") + # Support Turing GPUs. + list(APPEND CMAKE_CUDA_ARCHITECTURES "75") endif() - endif() - if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "10.0") - # Support Turing GPUs. - list(APPEND CMAKE_CUDA_ARCHITECTURES "75") - endif() - if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "11.0") - # Support Ampere GPUs. - list(APPEND CMAKE_CUDA_ARCHITECTURES "80") - endif() - if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "12.0") - # Support Hopper GPUs. - list(APPEND CMAKE_CUDA_ARCHITECTURES "90") + if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "11.0") + # Support Ampere GPUs. + list(APPEND CMAKE_CUDA_ARCHITECTURES "80") + endif() + if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "12.0") + # Support Hopper GPUs. + list(APPEND CMAKE_CUDA_ARCHITECTURES "90") + endif() endif() if (WITH_CUDA STREQUAL "static") @@ -525,19 +526,17 @@ # # The CACHE STRING logic here and elsewhere is needed to force CMake # to pay attention to the value of these variables. -if (NOT CMAKE_BUILD_TYPE) +if (PROJECT_IS_TOP_LEVEL AND NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "No build type specified; defaulting to CMAKE_BUILD_TYPE=Release.") set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE) -else (NOT CMAKE_BUILD_TYPE) - if (CMAKE_BUILD_TYPE STREQUAL "Debug") - message(STATUS "\n=================================================================================") - message(STATUS "\nBuild type: Debug. Performance will be terrible!") - message(STATUS "Add -DCMAKE_BUILD_TYPE=Release to the CMake command line to get an optimized build.") - message(STATUS "\n=================================================================================") - endif (CMAKE_BUILD_TYPE STREQUAL "Debug") -endif (NOT CMAKE_BUILD_TYPE) +elseif (CMAKE_BUILD_TYPE STREQUAL "Debug") + message(STATUS "\n=================================================================================") + message(STATUS "\nBuild type: Debug. Performance will be terrible!") + message(STATUS "Add -DCMAKE_BUILD_TYPE=Release to the CMake command line to get an optimized build.") + message(STATUS "\n=================================================================================") +endif() # After the tweaks for the compile settings, disable some warnings on MSVC. if (MSVC) @@ -875,7 +874,7 @@ export(TARGETS ${Ceres_EXPORT_TARGETS} NAMESPACE Ceres:: FILE ${Ceres_BINARY_DIR}/CeresTargets.cmake) -export(PACKAGE ${CMAKE_PROJECT_NAME}) +export(PACKAGE ${PROJECT_NAME}) unset (Ceres_EXPORT_TARGETS)
diff --git a/docs/source/CMakeLists.txt b/docs/source/CMakeLists.txt index 1164484..7f6009e 100644 --- a/docs/source/CMakeLists.txt +++ b/docs/source/CMakeLists.txt
@@ -3,14 +3,14 @@ set(SPHINX_DOCTREES_DIR "${Ceres_BINARY_DIR}/docs/doctrees") # Install documentation -install(DIRECTORY ${SPHINX_HTML_DIR} - DESTINATION ${CMAKE_INSTALL_DOCDIR} +install(DIRECTORY "${SPHINX_HTML_DIR}" + DESTINATION "${CMAKE_INSTALL_DOCDIR}" COMPONENT Doc - PATTERN "${SPHINX_HTML_DIR}/*") + OPTIONAL) add_custom_target(ceres_docs ALL "${Sphinx_BUILD_EXECUTABLE}" - -n -a -b html + -n -b html -d "${SPHINX_DOCTREES_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" "${SPHINX_HTML_DIR}"
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 355fc25..c365618 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt
@@ -50,7 +50,7 @@ target_link_libraries(rosenbrock_numeric_diff PRIVATE absl::log_initialize Ceres::ceres) add_executable(curve_fitting_c curve_fitting.c) -target_link_libraries(curve_fitting_c PRIVATE absl::log_initialize Ceres::ceres) +target_link_libraries(curve_fitting_c PRIVATE Ceres::ceres) # Force CMake to link curve_fitting_c using the C++ linker. set_target_properties(curve_fitting_c PROPERTIES LINKER_LANGUAGE CXX) # As this is a C file #including <math.h> we have to explicitly add the math @@ -67,7 +67,7 @@ target_link_libraries(robust_curve_fitting PRIVATE absl::log_initialize Ceres::ceres) add_executable(simple_bundle_adjuster simple_bundle_adjuster.cc) -target_link_libraries(simple_bundle_adjuster PRIVATE absl::log_initialize absl::time Ceres::ceres) +target_link_libraries(simple_bundle_adjuster PRIVATE absl::log_initialize Ceres::ceres) add_executable(bicubic_interpolation bicubic_interpolation.cc) target_link_libraries(bicubic_interpolation PRIVATE absl::log_initialize Ceres::ceres) @@ -100,7 +100,7 @@ add_executable(bundle_adjuster bundle_adjuster.cc bal_problem.cc) -target_link_libraries(bundle_adjuster PRIVATE absl::log_initialize absl::flags absl::flags_parse Ceres::ceres) +target_link_libraries(bundle_adjuster PRIVATE absl::log_initialize absl::flags absl::flags_parse absl::time Ceres::ceres) if (BUILD_TESTING) add_executable(bal_problem_test
diff --git a/examples/slam/CMakeLists.txt b/examples/slam/CMakeLists.txt index d0b03d7..223a32c 100644 --- a/examples/slam/CMakeLists.txt +++ b/examples/slam/CMakeLists.txt
@@ -27,7 +27,6 @@ # POSSIBILITY OF SUCH DAMAGE. # # Author: vitus@google.com (Michael Vitus) -include_directories(./) add_subdirectory(pose_graph_2d) add_subdirectory(pose_graph_3d)
diff --git a/examples/slam/pose_graph_2d/CMakeLists.txt b/examples/slam/pose_graph_2d/CMakeLists.txt index c6cfb80..a36a886 100644 --- a/examples/slam/pose_graph_2d/CMakeLists.txt +++ b/examples/slam/pose_graph_2d/CMakeLists.txt
@@ -34,4 +34,5 @@ pose_graph_2d.cc pose_graph_2d_error_term.h types.h) +target_include_directories(pose_graph_2d PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..") target_link_libraries(pose_graph_2d PRIVATE absl::flags absl::flags_parse absl::log absl::check absl::log_initialize Ceres::ceres)
diff --git a/examples/slam/pose_graph_3d/CMakeLists.txt b/examples/slam/pose_graph_3d/CMakeLists.txt index d1fbae6..af62bc6 100644 --- a/examples/slam/pose_graph_3d/CMakeLists.txt +++ b/examples/slam/pose_graph_3d/CMakeLists.txt
@@ -29,4 +29,5 @@ # Author: vitus@google.com (Michael Vitus) add_executable(pose_graph_3d pose_graph_3d.cc) +target_include_directories(pose_graph_3d PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..") target_link_libraries(pose_graph_3d PRIVATE absl::flags absl::flags_parse absl::log absl::check absl::log_initialize Ceres::ceres)
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt index 03150e3..dad7a25 100644 --- a/internal/ceres/CMakeLists.txt +++ b/internal/ceres/CMakeLists.txt
@@ -108,8 +108,6 @@ SKIP_UNITY_BUILD_INCLUSION ON) if (_Ceres_FEATURE_SUITESPARSE) - # Define version information for use in Solver::FullReport. - add_definitions(-DCERES_SUITESPARSE_VERSION="${SuiteSparse_VERSION}") list(APPEND CERES_LIBRARY_PRIVATE_DEPENDENCIES SuiteSparse::CHOLMOD SuiteSparse::SPQR) # SuiteSparse::Partition only signals METIS support in CHOLMOD. Linking it @@ -120,8 +118,6 @@ if (_Ceres_FEATURE_EIGEN_METIS OR (_Ceres_FEATURE_CHOLMOD_PARTITION AND SuiteSparse_CHOLMOD_NEEDS_EXTERNAL_METIS)) - # Define version information for use in Solver::FullReport. - add_definitions(-DCERES_METIS_VERSION="${METIS_VERSION}") list(APPEND CERES_LIBRARY_PRIVATE_DEPENDENCIES METIS::METIS) endif () @@ -319,6 +315,22 @@ PUBLIC ${CERES_LIBRARY_PUBLIC_DEPENDENCIES} PRIVATE ${CERES_LIBRARY_PRIVATE_DEPENDENCIES}) +if (NOT _Ceres_FEATURE_EIGEN_SPARSE) + target_compile_definitions(ceres PRIVATE EIGEN_MPL2_ONLY) +endif() +if (_Ceres_FEATURE_SUITESPARSE) + # Define version information for use in Solver::FullReport. + target_compile_definitions(ceres PRIVATE + CERES_SUITESPARSE_VERSION="${SuiteSparse_VERSION}") +endif() +if (_Ceres_FEATURE_EIGEN_METIS OR + (_Ceres_FEATURE_CHOLMOD_PARTITION AND + SuiteSparse_CHOLMOD_NEEDS_EXTERNAL_METIS)) + # Define version information for use in Solver::FullReport. + target_compile_definitions(ceres PRIVATE + CERES_METIS_VERSION="${METIS_VERSION}") +endif() + # Add the Ceres headers to its target. # # Force the location containing the configured config.h to the front of the @@ -384,7 +396,7 @@ endif (_Ceres_GTEST_TYPE STREQUAL SHARED_LIBRARY) unset(_Ceres_GTEST_TYPE) - macro (CERES_TEST NAME) + function (CERES_TEST NAME) add_executable(${NAME}_test ${NAME}_test.cc) # Pull in local headers from the generated test directories when ceres_test() @@ -407,7 +419,7 @@ endif (TARGET SuiteSparse::SPQR) add_test(NAME ${NAME}_test COMMAND ${NAME}_test) - endmacro (CERES_TEST) + endfunction (CERES_TEST) ceres_test(accurate_norm) ceres_test(array_utils) @@ -519,7 +531,7 @@ endif (BUILD_TESTING) -macro(add_dependencies_to_benchmark BENCHMARK_TARGET) +function(add_dependencies_to_benchmark BENCHMARK_TARGET) target_include_directories(${BENCHMARK_TARGET} PRIVATE ${Ceres_SOURCE_DIR}/internal) # Benchmarks include direct references/includes of private @@ -534,7 +546,7 @@ target_compile_options(${BENCHMARK_TARGET} PRIVATE "-mllvm" "-inlinehint-threshold=1000000" "-mno-outline") endif() -endmacro() +endfunction() if (_Ceres_FEATURE_BENCHMARKS) add_executable(evaluation_benchmark evaluation_benchmark.cc)