git_submodules.cmake 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. # for internal use:
  12. # skip submodule check if running on Gitlab CI and job is configured as not clone submodules
  13. if($ENV{IDF_SKIP_CHECK_SUBMODULES})
  14. if($ENV{IDF_SKIP_CHECK_SUBMODULES} EQUAL 1)
  15. message("skip submodule check on internal CI")
  16. return()
  17. endif()
  18. endif()
  19. execute_process(
  20. COMMAND ${GIT_EXECUTABLE} submodule status
  21. WORKING_DIRECTORY ${root_path}
  22. OUTPUT_VARIABLE submodule_status
  23. )
  24. # git submodule status output not guaranteed to be stable,
  25. # may need to check GIT_VERSION_STRING and do some fiddling in the
  26. # future...
  27. lines2list(submodule_status)
  28. foreach(line ${submodule_status})
  29. string(REGEX MATCH "(.)[0-9a-f]+ ([^\( ]+) ?" _ignored "${line}")
  30. set(status "${CMAKE_MATCH_1}")
  31. set(submodule_path "${CMAKE_MATCH_2}")
  32. if("${status}" STREQUAL "-") # missing submodule
  33. message(STATUS "Initialising new submodule ${submodule_path}...")
  34. execute_process(
  35. COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive ${submodule_path}
  36. WORKING_DIRECTORY ${root_path}
  37. RESULT_VARIABLE git_result
  38. )
  39. if(git_result)
  40. message(FATAL_ERROR "Git submodule init failed for ${submodule_path}")
  41. endif()
  42. elseif(NOT "${status}" STREQUAL " ")
  43. message(WARNING "Git submodule ${submodule_path} is out of date. "
  44. "Run 'git submodule update --init --recursive' to fix.")
  45. endif()
  46. # Force a re-run of cmake if the submodule's .git file changes or is changed (ie accidental deinit)
  47. get_filename_component(submodule_abs_path ${submodule_path} ABSOLUTE BASE_DIR ${root_path})
  48. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${submodule_abs_path}/.git)
  49. # same if the HEAD file in the submodule's directory changes (ie commit changes).
  50. # This will at least print the 'out of date' warning
  51. set(submodule_head "${root_path}/.git/modules/${submodule_path}/HEAD")
  52. if(EXISTS "${submodule_head}")
  53. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${submodule_head})
  54. endif()
  55. endforeach()
  56. endfunction()
  57. endif()