# cmake\_dependent\_option with complex conditionals

**URL:** https://discourse.cmake.org/t/cmake-dependent-option-with-complex-conditionals/2793
**Category:** Usage
**Created:** [February 17, 2021, 10:06pm UTC](https://discourse.cmake.org/t/cmake-dependent-option-with-complex-conditionals/2793 "2021-02-17T22:06:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![markd](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@markd](https://discourse.cmake.org/u/markd)
#### Post date: [February 17, 2021, 10:06pm UTC](https://discourse.cmake.org/t/cmake-dependent-option-with-complex-conditionals/2793/1 "2021-02-17T22:06:39Z")

</div>

It seems that cmake\_dependent\_option does not work with complex conditionals. For example:

```
OPTION(OPT1 "Option 1" ON)
OPTION(OPT2 "Option 2" ON)
OPTION(OPT3 "Option 3" OFF)

# Complex condition in if statement
IF (OPT1 AND (OPT2 OR OPT3))
    MESSAGE("true")
ELSE()
    MESSAGE("false")
ENDIF()

# Same condition in CMAKE_DEPENDENT_OPTION doesn't work
CMAKE_DEPENDENT_OPTION(DEP_OPT "dependent option" OFF "OPT1 AND (OPT2 OR OPT3)" OFF)
MESSAGE("DEP OPT = ${DEP_OPT}")

```

The issue is the macro converts each space to a “;” before using the conditional. This step breaks up the complex conditional.  
The documentation mentions that “Each element in the fourth parameter is evaluated as an if- condition”.  
This implies that more complex conditionals (besides the implicit AND of the list) will not work?

Am I missing some way of constructing the depends argument?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 18, 2021, 1:45pm UTC](https://discourse.cmake.org/t/cmake-dependent-option-with-complex-conditionals/2793/2 "2021-02-18T13:45:12Z")

</div>

Hmm. I think I would normally have used `"OPT1;OPT2 OR OPT3"` myself. Maybe it’s the parentheses which confuse things? Does `"OPT1 AND ( OPT2 OR OPT3 )"` work?

---

<div class="post-metadata">

### Author: ![markd](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@markd](https://discourse.cmake.org/u/markd)
#### Post date: [February 22, 2021, 5:16pm UTC](https://discourse.cmake.org/t/cmake-dependent-option-with-complex-conditionals/2793/3 "2021-02-22T17:16:50Z")

</div>

Removing the parenthesis, or adding spaces around the parenthesis works. So “OPT1 AND ( OPT2 OR OPT3 )” also works.

The key lines in the cmake\_dependent\_option macro are:  
foreach(d ${depends})  
string(REGEX REPLACE " +" “;” CMAKE\_DEPENDENT\_OPTION\_DEP “${d}”)  
if(${CMAKE\_DEPENDENT\_OPTION\_DEP})

The REGEX effectively tokenizes the line based on spaces. So “OPT1 AND ( OPT2 OR OPT3 )” becomes “OPT1;AND;(;OPT2;OR;OPT3;)”, but the same expression without spaces “OPT1 AND (OPT2 OR OPT3)” becomes “OPT1;AND;(OPT2;OR;OPT3)”  
My guess is the “if” statement expects input which is a list to be tokenized properly. In the second case, the parenthesis is not in a list element by itself, and so it is not recognized as a special token.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 22, 2021, 8:16pm UTC](https://discourse.cmake.org/t/cmake-dependent-option-with-complex-conditionals/2793/4 "2021-02-22T20:16:52Z")

</div>

Right. I think that makes sense. The way conditional-taking commands work is a bit different, I think, in that they do tokenization on `(` characters. They also “care” about quoting which most other commands just ignore. Maybe all commands do it, but that is likely done before any variable expansion.
