OK. If using ExternalProject, as Volker notes sudo may be making more complexities with the environment variables propagating through.
I would also strongly encourage in general using build systems including CMake without sudo. Perhaps only use sudo on the cmake --install build step if needed.