onqtam
onqtam

Reputation: 4548

How to get the path to the currently included cmake file in a function?

suppose I have this:

#root/CMakeLists.txt
include(folder/file.cmake)
foo()

and

#root/folder/file.cmake
function(foo)
    message(${SOME_SPECIAL_CMAKE_VARIABLE})
endfunction()

And I want to get path/to/root/folder printed. I tried these 2 but they don't get me what I want: CMAKE_CURRENT_LIST_DIR or CMAKE_CURRENT_SOURCE_DIR.

So is there a cmake variable that will get me what I want?

Or is there a way to get the full path to file.cmake so I can atleast get it's path that way?

Upvotes: 6

Views: 2983

Answers (2)

Corristo
Corristo

Reputation: 5520

CMake 3.17 introduced a new variable for just this case: CMAKE_CURRENT_FUNCTION_LIST_DIR. Used inside a CMake function this evaluates to absolute path of the file in which the function is defined.

Upvotes: 1

onqtam
onqtam

Reputation: 4548

I ended up caching the CMAKE_CURRENT_LIST_DIR variable when the file is included outside of the function and later I use the cached value inside the function - based on the comments of my question.

Upvotes: 2

Related Questions