git_submodules.cmake 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. find_package(Git)
  2. if(NOT GIT_FOUND)
  3. message(WARNING "Git executable was not found. Git submodule checks will not be executed. "
  4. "If you have any build issues at all, start by adding git executable to the PATH and "
  5. "rerun cmake to not see this warning again.")
  6. function(git_submodule_check root_path)
  7. # no-op
  8. endfunction()
  9. else()
  10. function(git_submodule_check root_path)
  11. execute_process(
  12. COMMAND ${GIT_EXECUTABLE} submodule status
  13. WORKING_DIRECTORY ${root_path}
  14. OUTPUT_VARIABLE submodule_status
  15. )
  16. # git submodule status output not guaranteed to be stable,
  17. # may need to check GIT_VERSION_STRING and do some fiddling in the
  18. # future...
  19. lines2list(submodule_status)
  20. foreach(line ${submodule_status})
  21. string(REGEX MATCH "(.)[0-9a-f]+ ([^\( ]+) ?" _ignored "${line}")
  22. set(status "${CMAKE_MATCH_1}")
  23. set(submodule_path "${CMAKE_MATCH_2}")
  24. if("${status}" STREQUAL "-") # missing submodule
  25. message(STATUS "Initialising new submodule ${submodule_path}...")
  26. execute_process(
  27. COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive ${submodule_path}
  28. WORKING_DIRECTORY ${root_path}
  29. RESULT_VARIABLE git_result
  30. )
  31. if(git_result)
  32. message(FATAL_ERROR "Git submodule init failed for ${submodule_path}")
  33. endif()
  34. elseif(NOT "${status}" STREQUAL " ")
  35. message(WARNING "Git submodule ${submodule_path} is out of date. "
  36. "Run 'git submodule update --init --recursive' to fix.")
  37. endif()
  38. # Force a re-run of cmake if the submodule's .git file changes or is changed (ie accidental deinit)
  39. get_filename_component(submodule_abs_path ${submodule_path} ABSOLUTE BASE_DIR ${root_path})
  40. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${submodule_abs_path}/.git)
  41. # same if the HEAD file in the submodule's directory changes (ie commit changes).
  42. # This will at least print the 'out of date' warning
  43. set(submodule_head "${root_path}/.git/modules/${submodule_path}/HEAD")
  44. if(EXISTS "${submodule_head}")
  45. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${submodule_head})
  46. endif()
  47. endforeach()
  48. endfunction()
  49. endif()