diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index 31cc85c97..c48bbba78 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -1,40 +1,54 @@ -############################################################################# -# json_feature_summary_begin() -# -# Initialize the feature summary buffer. -############################################################################# +# internal function used to implement json_feature(KEY_VALUE) +function(_json_feature_key_value key value_var) + cmake_parse_arguments(args "NEGATE;RAW" "" "SWITCH;VALUES" ${ARGN}) -function(json_feature_summary_begin) - set(_JSON_FEATURE_SUMMARY_TEXT "" PARENT_SCOPE) -endfunction() - -############################################################################# -# json_feature_summary_end() -# -# Print the feature summary buffer. -############################################################################# - -function(json_feature_summary_end) - if(NOT "${_JSON_FEATURE_SUMMARY_TEXT}" STREQUAL "") - message(STATUS "${_JSON_FEATURE_SUMMARY_TEXT}") + if(NOT "${args_SWITCH}" STREQUAL "") + if(${args_SWITCH}) + # noop + else() + return() + 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() ############################################################################# # json_feature( -# +# KEY_VALUE # [VALUES ...] -# [NEGATE]) +# [RAW] [NEGATE] +# [SWITCH ...]) # -# Append feature info using and the boolean value of converted -# to YES/NO. +# Append feature info using as description text and the boolean value +# of converted to YES/NO. # -# If additional values are given and matches any of them, is not -# converted to YES/NO. +# If RAW is specified, is appended as is. # -# If NEGATE is specified, the boolean value of is negated. +# If additional values are given and matches any of them, +# is not converted to YES/NO. +# +# If NEGATE is specified, the boolean value of is negated. +# +# If a conditional expression consisting of is listed +# following SWITCH, the feature is only added if the expression evaluates to +# TRUE. # # --- # @@ -49,99 +63,132 @@ endfunction() # Append raw text to feature summary. ############################################################################# -function(json_feature) - if(NOT DEFINED _JSON_FEATURE_SUMMARY_TEXT) - message(FATAL_ERROR "json_feature() called before json_feature_summary_begin()") - endif() - - set(var "") - set(text "") - if(${ARGC} EQUAL 1 OR ${ARGC} GREATER 1) - list(GET ARGN 0 var) - endif() - if(${ARGC} EQUAL 2 OR ${ARGC} GREATER 2) - list(GET ARGN 1 text) - endif() - - # json_feature(RAW ) - if("${var}" STREQUAL RAW AND NOT "${text}" STREQUAL "") - set(text " ${text}") - # json_feature(NEWLINE) - elseif("${var}" STREQUAL NEWLINE AND "${text}" STREQUAL "") - # noop - # json_feature( ...) - 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) +macro(json_feature mode) + if("${mode}" STREQUAL KEY_VALUE) + _json_feature_key_value(${ARGN}) + elseif("${mode}" STREQUAL RAW) + list(LENGTH ARGN len) + if(${len} EQUAL 1) + list(APPEND _JSON_FEATURE_SUMMARY R ${ARGN} _) + else() + message(FATAL_ERROR "json_feature(RAW) called with incorrect arguments") endif() - set(text " ${text} ${state}") + elseif("${mode}" STREQUAL NEWLINE) + list(APPEND _JSON_FEATURE_SUMMARY N _ _) else() message(FATAL_ERROR "json_feature() called with incorrect arguments") endif() +endmacro() - # add preamble if buffer is empty - if("${_JSON_FEATURE_SUMMARY_TEXT}" STREQUAL "") - set(_JSON_FEATURE_SUMMARY_TEXT "[nohmann_json]: Feature summary:\n") - else() - set(_JSON_FEATURE_SUMMARY_TEXT "${_JSON_FEATURE_SUMMARY_TEXT}\n") +############################################################################# +# json_print_feature_summary() +# +# Build and print the feature summary. +############################################################################# + +function(json_print_feature_summary) + if(NOT DEFINED _JSON_FEATURE_SUMMARY) + return() + endif() + if("${_JSON_FEATURE_SUMMARY}" STREQUAL "") + return() endif() - # append to buffer - set(_JSON_FEATURE_SUMMARY_TEXT "${_JSON_FEATURE_SUMMARY_TEXT}${text}" PARENT_SCOPE) + # calculate maximum key length + 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() ############################################################################# # print feature summary ############################################################################# -json_feature_summary_begin() +json_feature(KEY_VALUE "Build tests?" JSON_BuildTests) -json_feature(JSON_BuildTests "Build tests?") if(JSON_BuildTests) - json_feature(JSON_32bitTest "Build the 32bit unit test?" VALUES AUTO ONLY) - json_feature(JSON_FastTests "Skip expensive/slow tests?") + json_feature(KEY_VALUE "Build the 32bit unit test?" JSON_32bitTest VALUES AUTO ONLY) + json_feature(KEY_VALUE "Skip expensive/slow tests?" JSON_FastTests) if(JSON_TestDataDirectory) - json_feature(RAW "Test data directory: ${JSON_TestDataDirectory}") + json_feature(KEY_VALUE "Test data directory:" "${JSON_TestDataDirectory}" RAW) 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() - json_feature(JSON_Valgrind "Execute test suite with Valgrind?") + json_feature(KEY_VALUE "Execute test suite with Valgrind?" JSON_Valgrind) if(JSON_Valgrind) 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() list(JOIN JSON_TEST_CXX_STANDARDS_FEATURE_INFO " " test_cxx_standards) if(JSON_TEST_CXX_STANDARDS_FORCED) set(test_cxx_standards "${test_cxx_standards} (forced)") endif() - json_feature(RAW "Test C++ standards: ${test_cxx_standards}") + json_feature(KEY_VALUE "Test C++ standards:" "${test_cxx_standards}" RAW) endif() json_feature(NEWLINE) -json_feature(JSON_Diagnostics "Diagnostics enabled?") -json_feature(JSON_DisableEnumSerialization "Default integer enum serialization enabled?" NEGATE) -json_feature(JSON_GlobalUDLs "Define user-defined string literals globally?") -json_feature(JSON_ImplicitConversions "Implicit conversions enabled?") -json_feature(JSON_LegacyDiscardedValueComparison "Legacy discarded value comparison enabled?") +json_feature(KEY_VALUE "Diagnostics enabled?" JSON_Diagnostics SWITCH JSON_Diagnostics) +json_feature(KEY_VALUE "Default integer enum serialization enabled?" JSON_DisableEnumSerialization NEGATE) +json_feature(KEY_VALUE "Define user-defined string literals globally?" JSON_GlobalUDLs) +json_feature(KEY_VALUE "Implicit conversions enabled?" JSON_ImplicitConversions) +json_feature(KEY_VALUE "Legacy discarded value comparison enabled?" JSON_LegacyDiscardedValueComparison) json_feature(NEWLINE) -json_feature(JSON_MultipleHeaders "Use the multi-header code?") -json_feature(RAW "Include directory: ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") -json_feature(JSON_SystemInclude "Include as system headers?") +json_feature(KEY_VALUE "Use the multi-header code?" JSON_MultipleHeaders) +json_feature(KEY_VALUE "Include directory:" "${NLOHMANN_JSON_INCLUDE_BUILD_DIR}" RAW) +json_feature(KEY_VALUE "Include as system headers?" JSON_SystemInclude) -json_feature_summary_end() +json_print_feature_summary()