does POST_BUILD work with add_custom_target and Visual Studio?

I build a static library and then want to add two more static libs to it after it is built.
The documentation for add_custom_command/“BUILD EVENTS” says

The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.

I have a command like

add_custom_command( TARGET something
    POST_BUILD
    COMMAND lib $<TARGET_FILE:something> $<TARGET_FILE:extraLib> $<TARGET_FILE:anotherLib>
  )

However, in practice, this always creates a post build command in Visual Studio, and the libs are linked every time, leading to a ton of warnings.

Maybe explain what you’re trying to achieve by adding other libraries to a static library. That seems strange, and likely isn’t what you’re ultimately needing to do, but without understanding the use case, it’s hard to tell.

For better or worse, we want to deliver a single library to our clients. This library includes code we build as well as two third party libraries. So we build our lib, then add the other two after it has built.

That is unlikely to work. You’d need to add the contents of the other two libraries, not the libraries themselves. This has come a few times before, but I wasn’t able to find the past posts here in this form or the CMake issue tracker from a very quick scan. This stackoverflow answer contains the general gist though.

Edit: This issue is probably getting closer to discussing your scenario.

Those links describe what we’re trying to achieve. We want to merge our static lib with two other static libs.

The add_custom_command I posted does this. However, it always executes even when “TARGET something” is already built.

Let me put it a different way: what if the custom command looks like this:

add_custom_command( TARGET something
    POST_BUILD
    COMMAND copy nul "foo.txt"
  )

I would expect to get a file named foo.txt but only when building “something”. As it is, I get a file named foo.txt even though “something” isn’t rebuilt. Am I misinterpreting the documentation for add_custom_target?

If the target is already built, the command will not execute.

Targets from add_custom_target are always rebuilt. So adding a post build command should always run.

Can the linker grouping feature worked on by @marc.chevrier be used to add some --whole-archive flag or the like?

The goal of the feature WHOLE_ARCHIVE, which must be used with $<LINK_LIBRARY> genex, is to load all objects of an archive file specified during the link of a shared library or an executable.

This is not at all usable to aggregate multiple archives into another one.

Another discussion for this problem in this issue.