From 8a7d1d3fde6959af0bfa9fe4c3e87b1a0c5b3afd Mon Sep 17 00:00:00 2001 From: Luke Kersting Date: Thu, 17 Oct 2019 15:47:57 -0600 Subject: [PATCH] Adapt CMake project name to be coherent with nlohmann::json's naming Now when json-schema-validator is installed CMake config files are installed in the lib/cmake/json-schema-validator directory. The install json-schema-validatorTargets.cmake file properly imports the json-hpp and json-schema-validator libraries. The install json-schema-validatorConfig.cmake file is used by CMake find_package function to include the json-schema-validatorTargets.cmake file and to set the variable JSON_SCHEMA_VALIDATOR_INCLUDE_DIRS to the install include directory. To use find_package to find the json-schema-validator simply include. A new test (test_cmake_install) has been added. When NLohmann's JSON is install with CMake, it follows a certain naming convention. As we learned to do proper CMake-install thanks to @lkersting's work this project now adapts to the way NLohmann is doing it. Namely: - json-schema.hpp is now located (and installed) in a nlohmann/-subdirectory - the CMake library and project's name is now nlohmann_json_schema_validator Instead of doing non-standard acrobatics to find the json.hpp now find_package is used in order to find NLohmann's package Co-Authored-By: Patrick Boettcher --- .travis.yml | 2 +- CMakeLists.txt | 182 +++++++++++------- README.md | 143 ++++++++++---- app/json-schema-validate.cpp | 2 +- app/readme.cpp | 2 +- nlohmann_json_schema_validatorConfig.cmake.in | 13 ++ src/json-uri.cpp | 2 +- src/json-validator.cpp | 2 +- src/{ => nlohmann}/json-schema.hpp | 0 src/string-format-check.cpp | 2 +- test/CMakeLists.txt | 10 +- test/JSON-Schema-Test-Suite/CMakeLists.txt | 2 +- .../json-schema-test.cpp | 2 +- test/cmake-install/CMakeLists.txt | 15 ++ test/cmake-install/project/CMakeLists.txt | 34 ++++ test/cmake-install/test.sh.in | 55 ++++++ test/errors.cpp | 2 +- test/id-ref.cpp | 2 +- test/issue-70-root-schema-constructor.cpp | 2 +- test/issue-70.cpp | 2 +- test/string-format-check-test.cpp | 2 +- test/uri.cpp | 2 +- 22 files changed, 359 insertions(+), 121 deletions(-) create mode 100644 nlohmann_json_schema_validatorConfig.cmake.in rename src/{ => nlohmann}/json-schema.hpp (100%) create mode 100644 test/cmake-install/CMakeLists.txt create mode 100644 test/cmake-install/project/CMakeLists.txt create mode 100755 test/cmake-install/test.sh.in diff --git a/.travis.yml b/.travis.yml index 9bd8a92..11dd4c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -64,6 +64,6 @@ script: # compile and execute unit tests - mkdir -p build && cd build - - cmake .. -DNLOHMANN_JSON_DIR=.. ${CMAKE_OPTIONS} -GNinja && cmake --build . --config Release + - cmake .. -Dnlohmann_json_DIR=.. ${CMAKE_OPTIONS} -GNinja && cmake --build . --config Release - ctest -C Release -V -j - cd .. diff --git a/CMakeLists.txt b/CMakeLists.txt index 5db783e..80a77ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,78 +1,98 @@ -project(json-schema-validator +project(nlohmann_json_schema_validator LANGUAGES CXX) -set(PROJECT_VERSION 2.0.0) +set(PROJECT_VERSION 2.1.0) cmake_minimum_required(VERSION 3.2) option(BUILD_TESTS "Build tests" ON) option(BUILD_EXAMPLES "Build examples" ON) -# if used as a subdirectory just define a json-hpp-target as add_library(json-hpp INTERFACE) -# and associate the path to json.hpp via target_include_directories() -if(NOT TARGET json-hpp) - set(NLOHMANN_JSON_DIR "" CACHE STRING "path to json.hpp") - - # find nlohmann's json.hpp - find_path(JSON_HPP nlohmann/json.hpp - PATHS - ${NLOHMANN_JSON_DIR} - ${CMAKE_BINARY_DIR}/${NLOHMANN_JSON_DIR}) # in case it is a relative path - - # get the full, real path - get_filename_component(NLOHMANN_JSON_REALPATH ${JSON_HPP} REALPATH) - - if(NOT EXISTS ${NLOHMANN_JSON_REALPATH}/nlohmann/json.hpp) - message(FATAL_ERROR "please set NLOHMANN_JSON_DIR to a path in which NLohmann's json.hpp can be found. Looking for nlohmann/json.hpp in '${NLOHMANN_JSON_REALPATH}") - endif() - - # create an interface-library for simple cmake-linking - add_library(json-hpp INTERFACE) - target_include_directories(json-hpp - INTERFACE - ${NLOHMANN_JSON_REALPATH}) -endif() - -# and one for the validator -add_library(json-schema-validator +# the library +add_library(nlohmann_json_schema_validator src/json-schema-draft7.json.cpp src/json-uri.cpp src/json-validator.cpp src/string-format-check.cpp) -set_target_properties(json-schema-validator +target_include_directories(nlohmann_json_schema_validator + PUBLIC + $ + $) + +target_compile_features(nlohmann_json_schema_validator + PUBLIC + cxx_range_for) # for C++11 - flags + +set_target_properties(nlohmann_json_schema_validator PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1) -install(TARGETS json-schema-validator - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - RUNTIME DESTINATION bin) +# here we decice how nlohmann::json is found and used to build this project -install(DIRECTORY src/ - DESTINATION include - FILES_MATCHING PATTERN "*.h*") +# first, check whether a nlohmann_json::nlohmann_json target exists already +# -> we are used as a sub-directory from within another project +if(TARGET nlohmann_json::nlohmann_json) + message(STATUS "Found nlohmann_json::nlohmann_json-target - linking with it") + target_link_libraries( + nlohmann_json_schema_validator + PUBLIC nlohmann_json::nlohmann_json) -target_include_directories(json-schema-validator - PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src) +elseif(TARGET nlohmann_json) # or nlohmann_json, we are used a sub-project next to nlohmann-json's git repo + message(STATUS "Found nlohmann_json-target - linking with it") + target_link_libraries( + nlohmann_json_schema_validator + PUBLIC nlohmann_json) +else() + if (NOT IS_ABSOLUTE ${nlohmann_json_DIR}) # make nlohmann_json_DIR absolute + get_filename_component(nlohmann_json_DIR + "${CMAKE_CURRENT_BINARY_DIR}/${nlohmann_json_DIR}" + REALPATH) + endif() -target_compile_features(json-schema-validator - PUBLIC - cxx_range_for) # for C++11 - flags + set(nlohmann_json_orignal_DIR ${nlohmann_json_DIR}) # save path for later use -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - target_compile_options(json-schema-validator + # find nlohmann_json-cmake-package + find_package(nlohmann_json QUIET) + + if(TARGET nlohmann_json::nlohmann_json) + message(STATUS "Found nlohmann_json-cmake-package - linking with it") + target_link_libraries( + nlohmann_json_schema_validator + PUBLIC nlohmann_json::nlohmann_json) + else() + + # find nlohmann/json.hpp + + message(STATUS ${nlohmann_json_orignal_DIR}) + + find_path(JSON_HPP nlohmann/json.hpp + PATHS ${nlohmann_json_orignal_DIR}) + + if(EXISTS ${JSON_HPP}/nlohmann/json.hpp) + message(STATUS "Found nlohmann/json.hpp in given path: ${JSON_HPP}") + target_include_directories( + nlohmann_json_schema_validator + PUBLIC $) + else() + message(FATAL_ERROR "could not find nlohmann/json.hpp or any related cmake-target. Please set nlohmann_json_DIR.") + endif() + + # nlohmann_json_DIR has to be reset (for later use in tests) + set(nlohmann_json_DIR ${JSON_HPP}) + endif() +endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR + "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + target_compile_options(nlohmann_json_schema_validator PRIVATE -Wall -Wextra) endif() -target_link_libraries(json-schema-validator - PUBLIC - json-hpp) if(BUILD_SHARED_LIBS) - target_compile_definitions(json-schema-validator + target_compile_definitions(nlohmann_json_schema_validator PRIVATE -DJSON_SCHEMA_VALIDATOR_EXPORTS) endif() @@ -83,38 +103,68 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") find_package(Boost COMPONENTS regex) if(NOT Boost_FOUND) message(STATUS "GCC less then 4.9 and boost-regex NOT found - no regex used") - target_compile_definitions(json-schema-validator PRIVATE -DJSON_SCHEMA_NO_REGEX) + target_compile_definitions(nlohmann_json_schema_validator PRIVATE -DJSON_SCHEMA_NO_REGEX) else() message(STATUS "GCC less then 4.9 and boost-regex FOUND - using boost::regex") - target_compile_definitions(json-schema-validator PRIVATE -DJSON_SCHEMA_BOOST_REGEX) - target_include_directories(json-schema-validator PRIVATE ${Boost_INCLUDE_DIRS}) - target_link_libraries(json-schema-validator PRIVATE ${Boost_LIBRARIES}) + target_compile_definitions(nlohmann_json_schema_validator PRIVATE -DJSON_SCHEMA_BOOST_REGEX) + target_include_directories(nlohmann_json_schema_validator PRIVATE ${Boost_INCLUDE_DIRS}) + target_link_libraries(nlohmann_json_schema_validator PRIVATE ${Boost_LIBRARIES}) endif() endif() endif() -if(NOT TARGET json-hpp) # if used as a subdirectory do not install json-schema.hpp - install( - FILES - ${CMAKE_CURRENT_SOURCE_DIR}/src/json-schema.hpp - DESTINATION - ${CMAKE_INSTALL_PREFIX}/include - ) -endif() +install(TARGETS nlohmann_json_schema_validator + EXPORT ${PROJECT_NAME}Targets + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin) + +install(FILES src/nlohmann/json-schema.hpp + DESTINATION include/nlohmann) if (BUILD_EXAMPLES) - # simple json-schema-validator-executable + # simple nlohmann_json_schema_validator-executable add_executable(json-schema-validate app/json-schema-validate.cpp) - target_link_libraries(json-schema-validate json-schema-validator) + target_link_libraries(json-schema-validate nlohmann_json_schema_validator) add_executable(readme-json-schema app/readme.cpp) - target_link_libraries(readme-json-schema json-schema-validator) + target_link_libraries(readme-json-schema nlohmann_json_schema_validator) endif() -#add_subdirectory(ng) - if (BUILD_TESTS) # test-zone enable_testing() add_subdirectory(test) endif() +#---------------------------------------------------------------------------## +# Set Up the Project Targets and Config Files for CMake +#---------------------------------------------------------------------------## + +# Set the install path to the cmake config files +set(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_PREFIX}/lib/cmake/${PROJECT_NAME}) + +# Create the ConfigVersion file +include(CMakePackageConfigHelpers) # write_basic_package_version_file +write_basic_package_version_file( ${PROJECT_NAME}ConfigVersion.cmake + VERSION ${PACKAGE_VERSION} + COMPATIBILITY SameMajorVersion) + +# Get the relative path from the INSTALL_CMAKE_DIR to the include directory +file(RELATIVE_PATH REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}" "${CMAKE_INSTALL_PREFIX}/include") + + +# Configure the Config.cmake file with the proper include directory +set(CONF_INCLUDE_DIRS "\${JSON_SCHEMA_VALIDATOR_CMAKE_DIR}/${REL_INCLUDE_DIR}") +configure_file(${PROJECT_NAME}Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @ONLY) + +# Install the Config.cmake and ConfigVersion.cmake files +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION "${INSTALL_CMAKE_DIR}") + +# Install Targets +install(EXPORT ${PROJECT_NAME}Targets + FILE ${PROJECT_NAME}Targets.cmake + DESTINATION "${INSTALL_CMAKE_DIR}") diff --git a/README.md b/README.md index 8b39746..1190409 100644 --- a/README.md +++ b/README.md @@ -42,67 +42,138 @@ messages if a JSON-document/instance does not comply to its schema. By default this is done with exceptions thrown at the users with a helpful message telling what's wrong with the document while validating. -With **2.0.0** the user can pass a `json_schema::basic_error_handler`-derived -object along with the instance to validate to receive a callback each time a -validation error occurs and decide what to do (throwing, counting, collecting). +Starting with **2.0.0** the user can pass a `json_schema::basic_error_handler`-derived +object along with the instance to validate to receive a callback each time +a validation error occurs and decide what to do (throwing, counting, collecting). Another goal was to use Niels Lohmann's JSON-library. This is why the validator lives in his namespace. # Weaknesses -Numerical validation uses nlohmann integer, unsigned and floating point types, depending on if -the schema type is "integer" or "number". Bignum (i.e. arbitrary precision and -range) is not supported at this time. +Numerical validation uses nlohmann-json's integer, unsigned and floating point +types, depending on if the schema type is "integer" or "number". Bignum +(i.e. arbitrary precision and range) is not supported at this time. -# How to use +# Building -The current state of the build-system needs at least version **3.6.0** of NLohmann's -JSON library. It is looking for the `json.hpp` within a `nlohmann/`-path. +This library is based on Niels Lohmann's JSON-library and thus has +a build-dependency to it. -When build the library you need to provide the path to the directory where the include-file -is located as `nlohmann/json.hpp`. +Currently at least version **3.6.0** of NLohmann's JSON library +is required. -## Build +Various methods using CMake can be used to build this project. -### Within a build-dir +## Build out-of-source + +Do not run cmake inside the source-dir. Rather create a dedicated build-dir: ```Bash git clone https://github.com/pboettch/json-schema-validator.git cd json-schema-validator mkdir build cd build -cmake .. \ - -DNLOHMANN_JSON_DIR=nlohmann/json.hpp \ - -DJSON_SCHEMA_TEST_SUITE_PATH= # optional -make # install +cmake [..] +make +make install # if needed ctest # run unit, non-regression and test-suite tests ``` -### As a subdirectory from within -```CMake -# create an interface-target called json-hpp -add_library(json-hpp INTERFACE) -target_include_directories(json-hpp - INTERFACE - path/to/nlohmann/json.hpp) +## Building as shared library -# set this path to schema-test-suite to get tests compiled - optional -set(JSON_SCHEMA_TEST_SUITE_PATH "path/to/json-schema-test-suite") -enable_testing() # if you want to inherit tests - -add_subdirectory(path-to-this-project json-schema-validator) -``` - -### Building a shared library - -By default a static library is built. Shared libraries are generated by using +By default a static library is built. Shared libraries can be generated by using the `BUILD_SHARED_LIBS`-cmake variable: In your initial call to cmake simply add: ```bash -cmake -DBUILD_SHARED_LIBS=ON +cmake [..] -DBUILD_SHARED_LIBS=ON [..] ``` + +## Providing access to nlohmann-json + +The CMake-file of this libraries tries several ways to ultimately include +`nlohmann/json.hpp` + +During the cmake-configurate-step there are 3 tries done trying to +access nlohmann-json: + +1. link with a nlohmann_json::nlohmann_json-target, +2. find the nlohmann_json-cmake-package and link with nlohmann_json::nlohmann_json-target or +3. find path to `nlohmann/json.hpp`. + +1 is there to make it work when this project is added as +a sub-directory (via `add_subdirectory()`), 2 and 3 can be +assisted by setting the `nlohmann_json_DIR`-variable. + +### Building as a CMake-subdirectory from within another project + +Adding this library as a subdirectory to a parent project is one way of +building it. + +If the parent project + +- already used `find_package()` to find the CMake-package of nlohmann_json, method 1 will work. +- uses the git-repo of nlohmann_json as a subdirectory, method 1 will work. +- sets nlohmann_json_DIR, method 2 or 3 will work. + +Afterwards a target called `nlohmann_json_schema_validator` +is available in order to compile and link. + +### Building directly, finding a CMake-package. (short) + +When nlohmann-json has been installed, it provides files which allows +CMake's `find_package()` to be used. + +This library is using this mechanism if `nlohmann_json::nlohmann_json`-target +does not exist. + +The variable `nlohmann_json_DIR` can be used to help `find_package()` find this package. + +### Building directly: provide a path to where to find json.hpp + +The last method before fataling out is by providing a path where the file json.hpp can be found. + +The variable `nlohmann_json_DIR` has to be used to point to the path +where `json.hpp` is found in a subdirectory called `nlohmann`, e.g.: + +`json.hpp` is located in `/path/to/nlohmann/json.hpp`. The `cmake`-command has to be run as follows: + +```bash +cmake -Dnlohmann_json_DIR=/path/to [..] +``` + +### Method 1 - long version + +Since version 2.1.0 this library can be installed and CMake-package-files will be +created accordingly. If the installation of nlohmann-json and this library +is done into default unix-system-paths CMake will be able to find this +library by simply doing: + +```CMake +find_package(nlohmann_json REQUIRED) +find_package(nlohmann_json_schema_validator REQUIRED) +``` + +and + +```CMake +target_link_libraries( [..] nlohmann_json_schema_validator) +``` +to build and link. + +If a custom path has been used to install this library (and nlohmann-json), `find_package()` +needs a hint for where to find the package-files, it can be provided by setting the following variables + +```CMake +cmake .. \ + -Dnlohmann_json_DIR=lib/cmake/nlohmann_json \ + -Dnlohmann_json_schema_validator_DIR:PATH=/lib/cmake/nlohmann_json_schema_validator +``` + +Note that if the this library is used as cmake-package, nlohmann-json also has +to be used a cmake-package. + ## Code See also `app/json-schema-validate.cpp`. @@ -111,7 +182,7 @@ See also `app/json-schema-validate.cpp`. #include #include -#include "json-schema.hpp" +#include using nlohmann::json; using nlohmann::json_schema::json_validator; diff --git a/app/json-schema-validate.cpp b/app/json-schema-validate.cpp index 6f5cbcd..4b51237 100644 --- a/app/json-schema-validate.cpp +++ b/app/json-schema-validate.cpp @@ -6,7 +6,7 @@ * SPDX-License-Identifier: MIT * */ -#include +#include #include #include diff --git a/app/readme.cpp b/app/readme.cpp index 227a560..da95fed 100644 --- a/app/readme.cpp +++ b/app/readme.cpp @@ -1,7 +1,7 @@ #include #include -#include "json-schema.hpp" +#include using nlohmann::json; using nlohmann::json_schema::json_validator; diff --git a/nlohmann_json_schema_validatorConfig.cmake.in b/nlohmann_json_schema_validatorConfig.cmake.in new file mode 100644 index 0000000..6ee0df2 --- /dev/null +++ b/nlohmann_json_schema_validatorConfig.cmake.in @@ -0,0 +1,13 @@ +# Config file for the json-schema-validator +# It defines the following variables +# NLOHMANN_JSON_SCHEMA_VALIDATOR_INCLUDE_DIRS - include directories for json-schema-validator +# nlohmann_json_schema_validator - json-schema-validator library to link against + +# Compute paths +get_filename_component(NLOHMANN_JSON_SCHEMA_VALIDATOR_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +set(NLOHMANN_JSON_SCHEMA_VALIDATOR_INCLUDE_DIRS @CONF_INCLUDE_DIRS@) + +# Our library dependencies (contains definitions for IMPORTED targets) +if(NOT TARGET json-schema-validator) + include("${NLOHMANN_JSON_SCHEMA_VALIDATOR_CMAKE_DIR}/nlohmann_json_schema_validatorTargets.cmake") +endif() diff --git a/src/json-uri.cpp b/src/json-uri.cpp index 8b76615..2809fa1 100644 --- a/src/json-uri.cpp +++ b/src/json-uri.cpp @@ -6,7 +6,7 @@ * SPDX-License-Identifier: MIT * */ -#include "json-schema.hpp" +#include #include diff --git a/src/json-validator.cpp b/src/json-validator.cpp index bd9f5ee..e10234f 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -6,7 +6,7 @@ * SPDX-License-Identifier: MIT * */ -#include +#include #include #include diff --git a/src/json-schema.hpp b/src/nlohmann/json-schema.hpp similarity index 100% rename from src/json-schema.hpp rename to src/nlohmann/json-schema.hpp diff --git a/src/string-format-check.cpp b/src/string-format-check.cpp index d86bee5..8bae0e2 100644 --- a/src/string-format-check.cpp +++ b/src/string-format-check.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b256f8e..9ca65d7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,24 +20,24 @@ foreach(DIR ${TEST_DIRS}) endforeach() add_executable(uri uri.cpp) -target_link_libraries(uri json-schema-validator) +target_link_libraries(uri nlohmann_json_schema_validator) add_test(NAME uri COMMAND uri) add_executable(errors errors.cpp) -target_link_libraries(errors json-schema-validator) +target_link_libraries(errors nlohmann_json_schema_validator) add_test(NAME errors COMMAND errors) add_executable(issue-70 issue-70.cpp) -target_link_libraries(issue-70 json-schema-validator) +target_link_libraries(issue-70 nlohmann_json_schema_validator) add_test(NAME issue-70 COMMAND issue-70) add_executable(issue-70-root-schema-constructor issue-70-root-schema-constructor.cpp) -target_link_libraries(issue-70-root-schema-constructor json-schema-validator) +target_link_libraries(issue-70-root-schema-constructor nlohmann_json_schema_validator) add_test(NAME issue-70-root-schema-constructor COMMAND issue-70-root-schema-constructor) # Unit test for string format checks add_executable("string-format-check-test" "string-format-check-test.cpp") target_include_directories("string-format-check-test" PRIVATE "${PROJECT_SOURCE_DIR}/src/") -target_link_libraries("string-format-check-test" json-schema-validator) +target_link_libraries("string-format-check-test" nlohmann_json_schema_validator) add_test(NAME "string-format-check-test" COMMAND "string-format-check-test") diff --git a/test/JSON-Schema-Test-Suite/CMakeLists.txt b/test/JSON-Schema-Test-Suite/CMakeLists.txt index ba4d625..97a9950 100644 --- a/test/JSON-Schema-Test-Suite/CMakeLists.txt +++ b/test/JSON-Schema-Test-Suite/CMakeLists.txt @@ -15,7 +15,7 @@ endif() if(JSON_SCHEMA_TEST_SUITE_PATH) # json-schema-validator-tester add_executable(json-schema-test json-schema-test.cpp) - target_link_libraries(json-schema-test json-schema-validator) + target_link_libraries(json-schema-test nlohmann_json_schema_validator) target_compile_definitions(json-schema-test PRIVATE JSON_SCHEMA_TEST_SUITE_PATH="${JSON_SCHEMA_TEST_SUITE_PATH}") diff --git a/test/JSON-Schema-Test-Suite/json-schema-test.cpp b/test/JSON-Schema-Test-Suite/json-schema-test.cpp index 6d0ec24..05a255d 100644 --- a/test/JSON-Schema-Test-Suite/json-schema-test.cpp +++ b/test/JSON-Schema-Test-Suite/json-schema-test.cpp @@ -6,7 +6,7 @@ * SPDX-License-Identifier: MIT * */ -#include "json-schema.hpp" +#include #include #include diff --git a/test/cmake-install/CMakeLists.txt b/test/cmake-install/CMakeLists.txt new file mode 100644 index 0000000..603348f --- /dev/null +++ b/test/cmake-install/CMakeLists.txt @@ -0,0 +1,15 @@ +# Configure install script +configure_file(test.sh.in + ${CMAKE_CURRENT_BINARY_DIR}/test.sh @ONLY) + +get_filename_component(TEST_NAME + ${CMAKE_CURRENT_SOURCE_DIR} + NAME) + + +# this build test only works, if nlohmann-json was found via a cmake-package +if(TARGET nlohmann_json::nlohmann_json) + add_test(NAME Build::${TEST_NAME} + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +endif() diff --git a/test/cmake-install/project/CMakeLists.txt b/test/cmake-install/project/CMakeLists.txt new file mode 100644 index 0000000..b5d5553 --- /dev/null +++ b/test/cmake-install/project/CMakeLists.txt @@ -0,0 +1,34 @@ +# This is a simple project that tests using cmake to load the installed libraries +cmake_minimum_required(VERSION 3.2) + +project(cmake_install_test LANGUAGES CXX) + +set(PROJECT_VERSION 1.0.0) + +# Find the nlohmann_json and the validator package +set(CMAKE_FIND_DEBUG_MODE ON) +find_package(nlohmann_json REQUIRED) +find_package(nlohmann_json_schema_validator REQUIRED) + +# Add simple json-schema-validator-executable +add_executable(json-schema-validate ${CMAKE_CURRENT_SOURCE_DIR}/../../../app/json-schema-validate.cpp) +target_link_libraries(json-schema-validate nlohmann_json_schema_validator) + +enable_testing() + +# Add built-in tests function needed for issues +set(PIPE_IN_TEST_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/../../test-pipe-in.sh) +function(add_test_simple_schema name schema instance) + add_test( + NAME ${name} + COMMAND ${PIPE_IN_TEST_SCRIPT} + $ + ${schema} + ${instance} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +endfunction() + +# Run tests for issues 9, 12, 27, 48, 54 +foreach(NUMBER "9" "12" "27" "48" "54") + add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../issue-${NUMBER}" "${CMAKE_CURRENT_BINARY_DIR}/issue-${NUMBER}" EXCLUDE_FROM_ALL) +endforeach() diff --git a/test/cmake-install/test.sh.in b/test/cmake-install/test.sh.in new file mode 100755 index 0000000..466bcbf --- /dev/null +++ b/test/cmake-install/test.sh.in @@ -0,0 +1,55 @@ +#!/bin/bash + +## Configure, build, install, and test json-schema-validator with CMAKE +## This script is instantiated via configure_file() to run cmake the same the original build has been invoked. + +set -xe + +EXTRA_ARGS=$@ +SRC_DIR=@PROJECT_SOURCE_DIR@ +BUILD_DIR=@CMAKE_CURRENT_BINARY_DIR@/build-dir +INSTALL_DIR=@CMAKE_CURRENT_BINARY_DIR@/install-dir +NLOHMANN_JSON_DIR=@nlohmann_json_DIR@ +TEST_SRC_DIR=@CMAKE_CURRENT_SOURCE_DIR@/project + +cmake --version + +# Clear out build directory +rm -rf ${BUILD_DIR} +# Create build-dir +mkdir -p ${BUILD_DIR} +cd ${BUILD_DIR} + +# configure json-schema-validator +printf "\n-----------------------------------------------------------\n" +printf "Configuring, building, and installing json-schema-validator" +printf "\n-----------------------------------------------------------\n" +cmake \ + -DCMAKE_INSTALL_PREFIX:PATH=${INSTALL_DIR} \ + -Dnlohmann_json_DIR:PATH=${NLOHMANN_JSON_DIR} \ + ${EXTRA_ARGS} \ + ${SRC_DIR} + +CPU_COUNT=$(nproc) + +# Build and install json-schema-validator +cmake --build . -- -j${CPU_COUNT} +cmake --build . --target install -- -j${CPU_COUNT} + +# Make sure build directory is empty +rm -rf ./* + +# configure test project +printf "\n-----------------------------------------------------------\n" +printf "Configuring, building, and running test project" +printf "\n-----------------------------------------------------------\n" +cmake \ + -Dnlohmann_json_DIR:PATH=${NLOHMANN_JSON_DIR} \ + -Dnlohmann_json_schema_validator_DIR:PATH=${INSTALL_DIR}/lib/cmake/nlohmann_json_schema_validator \ + -DVALIDATOR_INSTALL_DIR:PATH=${INSTALL_DIR} \ + ${EXTRA_ARGS} \ + ${TEST_SRC_DIR} + +# Build test project and test +cmake --build . +ctest --output-on-failure diff --git a/test/errors.cpp b/test/errors.cpp index e862379..91c025c 100644 --- a/test/errors.cpp +++ b/test/errors.cpp @@ -1,4 +1,4 @@ -#include +#include #include diff --git a/test/id-ref.cpp b/test/id-ref.cpp index 339a3c2..de54a98 100644 --- a/test/id-ref.cpp +++ b/test/id-ref.cpp @@ -1,6 +1,6 @@ #include -#include "../src/json-schema.hpp" +#include #include diff --git a/test/issue-70-root-schema-constructor.cpp b/test/issue-70-root-schema-constructor.cpp index 02dc94f..7dad6a1 100644 --- a/test/issue-70-root-schema-constructor.cpp +++ b/test/issue-70-root-schema-constructor.cpp @@ -1,4 +1,4 @@ -#include +#include #include diff --git a/test/issue-70.cpp b/test/issue-70.cpp index 7d42181..bfac0a5 100644 --- a/test/issue-70.cpp +++ b/test/issue-70.cpp @@ -1,4 +1,4 @@ -#include +#include using nlohmann::json; using nlohmann::json_schema::json_validator; diff --git a/test/string-format-check-test.cpp b/test/string-format-check-test.cpp index ebf31ec..c6954a2 100644 --- a/test/string-format-check-test.cpp +++ b/test/string-format-check-test.cpp @@ -1,6 +1,6 @@ #include -#include +#include /** @return number of failed tests */ size_t diff --git a/test/uri.cpp b/test/uri.cpp index 01097d1..9083cd3 100644 --- a/test/uri.cpp +++ b/test/uri.cpp @@ -7,7 +7,7 @@ * */ #include -#include +#include #include