5

我使用 CMake 的树外构建。我有一个 CMake 自定义命令,可以从原始文件生成 *_pb2.py 文件。由于 proto-files 可能驻留在未知数量的子目录(包命名空间)中,例如$SRC/package1/package2/file.proto.,因此构建目录将包含类似$BLD/package1/package2/file_pb2.py.

我想从自动生成的 *_pb2.py 文件中隐式地制作包,因此,我想在所有子文件夹( , 等)中自动生成 __init__.py 文件$BLD/package1$BLD/package1/package2然后安装它们。

我怎样才能做到这一点?

PS我尝试过CMake 中的宏:如何获取目录的所有子目录的名称?(将GLOB更改为GLOB_RECURSE)但它只返回包含文件的子目录。我无法package1从上面的示例中获取 subdir。

4

2 回答 2

6

如果您在 *NIX 操作系统(包括 mac)下工作,您可以使用 shell find 命令,例如:

ROOT="./"
for DIR in $(find $ROOT -type d); do
    touch $DIR/__init__.py
done

或使用 python 脚本:

from os.path import isdir, walk, join

root = "/path/to/project"
finit = '__init__.py'
def visitor(arg, dirname, fnames):
    fnames = [fname for fname in fnames if isdir(fname)]
    # here you could do some additional checks ...
    print "adding %s to : %s" %(finit, dirname)
    with open(join(dirname, finit), 'w') as file_: file_.write('')

walk(root, visitor, None)
于 2012-07-12T09:53:55.583 回答
2

以下内容应为您提供变量中所需的目录列表AllPaths

# Get paths to all .py files (relative to build dir)
file(GLOB_RECURSE SubDirs RELATIVE ${CMAKE_BINARY_DIR} "${CMAKE_BINARY_DIR}/*.py")
# Clear the variable AllPaths ready to take the list of results
set(AllPaths)
foreach(SubDir ${SubDirs})
  # Strip the filename from the path
  get_filename_component(SubDir ${SubDir} PATH)
  # Change the path to a semi-colon separated list
  string(REPLACE "/" ";" PathParts ${SubDir})
  # Incrementally rebuild path, appending each partial path to list of results
  set(RebuiltPath ${CMAKE_BINARY_DIR})
  foreach(PathPart ${PathParts})
    set(RebuiltPath "${RebuiltPath}/${PathPart}")
    set(AllPaths ${AllPaths} ${RebuiltPath})
  endforeach()
endforeach()
# Remove duplicates
list(REMOVE_DUPLICATES AllPaths)
于 2012-07-12T10:18:01.633 回答