Skip to content

Commit

Permalink
Prevent infinite loop in DependentOption
Browse files Browse the repository at this point in the history
Signed-off-by: Tin <[email protected]>
  • Loading branch information
Caellian committed Nov 10, 2023
1 parent cebc045 commit 9d20e72
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmake/DependentOption.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ isn't "possible".
Actual checks are deferred until RUN_DEPENDENCY_CHECKS() is called in order to
allow out of order declaration of dependencies and dependecy graph cycles.
As the checks can affect each other they're run in a loop until the graph settles.
That means CMake can end up in an infinite loop, though it shouldn't happen with
normal use... (i.e. disable A if B not present)
#]=======================================================================]

set(__DEPENDENT_OPTIONS_CHANGE_HAPPENED true)
Expand Down Expand Up @@ -51,10 +49,19 @@ macro(DEPENDENT_OPTION option doc default depends else warn)
endmacro()

macro(RUN_DEPENDENCY_CHECKS)
# controls max allowed dependency chain to avoid infinite loops during
# configure phase
set(__DEPENDENT_OPTIONS_LOOP_COUNTER 10)

while(__DEPENDENT_OPTIONS_CHANGE_HAPPENED)
MATH(EXPR __DEPENDENT_OPTIONS_LOOP_COUNTER "${__DEPENDENT_OPTIONS_LOOP_COUNTER}-1")
set(__DEPENDENT_OPTIONS_CHANGE_HAPPENED false)
cmake_language(EVAL CODE "${__DEPENDENT_OPTIONS_LATER_INVOKED_CODE}")
if(__DEPENDENT_OPTIONS_LOOP_COUNTER LESS_EQUAL "0")
break()
endif()
endwhile()
unset(__DEPENDENT_OPTIONS_LOOP_COUNTER)
set(__DEPENDENT_OPTIONS_CHANGE_HAPPENED true)
set(__DEPENDENT_OPTIONS_LATER_INVOKED_CODE "")
endmacro()
endmacro()

0 comments on commit 9d20e72

Please sign in to comment.