2

For example:

/project/SConstruct
/project/main.cpp
/project/folder/bar.h
/project/folder/bar.cpp
/project/folder/foo.h
/project/folder/foo.cpp

What I want to for SCons to just compile all source files in all subdirectories without having to add a SConscript file in every subdirectory. Basically I want to pass a Glob('*.cpp') for /project and all subdirectoires in /project.

Thanks in advance to anyone who replies!

benbalach
  • 21
  • 2
  • 5

4 Answers4

2

A recursive globber that worked for me

def GlobRecursive(pattern, node='.'):
    results = []
    for f in Glob(str(node) + '/*', source=True):
        if type(f) is SCons.Node.FS.Dir:
            results += GlobRecursive(pattern, f)
    results += Glob(str(node) + '/' + pattern, source=True)
    return results

Robᵩ's answer didn't work for me because a) isdir() always returned False (scons 2.5.1) and b) list comprehensions are hard for me to comprehend :-)

krupan
  • 3,920
  • 5
  • 27
  • 24
1

You can do this from the root SConstruct, as follows:

env = Environment()
env.Program(source=[Glob('*.cpp'), Glob('folder/*.cpp')], target='yourBinaryName')

You will probably also need to configure the include directory as follows:

env.Append(CPPPATH='folder')

Remeber that Glob() is not recursive.

Brady
  • 10,207
  • 2
  • 20
  • 59
  • Thanks for the quick response! I don't want to manually have to enter the names of all the folders. All I want is for all *.cpp files in /project and subdirectories in /project to be compiled. – benbalach Oct 13 '14 at 15:56
  • @benbalach What you're asking for is called Recursive Glob, and that's not supported in SCons, that's why I mentioned it at the end of my answer. – Brady Oct 13 '14 at 16:12
1

As Brady points out, "Glob() is not recursive", but perhaps we could create a recursive globber:

def AllSources(node='.', pattern='*'):
    result = [AllSources(dir, pattern)
              for dir in Glob(str(node)+'/*')
              if dir.isdir()]
    result += [source
               for source in Glob(str(node)+'/'+pattern)
               if source.isfile()]
    return result

env = Environment()
env.Program('program', source=AllSources('.', '*.c*'))
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thank you so much for answering! I did a bit of searching and this is what I came across: http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python?answertab=votes#tab-top Is your's better or are they basically the same? – benbalach Oct 15 '14 at 07:44
  • I don't know that either is better. They are different, and will produce different answers under some circumstances. The key difference is that the accepted answer there doesn't use `Glob()`, which considers the SCons state in addition to the filesystem. – Robᵩ Oct 15 '14 at 12:56
  • isdir() is always returning False for me (scons 2.5.1) – krupan Nov 01 '17 at 12:34
1

Instead of globbing by Glob('*.cpp') you should also look in the subdirectories using Glob('**/*.cpp) and iterate over the received files.

Benjamin
  • 3,217
  • 2
  • 27
  • 42