Skip to content

Instantly share code, notes, and snippets.

@gauravjuvekar
Created December 17, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravjuvekar/9aed8d1ac117dc30ef34b28f4933e192 to your computer and use it in GitHub Desktop.
Save gauravjuvekar/9aed8d1ac117dc30ef34b28f4933e192 to your computer and use it in GitHub Desktop.
ParseDepends SCons tool
"""
Use the C preprocessor to generate include dependencies
The SCons ParseDepends can parse the dependency files output by the
preprocessor, however this requires a rebuild as the dependency files are
updated after the compiler is run once.
See the `-M` flags in `man gcc` for more info.
This implementation was taken from
https://bitbucket.org/scons/scons/wiki/ParseDepends
The entire example on that link is:
###############################################################################
# If you want to extract the dependencies (and call ParseDepends) before
# building the object files, the only viable solution is to use a multi-stage
# builder with a source scanner:
##################################
def parsedep(node, env, path):
print "ParseDepends(%s)" % str(node)
ParseDepends(str(node))
return []
def parsecheck(node, env):
return node.exists()
depscan = Scanner(function = parsedep, skeys = ['.d'],
scan_check=parsecheck)
depbuild = Builder(action = '$CC -M -MF $TARGET $CCFLAGS -c $SOURCE',
suffix='.d', src_suffix='.c')
depparse = Builder(action = Copy('$TARGET', '$SOURCE'),
suffix='.dep',
src_builder=depbuild,
source_scanner=depscan)
env = Environment(BUILDERS = {'ExtractDependencies': depparse})
dep = env.ExtractDependencies('hello.c')
obj = env.Object('hello.c')
env.Requires(obj, dep)
env.Program(obj)
###############################################################################
This files combines part of those steps as a SCons Tool.
"""
import SCons
def parsedep(node, env, path):
print "ParseDepends(%s)" % str(node)
env.ParseDepends(str(node))
return []
def parsecheck(node, env):
return node.exists()
depscan = SCons.Scanner.Base(
function=parsedep,
skeys=['.d'],
scan_check=parsecheck)
depbuild = SCons.Builder.Builder(
action='$CC -M -MG -MF $TARGET $CFLAGS $CCFLAGS $_CCCOMCOM -c $SOURCE',
suffix='.d',
src_suffix=['.c', '.S'])
ExtractDependencies = SCons.Builder.Builder(
action=SCons.Defaults.Copy('$TARGET', '$SOURCE'),
suffix='.dep',
src_builder=depbuild,
source_scanner=depscan)
def ObjectsWithDependencies(env, sources, *args, **kwargs):
dep = env.ExtractDependencies(sources)
obj = env.Object(sources, *args, **kwargs)
env.Requires(obj, dep)
return (obj, dep)
def generate(env):
"""
Add required methods and environment variables
"""
env.Append(BUILDERS={'ExtractDependencies': ExtractDependencies})
env.AddMethod(ObjectsWithDependencies, 'ObjectsWithDependencies')
def exists(env):
"""
This tool requires no additional dependencies
"""
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment