[CMake] Relative include() calls

David Cole david.cole at kitware.com
Thu May 28 18:05:40 EDT 2009


There is also one more technique to consider here.

There is a self-referential variable available in every CMakeLists.txt or
every included file called CMAKE_CURRENT_LIST_FILE.

You could do this inside your "common.cmake" file:
  get_filename_component( dir "${CMAKE_CURRENT_LIST_FILE}" PATH )
  include( ${dir}/BoostUtils.cmake )

That way, 'dir' always refers to the path to 'common.cmake' and you are
passing a full path to include for BoostUtils.cmake which unambiguously
identifies it without regard for the current working directory... The
assumption you are making by doing this is that 'common.cmake' and
'BoostUtils.cmake' are always coupled together in the same directory. If
that assumption is true, this technique should work...


HTH,
David


On Thu, May 28, 2009 at 1:05 PM, Michael Jackson <
mike.jackson at bluequartz.net> wrote:

> There is a pattern that I use that is similar to what you are wanting so
> I'll just throw it out there for your consideration:
>
> project (Foo)
>
> #CMake will now Define Foo_SOURCE_DIR which
> # is the same as the current directory. It is
> # also put into the cache.
>
> include (${Foo_SOURCE_DIR}/cmake/common.cmake)
>
> # CMake also defines PROJECT_NAME which takes on the last
> # invocation of the "project()" command. So you could rewrite
> # the above as:
>
> include (${${PROJECT_NAME}_SOURCE_DIR}/cmake/common.cmake)
>
> Then inside of common.cmake you can use the following:
>
> include (${Foo_SOURCE_DIR}/cmake/BoostUtils.cmake)
>
> which will generally work but will _still_ fail if you decide
> to try and reuse the code in other projects. So what I decided
> was to use "Convention Over Configuration" in all my projects.
>
> I _always_ have a "Resources" directory in my project. Inside of that
> directory is _always_ a CMake directory. So in the top level CMakeLists.txt
> file of my project I set the following:
>
> set (PROJECT_RESOURCES_DIR ${${PROJECT_NAME}_SOURCE_DIR}/Resources)
> set (PROJECT_CMAKE_DIR ${${PROJECT_NAME}_SOURCE_DIR}/Resources/CMake)
>
> That way if I have cmake code that needs to reference those directories I
> always have
> cmake variables that can point to the proper directory and I _always_ refer
> to
> files in those directories by their full name:
>
> include (${PROJECT_RESOURCES_DIR}/MyReallyCoolCMakeMacros.cmake)
>
>
> Just some thoughts.
> _________________________________________________________
> Mike Jackson                  mike.jackson at bluequartz.net
> BlueQuartz Software                    www.bluequartz.net
> Principal Software Engineer                  Dayton, Ohio
>
>
>
>
> On May 28, 2009, at 12:17 PM, Robert Dailey wrote:
>
>  On Thu, May 28, 2009 at 11:06 AM, Tyler Roscoe <tyler at cryptio.net> wrote:
>> On Thu, May 28, 2009 at 10:42:03AM -0500, Robert Dailey wrote:
>> > The most annoying thing about this solution, though, is that every
>> single
>> > CMakeLists.txt that includes this module needs to make sure it has
>> > CMAKE_MODULE_PATH setup properly. It's just a bit redundant and
>> > tedious.
>>
>> You're kind of a hater, aren't you Robert?
>>
>> I never said I hated anything, I just said it was just a little bit
>> obnoxious (redundant/tedious).
>>
>> Can you just set CMAKE_MODULE_PATH in your ../cmake/common.cmake? If all
>> your Lists include that then you only need to set it once.
>>
>> Yep I thought about this too, but it is still not the perfect solution I
>> had in mind :) Not having to set it at all is the ideal path, but we can't
>> all get our way :)
>>
>> Thanks for your help.
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
>>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090528/f1b81b3f/attachment.htm>


More information about the CMake mailing list