[CMake] Difference in CMake 2.8 / 3.1+ behaviour when looking at SOURCES property

Berteun Damman berteun at gmail.com
Mon Jul 20 15:45:05 EDT 2015


Hi,

Consider the following example make file (it assumes a foo.txt and
foo.cc -- can be both empty) are present:

cmake_minimum_required(VERSION 2.8.12)
add_library(lib_example STATIC "foo.cc" "foo.txt")

get_target_property(sources lib_example SOURCES)
foreach(source ${sources})
    MESSAGE("${source}")
endforeach()

When using CMake 2.8.12, you'll see:

build$ cmake ..
foo.cc
/home/berteun/tmp/cmake/foo.txt

Note that it displays the full path for foo.txt! With later CMake
versions (3.1 and up is my suspicion), it will display:

build$ cmake ..
foo.cc
foo.txt

I tried looking trough the release notes, and I've found that
add_library gained support for generator expressions in 3.1, but I
haven't found any explicit mention that would explain this behaviour.
Does anyone know why it changed? And secondly, is there a way to get
the 2.8 behaviour back?

To be precise, I'd like to get accumulate the various .txt from
different targets (including targets the current target depends on);
in CMake 2.8 this was doable by collecting the dependencies, iterating
over these targets and making a list of the .txt files; these would
contain the full path. From 3.1 onwards, this is not the case; so if
you do a get_filename_component on them requesting the absolue path,
you won't get the actual path anymore, you'll get a path that is a
combination of the current targets source directory and the filename.

So the more general question is, is there a canonical way to get the
full path to a source file by looking at the SOURCES of a target? It
seems that for some filetypes this happened to work by accident in
CMake 2.8 and 3.0?

Thanks,

-Berteun


More information about the CMake mailing list