Refactor examples

Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
This commit is contained in:
Cristian Le 2023-11-27 19:10:12 +01:00
parent 5af49cd72e
commit 8c41e17410
8 changed files with 173 additions and 12 deletions

View File

@ -5,6 +5,11 @@ if (POLICY CMP0140)
# Enables: return(PROPAGATE)
cmake_policy(SET CMP0140 NEW)
endif ()
# TODO: Remove when bumping cmake >= 3.22
if (POLICY CMP0127)
# Enables: cmake_dependent_option: Full Condition Syntax
cmake_policy(SET CMP0127 NEW)
endif ()
#[==============================================================================================[
# Basic project definition #
@ -38,7 +43,12 @@ include(CMakeDependentOption)
option(JSON_VALIDATOR_INSTALL "JsonValidator: Install targets" ${PROJECT_IS_TOP_LEVEL})
option(JSON_VALIDATOR_BUILD_TESTS "JsonValidator: Build tests" ${PROJECT_IS_TOP_LEVEL})
option(JSON_VALIDATOR_BUILD_EXAMPLES "JsonValidator: Build examples" ${PROJECT_IS_TOP_LEVEL})
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22)
# Only enable BUILD_EXAMPLES if we are building tests or installing the targets
cmake_dependent_option(JSON_VALIDATOR_BUILD_EXAMPLES "JsonValidator: Build examples" ${PROJECT_IS_TOP_LEVEL} "JSON_VALIDATOR_BUILD_TESTS OR JSON_VALIDATOR_INSTALL" OFF)
else ()
option(JSON_VALIDATOR_BUILD_EXAMPLES "JsonValidator: Build examples" ${PROJECT_IS_TOP_LEVEL})
endif ()
option(JSON_VALIDATOR_SHARED_LIBS "JsonValidator: Build as shared library" ${PROJECT_IS_TOP_LEVEL})
cmake_dependent_option(JSON_VALIDATOR_TEST_COVERAGE "JsonValidator: Build with test coverage" OFF "JSON_VALIDATOR_BUILD_TESTS" OFF)
mark_as_advanced(JSON_VALIDATOR_TEST_COVERAGE)

View File

@ -1,14 +1,87 @@
# simple nlohmann_json_schema_validator-executable
add_executable(json-schema-validate json-schema-validate.cpp)
target_link_libraries(json-schema-validate nlohmann_json_schema_validator)
# TODO: This definition should be moved to /test, together with the for loop
function(schema_validator_add_test name)
#[===[.md
# schema_validator_add_test
add_executable(readme-json-schema readme.cpp)
target_link_libraries(readme-json-schema nlohmann_json_schema_validator)
Internal helper for adding example/functional tests specific for the current template project
add_executable(format-json-schema format.cpp)
target_link_libraries(format-json-schema nlohmann_json_schema_validator)
## Synopsis
```cmake
schema_validator_add_test(<name>
[TEST_NAME <test_name>]
[TARGET <target>]
[LABELS <label1> <label2>])
```
if (JSON_VALIDATOR_INSTALL)
install(TARGETS json-schema-validate readme-json-schema format-json-schema
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
## Options
`<name>`
Path to the CMake project to be executed relative to `${CMAKE_CURRENT_SOURCE_DIR}`
`TEST_NAME` [Default: `<name>`]
Name for the test to be used as the ctest name
`LABELS`
Additional labels to be added
]===]
list(APPEND CMAKE_MESSAGE_CONTEXT "schema_validator_add_test")
set(ARGS_Options)
set(ARGS_OneValue
TEST_NAME
)
set(ARGS_MultiValue
LABELS
)
cmake_parse_arguments(PARSE_ARGV 1 ARGS "${ARGS_Options}" "${ARGS_OneValue}" "${ARGS_MultiValue}")
# Check required/optional arguments
if (ARGC LESS 1)
message(FATAL_ERROR "Missing test name")
endif ()
if (NOT DEFINED ARGS_TEST_NAME)
set(ARGS_TEST_NAME test-${name})
endif ()
set(extra_args)
if (nlohmann_json_schema_validator_IS_TOP_LEVEL)
list(APPEND extra_args
-DFETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS
# Generated Config file point to binary targets until it is installed
-Dnlohmann_json_schema_validator_ROOT=${Template_BINARY_DIR}
-DFETCHCONTENT_SOURCE_DIR_TEMPLATE=${Template_SOURCE_DIR}
)
endif ()
add_test(NAME ${ARGS_TEST_NAME}
COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test ${CMAKE_CURRENT_SOURCE_DIR}/${test}
${CMAKE_CURRENT_BINARY_DIR}/${test}
# Use the same build environment as the current runner
--build-generator "${CMAKE_GENERATOR}"
--build-options -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
${extra_args}
--test-command ${CMAKE_CTEST_COMMAND} --test-dir ${CMAKE_CURRENT_BINARY_DIR}/${test}
--output-on-failure --no-tests=ignore
)
set_tests_properties(${ARGS_TEST_NAME} PROPERTIES
LABELS "${ARGS_LABELS}"
)
endfunction()
foreach (example IN ITEMS
format
readme
validate
)
if (JSON_VALIDATOR_INSTALL)
# The projects need to be added only if they are to be installed
add_subdirectory(${example})
elseif (JSON_VALIDATOR_BUILD_TESTS)
schema_validator_add_test(${example})
else ()
# Can be simplified after CMake >= 3.22
message(WARNING
"Nothing specified to do with the examples. Enable this together with INSTALL or BUILD_TESTS"
)
endif ()
endforeach ()

