Possible inconsistent behavior in target_sources() and CMake 3.14

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Possible inconsistent behavior in target_sources() and CMake 3.14

Michele Rosso
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.
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?

Thanks,
Michele

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

cmake-bug.tar.gz (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Possible inconsistent behavior in target_sources() and CMake 3.14

Craig Scott-3


On Wed, Apr 3, 2019 at 11:10 AM Michele Rosso <[hidden email]> 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

Get the hand-book for every CMake user: Professional CMake: A Practical Guide

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Possible inconsistent behavior in target_sources() and CMake 3.14

Michele Rosso
Hi Craig,
thanks for the clarification.

Best,
Michele


On Mon, Apr 15, 2019 at 2:09 AM Craig Scott <[hidden email]> wrote:


On Wed, Apr 3, 2019 at 11:10 AM Michele Rosso <[hidden email]> 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

Get the hand-book for every CMake user: Professional CMake: A Practical Guide

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake