# Generate lots of files and folder @ build-time

**URL:** <https://discourse.cmake.org/t/generate-lots-of-files-and-folder-build-time/226>\
**Category:** Code\
**Created:** [November 18, 2019, 7:16pm UTC](https://discourse.cmake.org/t/generate-lots-of-files-and-folder-build-time/226 "2019-11-18T19:16:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Milosz\_Walter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a183cd/32.png) [@Milosz\_Walter](https://discourse.cmake.org/u/Milosz_Walter)\
**Post date:** [November 18, 2019, 7:16pm UTC](https://discourse.cmake.org/t/generate-lots-of-files-and-folder-build-time/226/1 "2019-11-18T19:16:57Z")

</div>

Hi All,

I’m currently refactoring an old CMake infrastructure which used to  
generate huge amount of source folders and source files @cmake-config  
step using several different scripts. I have to move them into the build  
step to be able to maintain dependencies between them better.  
Furthermore there are several input files for those scripts available  
at earliest @cmake-gen step. Thus using ‘execute\_process’ is not an  
option anymore.

The problem is I never know how many and which files are really  
generated. The process to deduce those is just to complex to deal with  
it in the config step.

The question is now: could I create an ‘add\_executable’ or ‘add\_library’  
statement without knowing all the relevant source files in advance, yet  
they aren’t available.

Would it help if I rewrite my generator scripts and force them to write  
all the output into one single folder?  
Bit even if, how could I glob them?  
Is there a way to define something like:  
add\_executable(myExec “somePath/\*”)  
This would help a lot.

Thanks in advance for your help  
-Foka

---

<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 19, 2019, 3:29pm UTC](https://discourse.cmake.org/t/generate-lots-of-files-and-folder-build-time/226/2 "2019-11-19T15:29:33Z")

</div>

> could I create an `add_executable` or `add_library` statement without knowing all the relevant source files in advance

The full list of sources needs to be known when CMake runs, though the actual files can be generated at build time.

To generate a bunch of sources not known ahead of time and then use them to build a project, you’ll need to configure build rules to generate the files first and then run CMake on a new inner project at build time. The [ExternalProject](https://cmake.org/cmake/help/v3.16/module/ExternalProject.html) module may help with that. The inner project will need to be given the list of sources in a generated `.cmake` file that it includes (or could use `file(GLOB)` but we do not recommend that in general for collecting source lists).

---

<div class="post-metadata">

**Author:** ![foka](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/4af34b/32.png) [@foka](https://discourse.cmake.org/u/foka)\
**Post date:** [November 20, 2019, 7:53am UTC](https://discourse.cmake.org/t/generate-lots-of-files-and-folder-build-time/226/3 "2019-11-20T07:53:45Z")

</div>

Thanks!  
This is tip looks promising. I’ll give it a shot today.

-Foka
