[英]CMake's FetchContent with Makefile-built dependency
My current project requires a library that is built with a Makefile.我当前的项目需要一个使用 Makefile 构建的库。 I would like to compile this library during my project compilation time;我想在我的项目编译期间编译这个库; this feature is the main selling point of FetchContent and it works pretty well with CMake dependencies.此功能是 FetchContent 的主要卖点,它与 CMake 依赖项配合得非常好。 Nevertheless, I am unable to get it to work with Makefiles, nor can I find examples on how to do so.不过,我无法让它与 Makefile 一起使用,也找不到有关如何使用它的示例。
FetchContent_Declare(
make_lib
URL http://url/library_code.tar.gz
BUILD_COMMAND ${CMAKE_COMMAND} -E env make -j 8
BUILD_IN_SOURCE true
BINARY_DIR ""
)
FetchContent_GetProperties(make_lib)
if (NOT make_lib_POPULATED)
FetchContent_Populate(make_lib)
# here I would like to declare imported libraries:
add_library(make_lib::libA STATIC IMPORTED GLOBAL)
target_include_directories(make_lib::libA INTERFACE ${make_lib_SOURCE_DIR}/include)
set_property(TARGET make_lib::libA PROPERTY IMPORTED_LOCATION <path to "to be built" lib>)
endif()
make
possible at all? make
的“编译时”执行是否可能?The FetchContent_*
commands simply fetch content or metadata from a particular external resource, and populate CMake variables; FetchContent_*
命令只是从特定的外部资源中获取内容或元数据,并填充 CMake 变量; they do not actually perform any configure, build, or install steps.它们实际上并不执行任何配置、构建或安装步骤。 Thus, any options related to these steps is explicitly ignored when calling FetchContent_Declare()
.因此,在调用FetchContent_Declare()
时,与这些步骤相关的任何选项都会被明确忽略。 That includes these options:这包括以下选项:
CONFIGURE_COMMAND
BUILD_COMMAND
INSTALL_COMMAND
TEST_COMMAND
From the FetchContent
documentation:从FetchContent
文档中:
This module enables populating content at configure time via any method supported by the
ExternalProject
module.该模块允许在配置时通过ExternalProject
模块支持的任何方法填充内容。 WhereasExternalProject_Add()
downloads at build time, theFetchContent
module makes content available immediately, allowing the configure step to use the content in commands likeadd_subdirectory()
,include()
orfile()
operations.ExternalProject_Add()
在构建时下载,而FetchContent
模块使内容立即可用,允许配置步骤在add_subdirectory()
、include()
或file()
操作等命令中使用内容。
This doesn't apply to your use case, as the calls like add_subdirectory()
would fail because there are no CMake files in the external library.这不适用于您的用例,因为像add_subdirectory()
这样的调用会失败,因为外部库中没有 CMake 文件。
As mentioned in this post, ExternalProject_Add()
makes more sense in your situation.如本文所述, ExternalProject_Add()
在您的情况下更有意义。 Your call may look something like this:您的电话可能如下所示:
ExternalProject_Add(make_lib
DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}
URL http://url/library_code.tar.gz
UPDATE_COMMAND ""
SOURCE_DIR ${make_lib_SOURCE_DIR}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ""
BUILD_COMMAND "make -j8"
INSTALL_COMMAND "${make_lib_install_commands}"
)
add_library(make_lib_libA STATIC IMPORTED GLOBAL)
set_property(TARGET make_lib_libA
PROPERTY IMPORTED_LOCATION
${make_lib_SOURCE_DIR}/path/to/make_lib_libA.a
)
add_dependencies(myOtherLib make_lib)
Note, BUILD_COMMAND
here will not be ignored, but will run make -j8
at compile-time .注意,这里的BUILD_COMMAND
不会被忽略,而是会在compile-time运行make -j8
。 You should also be able to declare the imported library as your code laid out.您还应该能够将导入的库声明为您的代码布局。 But importantly, do remember to call add_dependencies()
which tells CMake your make_lib
is used by another target;但重要的是,请记住调用add_dependencies()
来告诉 CMake 您的make_lib
已被另一个目标使用; otherwise, make-lib
will not build.否则, make-lib
将不会构建。
After calling ExternalProject_Add()
, you can use ExternalProject_Get_Property()
to query information about the external project target.调用ExternalProject_Add()
后,您可以使用ExternalProject_Get_Property()
查询有关外部项目目标的信息。 The linked example shows how to get the source directory of the project, which could be useful for getting the location of built library.链接的示例显示了如何获取项目的源目录,这对于获取构建库的位置很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.