使用CMake变量生成/修补XML文件 [英] Generating / patching an XML file with a CMake variable

查看:133
本文介绍了使用CMake变量生成/修补XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的基于CMake的项目中,我有一些XML文件用于生成代码(使用典型的 add_custom_command & add_custom_target

 <$ c 

$ c><?xml version =1.0encoding =utf-8?>
< definition
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:noNamespaceSchemaLocation =../../../ MyComponent / schema.xsd>
< include href =../../../ MyComponent / another.xml/>
< / definition>

现在我想让 ../../../ MyComponent 依赖于CMake变量。肯定会工作的是编写一个生成器,它接受一个XML模板,并用CMake变量的内容替换路径,这里也使用 add_custom_command & add_custom_target 模式。



是否有一个解决方案可以利用简单的CMake和/生成正确的路径?

解决方案

将我的评论转换为答案 b

我使用下面的技术来eg为CMake生成的VS环境生成用户项目设置XML文件(参见例如



在您的情况下,我会这样做:



some.xml.in

 <?xml version =1.0encoding =utf -8? 
< definition
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:noNamespaceSchemaLocation =$ {_ my_xml_root_path} /schema.xsd >
< include href =$ {_ my_xml_root_path} /another.xml/>
< / definition>

CMakeLists.txt (取决于要注入的路径):

  get_filename_component(_my_xml_root_path../../../MyComponent ABSOLUTE)
set(_some_xml_path$ {CMAKE_CURRENT_BINARY_DIR} / $ {CMAKE_FILES_DIRECTORY} /some.xml)
configure_file(some.xml.in$ {_ some_xml_path})

然后你可以使用 $ {_ some_xml_path} 输入文件到其他一些构建步骤。


In my CMake-based project I have some XML files used to generate code (using the typical add_custom_command & add_custom_target pattern).

This XML file includes other files as follows:

<?xml version="1.0" encoding="utf-8"?>
    <definition
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../MyComponent/schema.xsd">
    <include href="../../../MyComponent/another.xml" />
</definition>

Now I want to make ../../../MyComponent dependent on a CMake variable. What would definitely would work is to write a generator that takes an XML template and replaces the paths with the content of a CMake variable using, here also, the add_custom_command & add_custom_target pattern.

Is there a solution that would make use of simple CMake and/or XML mechanisms to patch or generate the correct path?

解决方案

Turning my comment into an answer

I've used the following technique to e.g. generate user project setting XML files for CMake generated VS environments (see e.g. this blog post by Jim Butler).

In your case I would do:

some.xml.in

<?xml version="1.0" encoding="utf-8"?>
    <definition
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="${_my_xml_root_path}/schema.xsd">
    <include href="${_my_xml_root_path}/another.xml" />
</definition>

And in your CMakeLists.txt something like (depending on the path you want to inject):

get_filename_component(_my_xml_root_path "../../../MyComponent" ABSOLUTE)
set(_some_xml_path "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/some.xml")
configure_file("some.xml.in" "${_some_xml_path}")

Then you can use ${_some_xml_path} later to give as an input file to some other build step.

这篇关于使用CMake变量生成/修补XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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