How to strip trailing whitespace in CMake variable?

前端 未结 4 1431
悲哀的现实
悲哀的现实 2020-12-09 18:50

We are trying to improve the makefiles produced by CMake. For Clang, GCC and ICC, we want to add -march=native. The block to do so looks like:

#         


        
相关标签:
4条回答
  • 2020-12-09 19:13

    Adding dollar sign and curly braces, ${UNAME_MACHINE}, results in the same original problem (the newline is still present).

    If you use curly braces you also need quote marks. EG:

    string(STRIP "${UNAME_MACHINE}" UNAME_MACHINE)
    
    0 讨论(0)
  • 2020-12-09 19:16

    This strips terminating newline in the variable <varname>:

    string(REGEX REPLACE "\n$" "" <varname> "${<varname>}")
    

    Works for one of the project I involved to since CMake 2.8.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-09 19:29

    You can also use CMake's string STRIP command which will remove leading and trailing spaces (and the trailing newline).

    string(STRIP <string> <output variable>)
    
    0 讨论(0)
提交回复
热议问题