[cmake-developers] Namespaces

Ruslan Baratov ruslan_baratov at yahoo.com
Thu Jan 28 14:25:34 EST 2016


On 29-Jan-16 01:17, Stephen Kelly wrote:
> Pau Garcia i Quiles wrote:
>
>> add_subdirectory(pugixml/scripts)
>> add_subdirectory(QtZeroConf)
>> add_subdirectory(QtDropBox)
>> add_subdirectory(websocketpp)
>>
>> This is an easy way to work with third-party dependences.
> Unfortunately this is 'easy, obvious and wrong'.
This is wrong because of CMake limitations. Actually I was thinking 
about "namespacing" subdirectories too. From my point of view we can add 
namespace prefix every time when command 'project' called.
Project Foo:

== standalone ==
project(Foo)
add_library(foo ...) # add target foo

== as a subdirectory ==
project(Boo)
add_subdirectory(Foo)
# target Foo::foo

Since regular variables has different scope there is no need to do any 
updates for them, we need only to do something with globals: targets, 
cache variables, global properties (?).

>> My problem comes from the fact several of these third-party libraries use
>> common variable names, e. g. BUILD_SHARED, BUILD_TESTS, etc).
> And indeed this is why.
>
>> Has some kind of namespacing been considered for add_subdirectory?
>>
>> E. g.
>>
>> add_subdirectory(QtZeroConf NAMESPACE QTZEROCONF)
> And what if someone builds your project with -DBUILD_SHARED=ON? Are you
> going to use
>
>   set(QTZEROCONF_BUILD_SHARED ${BUILD_SHARED})
>   add_subdirectory(QtZeroConf NAMESPACE QTZEROCONF)
>
> for every variable you're wrapping? Or do you want cmake to do that for
> every variable in scope when the directory is added? How would cmake decide
> which variables to namespace?
I think this can be solved by adding extra suboptions to 
add_subdirectory command.

>
> You should use ExternalProject instead, or fork the buildsystem of those
> projects to integrate them into your project. Not that I recommend the
> latter for you though :). That's what cmake does when integrating 3rd party
> code.
As far as I understand `add_subdirectory(3rdParty/package)` approach 
used in CMake sources:
* zlib: 
https://github.com/Kitware/CMake/blob/5335d275527d4f58e4f2992d307c1c62cd94dd3c/CMakeLists.txt#L306
* curl: 
https://github.com/Kitware/CMake/blob/5335d275527d4f58e4f2992d307c1c62cd94dd3c/CMakeLists.txt#L337
* bzip2: 
https://github.com/Kitware/CMake/blob/5335d275527d4f58e4f2992d307c1c62cd94dd3c/CMakeLists.txt#L355

Ruslo


More information about the cmake-developers mailing list