Genex JOIN combined with TARGET_OBJECTS unexpected behavior

  1. xxx is a custom target with custom link command
  2. xlib is an OBJECT library with say two objects a.obj and b.obj

I am using the following code:

add_custom_command(
TARGET xxx
PRE_BUILD
COMMENT ${CMAKE_COMMAND} -E echo “$<TARGET_OBJECTS:${xlib}>” >> listfile.txt
COMMAND ${CMAKE_COMMAND} -E echo “$<TARGET_OBJECTS:${xlib}>” >> listfile.txt)

In this case the list file is constructed as expected with the following content:
a.obj;b.obj
The comment generated is also as expected.

Although we need the list file not to have those ; character and with a new line between each file so I thought to use the JOIN function in the following way:

add_custom_command(
TARGET xxx
PRE_BUILD
COMMENT ${CMAKE_COMMAND} -E echo “$<JOIN:$<TARGET_OBJECTS:${xlib}>,\n>” >> listfile.txt
COMMAND ${CMAKE_COMMAND} -E echo “$<JOIN:$<TARGET_OBJECTS:${xlib}>,\n>” >> listfile.txt)

Well in this case the comment generated is as expected but the list file is empty…
Any idea will be welcome.

Thanks,
Serge

I think the problem is not on $<JOIN:...> but rather on the \n character.
For example, with “Unix Makefiles” generator, the makefile generated is erroneous because the \n character introduce unexpected new lines.

The best way to solve this is to create a small script (CMake script is OK) to generate the listfile.txt with the expected format.

Thanks Mark
and yes you are right when trying with some other character, it does the job.
There is no way to encode a special character in the JOIN expression above?

No, there is no mechanism for that. Use a script instead.