# configure\_file: generate code dependant on an if clause ?

**URL:** <https://discourse.cmake.org/t/configure-file-generate-code-dependant-on-an-if-clause/3269>\
**Category:** Code\
**Created:** [May 5, 2021, 11:17am UTC](https://discourse.cmake.org/t/configure-file-generate-code-dependant-on-an-if-clause/3269 "2021-05-05T11:17:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProTurm2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/proturm2/32/1061_2.png) [@ProTurm2](https://discourse.cmake.org/u/ProTurm2)\
**Post date:** [May 5, 2021, 11:17am UTC](https://discourse.cmake.org/t/configure-file-generate-code-dependant-on-an-if-clause/3269/1 "2021-05-05T11:17:54Z")

</div>

Id like to generate a .h file containing code dependent on an if clause set in the input file of the `configure_file`.

```
if (VAR)
 generate this line;
else()
 generate that line;
endif()

```

As for now, Im getting in the .h code all of the above lines generated.

Is there a way to generate only specific lines, either of the `if()` or of the `else()` from the input file? Or is there any `other way` except for the `configure_file` to this?

---

<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:** [May 5, 2021, 1:56pm UTC](https://discourse.cmake.org/t/configure-file-generate-code-dependant-on-an-if-clause/3269/2 "2021-05-05T13:56:00Z")

</div>

`configure_file` does not support CMake code in the files. All it does is expand variable references. So what you probably want is closer to:

```auto
my configured file
@dependent_line@

```

```cmake
if (special)
  set(dependent_line "is very special")
else ()
  set(dependent_line "is very boring")
endif ()
configure_file(input output)

```

---

<div class="post-metadata">

**Author:** ![ProTurm2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/proturm2/32/1061_2.png) [@ProTurm2](https://discourse.cmake.org/u/ProTurm2)\
**Post date:** [May 5, 2021, 1:57pm UTC](https://discourse.cmake.org/t/configure-file-generate-code-dependant-on-an-if-clause/3269/3 "2021-05-05T13:57:51Z")

</div>

Yes, I ended up with a similar solution.

Would be nice if this would have been done by the configure\_file though.

---

<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:** [May 5, 2021, 2:02pm UTC](https://discourse.cmake.org/t/configure-file-generate-code-dependant-on-an-if-clause/3269/4 "2021-05-05T14:02:35Z")

</div>

The way that CMake code works, this would involve running the top-level command parser over the input file. That parser is not very robust to non-CMake code, so some way of encoding what is something that `configure_file` should look at and ignore is a tough problem. Consider:

```cmake
if (bar)
if (foo)
  @expand_a@
else ()
  @expand_b@
endif ()
endif ()

```

Give this to `configure_file`. Is `if (bar)` something it should care about or something for `configure_file`? How about `if (foo)`? While it’d be nice, it’s just never going to be feasible, sorry.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [May 5, 2021, 2:07pm UTC](https://discourse.cmake.org/t/configure-file-generate-code-dependant-on-an-if-clause/3269/5 "2021-05-05T14:07:12Z")

</div>

This is basically a request for a templating engine built into `configure_file()`. Does not seem like a feasible idea.
