CMake: Wie man automatisch git submodule update --init ausführt

Wenn Sie Git-Submodule in Ihrem CMake-Projekt verwenden, möchten Sie möglicherweise sicherstellen, dass die Submodule beim Konfigurieren des Projekts automatisch initialisiert und aktualisiert werden. Sie können dies erreichen, indem Sie einen benutzerdefinierten Befehl in Ihrer CMakeLists.txt-Datei hinzufügen.

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()

Check out similar posts by category: CMake, Git