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

查看:183
本文介绍了如何剥离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()

消息语句显示<$ c $中的机器字符串c> 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?

还是我应该切换到正则表达式匹配项,它应该不受换行符的影响?

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< string><输出变量>)看起来很有希望,但 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).

推荐答案

这会删除va中终止换行符riable< varname>:

This strips terminating newline in the variable <varname>:

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

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

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

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

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