diff --git a/cmake/ecbuild_add_option.cmake b/cmake/ecbuild_add_option.cmake index ed3de7fb..9900d3de 100644 --- a/cmake/ecbuild_add_option.cmake +++ b/cmake/ecbuild_add_option.cmake @@ -109,7 +109,7 @@ macro( ecbuild_add_option ) set( _p_DEFAULT ON ) else() if( NOT _p_DEFAULT MATCHES "[Oo][Nn]" AND NOT _p_DEFAULT MATCHES "[Oo][Ff][Ff]" ) - ecbuild_critical("In macro ecbuild_add_option(), DEFAULT is either ON or OFF: \"${_p_DEFAULT}\"") + ecbuild_critical("In macro ecbuild_add_option(), DEFAULT must be either ON or OFF, but found: \"${_p_DEFAULT}\"") endif() endif() ecbuild_debug("ecbuild_add_option(${_p_FEATURE}): defaults to ${_p_DEFAULT}") @@ -153,12 +153,6 @@ macro( ecbuild_add_option ) # define the option -- for cmake GUI option( ENABLE_${_p_FEATURE} "${_p_DESCRIPTION}" ${_p_DEFAULT} ) - get_property( _feature_desc GLOBAL PROPERTY _CMAKE_${_p_FEATURE}_DESCRIPTION ) - if( _feature_desc ) - add_feature_info( ${_p_FEATURE} ENABLE_${_p_FEATURE} "${_feature_desc}, ${PROJECT_NAME}: ${_p_DESCRIPTION}" ) - else() - add_feature_info( ${_p_FEATURE} ENABLE_${_p_FEATURE} "${PROJECT_NAME}: ${_p_DESCRIPTION}" ) - endif() ecbuild_debug("ecbuild_add_option(${_p_FEATURE}): defining option ENABLE_${_p_FEATURE} '${_p_DESCRIPTION}' ${_p_DEFAULT}") ecbuild_debug("ecbuild_add_option(${_p_FEATURE}): ENABLE_${_p_FEATURE}=${ENABLE_${_p_FEATURE}}") @@ -176,6 +170,26 @@ macro( ecbuild_add_option ) set( ENABLE_${_p_FEATURE} ${${PNAME}_ENABLE_${_p_FEATURE}} ) endif() + ## Update the description of the feature summary + # Choose the correct tick + if (ENABLE_${_p_FEATURE}) + set ( _tick "✔") + else() + set ( _tick "✘") + endif() + set(_enabled "${ENABLE_${_p_FEATURE}}") + get_property( _enabled_features GLOBAL PROPERTY ENABLED_FEATURES ) + if( "${_p_FEATURE}" IN_LIST _enabled_features ) + set(_enabled ON) + endif() + # Retrieve any existing description (n.b. occurs when the same feature is added at multiple projects) + get_property( _feature_desc GLOBAL PROPERTY _CMAKE_${_p_FEATURE}_DESCRIPTION ) + # Append the new description + if( _feature_desc ) + add_feature_info( ${_p_FEATURE} ${_enabled} "${_feature_desc}, ${PROJECT_NAME}(${_tick}): '${_p_DESCRIPTION}'" ) + else() + add_feature_info( ${_p_FEATURE} ${_enabled} "${PROJECT_NAME}(${_tick}): '${_p_DESCRIPTION}'" ) + endif() set( ${PROJECT_NAME}_HAVE_${_p_FEATURE} 0 ) @@ -250,7 +264,7 @@ macro( ecbuild_add_option ) else() # if user provided input and we cannot satisfy FAIL otherwise WARN - ecbuild_disable_feature( ${_p_FEATURE} ) + ecbuild_disable_unused_feature( ${_p_FEATURE} ) if( ${_p_FEATURE}_user_provided_input ) if( NOT _${_p_FEATURE}_condition ) @@ -267,16 +281,16 @@ macro( ecbuild_add_option ) ecbuild_info( "Feature ${_p_FEATURE} was not enabled (also not requested) -- following required packages weren't found: ${_failed_to_find_packages}" ) endif() set( ENABLE_${_p_FEATURE} OFF ) - ecbuild_disable_feature( ${_p_FEATURE} ) + ecbuild_disable_unused_feature( ${_p_FEATURE} ) endif() endif() else() - ecbuild_debug("ecbuild_add_option(${_p_FEATURE}): feature disabled") + ecbuild_info( "Feature ${_p_FEATURE} disabled" ) set( ${PROJECT_NAME}_HAVE_${_p_FEATURE} 0 ) - ecbuild_disable_feature( ${_p_FEATURE} ) + ecbuild_disable_unused_feature( ${_p_FEATURE} ) endif() diff --git a/cmake/ecbuild_features.cmake b/cmake/ecbuild_features.cmake index 6c049486..8bf57fbb 100644 --- a/cmake/ecbuild_features.cmake +++ b/cmake/ecbuild_features.cmake @@ -55,3 +55,11 @@ function( ecbuild_disable_feature _name ) set_property(GLOBAL PROPERTY DISABLED_FEATURES "${_disabled_features}" ) endfunction() + +# Disable the feature ${_name} globally (if it has not been enabled in any subproject) +function( ecbuild_disable_unused_feature _name ) + get_property( _enabled GLOBAL PROPERTY ENABLED_FEATURES ) + if ( NOT _name IN_LIST _enabled ) # if not already disabled + ecbuild_disable_feature( ${_name} ) + endif() +endfunction() \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 472c97c3..fa0c3d4b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,6 +7,7 @@ add_subdirectory( ecbuild_find_package ) add_subdirectory( ecbuild_add_option ) add_subdirectory( ecbuild_add_flags ) add_subdirectory( find_ecbuild ) -add_subdirectory( project_import ) add_subdirectory( ecbuild_shared_libs ) add_subdirectory( interface_library ) +add_subdirectory( project_import ) +add_subdirectory( project_summary ) diff --git a/tests/project_summary/CMakeLists.txt b/tests/project_summary/CMakeLists.txt new file mode 100644 index 00000000..d6c558ae --- /dev/null +++ b/tests/project_summary/CMakeLists.txt @@ -0,0 +1,7 @@ + +ecbuild_add_test( + TARGET test_ecbuild_project_summary + TYPE SCRIPT + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/configure.sh + ENVIRONMENT CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR} +) diff --git a/tests/project_summary/clean.sh b/tests/project_summary/clean.sh new file mode 100755 index 00000000..3d88a017 --- /dev/null +++ b/tests/project_summary/clean.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -e + +HERE=${CMAKE_CURRENT_BINARY_DIR:-"$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd -P )"} + +# --------------------- cleanup ------------------------ +echo "cleaning $HERE" +rm -rf $HERE/build_* diff --git a/tests/project_summary/configure.sh b/tests/project_summary/configure.sh new file mode 100755 index 00000000..d1434e1f --- /dev/null +++ b/tests/project_summary/configure.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +set -e + +function EXPECT_ONE_OF() +{ + local file=$1 + local pattern=$2 + local found=$(cat ${file} | grep "${pattern}" | wc -l | xargs) + + if [ "$found" != "1" ]; then + echo "File ${file} does not contain exacly one of '$2'" + exit 1 + fi +} + +HERE=${CMAKE_CURRENT_BINARY_DIR:-"$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd -P )"} +SOURCE=${CMAKE_CURRENT_SOURCE_DIR:-$HERE} + +# Add ecbuild to path +export PATH=$SOURCE/../../bin:$PATH +echo $PATH +echo $SOURCE + +# --------------------- cleanup ------------------------ +$SOURCE/clean.sh + +# ----------------- configure project --------------------- + +# Options: -DENABLE_MYFEATURE=ON + +mkdir -p $HERE/build_1 +ecbuild -DENABLE_MYFEATURE=ON $SOURCE/test_project -B $HERE/build_1 | tee $HERE/build_1.log +EXPECT_ONE_OF $HERE/build_1.log "* MYFEATURE, proja(✔): '', projb(✔): ''" + +# Options: -DENABLE_MYFEATURE=ON -DPROJB_ENABLE_MYFEATURE=OFF + +mkdir -p $HERE/build_2 +ecbuild -DENABLE_MYFEATURE=ON -DPROJB_ENABLE_MYFEATURE=OFF $SOURCE/test_project -B $HERE/build_2 | tee $HERE/build_2.log +EXPECT_ONE_OF $HERE/build_2.log "* MYFEATURE, proja(✔): '', projb(✘): ''" + +# Options: -DENABLE_MYFEATURE=OFF -DPROJB_ENABLE_MYFEATURE=ON + +mkdir -p $HERE/build_3 +ecbuild -DENABLE_MYFEATURE=OFF -DPROJB_ENABLE_MYFEATURE=ON $SOURCE/test_project -B $HERE/build_3 | tee $HERE/build_3.log +EXPECT_ONE_OF $HERE/build_3.log "* MYFEATURE, proja(✘): '', projb(✔): ''" diff --git a/tests/project_summary/test_project/CMakeLists.txt b/tests/project_summary/test_project/CMakeLists.txt new file mode 100644 index 00000000..2a97a33e --- /dev/null +++ b/tests/project_summary/test_project/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required( VERSION 3.18.4 FATAL_ERROR ) +find_package( ecbuild 3.4 REQUIRED ) + +project( proja LANGUAGES NONE VERSION 0.2 ) +ecbuild_add_option( + FEATURE MYFEATURE + DEFAULT ON +) + +project( projb LANGUAGES NONE VERSION 0.1 ) +ecbuild_add_option( + FEATURE MYFEATURE + DEFAULT OFF +) + +ecbuild_print_summary() \ No newline at end of file