Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
880 views
in Technique[技术] by (71.8m points)

text processing - How to strip trailing whitespace in CMake variable?

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:

# -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()

The messages statements show the machine string from uname has a trailing newline:

STATUS,1
STATUS,Compiler: xGNUx
STATUS,2
STATUS,Machine: xx86_64
x

The block to produce UNAME_MACHINE is:

# 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)

How do I strip the trailing newline from UNAME_MACHINE in CMake?

Or should I switch to a regex matches, which should not be affected by the newline?

Or, should I do something else?


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 string(STRIP <string> <output variable>) looks promising, CMake does not supply version information with its documentation, so we are not sure if it will meet requirements.

EDIT it appears stripping does not work in 3.0.2, so it appears we need something else.

# Strip lead and trailing whitepasce
string(STRIP UNAME_MACHINE, UNAME_MACHINE)

Results in the following (we expect xx86_64x):

STATUS,1
STATUS,Compiler: xGNUx
STATUS,2
STATUS,Machine: xUNAME_MACHINE,x

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...