[CMake] Possible inconsistent behavior in target_sources() and CMake 3.14

Craig Scott craig.scott at crascit.com
Mon Apr 15 05:09:31 EDT 2019


On Wed, Apr 3, 2019 at 11:10 AM Michele Rosso <mrosso at lbl.gov> wrote:

> Hello,
> according to the doc for CMake 3.14, target_sources() interprets relative
> source file paths as being relative to the current source directory, i.e.
> it should
> prepend CMAKE_CURRENT_SOURCE_DIR to the relative paths given by the user.
> However, this is not the case if the target and the sources are defined in
> the same directory.
>

The docs say that target_sources() will *interpret* relative source file
paths that way, but they do not state that it will prepend
CMAKE_CURRENT_SOURCE_DIR. The docs for target_sources() only talk about how
relative paths are interpreted. The docs for policy CMP0076 are a bit more
explicit, but even they only say that target_sources() will convert the
relative path to absolute *if the conditions also specified in those policy
docs are met*. In the case where target_sources() is called in the same
directory as where the target is defined, the target's SOURCE_DIR property
and the CMAKE_CURRENT_SOURCE_DIR variable will hold the same value, so the
relative path is not modified, as per the CMP0076 docs.



> I attached a simple example to reproduce the issue. The structure of the
> example is as follows:
>
> - cmake-bug/
>      - CMakeLists.txt
>      - main.cpp
>      - foo/
>         - CMakeLists.txt
>         - foo.H
>         - foo.cpp
>      - bar/
>         - CMakeLists.txt
>         - bar.H
>         - bar.cpp
>
> Target "main.exe" is defined in the top-level CMakeLists.txt, while its
> headers and sources are located in the the top-level directory
> and in the sub-directories 'foo' and 'bar'.
> After target_sources is used to include all the headers and sources, I
> retrieve the "SOURCES" property for  `main.exe`: all the sources and headers
> but  `main.cpp` are given the correct (absolute) path.
>
> Is this the intended behavior? If so, why should a source file located in
> the same directory where the target is defined be treated any differently?
>

Yes, this is the intended behavior. There's no need to modify the path when
target_sources() is called in the same directory as that in which the
target is defined because the behavior is unambiguous and is the same
whether CMP0076 is set to OLD or NEW. This is consistent with what you'd
get if you had listed the sources directly in the add_executable() or
add_library() call instead.


-- 
Craig Scott
Melbourne, Australia
https://crascit.com

Get the hand-book for every CMake user: Professional CMake: A Practical
Guide <https://crascit.com/professional-cmake/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190415/66fc8c41/attachment.html>


More information about the CMake mailing list