Library target isn't rebuilt when it depends on another custom target.

I made a dependency on a library target for a custom target by
“ADD_DEPENDENCIES(lib_target extern_target)”.
It works well that every time lib_target is built, extern_target will be built.
However, when associated files in extern_taget is updated, it doesn’t introduce a rebuilt on lib_target.

SET(GENERATED_IMAGE ${CMAKE_BINARY_DIR}/test.bin)

ADD_CUSTOM_COMMAND(
    OUTPUT
        ${GENERATED_IMAGE}
    COMMAND
        CROSS_COMPILE=${CROSS_COMPILE}-...
        .....
     DEPENDS
        crm/aaa.c
        crm/bbb.h
        crm/ddd.c
        ...
     )

    ADD_CUSTOM_TARGET(extern_target  DEPENDS  ${GENERATED_IMAGE})
    
    ADD_LIBRARY(lib_target STATIC "")
    TARGET_SOURCES(lib_target 
    PRIVATE
        xxx.c
        yyy.c
 )

ADD_DEPENDENCIES(lib_target  extern_target)

When aaa.c is changed, custom command is executed and bin file is rebuilt.
But I suppose lib_target should be rebuilt as well.

How can I add the dependency?
Thanks.

Yes, this is a strange “feature” I also recently came along.

If you read the documentation on add_dependencies carefully, you can see that it only states:

Makes a top-level <target> depend on other top-level targets to ensure that they build before <target> does

And not: “If a rebuilds, b will also rebuild.”

I don’t know any good solution to your specific problem. I would touch all source files of lib_target in your custom command via generator expression.

Yap, it looks like add_dependencies() just sets a prerequisite of the target.
Thanks for the suggestion and it is what I am doing right now to touch source files.
Just wondering if any other solution here.

If ${GENERATED_IMAGE} is added to a source in lib_target in some way, that should do it. I don’t think there’s a way to do what it appears you’re looking for (cross compile something and then embed that into a static library).

Maybe ben is saying the same as me (I dont understand him, sorry) but a (nice?) solution would be:

ADD_CUSTOM_COMMAND(
    OUTPUT
        ${GENERATED_IMAGE}
       generated.cpp
    COMMAND
        CROSS_COMPILE=${CROSS_COMPILE}-...
        .....
        touch generated.cpp
     DEPENDS
        crm/aaa.c
        crm/bbb.h
        crm/ddd.c
        ...
     )
    
    ADD_LIBRARY(lib_target STATIC "")
    TARGET_SOURCES(lib_target 
    PRIVATE
        xxx.c
        yyy.c
       generated.cpp
 )