我使用scons几天而且有点困惑.为什么没有内置工具从给定的根开始递归地构建源?让我解释一下:我有这样的来源性格:
src
Core
folder1
folder2
subfolder2_1
Std
folder1
Run Code Online (Sandbox Code Playgroud)
..等等.这棵树可能更深.
现在我用这样的结构构建它:
sources = Glob('./builds/Std/*/*.cpp')
sources = sources + Glob('./builds/Std/*.cpp')
sources = sources + Glob('./builds/Std/*/*/*.cpp')
sources = sources + Glob('./builds/Std/*/*/*/*.cpp')
Run Code Online (Sandbox Code Playgroud)
这看起来并不那么完美.当然,我可以编写一些python代码,但是有更合适的方法吗?
正如Torsten所说,SCons中没有"内部"递归Glob().你需要自己写点东西.我的解决方案是:
import fnmatch
import os
matches = []
for root, dirnames, filenames in os.walk('src'):
for filename in fnmatch.filter(filenames, '*.c'):
matches.append(Glob(os.path.join(root, filename)[len(root)+1:]))
Run Code Online (Sandbox Code Playgroud)
我想强调你在这里需要Glob()(而不是python中的glob.glob()),特别是当你使用VariantDir()时.此外,当您使用VariantDir()时,不要忘记将绝对路径转换为相对路径(在示例中,我使用[len(root)+1:]实现此目的).
当然.你需要编写python包装器来遍历dirs.您可以在stackoverflow上找到许多食谱.这是我的简单函数,它返回当前目录中的子目录列表(并忽略以'.'开头的隐藏目录 - - 点)
def getSubdirs(abs_path_dir) :
lst = [ name for name in os.listdir(abs_path_dir) if os.path.isdir(os.path.join(abs_path_dir, name)) and name[0] != '.' ]
lst.sort()
return lst
Run Code Online (Sandbox Code Playgroud)
例如,我的dir模块包含foo,bar,ice.
corePath = 'abs/path/to/modules'
modules = getSubdirs(corePath)
# modules = [bar, foo, ice]
for module in modules :
sources += Glob(os.path.join(corePath, module, '*.cpp'))
Run Code Online (Sandbox Code Playgroud)
您可以改进getSubdirs函数,添加递归并深入到子目录.