如何在 CMake 中跟踪丢失的变量值?

How to trace a lost variable value in CMake?

编辑:已接受的答案尚未解决我的问题,但它回答了我提出的问题 - 如果您能帮助我解决下面描述的实际问题,您可能会回答 问题。

我有一个 CMake 项目,它使用需要设置变量的框架(即 https://github.com/queezythegreat/arduino-cmake,需要 ARDUINO_SDK_PATH

奇怪的是,在我在命令行上设置该变量后,它首先有一个值,但它似乎在一段时间后消失了。

我是 运行

cmake -DARDUINO_SDK_PATH=/path/to/sdk ..

.. 并收到一条错误消息,告诉我它未设置。打印出我的 CMakeLists.txt 顶部的值和正在检查变量的框架内部的深处,给我这样的结果:

>>> ARDUINO_SDK_PATH (beginning): '/home/me/project/arduino-1.8.2'
>>> ARDUINO_SDK_PATH (before check): '/home/me/project/arduino-1.8.2'
-- The C compiler identification is GNU 6.2.0
-- The CXX compiler identification is GNU 6.2.0
-- Check for working C compiler: /usr/bin/avr-gcc
>>> ARDUINO_SDK_PATH (before check): ''
CMake Error at /home/me/project/arduino-cmake/cmake/ArduinoToolchain.cmake:84 (message):
  Could not find Arduino SDK (set ARDUINO_SDK_PATH)!
Call Stack (most recent call first):
  /home/me/project/build/CMakeFiles/3.6.2/CMakeSystem.cmake:6 (include)
  /home/me/project/build/CMakeFiles/CMakeTmp/CMakeLists.txt:3 (project)

CMake Error: Internal CMake error, TryCompile configure of cmake failed
-- Configuring incomplete, errors occurred!
See also "/home/me/project/build/CMakeFiles/CMakeOutput.log".

所以看起来 ARDUINO_SDK_PATH 以某种方式失去了它的价值。我没有找到为它分配任何值的实际命令,所以我不知道如何继续。我现在当然可以在我的 CMake 项目中到处添加代码以打印出 ARDUINO_SDK_PATH 的值,但我想知道是否有内置的方法来跟踪变量值。

我尝试了 cmake --trace ..cmake --trace-expand .. 但输出似乎没有帮助..

系统:带有 CMake 3.6.2 的 Fedora 25

更新

感谢 Florian,我在 CMakeLists.txt 中添加了 variable_watch(ARDUINO_SDK_PATH) 作为第一行,现在我的可变跟踪线 (message()) 如下所示:

CMake Debug Log at arduino-cmake/cmake/ArduinoToolchain.cmake:41 (MESSAGE):
  Variable "ARDUINO_SDK_PATH" was accessed using READ_ACCESS with value
  "/home/me/project/arduino-1.8.2".
Call Stack (most recent call first):
  /usr/share/cmake/Modules/CMakeDetermineSystem.cmake:98 (include)
  CMakeLists.txt:10 (project)

>>> ARDUINO_SDK_PATH (before check): /home/me/project/arduino-1.8.2

我有大约 30 条这样的消息,但后面有几条跟踪线 没有 值和 没有 跟踪消息。

所以看起来变量 ARDUINO_SDK_PATH 被一个空的新变量替换了,并且不再被跟踪..

重现

为了使此行为可重现,我上传了代码:https://github.com/frans-fuerst/trinket_led

注意:提供的 CMakeLists.txt 还不包含有用的代码 - 它只是重现错误。

您需要下载Arduino-SDK,解压并在命令行提供路径:

tar xvf ~/Downloads/arduino-1.8.2-linux64.tar.xz 
git clone https://github.com/frans-fuerst/trinket_led
cd trinket_led
git submodules update --init
mkdir build
cd build
cmake -DARDUINO_SDK_PATH=/path/to/arduino-1.8.2 ..

注意:ArduinoToolchain.cmake 中有一个 find_path 命令看起来很可疑。但是你可以删除它并得到相同的结果..

CMakeFiles/CMakeOutput.log

The target system is: Arduino -  - 
The host system is: Linux - 4.10.12-200.fc25.x86_64+debug - x86_64
Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded.
Compiler: /usr/bin/avr-gcc 
Build flags: 
Id flags: 

The output was:
0


Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "a.out"

The C compiler identification is GNU, found in "/home/frans/_HOME/1704_trinket_led/build/CMakeFiles/3.6.2/CompilerIdC/a.out"

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: /usr/bin/avr-g++ 
Build flags: 
Id flags: 

The output was:
0


Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "a.out"

The CXX compiler identification is GNU, found in "/home/frans/_HOME/1704_trinket_led/build/CMakeFiles/3.6.2/CompilerIdCXX/a.out"

只需在 CMakeLists.txt 的顶部添加一个 variable_watch(ARDUINO_SDK_PATH)

参考资料