ProjectVersioning.cmake 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. define_property (TARGET
  2. PROPERTY GIT_DESCRIBE
  3. BRIEF_DOCS "Advanced version info for developers"
  4. FULL_DOCS "String with information that is important for developers during
  5. development process. This information includes git commit hash, durty status
  6. of repo, distance from the last tag.")
  7. define_property (TARGET
  8. PROPERTY GIT_UNTRACKED_FILES
  9. BRIEF_DOCS "Information about presence of untracked files"
  10. FULL_DOCS "Used in helper functions generation to add .with-untracked suffix
  11. to version string. Suffix is only added if there are some untracked not
  12. ignored files in repository.")
  13. set(HERE_DIR ${CMAKE_CURRENT_LIST_DIR})
  14. function (target_version_information
  15. TARGET_NAME i_target_name
  16. EXPORT_HEADER i_export_header
  17. EXPORT_MACRO i_export_macro
  18. VERSIONED_ENTITY i_versioned_entity)
  19. find_file (
  20. headerFileTemplate
  21. "ProjectVersioning/version.h.in"
  22. PATHS ${CMAKE_MODULE_PATH})
  23. if ( NOT ${headerFileTemplate} )
  24. set(headerFileTemplate "${HERE_DIR}/ProjectVersioning/version.h.in")
  25. endif()
  26. find_file (
  27. sourceFileTemplate
  28. "ProjectVersioning/version.c.in"
  29. PATHS ${CMAKE_MODULE_PATH})
  30. if ( NOT ${sourceFileTemplate} )
  31. set(sourceFileTemplate "${HERE_DIR}/ProjectVersioning/version.c.in")
  32. endif()
  33. exec_program (
  34. "git"
  35. ${CMAKE_SOURCE_DIR}
  36. ARGS "describe --always --dirty --long --tags"
  37. OUTPUT_VARIABLE gitDescribe)
  38. exec_program (
  39. "git"
  40. ${CMAKE_SOURCE_DIR}
  41. ARGS "ls-files --others --exclude-standard"
  42. OUTPUT_VARIABLE gitUntracked)
  43. if (gitUntracked)
  44. set (gitUntracked ".with-untracked")
  45. endif (gitUntracked)
  46. configure_file (
  47. "${headerFileTemplate}"
  48. "${CMAKE_CURRENT_BINARY_DIR}/${i_versioned_entity}_version.h")
  49. configure_file(
  50. "${sourceFileTemplate}"
  51. "${CMAKE_BINARY_DIR}/${i_versioned_entity}_version.c")
  52. target_sources ("${i_target_name}"
  53. PRIVATE
  54. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/${i_versioned_entity}_version.h>
  55. $<INSTALL_INTERFACE:include/${i_include_prefix}/${i_versioned_entity}_version.h>
  56. PRIVATE
  57. "${CMAKE_BINARY_DIR}/${i_versioned_entity}_version.c")
  58. set_target_properties (${i_target_name}
  59. PROPERTIES
  60. GIT_DESCRIBE "${gitDescribe}"
  61. GIT_UNTRACKED_FILES "${gitUntracked}")
  62. unset (headerFileTemplate PARENT_SCOPE)
  63. unset (sourceFileTemplate PARENT_SCOPE)
  64. endfunction (target_version_information)