target link quoted options with variable

Hi,
I need to pass some unorthodox path arguments to the linker, like:

-j"C:\some path\to directory\parser";"C:\some path\to directory\block"

Now my CMakeLists.txt looks like:

...
set(PATH_1 ${PROJECT_SOURCE_DIR}/parser)
set(PATH_2 ${PROJECT_SOURCE_DIR}/block)
string(REPLACE "/" "\\" PATH_1 ${PATH_1})
string(REPLACE "/" "\\" PATH_2 ${PATH_2})
set(paths
    \"${PATH_1}\"
    \"${PATH_2}\")

string(REPLACE ";" "\\;" paths "${paths}")
message("-j${paths}")
target_link_options(${target} PRIVATE
    "-j${paths}")
...

and generated build.ninja contain next line:

LINK_FLAGS = ... -j\"C:\some path\to directory\parser\";\"C:\some path\to directory\block\"

but I would like to see the following result:

LINK_FLAGS = ... -j"C:\some path\to directory\parser";"C:\some path\to directory\block"

Why "\;" works, but "\"" not works?

\; is an oddball artifact of the way CMake’s list representation was “designed”. It passes through rather than having a \ stripped off. Usually I just play around with escaping ; more and more until I get the result I want.

I’ve been playing with escape sequences for about 3 days now and went through a bunch of options. As a result, I cannot get the desired result. Also found these sources in which a similar problem was never properly resolved: 1, 2

Also found a similar problem, but the solution doesn’t work for me: 3

Hmm. Does using LINKER: or SHELL: help at all? See the bottom of the target_link_options docs.

Didn’t help, already tried

I tried:
makerules.cmake

set(CMAKE_CXX_LINKER_WRAPPER_FLAG "-j")
set(CMAKE_CXX_LINKER_WRAPPER_FLAG_SEP ";")

CMakeLists.txt

set(PATH_1 ${PROJECT_SOURCE_DIR}/parser)
set(PATH_2 ${PROJECT_SOURCE_DIR}/block)
string(REPLACE "/" "\\" PATH_1 ${PATH_1})
string(REPLACE "/" "\\" PATH_2 ${PATH_2})
set(paths
    \"${PATH_1}\"
    \"${PATH_2}\")
target_link_options(${target} PRIVATE
    "LINKER:${paths}")

output in build.ninja:

LINK_FLAGS = -j\"C:\some path\to directory\parser\" \"C:\some path\to directory\block\"

This variable is a toolchain-level thing and applies to the entire C++ toolchain. As such, it is only used from the top-level scope and it applies to all such linker flags. The separator variable is the same way.

sorry, update previous message*

last line in toolchain file:

...
set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/makerules.cmake)

I tried to run a build from Linux using Unix Makefiles:
link.txt:

... "-j\"C:\\some path\\to directory\\parser\";\"C:\\some path\\to directory\\block\"" ...

I noticed that when using Ninja, the handling of the "\\" escape sequence was.
Probably the way to solve my problem lies at the stage of passing the string containing the linker flags to the Ninja generator.

Hmm. Please file an issue with the expected command line as you would run it in a shell and how you’ve tried specifying it in CMake code to pass through. The results of each (especially those that are close) would be helpful. If it is per-generator behavior, differences there would also be helpful.

Sorry, quoting in CMake is problematic due to the stringly-typed language, but we try to work around the problem in various ways (such as LINKER: or SHELL:). A VERBATIM: might be warranted.