Escaping dollar sign for subshell in custom_command
observed in cmake version 3.5.2
(compiled from source on a Linux Ubuntu (legacy installation)).
When using the posix construct $(instruction)
(actually attempting the make-escape instruction $$(instruction)
substitute with the output of a subshell (equivalent but easier to nest than backticks (instruction
)) in a custom command, The $$(
is expanded to \$$\$$(
in VERBATIM modus, and to $$ (
in non-VERBATIM modus.
A similar issue also arrises with $${ - } (makefile escaped to shell variable expansion).
e.g.
This instruction copies a directory to another host (using tar|ssh tar), then executes make on the remote host, to transfer the result - on success - back to the cmake-running host.
add_custom_target(TARGET ${module}-other
COMMAND tar -cf - | ssh otherhost (T="$(mktemp -d)" && tar -C "$${T}" -xf - && make -C "$${T}" && tar -C "$${T}" -cf - result* | tar -xf -
)
The example is simple an can easily be converted to a working solution:
add_custom_target(TARGET ${module}-other
COMMAND tar -cf - | ssh otherhost (T=`$(mktemp -d)` && tar -C "$$T" -xf - && make -C "$$T" && tar -C "$$T" -cf - result* | tar -xf -
)
However what about this slight twist:
add_custom_target(TARGET ${module}-other
COMMAND tar -cf - | ssh otherhost (T="$(mktemp -d -p "$(custom_dir_outputting)" )" && tar -C "$${T}" -xf - && make -C "$${T}" && tar -C "$${T}" -cf - result* | tar -xf -
)