在多个项目之间共享 CMake 脚本
Shared CMake scripts between multiple projects
我正在寻找一种在多个项目之间共享 CMake 脚本的方法。
在一个名为 somelib
的存储库中,我有一个 cmake
文件夹,我想将其他项目中的脚本包含在其中。在 somelib
的 CMakeLists.txt
中,我包含了 cmake
文件夹中的许多文件。
我的项目将“somelib”声明为外部依赖项,如下所示:
include(FetchContent)
FetchContent_Declare(
somelib
GIT_REPOSITORY git@git.somewhere.com:somewhere/somelib.git
GIT_TAG origin/master
)
FetchContent_MakeAvailable(somelib)
我不确定这是一个好的做法,所以请随时纠正我,但我愿意包含 somelib
的 CMakeLists.txt
以便我的项目包含所有文件somelib
包括。
所以在 FetchContent_MakeAvailable
之后我做了 :
include(${somelib_SOURCE_DIR}/CMakeLists.txt)
这个 include 本身工作得很好,但问题出在 somelib
的 CMakeLists.txt
中。实际上,它所包含的内容与它的位置无关,会导致类似 :
的错误
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:26 (include):
include could not find load file:
cmake/Cache.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:29 (include):
include could not find load file:
cmake/Linker.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:33 (include):
include could not find load file:
cmake/CompilerWarnings.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
我意识到我可以简单地做 :
而不是包括 CMakeLists.txt
include(${somelib_SOURCE_DIR}/cmake/Cache.cmake)
include(${somelib_SOURCE_DIR}/cmake/Linker.cmake)
include(${somelib_SOURCE_DIR}/cmake/CompilerWarnings.cmake)
但是随着时间的推移,这会变得相当大(而且它会在我所有的项目之间重复),所以我想要一个单一的入口点,一个我可以包含的东西,让一切都在手边在我的 CMakeLists 中。可能吗?
不要执行 include(${somelib_SOURCE_DIR}/CMakeLists.txt)
,因为 FetchContent_MakeAvailable(somelib)
已经在同一个文件上调用了 add_subdirectory
。
如果你想访问它的脚本,那么只需 运行:
list(APPEND CMAKE_MODULE_PATH "${somelib_SOURCE_DIR}/cmake")
include(Cache)
include(Linker)
include(CompilerWarnings)
But this will become quite big as the time goes by (plus it will be duplicated between all my projects), so I would like to have a single entry point, a single thing I can include to have everything at hand in my CMakeLists. Is it possible?
您还可以将这些助手移动到它们自己的存储库中,并让每个项目 FetchContent that。那么它的 CMakeLists.txt 就会有这个:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" PARENT_SCOPE)
你会 运行:
FetchContent_MakeAvailable(my_helpers)
include(MyHelpers)
其中 MyHelpers.cmake
只是 include()
所有其他助手。
我正在寻找一种在多个项目之间共享 CMake 脚本的方法。
在一个名为 somelib
的存储库中,我有一个 cmake
文件夹,我想将其他项目中的脚本包含在其中。在 somelib
的 CMakeLists.txt
中,我包含了 cmake
文件夹中的许多文件。
我的项目将“somelib”声明为外部依赖项,如下所示:
include(FetchContent)
FetchContent_Declare(
somelib
GIT_REPOSITORY git@git.somewhere.com:somewhere/somelib.git
GIT_TAG origin/master
)
FetchContent_MakeAvailable(somelib)
我不确定这是一个好的做法,所以请随时纠正我,但我愿意包含 somelib
的 CMakeLists.txt
以便我的项目包含所有文件somelib
包括。
所以在 FetchContent_MakeAvailable
之后我做了 :
include(${somelib_SOURCE_DIR}/CMakeLists.txt)
这个 include 本身工作得很好,但问题出在 somelib
的 CMakeLists.txt
中。实际上,它所包含的内容与它的位置无关,会导致类似 :
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:26 (include):
include could not find load file:
cmake/Cache.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:29 (include):
include could not find load file:
cmake/Linker.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:33 (include):
include could not find load file:
cmake/CompilerWarnings.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
我意识到我可以简单地做 :
而不是包括CMakeLists.txt
include(${somelib_SOURCE_DIR}/cmake/Cache.cmake)
include(${somelib_SOURCE_DIR}/cmake/Linker.cmake)
include(${somelib_SOURCE_DIR}/cmake/CompilerWarnings.cmake)
但是随着时间的推移,这会变得相当大(而且它会在我所有的项目之间重复),所以我想要一个单一的入口点,一个我可以包含的东西,让一切都在手边在我的 CMakeLists 中。可能吗?
不要执行 include(${somelib_SOURCE_DIR}/CMakeLists.txt)
,因为 FetchContent_MakeAvailable(somelib)
已经在同一个文件上调用了 add_subdirectory
。
如果你想访问它的脚本,那么只需 运行:
list(APPEND CMAKE_MODULE_PATH "${somelib_SOURCE_DIR}/cmake")
include(Cache)
include(Linker)
include(CompilerWarnings)
But this will become quite big as the time goes by (plus it will be duplicated between all my projects), so I would like to have a single entry point, a single thing I can include to have everything at hand in my CMakeLists. Is it possible?
您还可以将这些助手移动到它们自己的存储库中,并让每个项目 FetchContent that。那么它的 CMakeLists.txt 就会有这个:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" PARENT_SCOPE)
你会 运行:
FetchContent_MakeAvailable(my_helpers)
include(MyHelpers)
其中 MyHelpers.cmake
只是 include()
所有其他助手。