Sunday, April 3, 2022

[SOLVED] cmake parse arguments incorrectly in add_custom_target command

Issue

I wrote a cmake command like this:

add_custom_target(testar
              COMMAND clearmake -C gnu ${CMD_ARGS})

the CMD_ARGS is defined on the command line like:

cmake -DCMD_ARGS="-d -w" 

But in the generated makefile, the -d -w is changed into -d\ -w; it added a slash before all spaces, resulting in:

clearmake -C gnu -d\ -w 

If I use VERBATIM option in add_custom_target, cmake doesn't add a slash, but it quotes the argument like

clearmake -C gnu "-d -w"

which is incorrect, I would like:

clearmake -C gnu -d -w

What is the syntax needed to generate the above target?


Solution

The arguments are expected to be a list, which "-d -w" is not (it's just a string). You can do two things:

  1. Pass in the arguments as -DCMD_ARGS="-d;-w" (the space is a semicolon)
  2. Use the separate_arguments command on CMD_ARGS before you pass it into add_custom_target (which makes spaces semi-colons to generate a proper list).

Nothing in the add_custom_target command needs to change, the input to CMake is incorrect which can be fixed with 1 or handled by 2.



Answered By - tpg2114
Answer Checked By - Marilyn (WPSolving Volunteer)