Scons. Go recursive with Glob

Alex Povar picture Alex Povar · May 15, 2012 · Viewed 10.4k times · Source

I using scons for a few days and confused a bit. Why there is no built-in tools for building sources recursively starting from given root? Let me explain: I have such source disposition:

src
    Core
       folder1
       folder2
           subfolder2_1
    Std
       folder1

..and so on. This tree could be rather deeper.

Now I build this with such construction:

sources = Glob('./builds/Std/*/*.cpp')
sources = sources + Glob('./builds/Std/*.cpp')
sources = sources + Glob('./builds/Std/*/*/*.cpp')
sources = sources + Glob('./builds/Std/*/*/*/*.cpp')

and this looks not so perfect as at can be. Of cause, I can write some python code, but is there more suitable ways of doing this?

Answer

Igor Chubin picture Igor Chubin · Jun 15, 2012

As Torsten already said, there is no "internal" recursive Glob() in SCons. You need to write something yourself. My solution is:

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:]))

I want to stress that you need Glob() here (not glob.glob() from python) especially when you use VariantDir(). Also when you use VariantDir() don't forget to convert absolute paths to relative (in the example I achieve this using [len(root)+1:]).