How to trace a lost variable value in CMake?

2019-07-25 11:22发布

问题:

Edit: the accepted answer does not solve my problem yet but it answered the question I've asked - if you can help me with my actual problem described below you might answer this question.

I have a CMake-project which makes use of a framework which needs a variable to be set (namely https://github.com/queezythegreat/arduino-cmake, which needs ARDUINO_SDK_PATH)

Strangely after I set that variable on the command line it first has a value but it looks like it disappears after a while.

I'm running

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

.. and get an error message which tells me that it's not set. Printing out the value at the top of my CMakeLists.txt and deep inside this framework where the variable is being checked gives me something like this:

>>> 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".

So it looks like ARDUINO_SDK_PATH looses it's value somehow. I didn't find an actual command where it get's assigned any value so I don't know how to proceed.. I can now of course add code to my CMake project everywhere to print out the value of ARDUINO_SDK_PATH but I wonder if there's a builtin way to trace variable values.

I tried cmake --trace .. and cmake --trace-expand .. but the output doesn't seem helpful..

System: Fedora 25 with CMake 3.6.2

Update

Thanks to Florian I've added variable_watch(ARDUINO_SDK_PATH) as my first line in CMakeLists.txt and now my variable trace lines (message()) look like this:

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

I have about 30 messages like this but there follow a couple of trace lines without the value and without the trace message.

So it looks like the variable ARDUINO_SDK_PATH gets replaced by a new one which is empty and which is not traced any more..

Reproduce

In order to make this behavior reproducible I've uploaded the code: https://github.com/frans-fuerst/trinket_led

Note: the provided CMakeLists.txt does not contain useful code yet - it just reproduces the error.

You need to download the Arduino-SDK, extract it and provide the path on the command line:

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 ..

Note: there is a find_path command in ArduinoToolchain.cmake which looks suspicious. But you can remove it with the same result..

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"

回答1:

Just put a variable_watch(ARDUINO_SDK_PATH) at the top of your CMakeLists.txt.

References

  • variable_watch()
  • What's the CMake syntax to set and use variables?