[CMake] Use-like inheritance

Christian Arnault arnault at lal.in2p3.fr
Fri Apr 20 03:54:39 EDT 2012


Hi Petr,

Thanks for your advices which goes on the line I already explored.

Indeed I have played a bit with similar constructions (either using 
functions or macros) and yes it works (simply I thought I had missed 
some features in CMake)

One point which forced me to manually add some tricks to fully exploit 
my working model:

+------- B project

function(goto project)
   add_subdirectory(${project} ${project}/build)
endfunction()

project(B)
goto(/there/A)
...

+------

+------- A project

project(A)
message("entering A CMAKE_PROJECT_NAME=${CMAKE_PROJECT_NAME}")

+------


Produces:

...
entering A CMAKE_PROJECT_NAME=B


Which forced me to create a new 'project' function in order to exploit 
the local project name

The point is thus that the inheritance model of CMake is not what I had 
expected... In the traditional Object model, classes 'export' or 'hide' 
their properties independently of the clients, but local features are 
NOT overriden by clients.

My feelings is that in CMake, the mechanism is different: the meaning of 
'inheritance' in CMake is more a client-based relationship!! Once I 
understood that, I realized that I had to re-create an object 
relationship by ad-hoc mechanisms (functions, properties, conventions, 
policies...)

Cheers
Christian






Le 19/04/2012 21:57, Petr Kmoch a écrit :
> Depends on who knows the directories to be included. If you're in a
> situation when B doesn't know which directories A will add, you can
> use global properties for this purpose. Something like this:
>
> #-------------
> project(B)
> define_property(GLOBAL PROPERTY my_include_files ...)
> add_subdirectory(/there/A) #1
> get_property(added_by_a GLOBAL PROPERTY my_include_files) #1
> include_directories(${added_by_a}) #1
>
> #---------------
> project(A)
> include_directories(/there/A/include) #2
> set_property(GLOBAL PROPERTY my_include_files /there/A/include) #2
>
> #-------------
>
> Lines marked as #1 and #2 could be encapsulated in macros, something
> like "add_subdirectory_and_import_includes()" and
> "set_and_import_includes()", respectively.
>
> Petr
>
> On Thu, Apr 19, 2012 at 8:58 PM,<norulez at me.com>  wrote:
>> You could create a macro for such purpose:
>>
>> macro(AddAndInclude directory)
>>     add_subdirectory(${directory})
>>     include_directories(${directory}/include)
>> endmacro(AddAndInclude)
>>
>> Usage:
>> AddAndInclude(/there/A)
>>
>> Best Regards
>> NoRulez
>>
>> Am 19.04.2012 um 16:42 schrieb Christian Arnault<arnault at lal.in2p3.fr>:
>>
>>> Hi,
>>>
>>> My simple work model is as follows:
>>>
>>> 1) I have two projects A&  B, located in independant locations. Both provide some libraries&  executable
>>> 2) B makes use of libraries&  headers provided in A
>>>
>>>
>>> I want to launch the build of A together B thus I do:
>>>
>>> #-------------------------
>>> project(B)
>>> add_subdirectory (/there/A)
>>> ...
>>> #-------------------------
>>>
>>> OK!
>>>
>>> Then I'd like to inherit some properties of A (let's say 'include directories' !) of course I could simply say:
>>>
>>> #--------------------
>>> project(B)
>>> ...
>>> add_subdirectory (/there/A)
>>> include_directories (/there/A/includes)
>>> add_library (MyBLib...)
>>> ...
>>> #----------------------
>>>
>>>
>>> OK! this works
>>>
>>> Now, suppose I want rather to 'export' the include_directories property while I am within the CMakeLists.txt of A, so that, it's enough to add_subdirectory(A). such as:
>>>
>>> #--------------------------------
>>> project(B)
>>> ...
>>> add_subdirectory (/there/A)
>>> add_library (MyBLib...)
>>> #--------------------------------
>>>
>>>
>>> So as the include_directories property is transparently acquired.
>>> How to do this in CMake ?
>>> (of course, I tried many features of CMake, but perhaps I have missed some important point....)
>>>
>>> Thanks for any hint
>>>
>>> Christian
>>>
>>>
>>>
>>>
>>> --
>>> --------------------------------------------
>>> | Christian Arnault                        |
>>> | LAL Bat 200 pièce 03a                    |
>>> | 91405 Orsay CEDEX                        |
>>> | phone   : (33) 1 64 46 84 24             |
>>> | gsm     : (33) 6 77 27 62 30             |
>>> | fax     : (33) 1 69 07 94 04             |
>>> | e-mail  : christian.arnault at lal.in2p3.fr |
>>> --------------------------------------------
>>>
>>> --
>>>
>>> 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
> --
>
> 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


-- 
--------------------------------------------
| Christian Arnault                        |
| LAL Bat 200 pièce 03a                    |
| 91405 Orsay CEDEX                        |
| phone   : (33) 1 64 46 84 24             |
| gsm     : (33) 6 77 27 62 30             |
| fax     : (33) 1 69 07 94 04             |
| e-mail  : christian.arnault at lal.in2p3.fr |
--------------------------------------------



More information about the CMake mailing list