project_options icon indicating copy to clipboard operation
project_options copied to clipboard

Library options are not exported to calling projects

Open pmatalon opened this issue 1 year ago • 8 comments

I have a header-only library project, and I am using project_options to package it, so that it can be used as a dependency for other projects. The library has numerous options, defined with the cmake functions option or set. What I want is to export those options, so that calling projects can configure them. Here is the skeleton of its CMakeLists.txt, with the options WITH_OPENMP and CHECK_NAN:

project(my_lib LANGUAGES CXX)

(...) 

project_options(
  ENABLE_CACHE
  # ...other arguments
)

add_library(my_lib INTERFACE)
target_link_libraries(my_lib INTERFACE project_options project_warnings)

set(INCLUDE_DIR "include")
target_include_directories(my_lib INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${INCLUDE_DIR}>"
                          "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

# dependencies
set(DEPENDENCIES_CONFIGURED pugixml fmt)
foreach(DEPENDENCY ${DEPENDENCIES_CONFIGURED})
  find_package(${DEPENDENCY} CONFIG REQUIRED)
endforeach()
target_link_system_libraries(my_lib INTERFACE pugixml::pugixml fmt::fmt)

# optional dependency
option(WITH_OPENMP "Enable OpenMP" OFF)
if(${WITH_OPENMP})
  find_package(OpenMP)
  target_link_libraries(my_lib INTERFACE OpenMP::OpenMP_CXX)
  target_compile_definitions(my_lib INTERFACE MYLIB_WITH_OPENMP)
endif()

# another option
option(CHECK_NAN "Check if NaN occur in computations" OFF)
if(CHECK_NAN)
  target_compile_definitions(my_lib INTERFACE MYLIB_CHECK_NAN)
endif()

(...)

# packaging of the project
package_project(
  TARGETS my_lib project_options project_warnings libdeps
  INTERFACE_DEPENDENCIES_CONFIGURED ${DEPENDENCIES_CONFIGURED}
  INTERFACE_INCLUDES ${INCLUDE_DIR}
)

Now, another project has my_lib as a dependency. Here is its CMakeLists.txt:

project(caller_project CXX)
find_package(my_lib CONFIG REQUIRED)
add_executable(caller_project main.cpp)
target_link_libraries(caller_project PRIVATE my_lib::my_lib my_lib::project_options my_lib::project_warnings my_lib::libdeps)

# How do I set the options WITH_OPENMP and CHECK_NAN of `my_lib`?

The compilation of caller_project works fine with this CMakeLists.txt. My problem is that I can't set my_lib's options. I think the options are simply not exported, since I don't see them when I use the ccmake command. (On the other hand, if my_lib's dependencies have options themselves, I can see them in ccmake.) I tried using dynamic_project_options with and without ENABLE_CACHE, but no luck... Thank you for your support.

pmatalon avatar Sep 03 '24 09:09 pmatalon