Skip to content

Commit

Permalink
COMP: Fix standalone build updating JsonCpp integration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jcfr committed Sep 25, 2023
1 parent 38cc22c commit 0762d92
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
40 changes: 40 additions & 0 deletions CMake/FindJsonCpp.cmake
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 17 additions & 8 deletions Superbuild/External_JsonCpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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=<INSTALL_DIR>
-DCMAKE_INSTALL_BINDIR:STRING=${Autoscoper_BIN_DIR}
Expand All @@ -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}")
3 changes: 2 additions & 1 deletion libautoscoper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 0762d92

Please sign in to comment.