Skip to content

Commit

Permalink
Allow making CMake options dependent on others
Browse files Browse the repository at this point in the history
  • Loading branch information
msimberg committed Aug 8, 2022
1 parent 2c0b8be commit 9e4a8c5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 127 deletions.
47 changes: 13 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,74 +288,48 @@ pika_option(
)
pika_option(
PIKA_WITH_TESTS_BENCHMARKS BOOL "Build benchmark tests (default: ON)" ON
DEPENDS "PIKA_WITH_TESTS"
CATEGORY "Build Targets"
)
pika_option(
PIKA_WITH_TESTS_REGRESSIONS BOOL "Build regression tests (default: ON)" ON
DEPENDS "PIKA_WITH_TESTS"
CATEGORY "Build Targets"
)
pika_option(
PIKA_WITH_TESTS_UNIT BOOL "Build unit tests (default: ON)" ON
DEPENDS "PIKA_WITH_TESTS"
CATEGORY "Build Targets"
)
pika_option(
PIKA_WITH_TESTS_HEADERS BOOL "Build header tests (default: ON)" ON ADVANCED
DEPENDS "PIKA_WITH_TESTS"
CATEGORY "Build Targets"
)
pika_option(
PIKA_WITH_TESTS_EXTERNAL_BUILD BOOL
"Build external CMake build tests (default: ON)" ON ADVANCED
DEPENDS "PIKA_WITH_TESTS"
CATEGORY "Build Targets"
)
pika_option(
PIKA_WITH_TESTS_EXAMPLES BOOL "Add examples as tests (default: ON)" ON
DEPENDS "PIKA_WITH_TESTS;PIKA_WITH_EXAMPLES"
ADVANCED CATEGORY "Build Targets"
)
pika_option(
PIKA_WITH_COMPILE_ONLY_TESTS BOOL
"Create build system support for compile time only tests (default: ON)" ON
DEPENDS "PIKA_WITH_TESTS"
CATEGORY "Build Targets"
)
pika_option(
PIKA_WITH_FAIL_COMPILE_TESTS BOOL
"Create build system support for fail compile tests (default: ON)" ON
DEPENDS "PIKA_WITH_TESTS"
CATEGORY "Build Targets"
)

# disable all tests if PIKA_WITH_TESTS=OFF
if(NOT PIKA_WITH_TESTS)
pika_set_option(
PIKA_WITH_TESTS_BENCHMARKS
VALUE OFF
FORCE
)
pika_set_option(
PIKA_WITH_TESTS_REGRESSIONS
VALUE OFF
FORCE
)
pika_set_option(
PIKA_WITH_TESTS_UNIT
VALUE OFF
FORCE
)
pika_set_option(
PIKA_WITH_TESTS_HEADERS
VALUE OFF
FORCE
)
pika_set_option(
PIKA_WITH_TESTS_EXTERNAL_BUILD
VALUE OFF
FORCE
)
pika_set_option(
PIKA_WITH_TESTS_EXAMPLES
VALUE OFF
FORCE
)
endif()

pika_option(
PIKA_WITH_TOOLS BOOL "Build tools (default: OFF)" OFF ADVANCED
CATEGORY "Build Targets"
Expand Down Expand Up @@ -790,6 +764,7 @@ pika_option(
PIKA_WITH_EXAMPLES_OPENMP BOOL
"Enable examples requiring OpenMP support (default: OFF)." OFF
CATEGORY "Build Targets"
DEPENDS "PIKA_WITH_EXAMPLES"
ADVANCED
)
if(PIKA_WITH_EXAMPLES_OPENMP)
Expand All @@ -799,6 +774,7 @@ pika_option(
PIKA_WITH_EXAMPLES_TBB BOOL
"Enable examples requiring TBB support (default: OFF)." OFF
CATEGORY "Build Targets"
DEPENDS "PIKA_WITH_EXAMPLES"
ADVANCED
)
if(PIKA_WITH_EXAMPLES_TBB)
Expand All @@ -811,6 +787,7 @@ pika_option(
PIKA_WITH_EXAMPLES_QTHREADS BOOL
"Enable examples requiring QThreads support (default: OFF)." OFF
CATEGORY "Build Targets"
DEPENDS "PIKA_WITH_EXAMPLES"
ADVANCED
)
if(PIKA_WITH_EXAMPLES_QTHREADS)
Expand All @@ -823,6 +800,7 @@ pika_option(
PIKA_WITH_EXAMPLES_HDF5 BOOL
"Enable examples requiring HDF5 support (default: OFF)." OFF
CATEGORY "Build Targets"
DEPENDS "PIKA_WITH_EXAMPLES"
ADVANCED
)
if(PIKA_WITH_EXAMPLES_HDF5)
Expand All @@ -838,6 +816,7 @@ if(NOT "${PIKA_PLATFORM_UC}" STREQUAL "BLUEGENEQ")
PIKA_WITH_EXAMPLES_QT4 BOOL
"Enable examples requiring Qt4 support (default: OFF)." OFF
CATEGORY "Build Targets"
DEPENDS "PIKA_WITH_EXAMPLES"
ADVANCED
)
if(PIKA_WITH_EXAMPLES_QT4)
Expand Down
143 changes: 50 additions & 93 deletions cmake/pika_option.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,75 @@
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

