Custom command output location

When using a multi-config generator like Visual Studio, objects and libraries are (by default) placed in ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>. Now, suppose I have a library target, myLib and I want to process it with some script ${PROJECT_SOURCE_DIR}/tools/process.bat and place its output next to myLib. I try:

add_custom_command(
    OUTPUT $<TARGET_FILE_DIR:myLib>/myLib.txt
    COMMAND ${PROJECT_SOURCE_DIR}/tools/process.bat $<TARGET_FILE:myLib> myLib.txt
    DEPENDS myLib
    WORKING_DIRECTORY $<TARGET_FILE_DIR:myLib>
    VERBATIM)

But then I get:

add_custom_command called with OUTPUT containing a "<".  This character is not allowed.

How am I supposed to get per-configuration custom command outputs?

For some cases, you can use the variable CMAKE_CFG_INTDIR containing a buildtool-specific string which expands to the active configuration at build time.

For more complex cases, sometimes you have to bite the bullet and create a separate custom command for each configuration and then “assign” it to the appropriate configuration by other means (such as a config-conditional genex in the list of sources, or creating a custom target for each config, or …)

1 Like

Thank you, that worked for me!

I also saw that OUTPUT is getting support for generator expressions in 3.20, so that’s cool!

That’s not just “cool”, that’s absolutely fantastic!, at least in my view. Thanks for letting me know. Oh, the number of workarounds I had to write around that one…

Now, I can just hope I will be given the time to reverse them all :slight_smile: