Confusion with include() relativity

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

Confusion with include() relativity

Robert Dailey-2
I have an interesting structure for my code & build scripts:

root/
  source/
    CMakeLists.txt
    build/
      CMakeLists.txt
  cmake/
    common.cmake

My root CMakeLists.txt is actually in 'root/source/build", and my
common scripts are in "root/cmake".

From my root CMakeLists.txt file, I do this:

get_filename_component( BUILD_PRODUCT_ROOT ${CMAKE_SOURCE_DIR}/../.. ABSOLUTE )
set( CMAKE_MODULE_PATH
    "${BUILD_PRODUCT_ROOT}/cmake"
    "${BUILD_PRODUCT_ROOT}/cmake/find"
)

add_subdirectory( ${BUILD_PRODUCT_ROOT}/source source )

Note that the script "root/source/CMakeLists.txt" is where I begin
defining targets, and it never steps into "build" (to avoid infinite
recursion).

Later on (still in the root script) I do:

include( common )

This includes "root/cmake/common.cmake" as expected, but inside
common.cmake when I try to include a file (not a module) relative to
common.cmake, it says it can't find it. Example:

include( foo/bar/stuff.cmake )

The absolute path for this would be:

root/cmake/foo/bar/stuff.cmake

Naturally I assume that when including a FILE (not a module!) that it
is relative to the CMAKE_CURRENT_LIST_DIR, which in this case
correctly displays as "root/cmake".

Can anyone explain why I can't include files from common.cmake? Thanks
in advance.
--

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:
http://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Confusion with include() relativity

Robert Dailey-2
I am attaching a sample that reproduces the issue. I'm using CMake
version 2.8.11.2. To reproduce this, just run the 'generate.bat' batch
file from its current directory (WINDOWS ONLY). I have set it up to
generate for Visual Studio 2008, but you can edit the batch file to
change this.

Password to the archive is: cmake

On Thu, Sep 5, 2013 at 5:26 PM, Robert Dailey <[hidden email]> wrote:

> I have an interesting structure for my code & build scripts:
>
> root/
>   source/
>     CMakeLists.txt
>     build/
>       CMakeLists.txt
>   cmake/
>     common.cmake
>
> My root CMakeLists.txt is actually in 'root/source/build", and my
> common scripts are in "root/cmake".
>
> From my root CMakeLists.txt file, I do this:
>
> get_filename_component( BUILD_PRODUCT_ROOT ${CMAKE_SOURCE_DIR}/../.. ABSOLUTE )
> set( CMAKE_MODULE_PATH
>     "${BUILD_PRODUCT_ROOT}/cmake"
>     "${BUILD_PRODUCT_ROOT}/cmake/find"
> )
>
> add_subdirectory( ${BUILD_PRODUCT_ROOT}/source source )
>
> Note that the script "root/source/CMakeLists.txt" is where I begin
> defining targets, and it never steps into "build" (to avoid infinite
> recursion).
>
> Later on (still in the root script) I do:
>
> include( common )
>
> This includes "root/cmake/common.cmake" as expected, but inside
> common.cmake when I try to include a file (not a module) relative to
> common.cmake, it says it can't find it. Example:
>
> include( foo/bar/stuff.cmake )
>
> The absolute path for this would be:
>
> root/cmake/foo/bar/stuff.cmake
>
> Naturally I assume that when including a FILE (not a module!) that it
> is relative to the CMAKE_CURRENT_LIST_DIR, which in this case
> correctly displays as "root/cmake".
>
> Can anyone explain why I can't include files from common.cmake? Thanks
> in advance.

--

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:
http://www.cmake.org/mailman/listinfo/cmake

cmake-test.7z (856 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Confusion with include() relativity

Andreas Pakulat-2
In reply to this post by Robert Dailey-2
Hi,

On Fri, Sep 6, 2013 at 12:26 AM, Robert Dailey <[hidden email]> wrote:
I have an interesting structure for my code & build scripts:

root/
  source/
    CMakeLists.txt
    build/
      CMakeLists.txt
  cmake/
    common.cmake

My root CMakeLists.txt is actually in 'root/source/build", and my
common scripts are in "root/cmake".

From my root CMakeLists.txt file, I do this:

get_filename_component( BUILD_PRODUCT_ROOT ${CMAKE_SOURCE_DIR}/../.. ABSOLUTE )
set( CMAKE_MODULE_PATH
    "${BUILD_PRODUCT_ROOT}/cmake"
    "${BUILD_PRODUCT_ROOT}/cmake/find"
)

add_subdirectory( ${BUILD_PRODUCT_ROOT}/source source )

Note that the script "root/source/CMakeLists.txt" is where I begin
defining targets, and it never steps into "build" (to avoid infinite
recursion).

Later on (still in the root script) I do:

include( common )

This includes "root/cmake/common.cmake" as expected, but inside
common.cmake when I try to include a file (not a module) relative to
common.cmake, it says it can't find it. Example:

include( foo/bar/stuff.cmake )

The absolute path for this would be:

root/cmake/foo/bar/stuff.cmake

Naturally I assume that when including a FILE (not a module!) that it
is relative to the CMAKE_CURRENT_LIST_DIR, which in this case
correctly displays as "root/cmake".

Can anyone explain why I can't include files from common.cmake? Thanks
in advance.

Your expectation is wrong here, IMO. The include() command works very much like the preprocessor's #include in C/C++, it takes the content of the file and puts it verbatim into the place where the include() occurs. In particular once the data has been read the command forgets where it came from and this is before the code gets executed. So the 'context' CMakeLists.txt file is still the one in root/build/.

Andreas 


--

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:
http://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Confusion with include() relativity

Robert Dailey-2
If this is indeed the case I think the behavior should be modified to
respect CMAKE_CURRENT_LIST_DIR. Because, the parent include() command
hasn't completed until the nested include() commands have completed,
and I think it's a huge limitation that nested include() commands are
still referencing the location of the file that initiated the first
include() command. This affects portability of common CMake scripts,
because the root location may not be in the same place at all times.

On Fri, Sep 6, 2013 at 1:46 AM, Andreas Pakulat <[hidden email]> wrote:

> Hi,
>
> On Fri, Sep 6, 2013 at 12:26 AM, Robert Dailey <[hidden email]>
> wrote:
>>
>> I have an interesting structure for my code & build scripts:
>>
>> root/
>>   source/
>>     CMakeLists.txt
>>     build/
>>       CMakeLists.txt
>>   cmake/
>>     common.cmake
>>
>> My root CMakeLists.txt is actually in 'root/source/build", and my
>> common scripts are in "root/cmake".
>>
>> From my root CMakeLists.txt file, I do this:
>>
>> get_filename_component( BUILD_PRODUCT_ROOT ${CMAKE_SOURCE_DIR}/../..
>> ABSOLUTE )
>> set( CMAKE_MODULE_PATH
>>     "${BUILD_PRODUCT_ROOT}/cmake"
>>     "${BUILD_PRODUCT_ROOT}/cmake/find"
>> )
>>
>> add_subdirectory( ${BUILD_PRODUCT_ROOT}/source source )
>>
>> Note that the script "root/source/CMakeLists.txt" is where I begin
>> defining targets, and it never steps into "build" (to avoid infinite
>> recursion).
>>
>> Later on (still in the root script) I do:
>>
>> include( common )
>>
>> This includes "root/cmake/common.cmake" as expected, but inside
>> common.cmake when I try to include a file (not a module) relative to
>> common.cmake, it says it can't find it. Example:
>>
>> include( foo/bar/stuff.cmake )
>>
>> The absolute path for this would be:
>>
>> root/cmake/foo/bar/stuff.cmake
>>
>> Naturally I assume that when including a FILE (not a module!) that it
>> is relative to the CMAKE_CURRENT_LIST_DIR, which in this case
>> correctly displays as "root/cmake".
>>
>> Can anyone explain why I can't include files from common.cmake? Thanks
>> in advance.
>
>
> Your expectation is wrong here, IMO. The include() command works very much
> like the preprocessor's #include in C/C++, it takes the content of the file
> and puts it verbatim into the place where the include() occurs. In
> particular once the data has been read the command forgets where it came
> from and this is before the code gets executed. So the 'context'
> CMakeLists.txt file is still the one in root/build/.
>
> Andreas
>
--

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:
http://www.cmake.org/mailman/listinfo/cmake