# bison-like tools run in both Debug & Release (in Visual Studio) ?

**URL:** https://discourse.cmake.org/t/bison-like-tools-run-in-both-debug-release-in-visual-studio/5152
**Category:** Usage
**Created:** [March 3, 2022, 9:40am UTC](https://discourse.cmake.org/t/bison-like-tools-run-in-both-debug-release-in-visual-studio/5152 "2022-03-03T09:40:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![johan556](https://discourse.cmake.org/user_avatar/discourse.cmake.org/johan556/32/788_2.png) [@johan556](https://discourse.cmake.org/u/johan556)
#### Post date: [March 3, 2022, 9:40am UTC](https://discourse.cmake.org/t/bison-like-tools-run-in-both-debug-release-in-visual-studio/5152/1 "2022-03-03T09:40:08Z")

</div>

We have an in-house code generator similar to Bison (generating both h- and cpp-files). We describe it with an add\_custom\_command() call. The generation of the h/cpp-files is independent of Debug/Release, so I had assumed that CMake + Visual Studio would arrange for the files to be generated just once.

But the behavior I see is:

- first Debug build → generates the files
- first Release build → generates the files again
- later Debug build → no further generation

The CMake fragment we use look like:

```auto
add_custom_command(
  OUTPUT
     ${CMAKE_BINARY_DIR}/foo.h
     ${CMAKE_BINARY_DIR}/foo.cpp
  DEPENDS
     ${CMAKE_SOURCE_DIR}/foo.spec
  COMMAND
     ${foogen} ${CMAKE_SOURCE_DIR}/foo.spec --outdir ${CMAKE_BINARY_DIR}
  )

target_sources(prog1 PUBLIC
    prog1.cpp
    ${CMAKE_BINARY_DIR}/foo.h
    ${CMAKE_BINARY_DIR}/foo.cpp
    )

```

The second generation of the files also causes unnecessary rebuild of other sources depending on the h-file.

Am I using add\_custom\_command() the wrong way? Or is there a better way to do this?

Regards,  
/Johan Holmberg

---

<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: [March 3, 2022, 2:01pm UTC](https://discourse.cmake.org/t/bison-like-tools-run-in-both-debug-release-in-visual-studio/5152/2 "2022-03-03T14:01:46Z")

</div>

In VS, the commands are duplicated (IIRC) and stamp files might be used behind the scenes.

A more robust way would be to generate into a temporary file and then `cmake -E copy_if_different` it into the final location.

---

<div class="post-metadata">

### Author: ![johan556](https://discourse.cmake.org/user_avatar/discourse.cmake.org/johan556/32/788_2.png) [@johan556](https://discourse.cmake.org/u/johan556)
#### Post date: [March 4, 2022, 3:25pm UTC](https://discourse.cmake.org/t/bison-like-tools-run-in-both-debug-release-in-visual-studio/5152/3 "2022-03-04T15:25:10Z")

</div>

Thanks, I will try that.

Currently I have already changed the add\_custom\_command() to put the output files in a configuration dependent location (via $\<CONFIG\> generator expression). That way I still get the files generated twice, but “Debug” and “Release” no longer interfer with each other. Not optimal, but better than before for us.

---

<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: [March 4, 2022, 8:39pm UTC](https://discourse.cmake.org/t/bison-like-tools-run-in-both-debug-release-in-visual-studio/5152/4 "2022-03-04T20:39:54Z")

</div>

I think a `$<CONFIG>`-using pattern is likely the simpler solution long-term.
