是否所有 CMAKE_ 变量都在 CMake 中自动定义(尤其是 CMAKE_EXPORT_COMPILE_COMMANDS) [英] Are all CMAKE_ variables automatically defined in CMake (esp CMAKE_EXPORT_COMPILE_COMMANDS)

查看:43
本文介绍了是否所有 CMAKE_ 变量都在 CMake 中自动定义(尤其是 CMAKE_EXPORT_COMPILE_COMMANDS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的一个 CMakeLists.txt 文件中,我们有一个测试来决定是否应该导出 compile_commands.json.

In one of our CMakeLists.txt files we have a test to decide if the compile_commands.json should be exported.

if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
    set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

CMAKE_EXPORT_COMPILE_COMMANDS 变量是 V3.5 中的新变量,因此在此之前,应该触发此测试(它在对正在使用的操作系统进行的更广泛的测试中),但我真的希望它在以下情况下触发它尚未在命令行上设置(即通过 Visual Studio (Ninja) 自动生成检测 CMakeLists.txt 文件的存在).它似乎不是那样工作的.

The CMAKE_EXPORT_COMPILE_COMMANDS variable was new in V3.5, so before then, this test should trigger (it's inside a wider test for the OS in use), but really I am wanting it to trigger if it hasn't been set on a command line (i.e. an auto generation via Visual Studio (Ninja) detecting the presence of the CMakeLists.txt file). It doesn't appear to be working like that.

它是这个(CMAKE_EXPORT_COMPILE_COMMANDS 变量),而另一个 CMAKE_ 变量总是在 CMake 运行时隐式定义?

It this (CMAKE_EXPORT_COMPILE_COMMANDS variable), and the other CMAKE_ variable always implicitly defined when CMake runs?

我知道 CMAKE_EXPORT_COMPILE_COMMANDS 的两次运行"问题,它与 if(NOT DEFINED CMAKE_*) 问题是分开的.

I'm aware of the 'two runs' problem for CMAKE_EXPORT_COMPILE_COMMANDS, which is separate to the if(NOT DEFINED CMAKE_*) question.

推荐答案

它是这个(CMAKE_EXPORT_COMPILE_COMMANDS 变量),而另一个 CMAKE_ 变量总是在 CMake 运行时隐式定义?

It this (CMAKE_EXPORT_COMPILE_COMMANDS variable), and the other CMAKE_ variable always implicitly defined when CMake runs?

这部分问题的答案是这取决于您所谈论的变量".对于CMAKE_EXPORT_COMPILE_COMMANDS的具体情况,其作用如下:

The answer to this part of the question is "it depends on which variable you're talking about". For the specific case of CMAKE_EXPORT_COMPILE_COMMANDS, it works as follows:

  1. 第一次配置CMakeLists.txt时,在开始执行时,CMAKE_EXPORT_COMPILE_COMMANDS没有定义.
  2. 调用项目时,会创建缓存变量CMAKE_EXPORT_COMPILE_COMMANDS初始化,其值为ENV{CMAKE_EXPORT_COMPILE_COMMANDS}.
  3. 随后的重新配置将从缓存中加载 CMAKE_EXPORT_COMPILE_COMMANDS 的值,而不是参考环境变量.
  4. 无论如何,CMake根据normal变量CMAKE_EXPORT_COMPILE_COMMANDS的值来决定是否生成compile_commands.json;如果它不存在,那么它会查看缓存变量.如果连这个都不存在(即如果你手动 unset() 它),那么它将 not 生成 compile_commands.json.它绝对不会在这一点上查询环境变量.
  1. When the CMakeLists.txt is first configured, at the start of execution, CMAKE_EXPORT_COMPILE_COMMANDS is not defined.
  2. When project is called, the cache variable CMAKE_EXPORT_COMPILE_COMMANDS is created and initialized with the value of ENV{CMAKE_EXPORT_COMPILE_COMMANDS}.
  3. Subsequent re-configures will load the value of CMAKE_EXPORT_COMPILE_COMMANDS from the cache and not consult the environment variable.
  4. In any case, CMake decides whether or not to generate compile_commands.json based on the value of the normal variable CMAKE_EXPORT_COMPILE_COMMANDS; if it does not exist, then it looks at the cache variable. If even this does not exist (i.e. if you manually unset() it), then it will not generate compile_commands.json. It absolutely does not consult the environment variable at this point.

还请记住,设置环境变量会影响当前的 CMake 进程.它不会逃到控制终端中,也不会在自定义命令执行之前继续存在.注意execute_process看到变量,还有相关的Bug 在 CTest 的构建和测试模式中,这里描述的太痛苦了.

Remember also that setting an environment variable affects on the current CMake process. It does not escape into the controlling terminal, nor will it survive until a custom command executes. Note that execute_process will see the variable, and there is a related bug in CTest's build-and-test mode that is too painful to describe here.

加载设置缓存变量的工具链文件,或在父项目中设置(即add_subdirectory)无法区分在命令行中设置.根本没有办法可靠地检测到这种情况.

Loading a toolchain file that sets the cache variable, or setting it in a parent project (i.e. add_subdirectory) cannot be distinguished from setting it at the command line. There is simply no way to reliably detect this case.

如果您希望 CMAKE_EXPORT_COMPILE_COMMANDS 默认为 ON,那么您可以在调用 project() 之前添加此代码(我通常不建议这样做):

If you want CMAKE_EXPORT_COMPILE_COMMANDS to be ON by default, then you may add this code before the call to project() (which I do not normally recommend doing):

if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS AND
   NOT DEFINED ENV{CMAKE_EXPORT_COMPILE_COMMANDS} AND
   CMAKE_GENERATOR MATCHES "Ninja|Unix Makefiles")
  set(CMAKE_EXPORT_COMPILE_COMMANDS "ON"
      CACHE BOOL "Enable/Disable output of compile commands during generation.")
  mark_as_advanced(CMAKE_EXPORT_COMPILE_COMMANDS)
endif()

这篇关于是否所有 CMAKE_ 变量都在 CMake 中自动定义(尤其是 CMAKE_EXPORT_COMPILE_COMMANDS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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