我们正在努力改进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中删除尾部换行符?
或者我应该切换到正则表达式匹配,不应该受到换行符的影响?
或者,我应该做别的吗?
我们正试图通过Current支持CMake 2.8.这大致将我们带回到Ubuntu 12.04 LTS.当时还有其他一些操作系统可以将事情推迟一些.虽然字符串(STRIP< string><输出变量>)看起来很有希望,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,Machine: xUNAME_MACHINE,x
添加美元符号和花括号${UNAME_MACHINE}会导致相同的原始问题(换行仍然存在).
解决方法
这条带在变量< varname>中终止换行符:
string(REGEX REPLACE "\n$" "" <varname> "${<varname>}")
适用于自CMake 2.8以来参与的项目之一.