View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.24)
project(example_format LANGUAGES CXX)
include(FetchContent)
# The target check is not generally needed. It is used here because these projects are reused by the top-level project
if (NOT TARGET nlohmann_json_schema_validator::validator)
# Note: The 3.24 cmake requirement only appears due to `FetchContent_Declare(FIND_PACKAGE_ARGS)`
# To support earlier versions, you can replace this with `find_package` or remove `FIND_PACKAGE_ARGS`
FetchContent_Declare(nlohmann_json_schema_validator
GIT_REPOSITORY https://github.com/pboettch/json-schema-validator
GIT_TAG main
FIND_PACKAGE_ARGS CONFIG
)
FetchContent_MakeAvailable(nlohmann_json_schema_validator)
endif ()
add_executable(format-json-schema format.cpp)
target_link_libraries(format-json-schema PRIVATE nlohmann_json_schema_validator::validator)
# Reusing the top-level install in order to bundle these executables
if (JSON_VALIDATOR_INSTALL)
install(TARGETS format-json-schema
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()

View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.24)
project(example_readme LANGUAGES CXX)
include(FetchContent)
# The target check is not generally needed. It is used here because these projects are reused by the top-level project
if (NOT TARGET nlohmann_json_schema_validator::validator)
# Note: The 3.24 cmake requirement only appears due to `FetchContent_Declare(FIND_PACKAGE_ARGS)`
# To support earlier versions, you can replace this with `find_package` or remove `FIND_PACKAGE_ARGS`
FetchContent_Declare(nlohmann_json_schema_validator
GIT_REPOSITORY https://github.com/pboettch/json-schema-validator
GIT_TAG main
FIND_PACKAGE_ARGS CONFIG
)
FetchContent_MakeAvailable(nlohmann_json_schema_validator)
endif ()
add_executable(readme-json-schema readme.cpp)
target_link_libraries(readme-json-schema PRIVATE nlohmann_json_schema_validator::validator)
# Reusing the top-level install in order to bundle these executables
if (JSON_VALIDATOR_INSTALL)
install(TARGETS readme-json-schema
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()

View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.24)
project(example_validate LANGUAGES CXX)
include(FetchContent)
# The target check is not generally needed. It is used here because these projects are reused by the top-level project
if (NOT TARGET nlohmann_json_schema_validator::validator)
# Note: The 3.24 cmake requirement only appears due to `FetchContent_Declare(FIND_PACKAGE_ARGS)`
# To support earlier versions, you can replace this with `find_package` or remove `FIND_PACKAGE_ARGS`
FetchContent_Declare(nlohmann_json_schema_validator
GIT_REPOSITORY https://github.com/pboettch/json-schema-validator
GIT_TAG main
FIND_PACKAGE_ARGS CONFIG
)
FetchContent_MakeAvailable(nlohmann_json_schema_validator)
endif ()
add_executable(json-schema-validate json-schema-validate.cpp)
target_link_libraries(json-schema-validate PRIVATE nlohmann_json_schema_validator::validator)
# Reusing the top-level install in order to bundle these executables
if (JSON_VALIDATOR_INSTALL)
install(TARGETS json-schema-validate
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()