Cmake supports file(GLOB MY_SOURCES src/*) to select files with a wildcard.
This is generally bad: Is it better to specify source files with GLOB or each file individually in CMake?
For a simple example, git mergetool leaves backup files when resolving merge conflicts (In a git merge conflict, what are the BACKUP, BASE, LOCAL, and REMOTE files that are generated?) - with the original extension, not *.bak or something. If using globbing/wildcards in cmake, these will automatically just be included in the next build and it'll fail. There are other gotchas, but this is one.
Some people still insist on using globs/wildcards. Is there a way to filter globbed files to only those tracked by git?
Something like setting MY_SOURCES from shelling out to git ls-files src/*. Can this even be done at configure time?
- This way, cmake would ignore random other files that happen to match the wildcard.
- As a bonus, it'll help catch those times I forget to
git addnew source files before pushing :)
Sure, the project should still compile without the .git directory, so this will need to be a best effort/only-if-available approach that falls back to file(GLOB ...) if git fails. Also still to be frowned upon 🙄.
git ls-files src/*. Can this even be done at configure time?" - Yes, you could use command execute_process for run arbitrary executable with parameters. UsingOUTPUT_VARIABLEoption for that command you could grub output into a variable, and with string command (or with other commands) transform that output into the list of files.file(GLOB): when you add new source file (even withgit addandgit commit), this file won't be used untilcmakewill be manually re-run.*.bak,*~, etc backup files (the previous two examples are common in Emacs-like editors). If you're sure you want to depend on Git, usinggit ls-filesdirectly is an option.