如何去除CMake变量中的尾随空格? [英] How to strip trailing whitespace in CMake variable?

查看:42
本文介绍了如何去除CMake变量中的尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在努力改进 CMake 生成的 makefile.对于 Clang、GCC 和 ICC,我们要添加 -march=native.这样做的块看起来像:

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

messages 语句显示来自 uname 的机器字符串有一个尾随换行符:

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

生成UNAME_MACHINE的块是:

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

如何从 CMake 中的 UNAME_MACHINE 中去除尾随换行符?

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

或者我应该切换到一个正则表达式 matches,它应该不受换行符的影响?

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

或者,我应该做点别的吗?

Or, should I do something else?

我们正在尝试通过 Current 支持 CMake 2.8.这大致将我们带回 Ubuntu 12.04 LTS.那个时候还有其他一些操作系统将事情推得更远.虽然 string(STRIP ) 看起来很有希望,但 CMake 没有在其文档中提供版本信息,因此我们不确定它是否满足要求.

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 看起来剥离在 3.0.2 中不起作用,所以看起来我们需要别的东西.

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)

结果如下(我们期望 xx86_64x):

Results in the following (we expect xx86_64x):

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

添加美元符号和花括号 ${UNAME_MACHINE} 会导致相同的原始问题(换行符仍然存在).

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

推荐答案

这会去除变量 中的终止换行符:

This strips terminating newline in the variable <varname>:

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

为我自 CMake 2.8 以来参与的项目之一工作.

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

这篇关于如何去除CMake变量中的尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