# CMake module for Git version detection

**URL:** https://discourse.cmake.org/t/cmake-module-for-git-version-detection/8261
**Category:** Community
**Created:** [June 5, 2023, 1:46pm UTC](https://discourse.cmake.org/t/cmake-module-for-git-version-detection/8261 "2023-06-05T13:46:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![antoniovazquezblanco](https://discourse.cmake.org/user_avatar/discourse.cmake.org/antoniovazquezblanco/32/3448_2.png) [@antoniovazquezblanco](https://discourse.cmake.org/u/antoniovazquezblanco)
#### Post date: [June 5, 2023, 1:46pm UTC](https://discourse.cmake.org/t/cmake-module-for-git-version-detection/8261/1 "2023-06-05T13:46:25Z")

</div>

It seems that many projects may be able to automatically detect source code version from git tags but many don’t do it. I have also found other versions of similar scripts that are able to detect Git versions but it is difficult to locate a single source for a maintained alternative.

For the above reasons, I wanted to share my take on Git version detection for CMake projects. You can find it at [GitHub - antoniovazquezblanco/cmake-gitversiondetect: Make GIT versioning info available in your CMakeLists.txt file](https://github.com/antoniovazquezblanco/cmake-gitversiondetect)

Any feedback would be apreciated! Thanks!

---

<div class="post-metadata">

### Author: ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)
#### Post date: [June 7, 2023, 12:43pm UTC](https://discourse.cmake.org/t/cmake-module-for-git-version-detection/8261/2 "2023-06-07T12:43:44Z")

</div>

I’ve also worked on something like that [here](https://github.com/LecrisUT/CMakeExtraUtils/blob/main/cmake/DynamicVersion.md), so I can offer a few tips:

- support `.git_archival.txt` for git archives
- needs a target that would re-trigger builds when a commit/tag is created
- `git describe tags` should allow for different version formats
- packaging for other projects to use via FetchContent/find\_package?

---

<div class="post-metadata">

### Author: ![antoniovazquezblanco](https://discourse.cmake.org/user_avatar/discourse.cmake.org/antoniovazquezblanco/32/3448_2.png) [@antoniovazquezblanco](https://discourse.cmake.org/u/antoniovazquezblanco)
#### Post date: [June 7, 2023, 1:54pm UTC](https://discourse.cmake.org/t/cmake-module-for-git-version-detection/8261/3 "2023-06-07T13:54:55Z")

</div>

Thank you @Lecris for the pointers. I see your DynamicVersion script is quite nicely packed with features I did not even thought I needed!

Awesome!

---

<div class="post-metadata">

### Author: ![shabiel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shabiel/32/1285_2.png) [@shabiel](https://discourse.cmake.org/u/shabiel)
#### Post date: [June 8, 2023, 5:04pm UTC](https://discourse.cmake.org/t/cmake-module-for-git-version-detection/8261/4 "2023-06-08T17:04:53Z")

</div>

We use this project: [cmake-git-version-tracking/git\_watcher.cmake at master · andrew-hardin/cmake-git-version-tracking · GitHub](https://github.com/andrew-hardin/cmake-git-version-tracking/blob/master/git_watcher.cmake)

---

<div class="post-metadata">

### Author: ![antoniovazquezblanco](https://discourse.cmake.org/user_avatar/discourse.cmake.org/antoniovazquezblanco/32/3448_2.png) [@antoniovazquezblanco](https://discourse.cmake.org/u/antoniovazquezblanco)
#### Post date: [June 9, 2023, 8:03am UTC](https://discourse.cmake.org/t/cmake-module-for-git-version-detection/8261/5 "2023-06-09T08:03:38Z")

</div>

Wow! It even has testing! I see it also detects git changes! 😃

I am quite new to CMake but those examples really help me improve this and maybe other modules I may try to implement soon.

Thank you! 🙂
