Cmake修改FetchContent_Declare指向本地已经下载好的代码

        在使用CMake构建代码时,某些项目可能需要额外下载第三方库。然而,由于网络不稳定(github由于不明原因经常被墙),git clone过程可能会出现问题,无法下载第三方库。在这种情况下,就需要科学上网,然后手动修改项目配置,将其指向已经下载到本地的库文件。例如在编译brunsli时,它会下载googletest和brotli,CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.11)

project(BRUNSLI C CXX)

include(CheckCSourceCompiles)
check_c_source_compiles(
  "#if defined(__EMSCRIPTEN__)
   int main() {return 0;}
   #endif"
  BRUNSLI_EMSCRIPTEN
)
if (BRUNSLI_EMSCRIPTEN)
  message("-- Compiler is EMSCRIPTEN")
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -flto -fno-exceptions -fno-rtti")
  set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -flto -fno-exceptions -fno-rtti")
  set(CMAKE_STATIC_LIBRARY_SUFFIX ".bc")
  set(CMAKE_EXECUTABLE_SUFFIX ".js")
else()
  message("-- Compiler is not EMSCRIPTEN")
endif()

include(GNUInstallDirs)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

include(FetchContent)

# Add GTest
FetchContent_Declare(
  GTest
  GIT_REPOSITORY https://github.com/google/googletest
  GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # v1.11.0
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE INTERNAL "")
FetchContent_MakeAvailable(GTest)

# Add Brotli
FetchContent_Declare(
  Brotli
  GIT_REPOSITORY https://github.com/google/brotli
  GIT_TAG e61745a6b7add50d380cfd7d3883dd6c62fc2c71 # v1.0.9
)
set(BROTLI_DISABLE_TESTS ON CACHE INTERNAL "")
FetchContent_MakeAvailable(Brotli)

include(CTest)

# The Brunsli library definition.
include(brunsli.cmake)

        其中FetchContent_Declare的部分为:

include(FetchContent)

# Add GTest
FetchContent_Declare(
  GTest
  GIT_REPOSITORY https://github.com/google/googletest
  GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # v1.11.0
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE INTERNAL "")
FetchContent_MakeAvailable(GTest)

# Add Brotli
FetchContent_Declare(
  Brotli
  GIT_REPOSITORY https://github.com/google/brotli
  GIT_TAG e61745a6b7add50d380cfd7d3883dd6c62fc2c71 # v1.0.9
)
set(BROTLI_DISABLE_TESTS ON CACHE INTERNAL "")
FetchContent_MakeAvailable(Brotli)

        将CMakeLists.txt中git下载的代码屏蔽掉,使用SOURCE_DIR修改为指向本地下载好的代码路径:

include(FetchContent)

# Add GTest
FetchContent_Declare(
  GTest
  SOURCE_DIR ./googletest
  #GIT_REPOSITORY https://github.com/google/googletest
  #GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # v1.11.0
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE INTERNAL "")
FetchContent_MakeAvailable(GTest)

# Add Brotli
FetchContent_Declare(
  Brotli
  SOURCE_DIR ./brotli
  #GIT_REPOSITORY https://github.com/google/brotli
  #GIT_TAG e61745a6b7add50d380cfd7d3883dd6c62fc2c71 # v1.0.9
)
set(BROTLI_DISABLE_TESTS ON CACHE INTERNAL "")
FetchContent_MakeAvailable(Brotli)