Skip to content
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

Allow distinction between enabled/disabled features #69

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions cmake/ecbuild_add_option.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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}}")
Expand All @@ -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 )

Expand Down Expand Up @@ -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 )
Expand All @@ -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()

Expand Down
8 changes: 8 additions & 0 deletions cmake/ecbuild_features.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
7 changes: 7 additions & 0 deletions tests/project_summary/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
9 changes: 9 additions & 0 deletions tests/project_summary/clean.sh
Original file line number Diff line number Diff line change
@@ -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_*
46 changes: 46 additions & 0 deletions tests/project_summary/configure.sh
Original file line number Diff line number Diff line change
@@ -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(✔): ''"
16 changes: 16 additions & 0 deletions tests/project_summary/test_project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Loading