From 0aebeb9c84086b19c56a3ced5c95c60063ab37e2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 16 Jan 2015 14:11:38 -0500 Subject: FindJsonCpp: Add module to find JsonCpp package --- Help/manual/cmake-modules.7.rst | 1 + Help/module/FindJsonCpp.rst | 1 + Help/release/dev/add-FindJsonCpp.rst | 5 ++ Modules/FindJsonCpp.cmake | 117 ++++++++++++++++++++++++++++++++++ Tests/CMakeLists.txt | 3 + Tests/FindJsonCpp/CMakeLists.txt | 10 +++ Tests/FindJsonCpp/Test/CMakeLists.txt | 14 ++++ Tests/FindJsonCpp/Test/main.cxx | 8 +++ 8 files changed, 159 insertions(+) create mode 100644 Help/module/FindJsonCpp.rst create mode 100644 Help/release/dev/add-FindJsonCpp.rst create mode 100644 Modules/FindJsonCpp.cmake create mode 100644 Tests/FindJsonCpp/CMakeLists.txt create mode 100644 Tests/FindJsonCpp/Test/CMakeLists.txt create mode 100644 Tests/FindJsonCpp/Test/main.cxx diff --git a/Help/manual/cmake-modules.7.rst b/Help/manual/cmake-modules.7.rst index 5196485..0a0ca23 100644 --- a/Help/manual/cmake-modules.7.rst +++ b/Help/manual/cmake-modules.7.rst @@ -125,6 +125,7 @@ All Modules /module/FindJava /module/FindJNI /module/FindJPEG + /module/FindJsonCpp /module/FindKDE3 /module/FindKDE4 /module/FindLAPACK diff --git a/Help/module/FindJsonCpp.rst b/Help/module/FindJsonCpp.rst new file mode 100644 index 0000000..ba87ece --- /dev/null +++ b/Help/module/FindJsonCpp.rst @@ -0,0 +1 @@ +.. cmake-module:: ../../Modules/FindJsonCpp.cmake diff --git a/Help/release/dev/add-FindJsonCpp.rst b/Help/release/dev/add-FindJsonCpp.rst new file mode 100644 index 0000000..5d1cb18 --- /dev/null +++ b/Help/release/dev/add-FindJsonCpp.rst @@ -0,0 +1,5 @@ +add-FindJsonCpp +--------------- + +* A :module:`FindJsonCpp` module was introduced to find the + JsonCpp package. diff --git a/Modules/FindJsonCpp.cmake b/Modules/FindJsonCpp.cmake new file mode 100644 index 0000000..cbb4fb3 --- /dev/null +++ b/Modules/FindJsonCpp.cmake @@ -0,0 +1,117 @@ +#[=======================================================================[.rst: +FindJsonCpp +----------- + +Find JsonCpp includes and library. + +Imported Targets +^^^^^^^^^^^^^^^^ + +An :ref:`imported target ` named +``JsonCpp::JsonCpp`` is provided if JsonCpp has been found. + +Result Variables +^^^^^^^^^^^^^^^^ + +This module defines the following variables: + +``JsonCpp_FOUND`` + True if JsonCpp was found, false otherwise. +``JsonCpp_INCLUDE_DIRS`` + Include directories needed to include JsonCpp headers. +``JsonCpp_LIBRARIES`` + Libraries needed to link to JsonCpp. +``JsonCpp_VERSION_STRING`` + The version of JsonCpp found. + May not be set for JsonCpp versions prior to 1.0. +``JsonCpp_VERSION_MAJOR`` + The major version of JsonCpp. +``JsonCpp_VERSION_MINOR`` + The minor version of JsonCpp. +``JsonCpp_VERSION_PATCH`` + The patch version of JsonCpp. + +Cache Variables +^^^^^^^^^^^^^^^ + +This module uses the following cache variables: + +``JsonCpp_LIBRARY`` + The location of the JsonCpp library file. +``JsonCpp_INCLUDE_DIR`` + The location of the JsonCpp include directory containing ``json/json.h``. + +The cache variables should not be used by project code. +They may be set by end users to point at JsonCpp components. +#]=======================================================================] + +#============================================================================= +# Copyright 2014-2015 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +#----------------------------------------------------------------------------- +find_library(JsonCpp_LIBRARY + NAMES jsoncpp + ) +mark_as_advanced(JsonCpp_LIBRARY) + +find_path(JsonCpp_INCLUDE_DIR + NAMES json/json.h + PATH_SUFFIXES jsoncpp + ) +mark_as_advanced(JsonCpp_INCLUDE_DIR) + +#----------------------------------------------------------------------------- +# Extract version number if possible. +set(_JsonCpp_H_REGEX "^#[ \t]*define[ \t]+JSONCPP_VERSION_STRING[ \t]+\"(([0-9]+)\\.([0-9]+)\\.([0-9]+)[^\"]*)\".*$") +if(JsonCpp_INCLUDE_DIR AND EXISTS "${JsonCpp_INCLUDE_DIR}/json/version.h") + file(STRINGS "${JsonCpp_INCLUDE_DIR}/json/version.h" _JsonCpp_H REGEX "${_JsonCpp_H_REGEX}") +else() + set(_JsonCpp_H "") +endif() +if(_JsonCpp_H MATCHES "${_JsonCpp_H_REGEX}") + set(JsonCpp_VERSION_STRING "${CMAKE_MATCH_1}") + set(JsonCpp_VERSION_MAJOR "${CMAKE_MATCH_2}") + set(JsonCpp_VERSION_MINOR "${CMAKE_MATCH_3}") + set(JsonCpp_VERSION_PATCH "${CMAKE_MATCH_4}") +else() + set(JsonCpp_VERSION_STRING "") + set(JsonCpp_VERSION_MAJOR "") + set(JsonCpp_VERSION_MINOR "") + set(JsonCpp_VERSION_PATCH "") +endif() +unset(_JsonCpp_H_REGEX) +unset(_JsonCpp_H) + +#----------------------------------------------------------------------------- +include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(JsonCpp + FOUND_VAR JsonCpp_FOUND + REQUIRED_VARS JsonCpp_LIBRARY JsonCpp_INCLUDE_DIR + VERSION_VAR JsonCpp_VERSION_STRING + ) +set(JSONCPP_FOUND ${JsonCpp_FOUND}) + +#----------------------------------------------------------------------------- +# Provide documented result variables and targets. +if(JsonCpp_FOUND) + set(JsonCpp_INCLUDE_DIRS ${JsonCpp_INCLUDE_DIR}) + set(JsonCpp_LIBRARIES ${JsonCpp_LIBRARY}) + if(NOT TARGET JsonCpp::JsonCpp) + add_library(JsonCpp::JsonCpp UNKNOWN IMPORTED) + set_target_properties(JsonCpp::JsonCpp PROPERTIES + IMPORTED_LOCATION "${JsonCpp_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${JsonCpp_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + ) + endif() +endif() diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index dea30e4..3aecd9b 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1247,6 +1247,9 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release if(CMake_TEST_FindGSL) add_subdirectory(FindGSL) endif() + if(CMake_TEST_FindJsonCpp) + add_subdirectory(FindJsonCpp) + endif() find_package(GTK2 QUIET) if(GTK2_FOUND) diff --git a/Tests/FindJsonCpp/CMakeLists.txt b/Tests/FindJsonCpp/CMakeLists.txt new file mode 100644 index 0000000..9a1fa38 --- /dev/null +++ b/Tests/FindJsonCpp/CMakeLists.txt @@ -0,0 +1,10 @@ +add_test(NAME FindJsonCpp.Test COMMAND + ${CMAKE_CTEST_COMMAND} -C $ + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindJsonCpp/Test" + "${CMake_BINARY_DIR}/Tests/FindJsonCpp/Test" + ${build_generator_args} + --build-project TestFindJsonCpp + --build-options ${build_options} + --test-command ${CMAKE_CTEST_COMMAND} -V -C $ + ) diff --git a/Tests/FindJsonCpp/Test/CMakeLists.txt b/Tests/FindJsonCpp/Test/CMakeLists.txt new file mode 100644 index 0000000..4e1e271 --- /dev/null +++ b/Tests/FindJsonCpp/Test/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.1) +project(TestFindJsonCpp CXX) +include(CTest) + +find_package(JsonCpp REQUIRED) + +add_executable(test_jsoncpp_tgt main.cxx) +target_link_libraries(test_jsoncpp_tgt JsonCpp::JsonCpp) +add_test(NAME test_jsoncpp_tgt COMMAND test_jsoncpp_tgt) + +add_executable(test_jsoncpp_var main.cxx) +target_include_directories(test_jsoncpp_var PRIVATE ${JsonCpp_INCLUDE_DIRS}) +target_link_libraries(test_jsoncpp_var PRIVATE ${JsonCpp_LIBRARIES}) +add_test(NAME test_jsoncpp_var COMMAND test_jsoncpp_var) diff --git a/Tests/FindJsonCpp/Test/main.cxx b/Tests/FindJsonCpp/Test/main.cxx new file mode 100644 index 0000000..0fefe32 --- /dev/null +++ b/Tests/FindJsonCpp/Test/main.cxx @@ -0,0 +1,8 @@ +#include + +int main() +{ + int zero = 0; + Json::Value value(zero); + return value.asInt(); +} -- cgit v0.12