如何忽略CMakeLists.txt中单个CMake命令的错误? [英] How can I ignore errors on an individual CMake command in my CMakeLists.txt?

查看:334
本文介绍了如何忽略CMakeLists.txt中单个CMake命令的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中 CMakeLists.txt 尝试读取可能存在或可能不存在的文件。丢失文件不是问题,脚本可以处理任何一种情况。如果我们可以检测到已知的Linux发行版,则可以使用它来稍微调整编译环境:

  file(读/ etc / redhat-发行RHREL)
if(RHREL匹配 Red Hat Enterprise Linux Server版本6 *)
#调整RHEL6
elseif(RHREL匹配 Red Hat Enterprise Linux Server版本7 *)
#调整RHEL7
else()
#读取失败,或者我们没有匹配已知的RHEL版本
#后退到合理的默认值
endif()

问题在于,当 file(READ ...)失败,似乎正在使用 SEND_ERROR 参数输出一条消息。这意味着我的配置按照我的预期继续进入了所有的 else(),但是在配置结束时 CMake 拒绝生成 Makefile



如何运行 file(READ)以这样的方式我可以在本地处理错误并防止它们使整个配置失败?

解决方案

将我的评论变成答案



在这种特殊情况下,您还可以检查 CMAKE_HOST_SYSTEM



更普遍的答案是,您必须将代码移至外部CMake脚本中,并使用所有功能强大的 execute_process() 命令:



FileDumpRedHatRelease .cmake

  file(读取/ etc / redhat-release RHREL)
execute_process(COMMAND $ {CMAKE_COMMAND} -E echo $ {RHREL})

#或如果您不介意以STATUS消息
#开头的破折号(因为正常的CMake消息发送到stderr)
#message(STATUS $ {RHREL})

CMakeLists.txt

  execute_process(
COMMAND $ {CMAKE_COMMAND} -P $ {CMAKE_CURRENT_LIST_DIR} /FileDumpRedHatRelease.cmake
OUTPUT_VARIABLE RHREL
ERROR_QUIET

参考文献



即使这些参考文献都在讨论可能失败的 add_custom_command()命令,您也可以看到 execute_process()和外部CMake脚本,例如:


I have a project where the CMakeLists.txt attempts to read a file which may or may not be present. It is not a problem for the file to be missing and the script handles either case. This is used to tweak the compilation environment slightly if we can detect a known Linux distribution:

file (READ /etc/redhat-release RHREL)
if (RHREL MATCHES "Red Hat Enterprise Linux Server release 6*")
   # tweak for RHEL6
elseif (RHREL MATCHES "Red Hat Enterprise Linux Server release 7*")
   # tweak for RHEL7
else()
   # either read failed, or we didn't match a known RHEL release
   # fallback to reasonable defaults
endif()

The problem is that when file(READ...) fails it seems to be outputting a message using the SEND_ERROR parameter. This means that my configuration continues into the catch-all else() as I expect, but at the end of configuration CMake refuses to generate a Makefile.

How can I run file(READ) in such a way that I can cope with errors locally and prevent them from failing the entire configuration?

解决方案

Turning my comment into an answer

In this special case, you could also check CMAKE_HOST_SYSTEM.

The more general answer is you have to move your code into an external CMake script and call it with the all mighty execute_process() command:

FileDumpRedHatRelease.cmake

file(READ /etc/redhat-release RHREL)
execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${RHREL}")

# or if you don't mind the '-- ' leading dashes with STATUS messages
# (because normal CMake messages go to stderr)
#message(STATUS "${RHREL}")

CMakeLists.txt

execute_process( 
    COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_LIST_DIR}/FileDumpRedHatRelease.cmake"
    OUTPUT_VARIABLE RHREL
    ERROR_QUIET
)

References

Even those references are all discussing an add_custom_command() command that could fail, you can see the usage of execute_process() and external CMake scripts in such cases:

这篇关于如何忽略CMakeLists.txt中单个CMake命令的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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