Skip to content

[WIP] feat: CMake improvements and Catch2 migration #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/Takishima/cmake-pre-commit-hooks
rev: v1.8.1
rev: v1.9.4
hooks:
- id: clang-format
args:
Expand All @@ -13,20 +13,21 @@ repos:
- '-Bcmake-build-pre-commit'
- '--preset'
- 'pre-commit'
stages: [ manual ]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.16
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies:
- mdformat-gfm
- mdformat-tables
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.23.0
rev: 0.27.2
hooks:
- id: check-github-workflows
41 changes: 21 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ 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 #
]==============================================================================================]

list(APPEND CMAKE_MESSAGE_CONTEXT schema_validator)

# TODO: CMake >= 3.19 can use string(JSON VERSION GET "${METADATA}" "version") to load from JSON
set(PROJECT_VERSION 2.4.0)

Expand All @@ -23,21 +30,28 @@ project(nlohmann_json_schema_validator
if (NOT DEFINED nlohmann_json_schema_validator_IS_TOP_LEVEL)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(PROJECT_IS_TOP_LEVEL ON)
set(nlohmann_json_schema_validator_IS_TOP_LEVEL ON)
else ()
set(PROJECT_IS_TOP_LEVEL OFF)
set(nlohmann_json_schema_validator_IS_TOP_LEVEL OFF)
endif ()
endif ()

#[==============================================================================================[
# Options #
]==============================================================================================]

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})
option(JSON_VALIDATOR_TEST_COVERAGE "JsonValidator: Build with test coverage" OFF)
mark_as_advanced(JSON_VALIDATOR_TEST_COVERAGE)
# Get a default JSON_FETCH_VERSION from environment variables to workaround the CI
if (DEFINED ENV{NLOHMANN_JSON_VERSION})
set(JSON_FETCH_VERSION_DEFAULT $ENV{NLOHMANN_JSON_VERSION})
Expand Down Expand Up @@ -65,19 +79,6 @@ endif ()
# Enable cmake's BUILD_SHARED_LIBS
set(BUILD_SHARED_LIBS ${nlohmann_json_schema_validator_SHARED_LIBS})

if (JSON_VALIDATOR_TEST_COVERAGE)
if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
else ()
message(WARNING
"JsonValidator: Other toolchain coverage flags unknown.\n"
"Using --coverage as default")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
endif ()
endif ()

#[==============================================================================================[
# External packages #
]==============================================================================================]
Expand Down Expand Up @@ -119,7 +120,7 @@ FetchContent_MakeAvailable(${fetch_packages})
if (JSON_VALIDATOR_INSTALL AND NOT nlohmann_json_FOUND AND JSON_Install)
# TODO: This is not ideal
message(WARNING
"JsonValidator: No nlohmann::json found on the system and nlohmann_json_schema_validator will be installed\n"
"No nlohmann::json found on the system and nlohmann_json_schema_validator will be installed\n"
"This will also install nlohmann::json in its typical installation path\n"
"This is not ideal because it might overwrite system installed")
endif ()
Expand All @@ -128,11 +129,11 @@ endif ()
# Main definition #
]==============================================================================================]

message(STATUS "JsonValidator: Configured for ${CMAKE_BUILD_TYPE}")
message(STATUS "Configured for ${CMAKE_BUILD_TYPE}")
if (DEFINED nlohmann_json_VERSION)
message(STATUS "JsonValidator: Using nlohmann/json version: ${nlohmann_json_VERSION}")
message(STATUS "Using nlohmann/json version: ${nlohmann_json_VERSION}")
else ()
message(STATUS "JsonValidator: nlohmann_json_VERSION is not set. Possible value: ${JSON_FETCH_VERSION}")
message(STATUS "nlohmann_json_VERSION is not set. Possible value: ${JSON_FETCH_VERSION}")
endif ()

## Main targets
Expand Down
4 changes: 4 additions & 0 deletions cmake/CMakePresets-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"CMAKE_BUILD_TYPE": {
"type": "STRING",
"value": "Release"
},
"CMAKE_MESSAGE_CONTEXT_SHOW": {
"type": "BOOL",
"value": true
}
}
}
Expand Down
95 changes: 84 additions & 11 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
26 changes: 26 additions & 0 deletions example/format/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
File renamed without changes.
26 changes: 26 additions & 0 deletions example/readme/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
File renamed without changes.
26 changes: 26 additions & 0 deletions example/validate/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
Loading