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 .

torek
  • 448,244
  • 59
  • 642
  • 775
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • "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](https://cmake.org/cmake/help/latest/command/execute_process.html) for run arbitrary executable with parameters. Using `OUTPUT_VARIABLE` option for that command you could grub output into a variable, and with [string](https://cmake.org/cmake/help/latest/command/string.html) command (or with other commands) transform that output into the list of files. – Tsyvarev Dec 15 '22 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. – Tsyvarev Dec 15 '22 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. – torek Dec 16 '22 at 09:38
  • Thanks, @Tsyvarev. Will give it a shot, maybe with ideas from [here](https://stackoverflow.com/questions/35326218/git-ls-files-how-to-escape-spaces-in-files-paths) for dealing with spaces. – jozxyqk Dec 20 '22 at 21:27

0 Answers0