CMake: Jak automaticky git submodule update --init

Pokud používáte git submoduly ve vašem CMake projektu, možná budete chtít zajistit, aby byly submoduly automaticky inicializovány a aktualizovány při konfiguraci projektu. Toho můžete dosáhnout přidáním vlastního příkazu do vašeho CMakeLists.txt souboru.

CMakeLists.txt
# Try to initialize git submodules automatically during configure so
# downstream consumers don't need a separate manual step. This is best-effort
# and will only run when Git is available.
find_package(Git QUIET)
if(GIT_FOUND)
  message(STATUS "Git found: initializing/updating submodules...")
  execute_process(
    COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    RESULT_VARIABLE _git_submodule_result
    OUTPUT_QUIET ERROR_QUIET
  )
  if(NOT _git_submodule_result EQUAL 0)
    message(WARNING "git submodule update --init --recursive failed with exit code ${_git_submodule_result}")
  endif()
else()
  message(STATUS "Git not found; skipping automatic git submodule initialization")
endif()

Podívejte se na podobné články podle kategorie: CMake, Git