# Top project/ master project : vocabulary

**URL:** https://discourse.cmake.org/t/top-project-master-project-vocabulary/412
**Category:** Development
**Created:** [December 18, 2019, 3:30pm UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412 "2019-12-18T15:30:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lieven](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@Lieven](https://discourse.cmake.org/u/Lieven)
#### Post date: [December 18, 2019, 3:30pm UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412/1 "2019-12-18T15:30:47Z")

</div>

Just like Design patterns gives us vocabulary to identify the same thing,pattern. I would like to request a similar thing for cmake, and request that cmake itself provide an implementation.

Thanks to FetchContent “other” libraries can now be source consumed, which is great. This requires off course some discipline of the library writers, they no longer should assume they are in charge of everything, and the first thing they need to know if they are in charge or not is to determine if they are the top/master project, yes or no.

If have already seen 2 mechanisms to detect that:

1. compare CMAKE\_CURRENT\_SOURCE\_DIR and CMAKE\_SOURCE\_DIR
2. compare CMAKE\_PROJECT\_NAME and PROJECT\_NAME

In order to avoid that everyone is writing his/her own version of this boilerplate, it would be nice that cmake provides a function that can be called and gives this as an outcome. That way all use the same implementation, and it has a fixed name (vocabulary).

What do you think ?

kind regards,  
Lieven

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [December 18, 2019, 3:49pm UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412/2 "2019-12-18T15:49:33Z")

</div>

> the first thing they need to know if they are in charge or not is to determine if they are the top/master project, yes or no.

There is also the [PARENT\_DIRECTORY](https://cmake.org/cmake/help/v3.16/prop_dir/PARENT_DIRECTORY.html) directory property that is an empty string at the real top-level.

```cmake
get_property(parent DIRECTORY PROPERTY PARENT_DIRECTORY)
if(NOT parent)
  # I am in charge.
endif()

```

---

<div class="post-metadata">

### Author: ![Lieven](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@Lieven](https://discourse.cmake.org/u/Lieven)
#### Post date: [December 18, 2019, 4:31pm UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412/3 "2019-12-18T16:31:20Z")

</div>

so that is a third way.

Which makes me case more firm 😉

there should be some is\_top\_project() function.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [December 18, 2019, 4:54pm UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412/4 "2019-12-18T16:54:44Z")

</div>

I agree, it should be easy to do the right thing.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [December 19, 2019, 1:11am UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412/5 "2019-12-19T01:11:40Z")

</div>

> If have already seen 2 mechanisms to detect that:
> 
> 1. compare CMAKE\_CURRENT\_SOURCE\_DIR and CMAKE\_SOURCE\_DIR
> 2. compare CMAKE\_PROJECT\_NAME and PROJECT\_NAME

I always use option 1. The second option technically isn’t guaranteed, since a project name isn’t required to be unique.

> There is also the [PARENT\_DIRECTORY](https://cmake.org/cmake/help/v3.16/prop_dir/PARENT_DIRECTORY.html) directory property that is an empty string at the real top-level.
> 
> ```auto
> get_property(parent DIRECTORY PROPERTY PARENT_DIRECTORY)
> if(NOT parent)
> # I am in charge.
> endif()
> 
> ```

A while back, I hit some sort of bug where this or something like it wasn’t reliable. I don’t recall the exact scenario and I can’t be certain that it wasn’t an error in the project at the time, but for this reason, I don’t use this approach myself.

> there should be some is\_top\_project() function.

Unfortunately, CMake doesn’t support returning values from functions, so you’d have to provide a variable name to set. That means you would require two separate lines if using this approach. Comparing `if(CMAKE_SOURCE_DIR` and `CMAKE_CURRENT_SOURCE_DIR)` directly is about the most concise way to express it given the current language limitations. I agree it would be nice if we had a better alternative. Perhaps the `project()` command could set/update a variable like `CMAKE_PROJECT_IS_TOP_LEVEL`, but this would be new functionality and projects would have to do `cmake_minimum_required(VERSION X.Y)` to ensure it was supported.

---

<div class="post-metadata">

### Author: ![Lieven](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@Lieven](https://discourse.cmake.org/u/Lieven)
#### Post date: [December 19, 2019, 9:00am UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412/6 "2019-12-19T09:00:28Z")

</div>

well it could be similar like this snippet :

get\_property(isMultiConfig GLOBAL PROPERTY GENERATOR\_IS\_MULTI\_CONFIG)  
if(isMultiConfig)  
…  
endif()

(just took this as an example, I don’t suggest it should be a global property or whatsoever, just as a style example)

and indeed it would be a new function or variable. And code that wants to support older versions, will need to continue to do the old thing, or require a certain minimum version.  
But that is the same with every new thing that gets added, like in the last couple of years.

---

<div class="post-metadata">

### Author: ![Lieven](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@Lieven](https://discourse.cmake.org/u/Lieven)
#### Post date: [February 4, 2020, 7:21pm UTC](https://discourse.cmake.org/t/top-project-master-project-vocabulary/412/7 "2020-02-04T19:21:39Z")

</div>

create an issue for it : [https://gitlab.kitware.com/cmake/cmake/issues/20310](https://gitlab.kitware.com/cmake/cmake/issues/20310)
