# file manipulation at configure time or generate time?

**URL:** <https://discourse.cmake.org/t/file-manipulation-at-configure-time-or-generate-time/4655>\
**Category:** Code\
**Created:** [December 13, 2021, 6:35pm UTC](https://discourse.cmake.org/t/file-manipulation-at-configure-time-or-generate-time/4655 "2021-12-13T18:35:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [December 13, 2021, 6:35pm UTC](https://discourse.cmake.org/t/file-manipulation-at-configure-time-or-generate-time/4655/1 "2021-12-13T18:35:57Z")

</div>

It’s not immediately obvious which functions run at which time. For example `file(WRITE | APPEND)` runs at configure time, `file(CONFIGURE)` at generate time.

This code runs at configure time:

```cmake
file(WRITE eval.cmake ...)
include(eval.cmake)

```

This code runs at generate time:

```cmake
file(CONFIGURE OUTPUT eval.cmake CONTENT ...)
# error: include could not find requested file
# include(eval.cmake)

```

file(WRITE) creates the file in the source directory and file(CONFIGURE) in the binary directory. This is hard to remember. Shouldn’t the creation of any file default to the binary directory? I know it makes sense the way it is but it makes it much harder to write CMake code.

Is there any resource get an overview, or a way to make it easier to memorize which functions are used at which time?

---

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [December 13, 2021, 7:22pm UTC](https://discourse.cmake.org/t/file-manipulation-at-configure-time-or-generate-time/4655/2 "2021-12-13T19:22:31Z")

</div>

ah, for `file(GENERATE)` the documentation draws attention to it:

> Note also that `file(GENERATE)` does not create the output file until the generation phase.

Perhaps it is easier to notice when all functions are listed there. Are `file(GENERATE)` and `file(CONFIGURE)` the only one running at generate time?

---

<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:** [December 13, 2021, 10:49pm UTC](https://discourse.cmake.org/t/file-manipulation-at-configure-time-or-generate-time/4655/3 "2021-12-13T22:49:00Z")

</div>

`file(CONFIGURE)` seems like it’d run at configure, not generate, time to me.

> [@hex](#):
>
> ```auto
> file(CONFIGURE OUTPUT eval.cmake CONTENT ...)
> # error: include could not find requested file
> # include(eval.cmake)
> 
> ```

Note that `OUTPUT` will treat relative paths as being relative to `${CMAKE_CURRENT_BINARY_DIR}` while `include()` will look in `CMAKE_MODULE_PATH`. Try doing `include("${CMAKE_CURRENT_BINARY_DIR}/eval.cmake")` instead.
