From 69eddfb6da081489ddb6d8997f6cbc7388c1a025 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sat, 4 Jun 2022 19:28:55 +0200 Subject: [PATCH] Use find module to link against OpenMP Depending on the compiler in use, linking against OpenMP may require passing specific compiler flags instead of linking against a library. Use the CMake OpenMP find module to abstract OpenMP activation. Change-Id: Ib43f576ac12e2c5e9598e9586df3dfa018e9c08b --- cmake/CeresConfig.cmake.in | 1 + internal/ceres/CMakeLists.txt | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cmake/CeresConfig.cmake.in b/cmake/CeresConfig.cmake.in index 028eabb93..06f0fddc8 100644 --- a/cmake/CeresConfig.cmake.in +++ b/cmake/CeresConfig.cmake.in @@ -180,6 +180,7 @@ find_dependency(Threads) # Optional dependencies @SuiteSparse_DEPENDENCY@ +@OpenMP_DEPENDENCY@ # As imported CMake targets are not re-exported when a dependent target is # exported, we must invoke find_package(XXX) here to reload the definition diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt index ace75db95..d6babfb0f 100644 --- a/internal/ceres/CMakeLists.txt +++ b/internal/ceres/CMakeLists.txt @@ -33,11 +33,14 @@ if (CERES_THREADING_MODEL STREQUAL "CXX_THREADS") set(CERES_PARALLEL_FOR_SRC parallel_for_cxx.cc thread_pool.cc) elseif (CERES_THREADING_MODEL STREQUAL "OPENMP") + # OpenMP requires linking to the corresponding library. + find_package(OpenMP REQUIRED COMPONENTS CXX) + set(CERES_PARALLEL_FOR_SRC parallel_for_openmp.cc) - if (CMAKE_COMPILER_IS_GNUCXX) - # OpenMP in GCC requires the GNU OpenMP library. - list(APPEND CERES_LIBRARY_PRIVATE_DEPENDENCIES gomp) - endif() + list(APPEND CERES_LIBRARY_PRIVATE_DEPENDENCIES OpenMP::OpenMP_CXX) + + # Make dependency visible to the parent CMakeLists.txt + set(OpenMP_DEPENDENCY "find_dependency (OpenMP ${OpenMP_CXX_VERSION} COMPONENTS CXX)" PARENT_SCOPE) elseif (CERES_THREADING_MODEL STREQUAL "NO_THREADS") set(CERES_PARALLEL_FOR_SRC parallel_for_nothreads.cc) endif()