[CMake] How to find the path to the currently include() or find_package file()

Michael Hertling mhertling at online.de
Tue Jan 4 01:41:41 EST 2011


On 01/04/2011 05:47 AM, John McGehee wrote:
> I am using CMake 2.8 on Linux and Windows.
> 
> When I include() or find_package() a .cmake file, is there a variable that I can use within the included .cmake file that will tell me its path?
> 
> For example,
> 
>   # In CMakeLists.txt
>   include(somePath/foo.cmake)
> 
> Within somePath/foo.cmake, I want to include bar.cmake which is in the same directory as foo.cmake,
> 
>   # In somePath/foo.cmake
>   include(${CMAKE_VARIABLE_THAT_IS_THE_ANSWER_TO_THIS_QUESTION}/bar.cmake)
> 
> where ${CMAKE_VARIABLE_THAT_IS_THE_ANSWER_TO_THIS_QUESTION} = "somePath", the path to foo.cmake, which is currently being evaluated.

Use CMAKE_CURRENT_LIST_FILE in conjunction with GET_FILENAME_COMPONENT():

# In somePath/foo.cmake
get_filename_component(somePath ${CMAKE_CURRENT_LIST_FILE} PATH)
include(${somePath}/bar.cmake)

Regards,

Michael


More information about the CMake mailing list