Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ endif()
# Build the different applications
add_subdirectory(src)

# Verify the installed CMake package exposes usable namespaced targets.
# cmake --install requires CMake 3.15, so the test is skipped on older versions.
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15 AND JSONCPP_WITH_TESTS AND JSONCPP_WITH_CMAKE_PACKAGE AND JSONCPP_WITH_INSTALL AND NOT CMAKE_CROSSCOMPILING)
add_test(NAME jsoncpp_cmake_package_namespaced_targets
COMMAND ${CMAKE_COMMAND}
"-DJSONCPP_BUILD_DIR=${CMAKE_BINARY_DIR}"
"-DJSONCPP_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
"-DEXPECT_SHARED_LIBS=${BUILD_SHARED_LIBS}"
"-DEXPECT_STATIC_LIBS=${BUILD_STATIC_LIBS}"
"-DJSONCPP_STATIC_WINDOWS_RUNTIME=${JSONCPP_STATIC_WINDOWS_RUNTIME}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/test/cmake/verify_package_namespaced_targets.cmake")
endif()

#install the includes
add_subdirectory(include)

Expand Down
27 changes: 23 additions & 4 deletions jsoncpp-namespaced-targets.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
if (TARGET jsoncpp_lib)
if (NOT TARGET JsonCpp::JsonCppShared)
add_library(JsonCpp::JsonCppShared INTERFACE IMPORTED)
set_target_properties(JsonCpp::JsonCppShared PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_lib")
endif ()
endif ()

if (TARGET jsoncpp_static)
if (NOT TARGET JsonCpp::JsonCppStatic)
add_library(JsonCpp::JsonCppStatic INTERFACE IMPORTED)
set_target_properties(JsonCpp::JsonCppStatic PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_static")
endif ()
endif ()

if (NOT TARGET JsonCpp::JsonCpp)
if (TARGET jsoncpp_static)
if (TARGET JsonCpp::JsonCppShared)
add_library(JsonCpp::JsonCpp INTERFACE IMPORTED)
set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_static")
elseif (TARGET jsoncpp_lib)
set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "JsonCpp::JsonCppShared")
elseif (TARGET JsonCpp::JsonCppStatic)
add_library(JsonCpp::JsonCpp INTERFACE IMPORTED)
set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_lib")
set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "JsonCpp::JsonCppStatic")
endif ()
endif ()

if (NOT TARGET JsonCpp::jsoncpp AND TARGET JsonCpp::JsonCpp)
add_library(JsonCpp::jsoncpp INTERFACE IMPORTED)
set_target_properties(JsonCpp::jsoncpp PROPERTIES INTERFACE_LINK_LIBRARIES "JsonCpp::JsonCpp")
endif ()
55 changes: 55 additions & 0 deletions test/cmake/consumer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
cmake_minimum_required(VERSION 3.10)

# Match the MSVC runtime library used to build jsoncpp when it was compiled with
# JSONCPP_STATIC_WINDOWS_RUNTIME, otherwise linking fails with LNK2038.
if(JSONCPP_STATIC_WINDOWS_RUNTIME)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

project(jsoncpp_package_consumer CXX)

find_package(jsoncpp REQUIRED)

set(_expected_targets JsonCpp::JsonCpp JsonCpp::jsoncpp)
if(EXPECT_SHARED_LIBS)
list(APPEND _expected_targets JsonCpp::JsonCppShared)
endif()
if(EXPECT_STATIC_LIBS)
list(APPEND _expected_targets JsonCpp::JsonCppStatic)
endif()

foreach(_target IN LISTS _expected_targets)
if(NOT TARGET ${_target})
message(FATAL_ERROR "find_package(jsoncpp) did not provide ${_target}")
endif()
endforeach()

if(EXPECT_SHARED_LIBS)
get_target_property(_shared_link JsonCpp::JsonCppShared INTERFACE_LINK_LIBRARIES)
if(NOT _shared_link STREQUAL "jsoncpp_lib")
message(FATAL_ERROR "JsonCpp::JsonCppShared links '${_shared_link}', expected 'jsoncpp_lib'")
endif()
set(_expected_default JsonCpp::JsonCppShared)
else()
set(_expected_default JsonCpp::JsonCppStatic)
endif()

get_target_property(_default_link JsonCpp::JsonCpp INTERFACE_LINK_LIBRARIES)
if(NOT _default_link STREQUAL "${_expected_default}")
message(FATAL_ERROR "JsonCpp::JsonCpp links '${_default_link}', expected '${_expected_default}'")
endif()

get_target_property(_lowercase_link JsonCpp::jsoncpp INTERFACE_LINK_LIBRARIES)
if(NOT _lowercase_link STREQUAL "JsonCpp::JsonCpp")
message(FATAL_ERROR "JsonCpp::jsoncpp links '${_lowercase_link}', expected 'JsonCpp::JsonCpp'")
endif()

if(EXPECT_STATIC_LIBS)
get_target_property(_static_link JsonCpp::JsonCppStatic INTERFACE_LINK_LIBRARIES)
if(NOT _static_link STREQUAL "jsoncpp_static")
message(FATAL_ERROR "JsonCpp::JsonCppStatic links '${_static_link}', expected 'jsoncpp_static'")
endif()
endif()

