在我的基于CMake的项目中,我有一些用于生成代码的XML文件(使用典型的site filetype=jes
& add_custom_command
模式)。
此XML文件包含以下其他文件:
add_custom_target
现在我想让<?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>
依赖于CMake变量。绝对可行的是编写一个生成器,该生成器采用XML模板并使用CMake变量的内容替换路径,此处还使用../../../MyComponent
&amp; add_custom_command
模式。
是否有一种解决方案可以利用简单的CMake和/或XML机制来修补或生成正确的路径?
答案 0 :(得分:2)
将我的评论转化为答案
我使用了以下技术,例如:为CMake生成的VS环境生成用户项目设置XML文件(参见Jim Butler的this博客文章)。
在你的情况下,我会这样做:
<强> 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>
在你的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}
作为输入文件提供给其他构建步骤。