# Relative including cmake scripts in a subproject

**URL:** <https://discourse.cmake.org/t/relative-including-cmake-scripts-in-a-subproject/4638>\
**Category:** Code\
**Created:** [December 10, 2021, 4:13pm UTC](https://discourse.cmake.org/t/relative-including-cmake-scripts-in-a-subproject/4638 "2021-12-10T16:13:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcopik](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcopik/32/2024_2.png) [@mcopik](https://discourse.cmake.org/u/mcopik)\
**Post date:** [December 10, 2021, 4:13pm UTC](https://discourse.cmake.org/t/relative-including-cmake-scripts-in-a-subproject/4638/1 "2021-12-10T16:13:23Z")

</div>

HI!

I have the following problem: I’m fetching and building a few dependencies that should ship with my applications, primarily header-only and static libraries. I’m using FetchContent for that and it has been working great so far.

However, the new project has the following include in their CMake:

```auto
include(dependencies)

```

and it is used to include `cmake/dependencies.cmake`.  
Unfortunately, my project uses exactly the same structure, and for some reason, this resolves to a recursive inclusion of my file.

Is there a flag or option that I might have forgotten to set? I haven’t found anything in the documentation on `include` function. At the moment, I resolved it by replacing the line in the dependency with

```auto
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies.cmake)

```

but it’s not a long-term solution. Ideally, I’d like to make sure that all CMake configuration in the dependency uses path relative to `<build-dir>/_deps/<dependency>-src`.

My fetching script:

```auto
###                                                                                                 
# AWS SDK - just the S3 part.                                                                       
###                                                                                                 
 find_package(AWSSDK COMPONENTS s3 QUIET)                                                            
  if(NOT AWSSDK_FOUND)                                                                                
    message(STATUS "Downloading and building AWS SDK dependency")                                     
    FetchContent_Declare(awsSDK                                                                       
     GIT_REPOSITORY https://github.com/aws/aws-sdk-cpp                                              
   )                                                                                                 
   FetchContent_Populate(awsSDK)                                                                     
   set(BUILD_ONLY "s3" CACHE INTERNAL "")                                                            
   set(ENABLE_TESTING OFF CACHE INTERNAL "")                                                         
   FetchContent_MakeAvailable(awsSDK)                                                                
   add_subdirectory(${awssdk_SOURCE_DIR} ${awssdk_BINARY_DIR})                                       
endif() 

```

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [December 11, 2021, 6:52pm UTC](https://discourse.cmake.org/t/relative-including-cmake-scripts-in-a-subproject/4638/2 "2021-12-11T18:52:47Z")

</div>

Projects should add their local CMake script directories to the _front_ of `CMAKE_MODULE_PATH`, not the end. The AWS SDK project will need to use `list(INSERT CMAKE_MODULE_PATH 0)` instead of `list(APPEND)` (or something similar).

---

<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 17, 2021, 7:40am UTC](https://discourse.cmake.org/t/relative-including-cmake-scripts-in-a-subproject/4638/3 "2021-12-17T07:40:19Z")

</div>

> [@mcopik](#):
>
> ```auto
> FetchContent_MakeAvailable(awsSDK)                                                                
> add_subdirectory(${awssdk_SOURCE_DIR} ${awssdk_BINARY_DIR})
> 
> ```

Why do you call `add_subdirectory()` here? This is already something that `FetchContent_MakeAvailable()` does, so you will be including the same source directory with the same binary directory twice. I’m somewhat surprised CMake doesn’t complain about that.

---

<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 17, 2021, 7:44am UTC](https://discourse.cmake.org/t/relative-including-cmake-scripts-in-a-subproject/4638/4 "2021-12-17T07:44:42Z")

</div>

> [@mcopik](#):
>
> ```cmake
> FetchContent_Declare(awsSDK
> GIT_REPOSITORY https://github.com/aws/aws-sdk-cpp
> )
> FetchContent_Populate(awsSDK)
> set(BUILD_ONLY "s3" CACHE INTERNAL "")
> set(ENABLE_TESTING OFF CACHE INTERNAL "")
> FetchContent_MakeAvailable(awsSDK)
> add_subdirectory(${awssdk_SOURCE_DIR} ${awssdk_BINARY_DIR})
> 
> ```

Actually this whole block is a bit of a muddle. Don’t call `FetchContent_Populate()` if you’re using `FetchContent_MakeAvailable()`. And setting the variables as cache variables also looks suspicious (sometimes you might need to do that, but I’d normally set non-cache variables for local changes like this unless the dependency requires them to be cache variables). I’d expect this block to look something more like this:

```cmake
   FetchContent_Declare(awsSDK
     GIT_REPOSITORY https://github.com/aws/aws-sdk-cpp
   )
   set(BUILD_ONLY "s3")
   set(ENABLE_TESTING OFF)
   FetchContent_MakeAvailable(awsSDK)

```
