Limit on add_custom_command COMMAND arguments

Hi,

Recently I have run into a problem where the data I pass to COMMAND in an add_custom_command,
like this:

add_custom_command(
OUTPUT
"${CMAKE_BINARY_DIR}/list.txt"
COMMAND
python3 "${CMAKE_SOURCE_DIR}/make_list.py"
"${CMAKE_BINARY_DIR}/list.txt"
$<TARGET_OBJECTS:foo>
COMMAND_EXPAND_LISTS
DEPENDS
$<TARGET_OBJECTS:foo>
"${CMAKE_SOURCE_DIR}/build/ada_obj_to_ali.py"
VERBATIM
)

results in make_list.py have some corrupted values in argv (the majority
are correct, though).

This doesn’t happen on Linux, or Windows 10. What we have seen, though,
is that this code snippet fails on Windows Server 2012 and 2016.

I am in the process of making a reproducer (if I can) so you have something
to go on for a diagnosis.

So, for now, the question is there a know limit on how many arguments can
be passed to COMMAND?

Thanks,

Tom

If you have many object files, you may be hitting Windows command line length limits. A more robust arrangement may be to write the file list into a file and read that file from make_list.py. It might look something like this (untested, and I leave the modification of make_list.py up to you):

set(out_file "${CMAKE_BINARY_DIR}/list.txt")
set(objects_file "${CMAKE_CURRENT_BINARY_DIR}/file_list.txt")
file(GENERATE
    OUTPUT  "${objects_file}"
    CONTENT "$<JOIN:$<TARGET_OBJECTS:foo>,\n>"
)
add_custom_command(
    OUTPUT "${out_file}"
    COMMAND python3 "${CMAKE_SOURCE_DIR}/make_list.py" "${out_file}" "${objects_file}"
    DEPENDS
        "${objects_file}"
        $<TARGET_OBJECTS:foo>
        "${CMAKE_SOURCE_DIR}/build/ada_obj_to_ali.py"
    VERBATIM
)

Thank you Craig,

We had originally used file(GENERATE ...), but we wanted to have targets and dependencies to trigger the generation of the files only when necessary.

Tom

file(GENERATE) should only replace the file if the contents change.