Reputation: 8860
Due to very special needs, I have a static library which I need to "post-process" after it is created. So I have this:
add_library(CANopenNode2 STATIC
...)
add_custom_command(TARGET CANopenNode2 POST_BUILD
COMMAND objcopy --redefine-syms=renames.txt $<TARGET_FILE:CANopenNode2>)
My problem here is that the whole build step does not depend on renames.txt
regular file - if I change it, the library does not get re-archived or re-processed. The add_custom_command(TARGET ... POST_BUILD ...)
signature does not accept any DEPENDS
argument. I've tried some regular tricks with creating a custom command (depending on that file), wrapped in custom target, which is then marked as a dependency for my static library CANopenNode2
, but I most likely did something wrong, as it did not work at all. As I later need this "post-processed" library to be linked to my executable, I would prefer to work with POST_BUILD
somehow, as I guess that using a regular custom command (which can depend on a file) would also be complicated, just in some other aspect, but I'm obviously open to suggestions.
Is there any way I could simply make the whole library depend on this regular file, or is this yet another thing that CMake makes extremely complicated?
Upvotes: 1
Views: 541
Reputation: 8860
In my case the best solution I came up with is this:
add_custom_command(OUTPUT CANopenNode2.c
COMMAND ${CMAKE_COMMAND} -E touch CANopenNode2.c
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/renames.txt)
add_library(CANopenNode2 STATIC
...
CANopenNode2.c)
add_custom_command(TARGET CANopenNode2 POST_BUILD
COMMAND objcopy --redefine-syms=${CMAKE_CURRENT_LIST_DIR}/renames.txt $<TARGET_FILE:CANopenNode2>)
This basically generates an empty source file (depending on my regular renames.txt
file), which then gets archived in the static library along with other objects. Whenever renames.txt
changes, this empty source is regenerated, the library is re-archived and reprocessed correctly.
Two other solutions that I've tried or considered:
LINK_DEPENDS
property on the static library. Won't work, because static library does not have a link step. This is the best solution when you build an executable, but for a static library it's not useful.
OBJECT_DEPENDS
property on one of the source files that are archived in the static library. This would work fine, however in my case it would be inconvenient, as I compile the sources twice, generate two static libraries, but only want to post-process one of them. With this approach when renames.txt
file would change, CMake would rebuild both static libraries, while I need to rebuild only one of them.
Upvotes: 1