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.