# Method for allowing generate Makefiles to internally environment variables

**URL:** https://discourse.cmake.org/t/method-for-allowing-generate-makefiles-to-internally-environment-variables/13248
**Category:** Code
**Tags:** gen:makefiles
**Created:** [December 21, 2024, 4:24pm UTC](https://discourse.cmake.org/t/method-for-allowing-generate-makefiles-to-internally-environment-variables/13248 "2024-12-21T16:24:06Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![leakec](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leakec/32/5216_2.png) [@leakec](https://discourse.cmake.org/u/leakec)
#### Post date: [December 21, 2024, 4:24pm UTC](https://discourse.cmake.org/t/method-for-allowing-generate-makefiles-to-internally-environment-variables/13248/1 "2024-12-21T16:24:06Z")

</div>

I use cmake to compile projects with many subdirectories that can be built independently. Sometimes, during debugging, I’d like to recompile just one of those subdirectories with a different optimization level for debugging. There is an option to go back and change the `CMakeLists.txt` file for that subdirectory. However, it would be more convenient if I could just do something like `OPT=-O0 make all -j 10`.

In order for this to work, I would need to be able to do something like

```auto
add_compile_options($(OPT))

```

where compile options wouldn’t escape anything. This would lead to the `OPT` `Makefile` variable being used in the compile options, which would allow me to set it via an environment variable. However, the generated `Makefile` gets `"\$(OPT)"` rather than `$(OPT)`. I’ve tried with string literals as well, but as mentioned in [this](https://discourse.cmake.org/t/string-escaping-with-target-compile-options/2117) post, certain characters still get escaped. Is this possible, or is the only option to go back and modify `CMakeLists.txt`?
