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

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?