execute_process
has a flag for stripping trailing whitespace on either standard out or standard error (or both)
[OUTPUT_STRIP_TRAILING_WHITESPACE]
[ERROR_STRIP_TRAILING_WHITESPACE]
https://cmake.org/cmake/help/v3.0/command/execute_process.html