我想在cmake中实现一个函数来递归搜索路径列表。
假设有search_paths
:/a/b
,/c
,/d/e
。我希望在给定的search_paths下有一个return_list
目录路径,其中包含.h文件。这是我的功能:
FUNCTION(R_SEARCH search_paths return_list)
FOREACH(search_path ${search_paths})
# POINT A
R_SEARCH_INNER(${search_path} inner_return)
SET(all_list ${all_list} ${inner_return})
ENDFOREACH()
SET(${return_list} ${all_list})
ENDFUNCTION(R_SEARCH)
FUNCTION(R_SEARCH_INNER search_path return_list)
FILE(GLOB_RECURSE new_list ${search_path} *.h)
SET(dir_list "")
FOREACH(file_path ${new_list})
GET_FILENAME_COMPONENT(dir_path ${file_path} PATH)
SET(dir_list ${dir_list} ${dir_path})
ENDFOREACH()
LIST(REMOVE_DUPLICATES dir_list)
SET(${return_list} ${dir_list})
ENDFUNCTION(R_SEARCH_INNER)
基于http://public.kitware.com/pipermail/cmake/2012-June/050674.html
但是,POINT A上的内部函数调用只会发生一次。 任何人都可以给我一些提示或提供可行的解决方案。
答案 0 :(得分:2)
你应该改变这个:
FILE(GLOB_RECURSE new_list ${search_path} *.h)
到
FILE(GLOB_RECURSE new_list ${search_path}/*.h)
请参阅documentation:
递归通配的示例包括:
/dir/*.py - match all python files in /dir and subdirectories
另外,正如已经建议的那样(另见here),请更改:
SET(${return_list} ${all_list})
[...]
SET(${return_list} ${dir_list})
成:
SET(${return_list} ${all_list} PARENT_SCOPE)
[...]
SET(${return_list} ${dir_list} PARENT_SCOPE)
因此更正的代码变为:
FUNCTION(R_SEARCH search_paths return_list)
FOREACH(search_path ${search_paths})
# POINT A
R_SEARCH_INNER(${search_path} inner_return)
SET(all_list ${all_list} ${inner_return})
ENDFOREACH()
SET(${return_list} ${all_list} PARENT_SCOPE)
ENDFUNCTION(R_SEARCH)
FUNCTION(R_SEARCH_INNER search_path return_list)
FILE(GLOB_RECURSE new_list ${search_path}/*.h)
SET(dir_list "")
FOREACH(file_path ${new_list})
GET_FILENAME_COMPONENT(dir_path ${file_path} PATH)
SET(dir_list ${dir_list} ${dir_path})
ENDFOREACH()
LIST(REMOVE_DUPLICATES dir_list)
SET(${return_list} ${dir_list} PARENT_SCOPE)
ENDFUNCTION(R_SEARCH_INNER)