but as I do this the unused shader variables are then not found by my glUniform location variables, and this makes it difficult to proceed.
Calling glUniform with -1 (the value returned from glGetUniformLocation if the uniform does not exist) is not considered an error. The command will execute and do nothing.
So there’s really no problem. Unless you have code detecting a -1 and creating an error based on that.
Or you can use a condition which is always true or false, but which can be optimized directly by the compiler.
Unless the compiler recompiles the code when you set a uniform (and apparently, they don’t do that anymore), there is no way for the compiler to know whether the branch will be taken or not. So nothing will be “optimized directly by the compiler.”