From 0762d92e514fe57ec764aea40fcbd0c8da2fc961 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Mon, 25 Sep 2023 16:21:18 -0400 Subject: [PATCH] COMP: Fix standalone build updating JsonCpp integration For sake of consistency with how Slicer integrates JsonCpp, we ignore the "jsoncppConfig.cmake" file provided by jsoncpp CMake project and instead set JsonCpp_INCLUDE_DIR and JsonCpp_LIBRARY expected by the "FindJsonCpp" CMake module. To streamline distribution of the standalone Autoscoper executable, update JsonCpp project to statically build the JsonCpp library. --- CMake/FindJsonCpp.cmake | 40 +++++++++++++++++++++++++++++++ Superbuild/External_JsonCpp.cmake | 25 ++++++++++++------- libautoscoper/CMakeLists.txt | 3 ++- 3 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 CMake/FindJsonCpp.cmake diff --git a/CMake/FindJsonCpp.cmake b/CMake/FindJsonCpp.cmake new file mode 100644 index 00000000..7115c8d7 --- /dev/null +++ b/CMake/FindJsonCpp.cmake @@ -0,0 +1,40 @@ +# Find the JsonCpp include files and library. +# +# JsonCpp is a C++ library that can read/write JSON (JavaScript Object Notation) +# documents. See http://jsoncpp.sourceforge.net/ for more details. +# +# This module defines: +# JsonCpp_INCLUDE_DIRS - where to find json/json.h +# JsonCpp_LIBRARIES - the libraries to link against to use JsonCpp +# JsonCpp_FOUND - if false the library was not found. + +find_path(JsonCpp_INCLUDE_DIR "json/json.h" + PATH_SUFFIXES "jsoncpp" + DOC "Specify the JsonCpp include directory here") + +find_library(JsonCpp_LIBRARY + NAMES jsoncpp + PATHS + DOC "Specify the JsonCpp library here") +set(JsonCpp_INCLUDE_DIRS ${JsonCpp_INCLUDE_DIR}) +set(JsonCpp_LIBRARIES "${JsonCpp_LIBRARY}") + +set(_JsonCpp_version_args) +if (EXISTS "${JsonCpp_INCLUDE_DIR}/json/version.h") + file(STRINGS "${JsonCpp_INCLUDE_DIR}/json/version.h" _JsonCpp_version_contents REGEX "JSONCPP_VERSION_[A-Z]+") + foreach (_JsonCpp_version_part MAJOR MINOR PATCH) + string(REGEX REPLACE ".*# *define +JSONCPP_VERSION_${_JsonCpp_version_part} +([0-9]+).*" + "\\1" JsonCpp_VERSION_${_JsonCpp_version_part} "${_JsonCpp_version_contents}") + endforeach () + + set(JsonCpp_VERSION_STRING "${JsonCpp_VERSION_MAJOR}.${JsonCpp_VERSION_MINOR}.${JsonCpp_VERSION_PATCH}") + + set(_JsonCpp_version_args VERSION_VAR JsonCpp_VERSION_STRING) +endif () + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(JsonCpp + REQUIRED_VARS JsonCpp_LIBRARIES JsonCpp_INCLUDE_DIRS + ${_JsonCpp_version_args}) + +mark_as_advanced(JsonCpp_INCLUDE_DIR JsonCpp_LIBRARY) diff --git a/Superbuild/External_JsonCpp.cmake b/Superbuild/External_JsonCpp.cmake index cbe0f229..4531a888 100644 --- a/Superbuild/External_JsonCpp.cmake +++ b/Superbuild/External_JsonCpp.cmake @@ -47,8 +47,8 @@ if((NOT DEFINED JsonCpp_INCLUDE_DIR -DJSONCPP_WITH_WARNING_AS_ERROR:BOOL=OFF -DJSONCPP_WITH_PKGCONFIG_SUPPORT:BOOL=OFF -DJSONCPP_WITH_CMAKE_PACKAGE:BOOL=ON - -DBUILD_SHARED_LIBS:BOOL=ON - -DBUILD_STATIC_LIBS:BOOL=OFF + -DBUILD_SHARED_LIBS:BOOL=OFF + -DBUILD_STATIC_LIBS:BOOL=ON # Install directories -DCMAKE_INSTALL_PREFIX:PATH= -DCMAKE_INSTALL_BINDIR:STRING=${Autoscoper_BIN_DIR} @@ -57,10 +57,19 @@ if((NOT DEFINED JsonCpp_INCLUDE_DIR DEPENDS ${${proj}_DEPENDENCIES} ) - set(JSONCPP_DIR ${EP_INSTALL_DIR}) - set(JSONCPP_SOURCE_DIR ${EP_SOURCE_DIR}) - set(JSONCPP_INCLUDE_DIR ${JSONCPP_DIR}/include) - set(JSONCPP_LIBRARY ${JSONCPP_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}jsoncpp${CMAKE_SHARED_LIBRARY_SUFFIX}) - - mark_as_superbuild(JSONCPP_DIR JSONCPP_SOURCE_DIR JSONCPP_INCLUDE_DIR JSONCPP_LIBRARY) + set(${proj}_INCLUDE_DIR ${EP_INSTALL_DIR}/include) + set(${proj}_LIBRARY ${EP_INSTALL_DIR}/${Autoscoper_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}jsoncpp${CMAKE_STATIC_LIBRARY_SUFFIX}) +else() + ExternalProject_Add_Empty(${proj} DEPENDS ${${proj}_DEPENDENCIES}) endif() + +# For sake of consistency with how Slicer integrates JsonCpp, we ignore the "jsoncppConfig.cmake" file provided by jsoncpp and +# instead set JsonCpp_INCLUDE_DIR and JsonCpp_LIBRARY expected by the "FindJsonCpp" CMake module. +mark_as_superbuild( + VARS + ${proj}_INCLUDE_DIR:PATH + ${proj}_LIBRARY:FILEPATH +) + +ExternalProject_Message(${proj} "${proj}_INCLUDE_DIR:${${proj}_INCLUDE_DIR}") +ExternalProject_Message(${proj} "${proj}_LIBRARY:${${proj}_LIBRARY}") diff --git a/libautoscoper/CMakeLists.txt b/libautoscoper/CMakeLists.txt index c44b55db..5cf231c7 100644 --- a/libautoscoper/CMakeLists.txt +++ b/libautoscoper/CMakeLists.txt @@ -106,7 +106,8 @@ target_link_libraries(libautoscoper PUBLIC TIFF::TIFF) target_compile_definitions(libautoscoper PUBLIC -DUSE_LIBTIFF) find_package(JsonCpp REQUIRED) -target_link_libraries(libautoscoper PUBLIC JsonCpp::JsonCpp) +target_link_libraries(libautoscoper PRIVATE ${JsonCpp_LIBRARY}) +target_include_directories(libautoscoper PRIVATE ${JsonCpp_INCLUDE_DIR}) target_include_directories(libautoscoper PUBLIC ${CMAKE_CURRENT_BINARY_DIR}