0

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 add new 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 🙄.

4
  • "Something like setting MY_SOURCES from shelling out to 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. Using OUTPUT_VARIABLE option 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. Commented Dec 15, 2022 at 21:29
  • Note, that given approach suffers from the same problems as file(GLOB): when you add new source file (even with git add and git commit), this file won't be used until cmake will be manually re-run. Commented Dec 15, 2022 at 21:32
  • Git isn't particularly relevant here as other things may leave *.bak, *~, etc backup files (the previous two examples are common in Emacs-like editors). If you're sure you want to depend on Git, using git ls-files directly is an option. Commented Dec 16, 2022 at 9:38
  • Thanks, @Tsyvarev. Will give it a shot, maybe with ideas from here for dealing with spaces. Commented Dec 20, 2022 at 21:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.