# CMake's add\_custom\_command doesnt trigger rebuild of library

**URL:** https://discourse.cmake.org/t/cmakes-add-custom-command-doesnt-trigger-rebuild-of-library/2376
**Category:** Usage
**Created:** [December 15, 2020, 2:00pm UTC](https://discourse.cmake.org/t/cmakes-add-custom-command-doesnt-trigger-rebuild-of-library/2376 "2020-12-15T14:00:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![skreyer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@skreyer](https://discourse.cmake.org/u/skreyer)
#### Post date: [December 15, 2020, 2:00pm UTC](https://discourse.cmake.org/t/cmakes-add-custom-command-doesnt-trigger-rebuild-of-library/2376/1 "2020-12-15T14:00:17Z")

</div>

Hello,

Iam using CMake together with ninja to build a library. The library is depending on some code which may be generated before-hand by a custom command. The source for this code is within the source-tree and it must stay there, I have no freedom here.

Here’s my CMake code:

```auto
add_library(some_lib some_source.c)

#some_source.c may be modified by the following custom command
add_custom_command(
    COMMAND codegen.exe -i some_input.xml
    COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_BINARY_DIR}/generated"
    OUTPUT ${CMAKE_BINARY_DIR}/generated
    DEPENDS some_input.xml
    COMMENT "Generating code ..."
)
add_custom_target(generate_something DEPENDS ${CMAKE_BINARY_DIR}/generated)
add_dependencies(some_lib generate_something)

```

Now if some\_input.xml is changed I want to also rebuild some\_lib. However in practice this code doesnt seem to work, the command is executed but after it is executed some\_lib is not beeing rebuild, though the timestamps of the output files (some\_source.c) of the custom command are newer than the library.

Can someone give me a hint on what am I doing wrong or how I can achieve this? Or is there a problem with CMake and ninja?

Thanks in advance, if you need more information please let me know.

Steve

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [December 15, 2020, 2:50pm UTC](https://discourse.cmake.org/t/cmakes-add-custom-command-doesnt-trigger-rebuild-of-library/2376/2 "2020-12-15T14:50:07Z")

</div>

If the custom command has `some_source.c` as output, specify it in `OUTPUT` argument so `CMake` can manage the dependencies:

```cmake
add_custom_command(
    COMMAND codegen.exe -i some_input.xml
    COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_BINARY_DIR}/generated"
    OUTPUT ${CMAKE_BINARY_DIR}/generated some_source.c
    DEPENDS some_input.xml
    COMMENT "Generating code ..."
)
add_custom_target(generate_something DEPENDS ${CMAKE_BINARY_DIR}/generated)

add_library(some_lib some_source.c)

```

---

<div class="post-metadata">

### Author: ![skreyer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@skreyer](https://discourse.cmake.org/u/skreyer)
#### Post date: [December 18, 2020, 12:00pm UTC](https://discourse.cmake.org/t/cmakes-add-custom-command-doesnt-trigger-rebuild-of-library/2376/3 "2020-12-18T12:00:18Z")

</div>

Hi Marc,

thanks für your answer. I didnt want the file some\_source.c to be deleted if a “cmake --build clean” is issued, thats why the OUTPUT option was no option for me. But I found the custom command option BYPRODUCTS. After specifying the sources which are modified by the generator tool to this option everything worked as expected.

Thanks very much for your help.

Kind Regards, Steve
