我们正在尝试改进CMake生成的makefile。对于Clang,GCC和ICC,我们要添加-march=native
。这样做的块的样子:如何去除CMake变量中的尾部空白?
# -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()
报表显示,从uname
机器串中的消息有一个结尾的新行:
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
中去除尾随的换行符?
或者我应该切换到正则表达式matches
,这应该不会受到换行符的影响吗?
或者,我应该做点别的吗?
我们试图通过Current来支持CMake 2.8。这大致把我们带回Ubuntu 12.04 LTS。当时还有一些其他的操作系统将事情推得更远。虽然string(STRIP <string> <output variable>)
看起来很有希望,CMake does not supply version information with its documentation,所以我们不确定它是否会符合要求。
编辑它的出现剥3.0.2不工作,所以会出现我们需要别的东西。
# Strip lead and trailing whitepasce
string(STRIP UNAME_MACHINE, UNAME_MACHINE)
结果如下(我们预计xx86_64x
):
STATUS,1
STATUS,Compiler: xGNUx
STATUS,2
STATUS,Machine: xUNAME_MACHINE,x
添加美元符号和大括号,${UNAME_MACHINE}
,导致相同的原始问题(换行仍然存在)。
'字符串(STRIP UNAME_MACHINE,UNAME_MACHINE)' - 这可能是因为它失败,因为你提供一个逗号('')当参数由cmake的空格隔开。 –
CMake文档[状态](https://cmake.org/cmake/help/v3.5/command/string.html#strip)'字符串(STRIP<输出变量>)'所以你的命令实际上应该是'字符串(STRIP $ {UNAME_MACHINE} UNAME_MACHINE)' –
AhmedBM