title: How to strip trailing whitespace in CMake variable?
body: <p>We are trying to improve the makefiles produced by CMake. For Clang, GCC and ICC, we want to add <code>-march=native</code>. The block to do so looks like:</p>
  
  <pre><code># -march=native for GCC, Clang and ICC on i386, i486, i586, i686 and x86_64.
  message(STATUS, "1")
  message(STATUS, "Compiler: x${CMAKE_CXX_COMPILER_ID}x")
  if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
      message(STATUS, "2")
      message(STATUS, "Machine: x${UNAME_MACHINE}x")
      if (("${UNAME_MACHINE}" MATCHES "i.86") OR ("${UNAME_MACHINE}" STREQUAL "x86_64"))
              message(STATUS, "3")
          if (CMAKE_VERSION VERSION_LESS 2.8.12)
              add_definitions(-march=native)
          else()
              add_compile_options(-march=native)
          endif()
      endif()
  endif()
  </code></pre>
  
  <p>The messages statements show the machine string from <code>uname</code> has a trailing newline:</p>
  
  <pre><code>STATUS,1
  STATUS,Compiler: xGNUx
  STATUS,2
  STATUS,Machine: xx86_64
  x
  </code></pre>
  
  <p>The block to produce <code>UNAME_MACHINE</code> is:</p>
  
  <pre><code># We need the output 'uname -m' for Unix and Linux platform detection
  #    Be prepared for i386-i686, amd64, x86_64, arm, arm64, armel, armhf,
  #    mips, mips64, aarch32 and aarch64 (for starters)
  set (UNAME_CMD "uname")
  set (UNAME_ARG "-m")
  execute_process(COMMAND ${UNAME_CMD} ${UNAME_ARG}
      WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
      RESULT_VARIABLE UNAME_RESULT
      OUTPUT_VARIABLE UNAME_MACHINE)
  </code></pre>
  
  <p>How do I strip the trailing newline from <code>UNAME_MACHINE</code> in CMake?</p>
  
  <p>Or should I switch to a regex <code>matches</code>, which should not be affected by the newline?</p>
  
  <p>Or, should I do something else?</p>
  
  <hr>
  
  <p>We are attempting to support CMake 2.8 through Current. That roughly takes us back to Ubuntu 12.04 LTS. There are some other operating systems around that time that push things back a little further. While <code>string(STRIP &lt;string&gt; &lt;output variable&gt;)</code> looks promising, <a href="https://cmake.org/cmake/help/v3.5/command/string.html" rel="noreferrer">CMake does not supply version information with its documentation</a>, so we are not sure if it will meet requirements.</p>
  
  <p><strong><em>EDIT</em></strong> it appears stripping does not work in 3.0.2, so it appears we need something else.</p>
  
  <pre><code># Strip lead and trailing whitepasce
  string(STRIP UNAME_MACHINE, UNAME_MACHINE)
  </code></pre>
  
  <p>Results in  the following (we expect <code>xx86_64x</code>):</p>
  
  <pre><code>STATUS,1
  STATUS,Compiler: xGNUx
  STATUS,2
  STATUS,Machine: xUNAME_MACHINE,x
  </code></pre>
  
  <p>Adding dollar sign and curly braces, <code>${UNAME_MACHINE}</code>, results in the same original problem (the newline is still present).</p>