我们正在努力改进由CMake生成的makefile。对于Clang、GCC和ICC,我们想要添加-march=native
选项。用于实现这一点的代码块如下:
# -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
的正则表达式,这不会受到换行符的影响吗?
或者,我应该做些其他的事情吗?
我们正在尝试支持从CMake 2.8到当前版本。大致上可以回到Ubuntu 12.04 LTS。有一些其他操作系统在那个时间左右,这使事情变得更加复杂。虽然string(STRIP <string> <output variable>)
看起来很有前途,但CMake的文档没有提供版本信息,所以我们不确定它是否符合要求。
编辑 看起来剥离在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}
后,结果与原问题相同(换行仍然存在)。
string(STRIP UNAME_MACHINE, UNAME_MACHINE)
— 可能由于在 cmake 中参数是用空格分隔而您却包含了逗号(,
)而导致失败。 - Alexis Wilkestring(STRIP <string> <output variable>)
,因此您的命令应该是string(STRIP ${UNAME_MACHINE} UNAME_MACHINE)
。 - AhmedBM