Escaping for variable content in a add_custom_command

The following CMakeLists.txt:

cmake_minimum_required(VERSION 3.30)
project(ProjectName)

set(F "Test with spaces and (parens)")

add_custom_target(t ALL COMMAND doit ${F})

Generate the folloing command line:

doit Test\ with\ spaces\ and\ (parens)

As we can see, spaces are correctly escaped, but not the parentheses? Why is that so?

Add the VERBATIM keyword to your add_custom_target() call. That should handle escaping more robustly, but I don’t know off-hand if it handles parentheses for your case.