This looks good to me, modulo the refactoring idea it inspired.
Hmmm, we've grown a lot of these grep commands. I wrote a generic version of this some time ago (7.x? 8.x?)e. I thought I'd committed it, but it appears that I didn't.
Personally, I'd create a mk.$OPTION file in $S/conf. For all options that are enabled that have a mk.$OPTION have config write them into the makefile at %BEFORE_DEPEND.
But I'll be the first to admit this is beyond the scope of this patch.
I'll have to add another of these for KMSAN, so a better solution is indeed warranted. The approach of having mk.KASAN seems good. We'd need to audit all of the existing uses of "grep <OPTION> opt_foo.h" to make sure they're all amenable to this approach.