The problem in your first CMake-command is that you need to wrap it with quotes "
add_custom_command( ... COMMAND "$<$CONFIG:Debug:cmake;-E;echo;foo>" )