custom command not getting executed while building.

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.

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

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.

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?

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.

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.