Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find directory where target is defined

Tags:

cmake

Is there a way in cmake to find the source directory of the CMakeLists.txt file in which a target was defined?

Somethign like:

if (TARGET Foo)
    message("Library Foo was alread built in ${LOCATION_OF_FOOS_CMAKE}")
else()
    add_library(Foo ...)
endif()

Edit:
Unfortunately, my cmake scripts have to work on a default ubuntu 14.04 installation. So I'm limited to cmake 2.8

like image 447
MikeMB Avatar asked Jan 05 '23 00:01

MikeMB


1 Answers

You can use target property SOURCE_DIR (added with CMake 3.7):

get_target_property(FOO_SOURCE_DIR Foo SOURCE_DIR)

For older versions of CMake you can overwrite e.g. add_library() and define your own SOURCE_DIR target property:

function(add_library _target)
    _add_library(${_target} ${ARGN})
    set_target_properties(${_target} PROPERTIES SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
endfunction(add_library)
like image 68
Florian Avatar answered Feb 04 '23 02:02

Florian