0

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?

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58
  • @Tsyvarev - thanks for the info. I know about `LINK_DEPENDS`, but this affects linking of final binaries only. If I do that for my case, then when the regular file is changed, the library is not re-archived and re-processed - only the final executable is re-linked again... – Freddie Chopin Jul 09 '20 at 07:42
  • Sorry, missed the point you build STATIC library which doesn't have link step, only the compile one. Does [OBJECT_DEPENDS](https://cmake.org/cmake/help/v3.18/prop_sf/OBJECT_DEPENDS.html) property works for you, as described in [that question](https://stackoverflow.com/questions/52685787/gcc-included-header-using-include-changes-not-detected-by-cmake) and its answer? I could "swap" the duplicate references, but if you feel that your question needs a completely different answer, I would reopen it. – Tsyvarev Jul 09 '20 at 09:43
  • @Tsyvarev - finally I settled for a `add_custom_command()` which generates an empty source (depending on my regular file), which then gets archived with the library. Adding `OBJECT_DEPENDS` is inconvenient in my case, as I actually compile the files twice, but I need to post-process only one copy of the library. – Freddie Chopin Jul 09 '20 at 09:55
  • 1
    Ok, I have reopened the question. Feel free to write your answer. – Tsyvarev Jul 09 '20 at 10:05

1 Answers1

1

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:

  1. 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.

  2. 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.

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58