Skip to content

Commit

Permalink
CMake: fix and harden Git version detection on Windows.
Browse files Browse the repository at this point in the history
Same change as in Corrade.
  • Loading branch information
mosra committed Apr 15, 2022
1 parent cb20800 commit 5a6aa83
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 12 additions & 0 deletions package/git/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This dir is used by `src/*/CMakeLists.txt` to run `git describe --match "v*"`.
Because, if it would be run in that directory, on Windows the `--match "v*"`
would get expanded to the `version.h.cmake` file there, causing Git to
obviously not find any such tag. The reason is probably that on Windows the
wildcard expansion is done on the application side and not by the shell, thus
being performed even though CMake docs say `execute_process()` doesn't involve
a shell. This directory is thus dedicated for that operation, *guaranteed* to
never contain any file starting with `v` (or `V` for that matter because,
again, HELLO WINDOWS).

No matter what you do, DON'T put any files starting with `v` or `V` here, or
hell breaks loose again.
18 changes: 15 additions & 3 deletions src/Magnum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@ if(Git_FOUND)
execute_process(COMMAND ${GIT_EXECUTABLE} describe --match "v*" --long
OUTPUT_VARIABLE MAGNUMINTEGRATION_VERSION_STRING
RESULT_VARIABLE _MAGNUMINTEGRATION_VERSION_RESULT
ERROR_VARIABLE _MAGNUMINTEGRATION_VERSION_ERROR
# Otherwise this gets executed in the build dir, which might be inside
# a totally different Git working copy
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
# a totally different Git working copy. But that's not enough, if it
# would be run in ${CMAKE_CURRENT_SOURCE_DIR}, on Windows the
# `--match "v*"` would get expanded to the `versionIntegration.h.cmake`
# file, causing Git to obviously not find any such tag. The reason is
# probably that on Windows the wildcard expansion is done on the
# application side and not by the shell, thus being performed even
# though CMake docs say `execute_process()` doesn't involve a shell.
# The package/git directory is thus dedicated for that operation,
# *guaranteed* to never contain any file starting with `v` (or `V` for
# that matter because, again, HELLO WINDOWS).
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/package/git
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(MAGNUMINTEGRATION_VERSION_STRING MATCHES "^v([0-9][0-9][0-9][0-9])\\.0?([0-9][0-9]?)-([0-9]+)-g([a-f0-9]+)$")
set(MAGNUMINTEGRATION_VERSION_YEAR ${CMAKE_MATCH_1})
set(MAGNUMINTEGRATION_VERSION_MONTH ${CMAKE_MATCH_2})
Expand All @@ -47,6 +57,8 @@ if(Git_FOUND)
set(MAGNUMINTEGRATION_VERSION_HASH ${CMAKE_MATCH_4})
elseif(_MAGNUMINTEGRATION_VERSION_RESULT EQUAL 0)
message(WARNING "Can't match Git version from ${MAGNUMINTEGRATION_VERSION_STRING}")
else()
message(WARNING "Can't match Git version: ${_MAGNUMINTEGRATION_VERSION_ERROR}")
endif()
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/versionIntegration.h.cmake
Expand Down

0 comments on commit 5a6aa83

Please sign in to comment.