include(CMakeDependentOption)
include(CMakeParseArguments)

set(PIKA_OPTION_CATEGORIES "Generic" "Build Targets" "Thread Manager"
"Profiling" "Debugging" "Modules"
)

function(pika_option option type description default)
macro(pika_option option type description default)
set(options ADVANCED)
set(one_value_args CATEGORY MODULE)
set(one_value_args CATEGORY DEPENDS MODULE)
set(multi_value_args STRINGS)
cmake_parse_arguments(
PIKA_OPTION "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}
)

if(NOT DEFINED ${option})
set(${option}
${default}
CACHE ${type} "${description}" FORCE
)
if(PIKA_OPTION_ADVANCED)
mark_as_advanced(${option})
if("${type}" STREQUAL "BOOL")
# Use regular CMake options for booleans
if(NOT PIKA_OPTION_DEPENDS)
option("${option}" "${description}" "${default}")
else()
cmake_dependent_option("${option}" "${description}" "${default}" "${PIKA_OPTION_DEPENDS}" OFF)
endif()
else()
# make sure that dependent projects can overwrite any of the pika options
unset(${option} PARENT_SCOPE)

get_property(
_option_is_cache_property
CACHE "${option}"
PROPERTY TYPE
SET
)
if(NOT _option_is_cache_property)
set(${option}
${default}
CACHE ${type} "${description}" FORCE
)
if(PIKA_OPTION_ADVANCED)
mark_as_advanced(${option})
endif()
if(PIKA_OPTION_DEPENDS)
message(FATAL_ERROR "pika_option DEPENDS keyword can only be used with BOOL options")
endif()
# Use custom cache variables for other types
if(NOT DEFINED ${option})
set(${option}
${default}
CACHE ${type} "${description}" FORCE
)
else()
set_property(CACHE "${option}" PROPERTY HELPSTRING "${description}")
set_property(CACHE "${option}" PROPERTY TYPE "${type}")
get_property(
_option_is_cache_property
CACHE "${option}"
PROPERTY TYPE
SET
)
if(NOT _option_is_cache_property)
set(${option}
${default}
CACHE ${type} "${description}" FORCE
)
if(PIKA_OPTION_ADVANCED)
mark_as_advanced(${option})
endif()
else()
set_property(CACHE "${option}" PROPERTY HELPSTRING "${description}")
set_property(CACHE "${option}" PROPERTY TYPE "${type}")
endif()
endif()
endif()

if(PIKA_OPTION_STRINGS)
if("${type}" STREQUAL "STRING")
set_property(CACHE "${option}" PROPERTY STRINGS "${PIKA_OPTION_STRINGS}")
else()
message(
FATAL_ERROR
"pika_option(): STRINGS can only be used if type is STRING !"
)
if(PIKA_OPTION_STRINGS)
if("${type}" STREQUAL "STRING")
set_property(CACHE "${option}" PROPERTY STRINGS "${PIKA_OPTION_STRINGS}")
else()
message(
FATAL_ERROR
"pika_option(): STRINGS can only be used if type is STRING !"
)
endif()
endif()
endif()

if(PIKA_OPTION_ADVANCED)
mark_as_advanced(${option})
endif()

if(PIKA_OPTION_MODULE)
string(TOUPPER ${PIKA_OPTION_MODULE} module_uc)
set(varname_uc PIKA_MODULE_CONFIG_${module_uc})
Expand All @@ -78,59 +90,4 @@ function(pika_option option type description default)
${_category}
CACHE INTERNAL ""
)
endfunction()

# simplify setting an option in cache
function(pika_set_option option)
set(options FORCE)
set(one_value_args VALUE TYPE HELPSTRING)
set(multi_value_args)
cmake_parse_arguments(
PIKA_SET_OPTION "${options}" "${one_value_args}" "${multi_value_args}"
${ARGN}
)

if(NOT DEFINED ${option})
pika_error("attempting to set an undefined option: ${option}")
endif()

set(${option}_force)
if(PIKA_SET_OPTION_FORCE)
set(${option}_force FORCE)
endif()

if(PIKA_SET_OPTION_HELPSTRING)
set(${option}_description ${PIKA_SET_OPTION_HELPSTRING})
else()
get_property(
${option}_description
CACHE "${option}"
PROPERTY HELPSTRING
)
endif()

if(PIKA_SET_OPTION_TYPE)
set(${option}_type ${PIKA_SET_OPTION_TYPE})
else()
get_property(
${option}_type
CACHE "${option}"
PROPERTY TYPE
)
endif()

if(DEFINED PIKA_SET_OPTION_VALUE)
set(${option}_value ${PIKA_SET_OPTION_VALUE})
else()
get_property(
${option}_value
CACHE "${option}"
PROPERTY VALUE
)
endif()

set(${option}
${${option}_value}
CACHE ${${option}_type} "${${option}_description}" ${${option}_force}
)
endfunction()
endmacro()

0 comments on commit 9e4a8c5

Please sign in to comment.