如何在CMake中包括来自第三方项目的选定来源 [英] Howto include selected sources from third party project in CMake

查看:44
本文介绍了如何在CMake中包括来自第三方项目的选定来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于CMake的项目,该项目取决于我需要下载和提取的外部资源。我决定使用ExternalProject_Add来完成其中的一部分。问题是如何使add_library规则依赖于某些提取的文件。



这是我到目前为止所了解的内容:

 #这行对于ExternalProject命令有效。 
#它引用了cmake发行版中的Module
include(ExternalProject)

#下载并提取FreeRTOS核心资源
set(FREERTOS_DIR $ {CMAKE_CURRENT_SOURCE_DIR} / FreeRTOS)
设置(FREERTOS_URL http://downloads.sourceforge.net/project/freertos/FreeRTOS/V6.0.2/FreeRTOSV6.0.2.zip)
设置(FREERTOS_CORE_DIR $ {FREERTOS_DIR} /来源)

设置(FREERTOS_SOURCES $ {FREERTOS_CORE_DIR} /croutine.c
$ {FREERTOS_CORE_DIR} /list.c
$ {FREERTOS_CORE_DIR} /queue.c
$ {FREERTOS_CORE_DIR} /tasks.c
$ {FREERTOS_CORE_DIR} /portable/MemMang/heap_3.c)

include_directories($ {FREERTOS_CORE_DIR} / include)

add_library(freertos STATIC $ {FREERTOS_SOURCES})

ExternalProject_Add(freertos_download
DOWNLOAD_DIR $ {CMAKE_CURRENT_SOURCE_DIR}
SOURCE_DIR $ {FREERTOS_DIR}
URL $ { b $ b CONFIGURE_COMMAND
BUILD_COMMAND
INSTALL_COMMAND)

注意我已经禁用了外部项目中的配置,构建和安装步骤。



按预期的那样,我没有指定依赖项,我得到以下错误

 -在第三方配置完成的
CMake错误/CMakeLists.txt:20(add_library):
找不到源文件:

/ home / dushara / prj / sw / djetk-demo / djetk / third-party / FreeRTOS / Source / croutine.c

试用扩展名.c .C .c ++ .cc .cpp .cxx .m .M .mm .h .hh .h ++ .hm .hpp
.hxx .in。 txx

我尝试使用

  add_dependencies(freertos_download freertos)

但这是行不通的。 p>

理想情况下,我希望FREERTOS_SOURCES中的所有项目都取决于外部项目的提取阶段。关于如何实现此目标的任何想法?

解决方案

CMake将尝试在配置步骤中查看库的源文件是否存在,但是您在配置过程中并未完成外部项目的下载步骤。



您可以通过设置CMake告诉CMake不要担心不存在的文件。 GENERATED 的属性:

  set_source_files_properties($ {FREERTOS_SOURCES}生成的属性TRUE)

您仍应将外部项目添加为库的依赖项,以确保已下载&首先提取,但是在您的示例中,命令不正确;应该是:

  add_dependencies(freertos freertos_download)

所以依赖某物的目标是第一个参数,其依赖项随后列出。


I've got a CMake based project that depends on external sources that I need to download and extract. I decided to use the ExternalProject_Add to do part of this. The question is how to make an add_library rule depend on some of the extracted files.

Here's what I've got so far:

# This line is needed for the ExternalProject command to work.
# It references the the Module in the cmake distro
include(ExternalProject)

# Download and extract the FreeRTOS core sources
set(FREERTOS_DIR    "${CMAKE_CURRENT_SOURCE_DIR}/FreeRTOS")
set(FREERTOS_URL    "http://downloads.sourceforge.net/project/freertos/FreeRTOS/V6.0.2/FreeRTOSV6.0.2.zip")
set(FREERTOS_CORE_DIR ${FREERTOS_DIR}/Source)

set(FREERTOS_SOURCES "${FREERTOS_CORE_DIR}/croutine.c"
    ${FREERTOS_CORE_DIR}/list.c 
    ${FREERTOS_CORE_DIR}/queue.c 
    ${FREERTOS_CORE_DIR}/tasks.c
    ${FREERTOS_CORE_DIR}/portable/MemMang/heap_3.c)

include_directories(${FREERTOS_CORE_DIR}/include)

add_library(freertos STATIC ${FREERTOS_SOURCES})

ExternalProject_Add(freertos_download
    DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    SOURCE_DIR ${FREERTOS_DIR}
    URL ${FREERTOS_URL}
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND "")

NOTE I've disabled the configure, build and install steps in the external project .

As expected because, I haven't specified the dependencies, I get the following error

-- Configuring done
CMake Error at third-party/CMakeLists.txt:20 (add_library):
  Cannot find source file:

    /home/dushara/prj/sw/djetk-demo/djetk/third-party/FreeRTOS/Source/croutine.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

I tried using

add_dependencies(freertos_download freertos)

But that wouldn't work.

Ideally I'd like all the items in FREERTOS_SOURCES depend on the extract phase of the external project. Any ideas on how I can achieve this?

解决方案

CMake will try to see if source files exists for a library during the configure step, but as you experienced the download step of the external project isn't done during configuration.

You can tell CMake not to worry about non-existing files, by setting the GENERATED property on them:

set_source_files_properties(${FREERTOS_SOURCES} PROPERTIES GENERATED TRUE)

You should still add the external project as a dependency to your library to ensure that it is downloaded & extracted first, but in your example the command was incorrect; it should be:

add_dependencies(freertos freertos_download)

so the target that depends on something is the first parameter, and its dependencies listed afterwards.

这篇关于如何在CMake中包括来自第三方项目的选定来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