# Escaping semicolons in CMAKE\_CONFIGURE\_DEPENDS?

**URL:** <https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447>\
**Category:** Code\
**Created:** [January 2, 2020, 8:43am UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447 "2020-01-02T08:43:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinda](https://discourse.cmake.org/user_avatar/discourse.cmake.org/spinda/32/271_2.png) [@spinda](https://discourse.cmake.org/u/spinda)\
**Post date:** [January 2, 2020, 8:43am UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447/1 "2020-01-02T08:43:57Z")

</div>

The [documentation](https://cmake.org/cmake/help/v3.16/prop_dir/CMAKE_CONFIGURE_DEPENDS.html) for `CMAKE_CONFIGURE_DEPENDS` states:

> Specify files as a semicolon-separated list of paths. Relative paths are interpreted as relative to the current source directory.

What should I do when the path itself contains a semicolon? How can I prevent CMake from incorrectly splitting it in two?

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [January 2, 2020, 9:43am UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447/2 "2020-01-02T09:43:33Z")

</div>

You can escape the semicolon with a backslash.

```auto
set(a "blue;red")
list(LENGTH a a_length)
set(b "blue\;red")
list(LENGTH b b_length)
message("${a_length} vs. ${b_length}")

```

---

<div class="post-metadata">

**Author:** ![spinda](https://discourse.cmake.org/user_avatar/discourse.cmake.org/spinda/32/271_2.png) [@spinda](https://discourse.cmake.org/u/spinda)\
**Post date:** [January 2, 2020, 10:24am UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447/3 "2020-01-02T10:24:05Z")

</div>

Ah, so `CMAKE_CONFIGURE_DEPENDS` follows the list expansion rules, then. Thanks!

What if I want a list with an unbalanced number of `[` and `]` in a given item? For example, how can I represent a list where the first item is `[` and the second is `]`?

```cmake
set(foo "[;]")
list(LENGTH foo foo_length)
message("foo_length = ${foo}")

```

```auto
foo_length = 1

```

The unbalanced `[` causes the list expansion rules to skip over the `;` separator.

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [January 2, 2020, 8:39pm UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447/4 "2020-01-02T20:39:20Z")

</div>

> [@spinda](#):
>
> What if I want a list with an unbalanced number of `[` and `]` in a given item?

You simply can’t…

There are two comments in the CMake code that make it very explicit:

```cpp
  // Break the string at non-escaped semicolons not nested in [].

```

([https://gitlab.kitware.com/cmake/cmake/blob/v3.16.2/Source%2FcmStringAlgorithms.cxx#L93](https://gitlab.kitware.com/cmake/cmake/blob/v3.16.2/Source%2FcmStringAlgorithms.cxx#L93))

and

```cpp
        // We only want to allow escaping of semicolons. Other
        // escapes should not be processed here.

```

([https://gitlab.kitware.com/cmake/cmake/blob/v3.16.2/Source%2FcmStringAlgorithms.cxx#L100-101](https://gitlab.kitware.com/cmake/cmake/blob/v3.16.2/Source%2FcmStringAlgorithms.cxx#L100-101))

---

<div class="post-metadata">

**Author:** ![spinda](https://discourse.cmake.org/user_avatar/discourse.cmake.org/spinda/32/271_2.png) [@spinda](https://discourse.cmake.org/u/spinda)\
**Post date:** [January 2, 2020, 9:54pm UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447/5 "2020-01-02T21:54:44Z")

</div>

I’ve seen those comments, yeah. Was holding out hope for some arcane, undocumented workaround. 😓

What’s the purpose of this “feature”, anyway? I would understand if the brackets were stripped out and just functioned as a method of avoiding manually escaping the individual semicolons between them. I can’t think of a use case where you’d want a list item containing semicolons _and_ surrounded by actual brackets.

EDIT: Answering my own question - tracked down the original commit responsible, and the answer is Windows registry keys, which are surrounded by `[` `]` and may contain `;`.

[https://gitlab.kitware.com/cmake/cmake/commit/2b4e802a947ad5fd86b1ed296ea2f31714270dfa](https://gitlab.kitware.com/cmake/cmake/commit/2b4e802a947ad5fd86b1ed296ea2f31714270dfa)

It just occurred to me that there’s no way to end a list item in a `\`, either, unless the item is at the very end of the list (so, better make sure no one passes in a Windows directory path with a trailing backslash). Oh, what a headache this is…

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [January 2, 2020, 10:23pm UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447/6 "2020-01-02T22:23:20Z")

</div>

> [@spinda](#):
>
> It just occurred to me that there’s no way to end a list item in a `\`

That happened to someone using a CMake module I wrote, and they were very confused ([Unknown keyword: "Use static runtime" · Issue #533 · McMartin/FRUT · GitHub](https://github.com/McMartin/FRUT/issues/533)).

> [@spinda](#):
>
> better make sure no one passes in a Windows directory path with a trailing backslash

You can sanitize user input using [`file(TO_CMAKE_PATH "<path>" <variable>)`](https://cmake.org/cmake/help/latest/command/file.html#path-conversion).

---

<div class="post-metadata">

**Author:** ![spinda](https://discourse.cmake.org/user_avatar/discourse.cmake.org/spinda/32/271_2.png) [@spinda](https://discourse.cmake.org/u/spinda)\
**Post date:** [January 2, 2020, 10:26pm UTC](https://discourse.cmake.org/t/escaping-semicolons-in-cmake-configure-depends/447/7 "2020-01-02T22:26:49Z")

</div>

Thanks for the pointer toward `TO_CMAKE_PATH`. That helps a bit. Of course, it’d be better if CMake lists behaved in a reasonable way in the first place.
