Patrick B.
Patrick B.

Reputation: 12363

How to change the reference path for get_filename_component in cmake?

I'm using get_filename_component in cmake to get the absolute path of a possibly relative path given in a variable.

And I want to do an out-of-tree/out-of-source-build.

It seems to me that get_filename_component is using CMAKE_SOURCE_DIR as reference-path.

Is there a way to change that or to workaround it?

One way I tried is to prefix my potential relative path with ${CMAKE_BINARY_DIR} but that stops working of the path given is not relative.

Upvotes: 0

Views: 1272

Answers (1)

Fraser
Fraser

Reputation: 78358

Assuming your relative path is always relative to CMAKE_BINARY_DIR, then you can handle this pretty easily using if(IS_ABSOLUTE ...):

if(NOT IS_ABSOLUTE ${MyPath})
  set(MyAbsPath ${CMAKE_BINARY_DIR}/${MyPath})
endif()

If the subject file or dir exists at CMake run time, then you can always do a find_file call, passing the possible NAMES and PATHS. If the file exists and is found, the resulting variable will hold the full path to the file.

Or you can use the if(EXISTS ...) signature of if to check for the existence or not of the given file.

Upvotes: 2

Related Questions