How do I filter an SCons Glob result?

情到浓时终转凉″ 提交于 2019-12-05 02:57:10

问题


I sometimes want to exclude certain source files from a Glob result in SCons. Usually it's because I want to compile that source file with different options. Something like this:

objs = env.Object(Glob('*.cc'))
objs += env.Object('SpeciallyTreatedFile.cc', CXXFLAGS='-O0')

Of course, that creates a problem for SCons:

scons: *** Two environments with different actions were specified
       for the same target: SpeciallyTreatedFile.o

I usually work around this using the following idiom:

objs = env.Object([f for f in Glob('*.cc')
  if 'SpeciallyTreatedFile.cc' not in f.path])

But that's pretty ugly, and gets even uglier if there's more than one file to be filtered out.

Is there a clearer way to do this?


回答1:


I got fed up duplicating the [f for f in Glob ...] expression in several places, so I wrote the following helper method and added it to the build Environment:

import os.path

def filtered_glob(env, pattern, omit=[],
  ondisk=True, source=False, strings=False):
    return filter(
      lambda f: os.path.basename(f.path) not in omit,
      env.Glob(pattern))

env.AddMethod(filtered_glob, "FilteredGlob");

Now I can just write

objs = env.Object(env.FilteredGlob('*.cc',
  ['SpeciallyTreatedFile.cc', 'SomeFileToIgnore.cc']))
objs += env.Object('SpeciallyTreatedFile.cc', CXXFLAGS='-O0')

Using this pattern it would be easy to write something similar that uses, say, a regexp filter as the omit argument instead of a simple list of filenames, but this works well for my current needs.



来源:https://stackoverflow.com/questions/12518715/how-do-i-filter-an-scons-glob-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!