sphinxrevcheck.cmake 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. cmake_minimum_required ( VERSION 3.17 )
  2. # this file included by cpack config in order to detect if configured and build version are the same
  3. # guess version strings from current git repo
  4. function ( guess_from_git )
  5. if (NOT EXISTS "${MANTICORE_SOURCE_DIR}/.git")
  6. return ()
  7. endif ()
  8. find_package ( Git QUIET )
  9. if (NOT GIT_FOUND)
  10. return ()
  11. endif ()
  12. # without this in some environments you can get error "detected dubious ownership in repository"
  13. # `git config --global --add safe.directory '*'` in the docker image it runs in may not help. TODO: check why
  14. execute_process ( COMMAND "${GIT_EXECUTABLE}" config --global --add safe.directory "${MANTICORE_SOURCE_DIR}")
  15. # extract short has as CHECK_GIT_COMMIT_ID
  16. execute_process ( COMMAND "${GIT_EXECUTABLE}" log -1 --format=%h
  17. WORKING_DIRECTORY "${MANTICORE_SOURCE_DIR}"
  18. RESULT_VARIABLE res
  19. OUTPUT_VARIABLE CHECK_GIT_COMMIT_ID
  20. OUTPUT_STRIP_TRAILING_WHITESPACE )
  21. set ( CHECK_GIT_COMMIT_ID "${CHECK_GIT_COMMIT_ID}" PARENT_SCOPE )
  22. endfunction ()
  23. # guess version strings from template header file (git archive mark it there)
  24. function ( extract_from_git_slug HEADER )
  25. if (EXISTS "${HEADER}")
  26. FILE ( STRINGS "${HEADER}" _CONTENT )
  27. foreach (LINE ${_CONTENT})
  28. # match definitions like - // GIT_*_ID VALUE
  29. if ("${LINE}" MATCHES "^//[ \t]+(GIT_.*_ID)[ \t]\"(.*)\"")
  30. set ( ${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" )
  31. endif ()
  32. endforeach ()
  33. if (GIT_COMMIT_ID STREQUAL "$Format:%h$")
  34. return () # no slug
  35. endif ()
  36. # commit id
  37. set ( CHECK_GIT_COMMIT_ID "${GIT_COMMIT_ID}" PARENT_SCOPE )
  38. endif ()
  39. endfunction ()
  40. # function definitions finished, execution starts from here
  41. ##################################
  42. # first try to use binary git
  43. guess_from_git ()
  44. # 2-nd try - if we build from git archive. Correct hash and date provided then, but no branch
  45. if (NOT CHECK_GIT_COMMIT_ID)
  46. message ( STATUS "guess_from_git failed. 2nd try - extract_from_git_slug" )
  47. extract_from_git_slug ( "${MANTICORE_SOURCE_DIR}/src/sphinxversion.h.in" )
  48. endif ()
  49. if (NOT CHECK_GIT_COMMIT_ID)
  50. message ( STATUS "extract_from_git_slug failed. 3rd try - forcing setting CHECK_GIT_COMMIT_ID" )
  51. set ( CHECK_GIT_COMMIT_ID "DEADBEEF" )
  52. endif ()
  53. message ( STATUS "CHECK_GIT_COMMIT_ID=${CHECK_GIT_COMMIT_ID}" )
  54. if (NOT CHECK_GIT_COMMIT_ID STREQUAL "${CONFIGURED_GIT_COMMIT_ID}")
  55. message ( FATAL_ERROR "Current commit ${CHECK_GIT_COMMIT_ID} differs from the stored ${CONFIGURED_GIT_COMMIT_ID}. Run 'cmake .' in build dir to fix" )
  56. endif ()