xcode CMake、source_group 使用不正确或行为不正确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17364616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CMake, source_group used incorrectly or has incorrect behaviour?
提问by kamjagin
Using source_group in CMake to organize source files (in this case for Xcode) turned out to be more tedious than expected.
在 CMake 中使用 source_group 来组织源文件(在这种情况下为 Xcode)被证明比预期的更乏味。
set(REG1 ".*/some_folder1/")
set(REG2 ".*/some_folder2/")
set(REC ".*([.]cpp|[.]c)")
source_group("src" REGULAR_EXPRESSION "${REC}" )
source_group("src\some_group" REGULAR_EXPRESSION "${REG1}${REC}" )
source_group("src\some_other_group" REGULAR_EXPRESSION "${REG2}${REC}" )
I expected the above to put all source files from some_folder1 in src/some_group in the IDE, some_folder2 in src/some_other_group, and the rest of the files directly in the src group.
我希望上面的代码将 some_folder1 中的所有源文件放在 IDE 的 src/some_group 中,将 some_folder2 放在 src/some_other_group 中,其余文件直接放在 src 组中。
However, the result is that all files go directly into the src group.
但是,结果是所有文件都直接进入 src 组。
If I remove the first source_group-line and only use the two non-overlapping ones:
如果我删除第一个 source_group-line 并且只使用两个不重叠的:
source_group("src\some_group" REGULAR_EXPRESSION "${REG1}${REC}" )
source_group("src\some_other_group" REGULAR_EXPRESSION "${REG2}${REC}" )
the behaviour is almost as intended. I.e. all files matching the two regular expressions are organized as intended, but the rest of the files go into the default "Source Files" group created by Xcode (a different IDE will have a different default folder).
行为几乎符合预期。即匹配两个正则表达式的所有文件都按预期组织,但其余文件进入由 Xcode 创建的默认“源文件”组(不同的 IDE 将具有不同的默认文件夹)。
So it seems like the first source_group somehow overrides the remaining two even if CMake's documentation states the opposite (excerpt from CMake's documentation on source_group):
因此,即使 CMake 的文档说明相反(摘自 CMake 关于 source_group 的文档),似乎第一个 source_group 以某种方式覆盖了其余两个:
If a file matches multiple groups, the LAST group that explicitly lists the file will be favored, if any. If no group explicitly lists the file, the LAST group whose regular expression matches the file will be favored.
如果一个文件匹配多个组,则明确列出该文件的 LAST 组将受到青睐(如果有)。如果没有组明确列出该文件,则优先考虑正则表达式与该文件匹配的 LAST 组。
Is this due to some misunderstanding on my part or does CMake->source_group actually behave in a different way than advertised?
这是由于我的一些误解还是 CMake->source_group 实际上的行为方式与宣传的方式不同?
I am using cmake version 2.8.10 on a OSX platform.
我在 OSX 平台上使用 cmake 2.8.10 版。
回答by kamjagin
Yes, looks like source groups generates incorrectly. If you interested in workaround, you can rewrite first regex to not match other groups:
是的,看起来源组生成不正确。如果您对解决方法感兴趣,您可以重写第一个正则表达式以不匹配其他组:
cmake_minimum_required(VERSION 2.8.4)
project(example_simple CXX)
file(
GLOB
src
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"
"${CMAKE_CURRENT_SOURCE_DIR}/headers/*.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/headers/*.h"
)
add_executable(simple_bin ${src})
set(REG_SRC ".*/src/")
set(REG_HDR ".*/headers/")
set(REG_EXT "[^/]*([.]cpp|[.]c|[.]h|[.]hpp)$")
source_group("src" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/${REG_EXT}")
source_group("src\[sources]" REGULAR_EXPRESSION "${REG_SRC}${REG_EXT}")
source_group("src\[headers]" REGULAR_EXPRESSION "${REG_HDR}${REG_EXT}")
or write your own regex order and use FILES, instead of REGULAR_EXPRESSION:
或编写您自己的正则表达式顺序并使用 FILES,而不是 REGULAR_EXPRESSION:
cmake_minimum_required(VERSION 2.8.4)
project(example_simple CXX)
file(
GLOB
src
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"
"${CMAKE_CURRENT_SOURCE_DIR}/headers/*.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/headers/*.h"
)
add_executable(simple_bin ${src})
set(REG_SRC ".*/src/")
set(REG_HDR ".*/headers/")
set(REG_EXT ".*([.]cpp|[.]c|[.]h|[.]hpp)")
foreach(filename ${src})
string(REGEX MATCH "${REG_SRC}${REG_EXT}" match_group_sources ${filename})
string(REGEX MATCH "${REG_HRD}${REG_EXT}" match_group_headers ${filename})
if(match_group_sources)
list(APPEND group_sources ${filename})
elseif(match_group_headers)
list(APPEND group_headers ${filename})
else()
list(APPEND group_other ${filename})
endif()
endforeach()
source_group("src" FILES ${group_other})
source_group("src\[sources]" FILES ${group_sources})
source_group("src\[headers]" FILES ${group_headers})