Turn list into arguments for cmake function
<p>So let's say we have a CMake function called <code>func</code> which takes named arguments, say <code>NAMED1</code> and <code>NAMED2</code>. The first argument is mandatory and the second is optional. For example</p>

<pre><code>func(NAMED1 foo NAMED2 optional)
</code></pre>

<p>Now I have a loop which needs to call this function <code>func</code> at each iteration, but depending on the current iteration I sometimes need to supply the optional value and sometimes not. So at each iteration I build a list of the arguments, say</p>

<pre><code>list(APPEND args "NAMED1" "foo")
if (...something...)
    list(APPEND args "NAMED2" "optional")
endif()
</code></pre>

<p>So I have the <code>args</code> list now. As a string, it may look like</p>

<pre><code>NAMED1;foo
</code></pre>

<p>or it may look like</p>

<pre><code>NAMED1;foo;NAMED2;optional
</code></pre>

<p>So I thought to string replace the whole list with</p>

<pre><code>string(REPLACE ";" " " args "${args}")
</code></pre>

<p>Then the resulting <code>args</code> will look like</p>

<pre><code>NAMED1 foo NAMED2 optional
</code></pre>

<p>So I thought to pass this string to the function <code>func</code>, with</p>

<pre><code>func("${args}")
</code></pre>

<p>But the problem now is that CMake thinks this is one big string, while it should interpret it as separate strings. How can I achieve this?</p>