I have encountered a problem where nlohmann_json is being included in the project in two different ways, which might lead to conflicts or duplication. Currently, nlohmann_json is being fetched directly and also set up manually as an external dependency. I would like to consolidate this setup to use an option that allows toggling between fetching the library and using an external version.
Current Configuration (mine on top-level)
Fetching nlohmann_json using FetchContent
In the top-level CMakeLists.txt, nlohmann_json is fetched from GitHub using FetchContent if it is not already a target:
In the him_add_nlohmann_json function, nlohmann_json is manually added as an external dependency if it is not found via find_package:
###################################################################################################
# Add nlohmann_json: API = him_add_nlohmann_json
###################################################################################################
function(him_add_nlohmann_json)
find_package(nlohmann_json CONFIG QUIET)
if(nlohmann_json_FOUND)
message(STATUS "HelloImGui: Using nlohmann_json from find_package(nlohmann_json)")
target_link_libraries(${HELLOIMGUI_TARGET} PUBLIC nlohmann_json::nlohmann_json)
set(HELLOIMGUI_NLOHMANN_JSON_SELECTED_INFO "Found via find_package(nlohmann_json)" CACHE INTERNAL "" FORCE)
else()
message(STATUS "HelloImGui: Using nlohmann_json from external/nlohmann_json")
set(nlohmann_json_dir ${HELLOIMGUI_BASEPATH}/external/nlohmann_json)
add_library(nlohmann_json INTERFACE)
target_include_directories(nlohmann_json INTERFACE $<BUILD_INTERFACE:${nlohmann_json_dir}>)
# target_compile_definitions(nlohmann_json INTERFACE NLOHMANN_JSON_NOEXCEPTION)
target_link_libraries(${HELLOIMGUI_TARGET} PUBLIC nlohmann_json)
set(HELLOIMGUI_NLOHMANN_JSON_SELECTED_INFO "Using external/nlohmann_json" CACHE INTERNAL "" FORCE)
him_add_installable_dependency(nlohmann_json)
if(HELLOIMGUI_INSTALL)
install(FILES ${nlohmann_json_dir}/nlohmann/json.hpp DESTINATION include/nlohmann/json.hpp)
install(FILES ${nlohmann_json_dir}/nlohmann/json_fwd.hpp DESTINATION include/nlohmann/json_fwd.hpp)
endif()
endif()
endfunction()
Proposed Solution
To resolve the duplication issue and make it easier to manage the inclusion of nlohmann_json, I propose introducing a CMake option to choose between using the fetched nlohmann_json or the external version. This option will allow users to specify their preference.
Issue Description
I have encountered a problem where nlohmann_json is being included in the project in two different ways, which might lead to conflicts or duplication. Currently, nlohmann_json is being fetched directly and also set up manually as an external dependency. I would like to consolidate this setup to use an option that allows toggling between fetching the library and using an external version.
Current Configuration (mine on top-level)
Fetching nlohmann_json using FetchContent In the top-level CMakeLists.txt, nlohmann_json is fetched from GitHub using FetchContent if it is not already a target:
Manual Setup of nlohmann_json
In the him_add_nlohmann_json function, nlohmann_json is manually added as an external dependency if it is not found via find_package:
Proposed Solution To resolve the duplication issue and make it easier to manage the inclusion of nlohmann_json, I propose introducing a CMake option to choose between using the fetched nlohmann_json or the external version. This option will allow users to specify their preference.
Add the following option to the CMakeLists.txt
Is it possible?
Best regards