# custom command not getting executed while building.

**URL:** <https://discourse.cmake.org/t/custom-command-not-getting-executed-while-building/8190>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [May 24, 2023, 9:07am UTC](https://discourse.cmake.org/t/custom-command-not-getting-executed-while-building/8190 "2023-05-24T09:07:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![insight](https://discourse.cmake.org/user_avatar/discourse.cmake.org/insight/32/3468_2.png) [@insight](https://discourse.cmake.org/u/insight)\
**Post date:** [May 24, 2023, 9:07am UTC](https://discourse.cmake.org/t/custom-command-not-getting-executed-while-building/8190/1 "2023-05-24T09:07:06Z")

</div>

Hello all,

I got provided with many CMakeLists files which on building generate a final executable. I am trying to write a custom command which on building should spit out .ll files from the C files, so I can be using those ll files for further analysis purposes.

Below is the code for the same, where sources is a variable that contains the list of C files, while the private\_headers contain the header files.

```auto
add_library(target_name SHARED ${sources} ${private_headers})

foreach(C_FILE ${sources})
  get_filename_component(EXT ${C_FILE} LAST_EXT)
  if(".c" STREQUAL ${EXT})
    get_filename_component(C_FILE_NAME ${C_FILE} NAME_WE)
    set(LL_FILE ${C_FILE_NAME}.ll)
${CMAKE_CURRENT_SOURCE_DIR}/${C_FILE}")
    add_custom_command(
      TARGET target_name
      POST_BUILD
      COMMAND /usr/bin/clang -c -emit-llvm ${CMAKE_CURRENT_SOURCE_DIR}/${C_FILE}
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${C_FILE} ${sources} ${private_headers} 
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
      )
    endif()
endforeach()

```

There is no issue in the above as a SHARED library is getting created and the custom command is getting executed successfully POST\_BUILD.

But there are a few files where the add\_library is creating an INTERFACE instead and interfaces don’t support the PRE/POST\_BUILD.

So to get around it I create a custom\_target which depends on that library as below

```auto
add_library(inter_lib INTERFACE)
add_custom_target(my_target DEPENDS inter_lib)

```

and now I am trying to run the custom command using the above target name ( **my\_target** in this case) but the custom commands are not getting executed.

Can anyone help me with where exactly I am going wrong in this case? I also can’t explicitly change the library type from INTERFACE to STATIC or SHARED as it may have some adverse effect on the analysis.

Thank you.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [May 24, 2023, 10:23pm UTC](https://discourse.cmake.org/t/custom-command-not-getting-executed-while-building/8190/2 "2023-05-24T22:23:59Z")

</div>

For the `INTERFACE` case, I’m assuming you don’t use the `POST_BUILD` keyword in your `add_custom_target()` call (it wouldn’t make sense, if I understand your scenario correctly)? Does adding `ALL` to your `add_custom_target()` call give you the behavior you’re looking for?

---

<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 29, 2023, 12:49pm UTC](https://discourse.cmake.org/t/custom-command-not-getting-executed-while-building/8190/3 "2023-05-29T12:49:51Z")

</div>

Besides the target-level question, I just want to note that I think this is also missing any flags that come from the target itself (and therefore any inherited usage requirements). Source-specific flags are also being lost here.

---

<div class="post-metadata">

**Author:** ![insight](https://discourse.cmake.org/user_avatar/discourse.cmake.org/insight/32/3468_2.png) [@insight](https://discourse.cmake.org/u/insight)\
**Post date:** [July 4, 2023, 9:16pm UTC](https://discourse.cmake.org/t/custom-command-not-getting-executed-while-building/8190/4 "2023-07-04T21:16:43Z")

</div>

Sorry for the late reply. But add\_custom\_target did work but I had to remove the **depends on** the interface to make it work. This was just a small test sample to make sure whether the required files are getting generated or not. So I can’t comment how it will play in the big picture.
