# Replacing a list of sources with an extraction from the target later

**URL:** <https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404>\
**Category:** Code\
**Created:** [November 10, 2023, 12:19pm UTC](https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404 "2023-11-10T12:19:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)\
**Post date:** [November 10, 2023, 12:19pm UTC](https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404/1 "2023-11-10T12:19:33Z")

</div>

Sorry for a very rookie question so please flame away.  
I had a list of sources that were passed around and I wanted to use `target_sources` instead so I can use more “elegant” genex processing of including various sources based on build options etc, instead of crude `if`s. This works well if I only build the thing.  
Now the problem I had overlooked is that this list of sources was also used as input to a custom script. I thought I could then just get the sources out of the target via a property and feed it to the script and it would look the same. Not so easy alas.  
Eg one parameter to this script is a semicolon separated list of files which was previously created with `separate_arguments(res UNIX_COMMAND "${some_sources};${other_sources}")`. The output of this is a list of files with the semicolon escaped, i.e. `file1\;file2`.  
Replacing this with `separate_arguments(res UNIX_COMMAND "$<TARGET_PROPERTY:my_tgt,INTERFACE_SOURCES>;${other_sources}")` gives the sources from the target as just semicolon separated (not escaped that is) and the sources from `${other_sources}` (a conventional list) as a “escaped semicolon” separated string.

Is there some good way of forcing the genex to be evaluated before I call the script so I can post-process it into the required shape? Or is it perhaps better to use some other method than this unorthodox usage of `separate_arguments` to create an “escaped semicolon” separated string?  
Or perhaps I should just keep the list of sources and avoid the genexing.

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [November 10, 2023, 1:32pm UTC](https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404/2 "2023-11-10T13:32:34Z")

</div>

You can use `file(GENERATE)` and `$<LIST:JOIN,...,...>` to solve the generator expressions and convert it to space separated for use in a script.

---

<div class="post-metadata">

**Author:** ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)\
**Post date:** [November 10, 2023, 1:49pm UTC](https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404/3 "2023-11-10T13:49:36Z")

</div>

Thanks. Would this entail then putting the list in this file and then reading it back up again to have it evaluated? If so I think that’s enough overhead so I’ll just stick with a non-genex list floating around :).

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [November 10, 2023, 6:02pm UTC](https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404/4 "2023-11-10T18:02:08Z")

</div>

> [@eriols](#):
>
> input to a custom script

You wrote that. Maybe you can show how that file list interacts with your script?

---

<div class="post-metadata">

**Author:** ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)\
**Post date:** [November 13, 2023, 9:02am UTC](https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404/5 "2023-11-13T09:02:26Z")

</div>

Yes, so I just tried to say that if I have to change the interface to this script (to read from a file instead of taking a parameter) then I’ll refrain from the change. It’s a custom python script that takes a bunch of parameters, one of them being a semicolon-separated string of source file names.  
A little pity that the genex style comes with a few rather hard-to-swallow differences and/or drawbacks from using temporary variables.

---

<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 18, 2023, 8:10pm UTC](https://discourse.cmake.org/t/replacing-a-list-of-sources-with-an-extraction-from-the-target-later/9404/6 "2023-11-18T20:10:25Z")

</div>

> [@eriols](#):
>
> It’s a custom python script that takes a bunch of parameters, one of them being a semicolon-separated string of source file names.

Have it take them as separate arguments. You can then just use unquoted CMake expansion (possibly with `VERBATIM`) to get proper empty element dropping and quoting.
