Scons. Go to recursive with Glob

I have been using scons for several days and am a bit confused. Why are there no built-in tools for reclamation of sources, starting from a given root? Let me explain: I have this source location:

src Core folder1 folder2 subfolder2_1 Std folder1 

.. etc. This tree can be much deeper.

Now I will build it with such a 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 it doesn’t look as perfect as it can be. Because of this, I can write python code, but are there any more suitable ways to do this?

+4
source share
3 answers

Of course. You need to write python covers to go through dirs. You can find many recipes on stackoverflow. Here is my simple function that returns a list of subdirectories in the current directory (and ignore hidden directories starting with '.' - dot)

 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 

For example, I have dir modules that contain 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')) 

You can improve the getSubdirs function by adding recursion and delving deeper into subdirs.

+3
source

As Torsten has already said, SCons does not have an "internal" recursive glob (). You need to write something. My decision:

 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 emphasize that you need Glob () here (and not glob.glob () from python), especially when you use VariantDir (). Also, when you use VariantDir (), do not forget to convert the absolute paths to relative (in the example, I achieve this using [len (root) +1:]).

+8
source

The SCOL Glob () function does not have the ability to go recursively.

It would be much more efficient if you change your Python code to use the list.extend () function, for example:

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

Instead of trying to switch to recursive, just like you, it is quite common to have a SConscript script in each subdirectory, and in the root SConstruct each of them using the SConscript () function. This is called a SCons hierarchical assembly .

+2
source

Source: https://habr.com/ru/post/1412531/


All Articles