Make cmake's check_const_exists() rely on cache (cherry picked from commit 4c61a04cd5207f2140b65f955559aa4ffc848e9c)