add_executable(jsoncpp_package_consumer main.cpp)
target_link_libraries(jsoncpp_package_consumer PRIVATE JsonCpp::JsonCpp)
10 changes: 10 additions & 0 deletions test/cmake/consumer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <json/json.h>

#include <iostream>

int main() {
Json::Value root;
root["answer"] = 42;
std::cout << root.toStyledString();
return root["answer"].asInt() == 42 ? 0 : 1;
}
90 changes: 90 additions & 0 deletions test/cmake/verify_package_namespaced_targets.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
cmake_minimum_required(VERSION 3.10)

foreach(_required_var JSONCPP_BUILD_DIR JSONCPP_SOURCE_DIR)
if(NOT DEFINED ${_required_var})
message(FATAL_ERROR "verify_package_namespaced_targets.cmake requires -D${_required_var}")
endif()
endforeach()

set(PREFIX "${JSONCPP_BUILD_DIR}/package-test-prefix")
set(CONSUMER_SOURCE "${JSONCPP_BUILD_DIR}/package-test-consumer")
set(CONSUMER_BUILD "${JSONCPP_BUILD_DIR}/package-test-consumer-build")

file(REMOVE_RECURSE "${PREFIX}" "${CONSUMER_SOURCE}" "${CONSUMER_BUILD}")

# Multi-config generators (e.g. Visual Studio) need the configuration that ctest
# selected via -C, exposed here as CTEST_CONFIGURATION_TYPE.
set(_build_config "$ENV{CTEST_CONFIGURATION_TYPE}")

set(_install_args
--install "${JSONCPP_BUILD_DIR}"
--prefix "${PREFIX}")
if(_build_config)
list(APPEND _install_args --config "${_build_config}")
endif()
execute_process(
COMMAND "${CMAKE_COMMAND}" ${_install_args}
RESULT_VARIABLE _install_result
OUTPUT_VARIABLE _install_output
ERROR_VARIABLE _install_error)
if(NOT _install_result EQUAL 0)
message(FATAL_ERROR "cmake --install failed:\n${_install_error}${_install_output}")
endif()

set(_consumer_cmake "${JSONCPP_SOURCE_DIR}/test/cmake/consumer/CMakeLists.txt")
set(_consumer_main "${JSONCPP_SOURCE_DIR}/test/cmake/consumer/main.cpp")
file(COPY "${_consumer_cmake}" DESTINATION "${CONSUMER_SOURCE}")
file(COPY "${_consumer_main}" DESTINATION "${CONSUMER_SOURCE}")

execute_process(
COMMAND "${CMAKE_COMMAND}" -S "${CONSUMER_SOURCE}" -B "${CONSUMER_BUILD}"
"-DCMAKE_PREFIX_PATH=${PREFIX}"
"-DEXPECT_SHARED_LIBS=${EXPECT_SHARED_LIBS}"
"-DEXPECT_STATIC_LIBS=${EXPECT_STATIC_LIBS}"
Comment on lines +40 to +43

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Windows runtime settings are lost

With BUILD_SHARED_LIBS=OFF and JSONCPP_STATIC_WINDOWS_RUNTIME=ON, JsonCpp is built with /MT, but this fresh consumer uses the default /MD. Linking the static library then fails with a runtime mismatch.

Pass the runtime choice into the consumer and apply it with the appropriate CMake policy before creating its target.

"-DJSONCPP_STATIC_WINDOWS_RUNTIME=${JSONCPP_STATIC_WINDOWS_RUNTIME}"
RESULT_VARIABLE _configure_result
OUTPUT_VARIABLE _configure_output
ERROR_VARIABLE _configure_error)
if(NOT _configure_result EQUAL 0)
message(FATAL_ERROR "consumer configure failed:\n${_configure_error}${_configure_output}")
endif()

set(_build_args --build "${CONSUMER_BUILD}")
if(_build_config)
list(APPEND _build_args --config "${_build_config}")
endif()
execute_process(
COMMAND "${CMAKE_COMMAND}" ${_build_args}
RESULT_VARIABLE _build_result
OUTPUT_VARIABLE _build_output
ERROR_VARIABLE _build_error)
if(NOT _build_result EQUAL 0)
message(FATAL_ERROR "consumer build failed:\n${_build_error}${_build_output}")
endif()

# Multi-config generators place the executable in a per-config subdirectory.
set(_exe_search_paths "${CONSUMER_BUILD}")
if(_build_config)
list(APPEND _exe_search_paths "${CONSUMER_BUILD}/${_build_config}")
endif()
find_program(_consumer_exe NAMES jsoncpp_package_consumer PATHS ${_exe_search_paths} NO_DEFAULT_PATH)
if(NOT _consumer_exe)
message(FATAL_ERROR "consumer executable not found in ${CONSUMER_BUILD}")
endif()

# On Windows the shared library is installed under <prefix>/bin, which is not on
# PATH, so the consumer executable cannot load the DLL unless we add it.
if(WIN32 AND EXPECT_SHARED_LIBS)
set(ENV{PATH} "${PREFIX}/bin;${ENV{PATH}}")
endif()

execute_process(
COMMAND "${_consumer_exe}"
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
RESULT_VARIABLE _run_result
OUTPUT_VARIABLE _run_output
ERROR_VARIABLE _run_error)
if(NOT _run_result EQUAL 0)
message(FATAL_ERROR "consumer run failed:\n${_run_error}${_run_output}")
endif()

message(STATUS "namespaced target verification output: ${_run_output}")
Loading