fixup! Add custom CMake feature summary

This commit is contained in:
Florian Albrechtskirchinger 2022-09-26 11:01:23 +02:00
parent 239a974fe1
commit 0ea7ec0864
No known key found for this signature in database
GPG Key ID: 19618CE9B2D4BE6D

View File

@ -1,40 +1,54 @@
############################################################################# # internal function used to implement json_feature(KEY_VALUE)
# json_feature_summary_begin() function(_json_feature_key_value key value_var)
# cmake_parse_arguments(args "NEGATE;RAW" "" "SWITCH;VALUES" ${ARGN})
# Initialize the feature summary buffer.
#############################################################################
function(json_feature_summary_begin) if(NOT "${args_SWITCH}" STREQUAL "")
set(_JSON_FEATURE_SUMMARY_TEXT "" PARENT_SCOPE) if(${args_SWITCH})
endfunction() # noop
else()
############################################################################# return()
# json_feature_summary_end() endif()
#
# Print the feature summary buffer.
#############################################################################
function(json_feature_summary_end)
if(NOT "${_JSON_FEATURE_SUMMARY_TEXT}" STREQUAL "")
message(STATUS "${_JSON_FEATURE_SUMMARY_TEXT}")
endif() endif()
unset(_JSON_FEATURE_SUMMARY_TEXT PARENT_SCOPE) if(${args_RAW})
set(value "${value_var}")
else()
set(value NO)
if(args_VALUES)
foreach(val ${args_VALUES})
if(${value_var} STREQUAL val)
set(value ${val})
break()
endif()
endforeach()
elseif(${args_NEGATE} AND NOT ${value_var} OR ${value_var})
set(value YES)
endif()
endif()
list(APPEND _JSON_FEATURE_SUMMARY KV "${key}" "${value}")
set(_JSON_FEATURE_SUMMARY "${_JSON_FEATURE_SUMMARY}" PARENT_SCOPE)
endfunction() endfunction()
############################################################################# #############################################################################
# json_feature( # json_feature(
# <var> <text> # KEY_VALUE <key> <value_var>
# [VALUES <value>...] # [VALUES <value>...]
# [NEGATE]) # [RAW] [NEGATE]
# [SWITCH <conditional_args>...])
# #
# Append feature info using <text> and the boolean value of <var> converted # Append feature info using <key> as description text and the boolean value
# to YES/NO. # of <value_var> converted to YES/NO.
# #
# If additional values are given and <var> matches any of them, <var> is not # If RAW is specified, <value_var> is appended as is.
# converted to YES/NO.
# #
# If NEGATE is specified, the boolean value of <var> is negated. # If additional values are given and <value_var> matches any of them,
# <value_var> is not converted to YES/NO.
#
# If NEGATE is specified, the boolean value of <value_var> is negated.
#
# If a conditional expression consisting of <conditional_args> is listed
# following SWITCH, the feature is only added if the expression evaluates to
# TRUE.
# #
# --- # ---
# #
@ -49,99 +63,132 @@ endfunction()
# Append raw text <text> to feature summary. # Append raw text <text> to feature summary.
############################################################################# #############################################################################
function(json_feature) macro(json_feature mode)
if(NOT DEFINED _JSON_FEATURE_SUMMARY_TEXT) if("${mode}" STREQUAL KEY_VALUE)
message(FATAL_ERROR "json_feature() called before json_feature_summary_begin()") _json_feature_key_value(${ARGN})
endif() elseif("${mode}" STREQUAL RAW)
list(LENGTH ARGN len)
set(var "") if(${len} EQUAL 1)
set(text "") list(APPEND _JSON_FEATURE_SUMMARY R ${ARGN} _)
if(${ARGC} EQUAL 1 OR ${ARGC} GREATER 1) else()
list(GET ARGN 0 var) message(FATAL_ERROR "json_feature(RAW) called with incorrect arguments")
endif()
if(${ARGC} EQUAL 2 OR ${ARGC} GREATER 2)
list(GET ARGN 1 text)
endif()
# json_feature(RAW <text>)
if("${var}" STREQUAL RAW AND NOT "${text}" STREQUAL "")
set(text " ${text}")
# json_feature(NEWLINE)
elseif("${var}" STREQUAL NEWLINE AND "${text}" STREQUAL "")
# noop
# json_feature(<var> <text> ...)
elseif(NOT "${var}" STREQUAL "" AND NOT "${text}" STREQUAL "")
cmake_parse_arguments(args "NEGATE" "" "VALUES" ${ARGN})
set(state NO)
if(args_VALUES)
foreach(value ${args_VALUES})
if(${var} STREQUAL value)
set(state ${value})
break()
endif()
endforeach()
elseif(${args_NEGATE} AND NOT ${var} OR ${var})
set(state YES)
endif() endif()
set(text " ${text} ${state}") elseif("${mode}" STREQUAL NEWLINE)
list(APPEND _JSON_FEATURE_SUMMARY N _ _)
else() else()
message(FATAL_ERROR "json_feature() called with incorrect arguments") message(FATAL_ERROR "json_feature() called with incorrect arguments")
endif() endif()
endmacro()
# add preamble if buffer is empty #############################################################################
if("${_JSON_FEATURE_SUMMARY_TEXT}" STREQUAL "") # json_print_feature_summary()
set(_JSON_FEATURE_SUMMARY_TEXT "[nohmann_json]: Feature summary:\n") #
else() # Build and print the feature summary.
set(_JSON_FEATURE_SUMMARY_TEXT "${_JSON_FEATURE_SUMMARY_TEXT}\n") #############################################################################
function(json_print_feature_summary)
if(NOT DEFINED _JSON_FEATURE_SUMMARY)
return()
endif()
if("${_JSON_FEATURE_SUMMARY}" STREQUAL "")
return()
endif() endif()
# append to buffer # calculate maximum key length
set(_JSON_FEATURE_SUMMARY_TEXT "${_JSON_FEATURE_SUMMARY_TEXT}${text}" PARENT_SCOPE) set(max_key_len 0)
set(feature_summary "${_JSON_FEATURE_SUMMARY}")
while(NOT "${feature_summary}" STREQUAL "")
list(GET feature_summary 0 mode)
list(GET feature_summary 1 key)
list(REMOVE_AT feature_summary 0 1 2)
if("${mode}" STREQUAL KV)
string(LENGTH "${key}" len)
if(${len} GREATER ${max_key_len})
set(max_key_len ${len})
endif()
endif()
endwhile()
# build feature summary string
set(last_newline FALSE)
set(text "[nohmann_json]: Feature summary:")
while(NOT "${_JSON_FEATURE_SUMMARY}" STREQUAL "")
list(GET _JSON_FEATURE_SUMMARY 0 mode)
list(GET _JSON_FEATURE_SUMMARY 1 key)
list(GET _JSON_FEATURE_SUMMARY 2 value)
list(REMOVE_AT _JSON_FEATURE_SUMMARY 0 1 2)
if("${mode}" STREQUAL KV)
# pad key to max_key_len with spaces
string(LENGTH "${key}" len)
math(EXPR pad_len "${max_key_len} - ${len}")
set(pad "")
if(${pad_len} GREATER 1)
foreach(i RANGE 1 ${pad_len})
set(pad "${pad} ")
endforeach()
endif()
set(text "${text}\n ${key}${pad} ${value}")
set(last_newline FALSE)
elseif("${mode}" STREQUAL R)
set(text "${text}\n ${key}")
set(last_newline FALSE)
elseif("${mode}" STREQUAL N AND NOT ${last_newline})
if("${_JSON_FEATURE_SUMMARY}" STREQUAL "")
# skip trailing newline
break()
endif()
set(text "${text}\n")
set(last_newline TRUE)
endif()
endwhile()
message(STATUS "${text}")
endfunction() endfunction()
############################################################################# #############################################################################
# print feature summary # print feature summary
############################################################################# #############################################################################
json_feature_summary_begin() json_feature(KEY_VALUE "Build tests?" JSON_BuildTests)
json_feature(JSON_BuildTests "Build tests?")
if(JSON_BuildTests) if(JSON_BuildTests)
json_feature(JSON_32bitTest "Build the 32bit unit test?" VALUES AUTO ONLY) json_feature(KEY_VALUE "Build the 32bit unit test?" JSON_32bitTest VALUES AUTO ONLY)
json_feature(JSON_FastTests "Skip expensive/slow tests?") json_feature(KEY_VALUE "Skip expensive/slow tests?" JSON_FastTests)
if(JSON_TestDataDirectory) if(JSON_TestDataDirectory)
json_feature(RAW "Test data directory: ${JSON_TestDataDirectory}") json_feature(KEY_VALUE "Test data directory:" "${JSON_TestDataDirectory}" RAW)
else() else()
json_feature(RAW "Test data source: ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})") json_feature(KEY_VALUE "Test data source:" "${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})" RAW)
endif() endif()
json_feature(JSON_Valgrind "Execute test suite with Valgrind?") json_feature(KEY_VALUE "Execute test suite with Valgrind?" JSON_Valgrind)
if(JSON_Valgrind) if(JSON_Valgrind)
string (REPLACE ";" " " memcheck_command "${CMAKE_MEMORYCHECK_COMMAND};${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}") string (REPLACE ";" " " memcheck_command "${CMAKE_MEMORYCHECK_COMMAND};${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}")
json_feature(RAW "Valgrind command: ${memcheck_command}") json_feature(KEY_VALUE "Valgrind command:" "${memcheck_command}" RAW)
endif() endif()
list(JOIN JSON_TEST_CXX_STANDARDS_FEATURE_INFO " " test_cxx_standards) list(JOIN JSON_TEST_CXX_STANDARDS_FEATURE_INFO " " test_cxx_standards)
if(JSON_TEST_CXX_STANDARDS_FORCED) if(JSON_TEST_CXX_STANDARDS_FORCED)
set(test_cxx_standards "${test_cxx_standards} (forced)") set(test_cxx_standards "${test_cxx_standards} (forced)")
endif() endif()
json_feature(RAW "Test C++ standards: ${test_cxx_standards}") json_feature(KEY_VALUE "Test C++ standards:" "${test_cxx_standards}" RAW)
endif() endif()
json_feature(NEWLINE) json_feature(NEWLINE)
json_feature(JSON_Diagnostics "Diagnostics enabled?") json_feature(KEY_VALUE "Diagnostics enabled?" JSON_Diagnostics SWITCH JSON_Diagnostics)
json_feature(JSON_DisableEnumSerialization "Default integer enum serialization enabled?" NEGATE) json_feature(KEY_VALUE "Default integer enum serialization enabled?" JSON_DisableEnumSerialization NEGATE)
json_feature(JSON_GlobalUDLs "Define user-defined string literals globally?") json_feature(KEY_VALUE "Define user-defined string literals globally?" JSON_GlobalUDLs)
json_feature(JSON_ImplicitConversions "Implicit conversions enabled?") json_feature(KEY_VALUE "Implicit conversions enabled?" JSON_ImplicitConversions)
json_feature(JSON_LegacyDiscardedValueComparison "Legacy discarded value comparison enabled?") json_feature(KEY_VALUE "Legacy discarded value comparison enabled?" JSON_LegacyDiscardedValueComparison)
json_feature(NEWLINE) json_feature(NEWLINE)
json_feature(JSON_MultipleHeaders "Use the multi-header code?") json_feature(KEY_VALUE "Use the multi-header code?" JSON_MultipleHeaders)
json_feature(RAW "Include directory: ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") json_feature(KEY_VALUE "Include directory:" "${NLOHMANN_JSON_INCLUDE_BUILD_DIR}" RAW)
json_feature(JSON_SystemInclude "Include as system headers?") json_feature(KEY_VALUE "Include as system headers?" JSON_SystemInclude)
json_feature_summary_end() json_print_feature_summary()