# Customize compiler warnings/definitions/includes on a per-source or per-directory basis

**URL:** https://discourse.cmake.org/t/customize-compiler-warnings-definitions-includes-on-a-per-source-or-per-directory-basis/14278
**Category:** Code
**Tags:** os:linux
**Created:** [June 21, 2025, 1:34am UTC](https://discourse.cmake.org/t/customize-compiler-warnings-definitions-includes-on-a-per-source-or-per-directory-basis/14278 "2025-06-21T01:34:34Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![sk\_anduril](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@sk\_anduril](https://discourse.cmake.org/u/sk_anduril)
#### Post date: [June 21, 2025, 1:34am UTC](https://discourse.cmake.org/t/customize-compiler-warnings-definitions-includes-on-a-per-source-or-per-directory-basis/14278/1 "2025-06-21T01:34:34Z")

</div>

I’d like to enhance my CMake build system so that I can “crank up” the warning level (ie, -Wall -Werror) on most of my code, but there are pieces of the source tree (maybe third-party vendor code or old legacy code we don’t want to modify) where I need to turn off certain warnings. How could I do this in a way that’s clean and scalable? Ideally using some kind of inheritence scheme that could be modified at a certain level of directory, target, or source file – this would not just be adding options but also removing them (for example, deleting -Wunused-variable).

CMake seems to have some features around this, but not exactly. Maybe I’m not understanding how to make this happen. Directory property COMPILE\_OPTIONS is set by add\_compile\_options() and in turn the target property COMPILE\_OPTIONS inherits its initial value from this property on its enclosing directory. The source-file property COMPILE\_OPTIONS apparently doesn’t inherit or have any initial value, and its contents are added _after_ the options from the target property.

I was trying to come up with some generic extensible way to start with a base set of options/warnings and have those inherited down through each directory/target, but with the ability to alter, add, or remove some at some point. But the source-file options getting appended after the target options doesn’t quite work. I think I would have to write a lot of custom macros which users would have to use to define targets

Would there be some way to use generator expressions at the top-level which depended upon inherited directory variable and target/source properties to then generate the required command-line options? Or does anyone have other ideas?

Sorry this isn’t a very crisp question, more of wondering what approach to take, or if it’s even doable? Or something requiring enhancements to CMake?
