# How to set a DEFER CALL for the directory added with add\_subdirectory()

**URL:** https://discourse.cmake.org/t/how-to-set-a-defer-call-for-the-directory-added-with-add-subdirectory/12843
**Category:** Code
**Created:** [October 26, 2024, 8:50am UTC](https://discourse.cmake.org/t/how-to-set-a-defer-call-for-the-directory-added-with-add-subdirectory/12843 "2024-10-26T08:50:25Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![petk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/petk/32/4555_2.png) [@petk](https://discourse.cmake.org/u/petk)
#### Post date: [October 26, 2024, 8:50am UTC](https://discourse.cmake.org/t/how-to-set-a-defer-call-for-the-directory-added-with-add-subdirectory/12843/1 "2024-10-26T08:50:25Z")

</div>

Hello,

is there any option perhaps to set a `DEFER` `CALL` in the scope of the  
directory that will be added with `add_subdirectory()`? Or if there is any other  
way to “inject” some CMake module or code into the scope of the  
`add_subdirectory()`?

Because, I’m adding a various number of project extensions with  
`add_subdirectory()` and trying to do as little code repeating inside the  
extension’s `CMakeLists.txt`:

```cmake
function(foo_deferred_call)
  message(
    STATUS
    "This should be called in the extension scope: ${CMAKE_CURRENT_SOURCE_DIR}"
  )
  configure_file(config.h.in config.h)
endfunction()

foreach(extension ${extensions})
  cmake_language(DEFER DIRECTORY ${extension} CALL foo_deferred_call)
  add_subdirectory(${extension})
endforeach()

```

Issue is that for the `DEFER` `CALL` the directory must be known to CMake  
otherwise the error pops up:

```auto
cmake_language DEFER DIRECTORY

    /path/to/ext/extension_name...

is not known. It may not have been processed yet.

```

Thank you for any possible hints or tips.
