First approach to clean out (#165)

* First approach to clean out

* Update readme remove example

* Correct readme for install and json integration
This commit is contained in:
Finkman 2021-11-11 20:23:50 +01:00 committed by GitHub
parent cc05de9327
commit 639e09b6f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 200 deletions

View File

@ -45,73 +45,28 @@ set_target_properties(nlohmann_json_schema_validator
VERSION ${PROJECT_VERSION}
SOVERSION 1)
# if used as a sub-directory, do not create install-rules -
# because of the dependency to nlohmann_json.
set(JSON_VALIDATOR_INSTALL ON)
# here we decice how nlohmann::json is found and used to build this project
# 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)
set(JSON_VALIDATOR_INSTALL OFF)
set(BUILD_TESTS OFF)
set(BUILD_EXAMPLES OFF)
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)
set(JSON_VALIDATOR_INSTALL OFF)
set(BUILD_TESTS OFF)
set(BUILD_EXAMPLES OFF)
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()
set(nlohmann_json_orignal_DIR ${nlohmann_json_DIR}) # save path for later use
# 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 $<BUILD_INTERFACE:${JSON_HPP}>)
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()
# disable tests and examples if project is not super project
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
# I am top-level project.
set(JSON_VALIDATOR_IS_TOP_LEVEL TRUE)
endif()
if(JSON_VALIDATOR_IS_TOP_LEVEL)
set(BUILD_TESTS ON)
set(BUILD_EXAMPLES ON)
else()
set(BUILD_TESTS OFF)
set(BUILD_EXAMPLES OFF)
endif()
if(NOT TARGET nlohmann_json::nlohmann_json)
find_package(nlohmann_json REQUIRED)
endif()
target_link_libraries(
nlohmann_json_schema_validator
PUBLIC nlohmann_json::nlohmann_json)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(nlohmann_json_schema_validator
@ -141,16 +96,14 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
endif()
endif()
if(JSON_VALIDATOR_INSTALL)
install(TARGETS nlohmann_json_schema_validator
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin)
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)
endif()
install(FILES src/nlohmann/json-schema.hpp
DESTINATION include/nlohmann)
if (BUILD_EXAMPLES)
# simple nlohmann_json_schema_validator-executable
@ -173,40 +126,36 @@ if (BUILD_TESTS)
add_subdirectory(test)
endif()
if(JSON_VALIDATOR_INSTALL)
# Set Up the Project Targets and Config Files for CMake
# Set Up the Project Targets and Config Files for CMake
# Set the install path to the cmake config files (Relative, so install works correctly under Hunter as well)
set(INSTALL_CMAKE_DIR "lib/cmake/${PROJECT_NAME}")
set(INSTALL_CMAKEDIR_ROOT share/cmake)
# Set the install path to the cmake config files (Relative, so install works correctly under Hunter as well)
set(INSTALL_CMAKE_DIR "lib/cmake/${PROJECT_NAME}")
set(INSTALL_CMAKEDIR_ROOT share/cmake)
# Install Targets
install(EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION "${INSTALL_CMAKE_DIR}")
# Install Targets
install(EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION "${INSTALL_CMAKE_DIR}")
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
${PROJECT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
configure_package_config_file(
${PROJECT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${INSTALL_CMAKEDIR_ROOT}/${PROJECT_NAME}
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${INSTALL_CMAKEDIR_ROOT}/${PROJECT_NAME}
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION
${INSTALL_CMAKE_DIR}
)
endif()
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION
${INSTALL_CMAKE_DIR}
)

View File

@ -105,23 +105,12 @@ In your initial call to cmake simply add:
cmake [..] -DBUILD_SHARED_LIBS=ON [..]
```
## Providing access to nlohmann-json
## nlohmann-json integration
The CMake-file of this libraries tries several ways to ultimately include
`nlohmann/json.hpp`
As nlohmann-json is a dependency, this library tries find it.
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.
For 1 there is an example of you to do in example/cmake-submodule.
The cmake-configuration first checks if nlohmann-json is available as a cmake-target. This may be the case, because it is used as a submodule in a super-project which already provides and uses nlohmann-json.
Otherwise, it calls `find_package` for nlohmann-json and requires nlohmann-json to be installed on the system.
### Building with Hunter package manager
@ -136,14 +125,7 @@ cmake [..] -DHUNTER_ENABLED=ON [..]
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.
If the parent project already used `find_package()` to find the CMake-package of nlohmann_json or includes it as a submodule likewise.
### Building directly, finding a CMake-package. (short)
@ -153,22 +135,7 @@ 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
### Install
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
@ -176,7 +143,6 @@ 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)
```
@ -187,18 +153,6 @@ target_link_libraries(<your-target> [..] 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=<path/to/>lib/cmake/nlohmann_json \
-Dnlohmann_json_schema_validator_DIR:PATH=<path/to/>/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`.

View File

@ -1,24 +0,0 @@
cmake_minimum_required(VERSION 3.2)
project(simple-json-validator-as-submodule-test)
# absolute-path
set(PATH_TO_NLOHMANN_JSON_REPO $ENV{HOME}/devel/upstream/json)
# build shared library
set(BUILD_SHARED_LIBS ON)
# JSON library
option(JSON_BuildTests OFF)
add_subdirectory(${PATH_TO_NLOHMANN_JSON_REPO}
json-binary-dir
EXCLUDE_FROM_ALL)
# JSON SCHEMA VALIDATOR library
option(BUILD_TESTS OFF)
add_subdirectory(../..
json-validator-binary-dir
EXCLUDE_FROM_ALL)
add_executable(validate validate.cpp)
target_link_libraries(validate nlohmann_json_schema_validator)

View File

@ -1,20 +0,0 @@
#include <nlohmann/json-schema.hpp>
#include <iostream>
int main(void)
{
nlohmann::json schema_json{{"type", "number"}};
nlohmann::json_schema::json_validator validator;
validator.set_root_schema(schema_json);
validator.validate(1);
try {
validator.validate("\"1\"");
} catch (const std::exception &e) {
std::cerr << "expected exception: " << e.what() << "\n";
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}