# What is the correct way to set options of a project before Fetch Content?

**URL:** <https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268>\
**Category:** Code\
**Created:** [November 27, 2019, 6:25am UTC](https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268 "2019-11-27T06:25:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adem\_Budak](https://discourse.cmake.org/user_avatar/discourse.cmake.org/adem_budak/32/144_2.png) [@Adem\_Budak](https://discourse.cmake.org/u/Adem_Budak)\
**Post date:** [November 27, 2019, 6:25am UTC](https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/1 "2019-11-27T06:25:02Z")

</div>

I want to disable some of the properties that the project I need to fetch, so I set the options just before the `FetchContent` declarations in my project/tests/CMakeLists.txt:

```auto
  set(XXX_BUILD_TESTING OFF)
  set(XXX_ENABLE_COVERAGE_INFO OFF)
  set(XXX_INSTALL_DOCS OFF)
  
  include(FetchContent)

  FetchContent_Declare(yyy
      QUITE
      GIT_REPOSITORY https://github.com/xxx/yyy
      GIT_TAG v9.9.9
      GIT_PROGRESS OFF
      GIT_SHALLOW ON )

```

But when I make `ccmake -S project -B build` , the options appears in default. I tried to set the options on top level `CMakeLists.txt`, but nothing change. I’m using the CMake version 3.15.5. Any pointer about what I’m doing wrong is really welcome. Thanks you kindly…

---

<div class="post-metadata">

**Author:** ![cristianadam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cristianadam/32/124_2.png) [@cristianadam](https://discourse.cmake.org/u/cristianadam)\
**Post date:** [November 27, 2019, 10:37am UTC](https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/2 "2019-11-27T10:37:06Z")

</div>

In your code snippet I don’t see the call to `FetchContent_MakeAvailable` which behind the scenes will call `add_subdirectory` which will enable your variables in the project.

This is how Hunter works:

```cmake
cmake_minimum_required(VERSION 3.14)

set(HUNTER_PACKAGES ZLIB)

include(FetchContent)
FetchContent_Declare(SetupHunter GIT_REPOSITORY https://github.com/cpp-pm/gate)
FetchContent_MakeAvailable(SetupHunter)

project(SetupHunter)

```

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [November 27, 2019, 7:07pm UTC](https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/3 "2019-11-27T19:07:46Z")

</div>

Moved this discussion to `Code` category.

---

<div class="post-metadata">

**Author:** ![phmarqui](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phmarqui/32/1506_2.png) [@phmarqui](https://discourse.cmake.org/u/phmarqui)\
**Post date:** [June 3, 2021, 9:31pm UTC](https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/4 "2021-06-03T21:31:01Z")

</div>

I have success with setting options when I use `CACHE INTERNAL`:

`set (XXX_BUILD_TESTING OFF CACHE INTERNAL "Turn off tests")`

---

<div class="post-metadata">

**Author:** ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)\
**Post date:** [June 9, 2021, 9:01pm UTC](https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/5 "2021-06-09T21:01:25Z")

</div>

I also use CACHE variables to propagate from top-level project to FetchContent subprojects. I also use `CACHE BOOL` etc. as relevant

---

<div class="post-metadata">

**Author:** ![barcharcraz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/barcharcraz/32/1558_2.png) [@barcharcraz](https://discourse.cmake.org/u/barcharcraz)\
**Post date:** [June 14, 2021, 5:39am UTC](https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/6 "2021-06-14T05:39:54Z")

</div>

CMP0077 is relevant here, if that policy is turned on (it was added in 3.13) then setting variables before calling FetchContent\_MakeAvailable will cause calls to `option` in the subproject to basically do nothing. This is nice as it means users of your project don’t even get cache entries for the subproject options you’re forcing.

Note that if the subproject calls cmake\_minimum\_required with some old version it may cause CMP0077 to have the OLD behavior, you can use `cmake_policy(SET)` to avoid this if I recall
