gitcommit.cmake 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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-cdup
  7. OUTPUT_VARIABLE path_to_top
  8. ERROR_VARIABLE stderr
  9. RESULT_VARIABLE status)
  10. string(REGEX REPLACE "\n$" "" path_to_top "${path_to_top}")
  11. # If we're at the top of a git repository, --show-cdup will print the
  12. # empty string (followed by a newline) and return success. If we're
  13. # lower down, it will return a sequence of "../../" that leads to the
  14. # top; if we're higher up it will fail with an error.
  15. if(status EQUAL 0 AND path_to_top STREQUAL "")
  16. execute_process(
  17. COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
  18. OUTPUT_VARIABLE git_commit
  19. ERROR_VARIABLE stderr
  20. RESULT_VARIABLE status)
  21. if(status EQUAL 0)
  22. string(REGEX REPLACE "\n$" "" commit "${git_commit}")
  23. else()
  24. if(commit STREQUAL "unavailable")
  25. message("Unable to determine git commit: 'git rev-parse HEAD' returned status ${status} and error output:\n${stderr}\n")
  26. endif()
  27. endif()
  28. else()
  29. if(commit STREQUAL "unavailable")
  30. message("Unable to determine git commit: top-level source dir ${CMAKE_SOURCE_DIR} is not the root of a repository")
  31. endif()
  32. endif()
  33. if(OUTPUT_TYPE STREQUAL header)
  34. file(WRITE "${OUTPUT_FILE}" "\
  35. /*
  36. * cmake_commit.c - string literal giving the source git commit, if known.
  37. *
  38. * Generated by cmake/gitcommit.cmake.
  39. */
  40. #include \"putty.h\"
  41. const char commitid[] = \"${commit}\";
  42. ")
  43. elseif(OUTPUT_TYPE STREQUAL halibut)
  44. if(commit STREQUAL "unavailable")
  45. file(WRITE "${OUTPUT_FILE}" "\
  46. \\versionid no version information available
  47. ")
  48. else()
  49. file(WRITE "${OUTPUT_FILE}" "\
  50. \\versionid built from git commit ${commit}
  51. ")
  52. endif()
  53. else()
  54. message(FATAL_ERROR "Set OUTPUT_TYPE when running this script")
  55. endif()