gitcommit.cmake 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Pure cmake script to write out cmake_commit.c and cmake_version.but
  2. set(DEFAULT_COMMIT "unavailable")
  3. set(commit "${DEFAULT_COMMIT}")
  4. set(TOPLEVEL_SOURCE_DIR ${CMAKE_SOURCE_DIR})
  5. execute_process(
  6. COMMAND ${GIT_EXECUTABLE} rev-parse --show-toplevel
  7. OUTPUT_VARIABLE git_worktree
  8. ERROR_VARIABLE stderr
  9. RESULT_VARIABLE status)
  10. string(REGEX REPLACE "\n$" "" git_worktree "${git_worktree}")
  11. if(status EQUAL 0)
  12. if(git_worktree STREQUAL CMAKE_SOURCE_DIR)
  13. execute_process(
  14. COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
  15. OUTPUT_VARIABLE git_commit
  16. ERROR_VARIABLE stderr
  17. RESULT_VARIABLE status)
  18. if(status EQUAL 0)
  19. string(REGEX REPLACE "\n$" "" commit "${git_commit}")
  20. else()
  21. if(commit STREQUAL "unavailable")
  22. message("Unable to determine git commit: 'git rev-parse HEAD' returned status ${status} and error output:\n${stderr}\n")
  23. endif()
  24. endif()
  25. else()
  26. if(commit STREQUAL "unavailable")
  27. message("Unable to determine git commit: top-level source dir ${CMAKE_SOURCE_DIR} is not the root of a repository")
  28. endif()
  29. endif()
  30. else()
  31. if(commit STREQUAL "unavailable")
  32. message("Unable to determine git commit: 'git rev-parse --show-toplevel' returned status ${status} and error output:\n${stderr}\n")
  33. endif()
  34. endif()
  35. if(OUTPUT_TYPE STREQUAL header)
  36. file(WRITE "${OUTPUT_FILE}" "\
  37. /*
  38. * cmake_commit.c - string literal giving the source git commit, if known.
  39. *
  40. * Generated by cmake/gitcommit.cmake.
  41. */
  42. #include \"putty.h\"
  43. const char commitid[] = \"${commit}\";
  44. ")
  45. elseif(OUTPUT_TYPE STREQUAL halibut)
  46. if(commit STREQUAL "unavailable")
  47. file(WRITE "${OUTPUT_FILE}" "\
  48. \\versionid no version information available
  49. ")
  50. else()
  51. file(WRITE "${OUTPUT_FILE}" "\
  52. \\versionid built from git commit ${commit}
  53. ")
  54. endif()
  55. else()
  56. message(FATAL_ERROR "Set OUTPUT_TYPE when running this script")
  57. endif()