# How to run different add\_custom\_command for each specific target?

**URL:** <https://discourse.cmake.org/t/how-to-run-different-add-custom-command-for-each-specific-target/5438>\
**Category:** Code\
**Created:** [April 11, 2022, 11:21am UTC](https://discourse.cmake.org/t/how-to-run-different-add-custom-command-for-each-specific-target/5438 "2022-04-11T11:21:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leo\_Abalckin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leo_abalckin/32/2340_2.png) [@Leo\_Abalckin](https://discourse.cmake.org/u/Leo_Abalckin)\
**Post date:** [April 11, 2022, 11:21am UTC](https://discourse.cmake.org/t/how-to-run-different-add-custom-command-for-each-specific-target/5438/1 "2022-04-11T11:21:45Z")

</div>

I work with Visual Studio and have different paths to work for different configurations (Debug, Release):

```auto
if(WIN32 )
    set(static_release ${my_lib}_release/SomeLibrary.lib)
    set(static_debug ${my_lib}_debug/SomeLibrary.lib)
endif(WIN32)

```

Then I want to run different commands \<\<add\_custom\_command(TARGET\>\> for those different configurations as follows:

# A command which can be applied for Debug configuration:

```auto
add_custom_command(TARGET mylib
                   POST_BUILD
				   COMMAND lib /out:$<TARGET_FILE:mylib> $<TARGET_FILE:mylib> ${static_debug}
                   COMMENT "Concatenating static libs"
				   CONFIG Debug)

```

# A command which can be applied for Release configuration:

```auto
add_custom_command(TARGET mylib
                   POST_BUILD
				   COMMAND lib /out:$<TARGET_FILE:mylib> $<TARGET_FILE:mylib> ${static_release}
                   COMMENT "Concatenating static libs"
				   CONFIG Release)

```

Unfortunately, Visual Studio runs both of those custom commands for each configuration.  
Is it possible to run only needed custom command for each configuration?

I also tried to use

```auto
add_custom_command(TARGET myLib
                   POST_BUILD
				   COMMAND lib /out:$<TARGET_FILE:myLib> $<TARGET_FILE:myLib> "$<IF:$<CONFIG:Debug>,${static_debug},$<CONFIG:Release>,${static_release}>"
                   COMMENT "Concatenating static libs"
				   CONFIG "$<IF:$<CONFIG:Debug>,Debug,$<CONFIG:Release>,Release")

```

but it doesn’t work at all.

---

<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:** [April 12, 2022, 11:50am UTC](https://discourse.cmake.org/t/how-to-run-different-add-custom-command-for-each-specific-target/5438/2 "2022-04-12T11:50:42Z")

</div>

[This issue](https://gitlab.kitware.com/cmake/cmake/-/issues/22679) may be of interest to you for the long-term.

> [@Leo\_Abalckin](#):
>
> I also tried to use
> 
> ```auto
> add_custom_command(TARGET myLib
> POST_BUILD
> COMMAND lib /out:$<TARGET_FILE:myLib> $<TARGET_FILE:myLib> "$<IF:$<CONFIG:Debug>,${static_debug},$<CONFIG:Release>,${static_release}>"
> COMMENT "Concatenating static libs"
> CONFIG "$<IF:$<CONFIG:Debug>,Debug,$<CONFIG:Release>,Release")
> 
> ```
> 
> but it doesn’t work at all.

You’re missing a `>` at the end of that `CONFIG` argument (for the `$<IF>`).
