CMake: improve dependency discovery