Please note:The SCons wiki is in read-only mode due to ongoing spam/DoS issues. Also, new account creation is currently disabled. We are looking into alternative wiki hosts.

This is a more generalized form of the technique described on the DynamicSourceGenerator page from where I got the idea. I too had source being generated which is why I read that page, but I also used the same idea for other reasons.

For example: In my case have many (>3000) source files in ~100 libraries and even more headers. Users are used to just dropping a new file into a folder and having it built into the library - so we have to glob the folders to find all the .cpp files. This glob can take a long time, and if I've only changed 1 .cpp file in 1 library then I only want to rebuild and link that one library - I don't really want scons to have glob all the files and check them all. Couple this with the fact that we do have generated source that we need to add in as well.. so even if we did the glob at "reading sconscript" time we have to wait to get the full list later on.

End result is I need to call scons at build time to do stuff. Scons has something called ActionFactory. These are objects that create Actions that call a function but can also remember the parameters to call them with. Things like Mkdir use this. Here I create my own ActionFactory which can then be used to create an Action that can be passed to a builder.

This is a heavily cutdown version of what I have to demonstrate this point.

My Sconscript files set up a list of libraries each of which has a directory of source (actually it can be more than one, but I've simplified):

   1 class MyLib:
   3     allLibs = {}
   5     def __init__( self, name, folder ):
   6         MyLib.allLibs[name] = self
   7 = name
   8         self.folder = folder
  10 foolib = MyLib( "foo", "foosrc" )
  11 barlib = MyLib( "bar", "barsrc" )

Now for each library we want to set up a dummy or phony target that when "built" does the globbing and tells scons about the real files. For this we use the afore mentioned trick that Scons uses to turn a function and parameters into an Action, which we can then use in a Command - an ActionFactory.

   1 from SCons.Script import *
   3 # These are "callbacks" that we set up as actions that Scons can call at build time.
   4 def libcallback( libname ):
   5     # Do something clever
   6     pass
   8 LibCallback = SCons.Action.ActionFactory( libcallback,
   9                                           lambda name: 'Doing "%s"' % name)

So a call to LibCallBack now creates a Action object that "remembers" the parameter it was called with, and when invoked will call my libcallback function with that parameter. This means I can now add a method to MyLib class to do the following:

   1     def doInitial( self ):
   2         """ This sets up a dummy target to do the lib globbing etc. at build time """
   4         self.myinitdummy = env.Command( "dummy" + + "_libinit",
   5                                         [],
   6                                         LibCallback( ) )
   8         env.Alias(, self.myinitdummy )

The Command line is telling scons about a target of "dummy_libfoo_libinit" which is a file that will never exist so scons will always attempt to build it. It has no source files (just an empty list) and to "build" it scons will call libcallback with

I'll explain the Alias at the end.

So now we need to do the something clever in the callback (well.. actually it's not that clever). All we do is lookup the library with the name, and then make a call to that object

   1 def libcallback( libname )
   2     if libname not in MyLib.allLibs:
   3         raise Exception( "The library %s is not defined" % libname )
   5     MyLib.allLibs[libname].doPreCompile()

Now we just need to add the doPreCompile to MyLib:

   1     def doPreCompile( self ):
   2         files = glob.glob( os.path.join( self.folder, "*.cxx" ) )
   4         mydll = env.SharedLibrary(, files, CPPPATH = self.folder )
   5         env.Depends(, mydll )

The last thing is then do the initial calls:

   1 foolib.doInitial()
   2 barlib.doInitial()

The Alias from before, with the above Depends now mean that we can invoke scons like

scons foo

and it will only glob and consider files in foosrc!

Of course there downsides in that you can't tell scons to compile a single object file - but we are writing things to get round that. And you can't do a scons -c. However the purpose was to show how you can call something at build time.

Complete code in context:

   1 import os
   2 import glob
   3 from SCons.Script import *
   5 env = Environment()
   7 class MyLib:
   9     allLibs = {}
  11     def __init__( self, name, folder ):
  12         MyLib.allLibs[name] = self
  13 = name
  14         self.folder = folder
  16     def doInitial( self ):
  17         """ This sets up a dummy target to do the lib globbing etc. at build time """
  19         self.myinitdummy = env.Command( "dummy" + + "_libinit",
  20                                         [],
  21                                         LibCallback( ) )
  23         env.Alias(, self.myinitdummy )
  26     def doPreCompile( self ):
  27         files = glob.glob( os.path.join( self.folder, "*.cxx" ) )
  29         mydll = env.SharedLibrary(, files, CPPPATH = self.folder )
  30         env.Depends(, mydll )
  33 # These are "callbacks" that we set up as actions that Scons can call at build time.
  34 def libcallback( libname ):
  35     if libname not in MyLib.allLibs:
  36         raise Exception( "MyLib: The library %s is not defined" % libname )
  38     MyLib.allLibs[libname].doPreCompile()
  40 LibCallback = SCons.Action.ActionFactory( libcallback,
  41                                           lambda name: 'Doing "%s"' % name)
  46 foolib = MyLib( "foo", "foosrc" )
  47 barlib = MyLib( "bar", "barsrc" )
  49 foolib.doInitial()
  50 barlib.doInitial()

Should have probably created a login first - but if you have any questions I follow the scons mailing list so post there and I'll pick it up :)

BuildTimeCallback (last edited 2010-03-19 04:42:50 by s235-111)