CMAKE_ How can I evaluate the current line where the cmake function is called?

For debugging purposes, it would be nice if I could access

  • script filename
  • the current line number in the script

at setup time where the CMake function / macro was called . (I want to automatically inject this information through the wrapper functions for my own component to keep track of when and where the cache variables were rewritten).

While 1. seems to work with evaluating CMAKE_CURRENT_LIST_FILE, I can't figure out how to achieve 2. CMAKE_CURRENT_LIST_LINE is always set to where it is actually placed, in my case a string inside a wrapper function.

As suggested in the CMake bugtracker , I could do this on the receiving site with [CMAKE_CURRENT_LIST_LINE] [3] as an additional argument. But I would like to avoid this in order to hide the debug logic from the "open interface".


A minimal working example to express what I want:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
include( myComponent )

# access via public interface
myComponentFunctionWrapperFunction( "CMakeLists.txt:5" )
myComponentFunctionWrapperMacro( "CMakeLists.txt:6" )
myComponentMacroWrapperFunction( "CMakeLists.txt:7" )
myComponentMacroWrapperMacro( "CMakeLists.txt:8" )

# direct access of the hidden interface
myComponentFunction( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} "CMakeLists.txt:11" )
myComponentFunction( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} "CMakeLists.txt:12" )
myComponentMacro( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} "CMakeLists.txt:13" )
myComponentMacro( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} "CMakeLists.txt:14" )

      

myComponent.cmake

# the actual component

function( myComponentFunction CALLER CALLER_LINE DATA )
    message( STATUS "myComponent.function( ${CALLER}, ${CALLER_LINE}, ${DATA} )" )
endfunction()

macro( myComponentMacro CALLER CALLER_LINE DATA )
    message( STATUS "myComponent.macro( ${CALLER}, ${CALLER_LINE}, ${DATA} )" )
endmacro()


# the public interface for injecting debug info

function( myComponentFunctionWrapperFunction DATA )
    myComponentFunction( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} ${DATA} )
endfunction()

macro( myComponentFunctionWrapperMacro DATA )
    myComponentFunction( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} ${DATA} )
endmacro()

function( myComponentMacroWrapperFunction DATA )
    myComponentMacro( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} ${DATA} )
endfunction()

macro( myComponentMacroWrapperMacro DATA )
    myComponentMacro( ${CMAKE_CURRENT_LIST_FILE} ${CMAKE_CURRENT_LIST_LINE} ${DATA} )
endmacro()

      

Execution

$ cmake -D CMAKE_MODULE_PATH=`pwd` .
-- myComponent.function( <myPath>/CMakeLists.txt, 15, CMakeLists.txt:5 )
-- myComponent.function( <myPath>/CMakeLists.txt, 19, CMakeLists.txt:6 )
-- myComponent.macro( <myPath>/CMakeLists.txt, 23, CMakeLists.txt:7 )
-- myComponent.macro( <myPath>/CMakeLists.txt, 27, CMakeLists.txt:8 )
-- myComponent.function( <myPath>/CMakeLists.txt, 11, CMakeLists.txt:11 )
-- myComponent.function( <myPath>/CMakeLists.txt, 12, CMakeLists.txt:12 )
-- myComponent.macro( <myPath>/CMakeLists.txt, 13, CMakeLists.txt:13 )
-- myComponent.macro( <myPath>/CMakeLists.txt, 14, CMakeLists.txt:14 )
-- Configuring done

      


Maybe I can access the stack trace? If you issue, for example, the message (WARNING ...), line numbers are available. But I want to store them, not just print them to the terminal. Any ideas?

+3


source to share


1 answer


Not sure if this is possible.

I want to automatically enter this information through wrapper functions for my own component to keep track of when and where cache variables are overwritten)



Have you tried using cmake with --trace (or even --trace-expand if you have a fairly recent version of CMake)? This might be the best way to achieve your goal.

0


source







All Articles