GenerateVersionFromVCS.cmake 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # CMake script that writes version control information to a header.
  2. #
  3. # Input variables:
  4. # NAMES - A list of names for each of the source directories.
  5. # <NAME>_SOURCE_DIR - A path to source directory for each name in NAMES.
  6. # HEADER_FILE - The header file to write
  7. #
  8. # The output header will contain macros <NAME>_REPOSITORY and <NAME>_REVISION,
  9. # where "<NAME>" is substituted with the names specified in the input variables,
  10. # for each of the <NAME>_SOURCE_DIR given.
  11. get_filename_component(LLVM_CMAKE_DIR "${CMAKE_SCRIPT_MODE_FILE}" PATH)
  12. list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
  13. include(VersionFromVCS)
  14. # Handle strange terminals
  15. set(ENV{TERM} "dumb")
  16. function(append_info name path)
  17. if(path)
  18. get_source_info("${path}" revision repository)
  19. endif()
  20. if(revision)
  21. file(APPEND "${HEADER_FILE}.tmp"
  22. "#define ${name}_REVISION \"${revision}\"\n")
  23. else()
  24. file(APPEND "${HEADER_FILE}.tmp"
  25. "#undef ${name}_REVISION\n")
  26. endif()
  27. if(repository)
  28. file(APPEND "${HEADER_FILE}.tmp"
  29. "#define ${name}_REPOSITORY \"${repository}\"\n")
  30. else()
  31. file(APPEND "${HEADER_FILE}.tmp"
  32. "#undef ${name}_REPOSITORY\n")
  33. endif()
  34. endfunction()
  35. foreach(name IN LISTS NAMES)
  36. if(NOT DEFINED ${name}_SOURCE_DIR)
  37. message(FATAL_ERROR "${name}_SOURCE_DIR is not defined")
  38. endif()
  39. append_info(${name} "${${name}_SOURCE_DIR}")
  40. endforeach()
  41. # Copy the file only if it has changed.
  42. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
  43. "${HEADER_FILE}.tmp" "${HEADER_FILE}")
  44. file(REMOVE "${HEADER_FILE}.tmp")