根据生成生成文件时不可用的源文件在CMake中创建库 [英] Creating a library in CMake depending on source files not available when generating build files

查看:99
本文介绍了根据生成生成文件时不可用的源文件在CMake中创建库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CMake配置文件,该文件构建了两个库:

I have a CMake configuration file building two libraries:


  1. 包含实时库的第三方库(此处称为ThirdPartyLib)供应商提供的OS /板支持软件包。它是使用自动工具工具链在CMake外部构建的。

  2. 前一个库的扩展版本(这里称为ExtendedThirdPartyLib)

不幸的是,我所需的某些源代码(各种工具)并未在(1)的普通构建脚本中构建。由于我不想弄乱供应商的构建脚本,因此我想添加另一个库(2),构建丢失的文件,从而从供应商处扩展该库。

Unfortunately, some source code that I need (various tools) are not built in the ordinary build script for (1). Since I don't want to mess with the suppliers build script I want to add another library (2), building the missing files and thus extending the library from the supplier.

我希望能够在 CMakeFiles.txt 中执行以下操作:

I want to able to do something like this in CMakeFiles.txt:

cmake_minimum_required(VERSION 3.2)
project(bsp)
include(ExternalProject)

ExternalProject_Add(
  ThirdPartyLib
  URL <http://some.url/bsp.tar.bz2
  BUILD_COMMAND make -C ../external/ThirdPartyLib/src
)
set_target_properties(ThirdPartyLib PROPERTIES EXCLUDE_FROM_ALL TRUE)

add_library(ExtendedThirdPartyLib
  ${CMAKE_CURRENT_BINARY_DIR}/some/path/missing_file1.c
  ${CMAKE_CURRENT_BINARY_DIR}/some/path/missing_file2.c
)
add_dependencies(ExtendedThirdPartyLib ThirdPartyLib)
target_include_directories(ExtendedThirdPartyLib PUBLIC
  ${CMAKE_CURRENT_BINARY_DIR}/some/path/include
)
target_link_libraries(ExtendedThirdPartyLib ThirdPartyLib)

这里的问题是 missing_file1.c missing_file2.c 在CMake生成构建文件时无效(它们是从供应商的压缩包中提取的)。

The problem here is that the path to missing_file1.c and missing_file2.c are not valid when CMake is generating the build files (they are extracted from the tarball from the supplier). CMake exits with an error output saying: "Cannot find source file".

是否有一种使之有效的整洁方法?,即CMake退出并显示错误消息:找不到源文件。是否可以说服CMake在开始构建库时某些不存在的输入文件存在?还是有其他建议的方法来解决此问题?

Is there a neat way to make this work? I.e. is it possible to convince CMake that certain non-existant input files will exist when building of the library begins? Or is there any other recommended way to solve this issue?

(我已经临时制作了需要从供应商压缩包中生成的文件的本地副本,但是当然,这不是一个好的解决方案。如果在以后的供应商软件包版本中更改了这些文件,而我忘了覆盖我的本地副本,那将是一团糟...

另一种解决方案是在CMake外部创建一个小的makefile,并以某种方式在CMakeFiles.txt中使用另一个 ExternalProject_Add 。但这不是还是一个很好的解决方案,例如,如果修改了编译器和链接器标志,我也要记住也要更改makefile。)

Another "solution" would be to create a small makefile outside CMake and use another ExternalProject_Add in the CMakeFiles.txt somehow. But that's not a good solution either, e.g. if compile and linker flags are modified I need to remember to change the makefile too.)

推荐答案

我个人不喜欢 ExternalProject_Add 命令,因为它确实满足了我的口味,但是我离题了。

Personally, I dislike the ExternalProject_Add command, because it does way too many things for my taste, but I've digressed.

如果您执行类似操作,其中 bar 模拟您的 ExtendedThirdPartyLib 目标,sinc,该怎么办? e取决于生成的文件

What if you do something like this, where bar is simulating your ExtendedThirdPartyLib target, since it depends on generated files

cmake_minimum_required(VERSION 3.11)

project(lol C)

set(SOURCES lol.c) # only this file exists

add_library(lol ${SOURCES})

set(FOO_FILES "foo1.c" "foo2.c")

add_custom_command(OUTPUT ${FOO_FILES}
  COMMAND ${CMAKE_COMMAND} -E touch ${FOO_FILES}
  WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
  COMMENT "Creating ${FOO_FILES}"
  VERBATIM)

add_custom_target(foo DEPENDS ${FOO_FILES})

add_library(bar ${FOO_FILES})

add_dependencies(bar foo)

target_link_libraries(lol bar)

整个方法取决于以下事实:采购/生成的文件的方法是通过custom命令和相关的定制目标明确定义的。

The whole approach hinges on the fact that the method, where produced/generated files are procured, is explicitly defined via the custom command and associated custom target.

您应该修改定制命令以提取所需文件(例如甚至可以从压缩包中调用某些外部脚本)(可能需要使用curl或类似工具下载)。

You should modify the custom command to extract the required files (e.g. could even call some external script) from the tarball (which might require downloading with curl or something similar).

这篇关于根据生成生成文件时不可用的源文件在CMake中创建库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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