Hello Chip!
I think this is not how it works. The compiler optimizes code that your app does not use, in the meaning of: does not refer, or mention in your code.
This is different than a case where the code is not used at runtime because of the content of some variable (like a config sensor variable). The compiler here cannot know if at runtime you will decide to use this code or not, so it has not way of optimizing it.
From another angle: if the compiler had a way, and the config changed and your device wanted to use that code, how would it use it if it was not included in the binary?
Correct, however I do not know a way of making this work. On a binary that you want to use in a product, you would have to include all the code.
If you are running out of memory, then you need to find a different strategy (eg: create another product, hence another binary).
If the devices are ok in memory use, then I would choose to keep them in one product or create more than one based on how many headaches I save with one approach or the other.
I do not have a recommendation there.
you can use #ifdef or #ifndef to have the same code base, but different builds if you end up with more than one product/binary.
Best
PS: I would split this in another topic