# Passing escaped strings from macros to functions

**URL:** <https://discourse.cmake.org/t/passing-escaped-strings-from-macros-to-functions/196>\
**Category:** Code\
**Created:** [November 13, 2019, 4:00pm UTC](https://discourse.cmake.org/t/passing-escaped-strings-from-macros-to-functions/196 "2019-11-13T16:00:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)\
**Post date:** [November 13, 2019, 4:00pm UTC](https://discourse.cmake.org/t/passing-escaped-strings-from-macros-to-functions/196/1 "2019-11-13T16:00:26Z")

</div>

Hi,

The following project:

```
cmake_minimum_required(VERSION 3.15)

project(test_quoting_macro_and_function)

function(my_func)
    message("func ARGV: ${ARGV}")
endfunction()

macro(my_macro)
    message("macro ARGV: ${ARGV}")
endmacro()

my_func (LIBS_SUFFIX="\\\\"_${CMAKE_SYSTEM_PROCESSOR}.so\\\\"")
my_macro(LIBS_SUFFIX="\\\\"_${CMAKE_SYSTEM_PROCESSOR}.so\\\\"")

```

generates the following output:

```
func ARGV: LIBS_SUFFIX="\\"_x86_64.so\\""
macro ARGV: LIBS_SUFFIX="\"_x86_64.so\""

```

In the macro case. a pair of slashes disappear.

My ultimate goal is to allow passing a value like `LIBS_SUFFIX="\\\\"_${CMAKE_SYSTEM_PROCESSOR}.so\\\\""` to a macro, and within that macro to forward the exact same value to a function.

So something like:

```
function(foo)
# do stuff
endfunction()
macro(wrapper)
 foo(${ARGV})
endmacro()

```

Is this somehow achievable? Am supposed to do something like? What else would i be missing then?  
`string(REPLACE "\\" "\\\\" my_var ${ARGV})`

The reasoning is to provide a compatibility api for a function that got renamed, and thus the arguments from the macro should be sent verbatim to the function.

Thanks.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [November 13, 2019, 6:30pm UTC](https://discourse.cmake.org/t/passing-escaped-strings-from-macros-to-functions/196/2 "2019-11-13T18:30:33Z")

</div>

Macros are weird. They act akin to C’s preprocessor. First, the argument variables are expanded within the body of the macro definition and then it is executed. This is why you lose a level of escaping through it. Do you need it to be a macro? `set(PARENT_SCOPE)` can help avoid using it if it is sufficient. I don’t know of a way to avoid the escaping problem because the argument is only available via the initial expansion (`if (DEFINED argname)` is false in a macro unless it leaks from the calling scope).

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [November 13, 2019, 6:32pm UTC](https://discourse.cmake.org/t/passing-escaped-strings-from-macros-to-functions/196/3 "2019-11-13T18:32:16Z")

</div>

Relevant issues:

- [19281: string escape sequence depends on macro call nesting level](https://gitlab.kitware.com/cmake/cmake/issues/19281)
- [19972: Different escaping in macros and functions?](https://gitlab.kitware.com/cmake/cmake/issues/19972): Closed in favor of this discourse topic.

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [November 13, 2019, 6:33pm UTC](https://discourse.cmake.org/t/passing-escaped-strings-from-macros-to-functions/196/4 "2019-11-13T18:33:02Z")

</div>

The `string(REPLACE)` approach to re-escape backslashes within macros before forwarding them on should be fine.

---

<div class="post-metadata">

**Author:** ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)\
**Post date:** [November 13, 2019, 6:46pm UTC](https://discourse.cmake.org/t/passing-escaped-strings-from-macros-to-functions/196/5 "2019-11-13T18:46:37Z")

</div>

Ok, thanks for the answers!

set(var PARENT\_SCOPE) + function only works if you know the whole set of variables that you need to propagate from within the function to the outside function.

After some consideration, I believe function wrapping instead of macro wrapping will work fine for me.

But it’s good to know that it’s a semi-issue.
