How can one make recursive Glob()
in a VariantDir()
environment in Python?
The answer from the question
I believe the accepted answer only works if your source directory is '.'
.
Here is a working example where the source directory is src
and the variant directory is build
.
.
├── include
│ └── sum.h
├── SConstruct
└── src
├── dir
│ └── sum.cpp
└── main.cpp
sum.h
#ifndef _SUM_H_
#define _SUM_H_
double sum(const double x, const double y);
#endif
main.cpp
#include
#include
using namespace std;
int main() {
cout << "Sum of 1 and 2 = " << sum(1., 2.) << endl;
return 0;
}
sum.cpp
#include
double sum(const double x, const double y) {
return x + y;
}
SConstruct
import os
def variantglob(env, pattern, ondisk=True, source=True, strings=False,
recursive=False):
matches = []
for root, dirs, filenames in os.walk(env['SOURCE_DIR']):
cwd = Dir(os.path.join(env['VARIANT_DIR'],
os.path.relpath(root, env['SOURCE_DIR'])))
matches.extend(cwd.glob(pattern, ondisk, source, strings))
return matches
# Create Build Environment
env = Environment()
# Customize Environment
env.Replace(VARIANT_DIR='build',
SOURCE_DIR='src')
env.Append(CPPPATH=['include'])
# Setup Variant Directory
VariantDir(variant_dir=env['VARIANT_DIR'],
src_dir=env['SOURCE_DIR'], duplicate=0)
# Build the executable
exe = env.Program(os.path.join(env['VARIANT_DIR'], 'example'),
variantglob(env, '*.cpp', recursive=True))
# Install the executable
Install('bin', exe)
Just execute scons
at the top level directory. This will create a build
directory and drop all your temporaries there (variant directory), and it will then install the result of the build into the bin folder.
Execute bin/example
to see it work.
This example was tested on linux.
When building with variant directories you have to specify the path to the source as though it were already sitting in the variant directory, but those directories may not exist yet. This glob function walks the source tree to construct the paths that will be in the variant directory, and then globs against those paths.