Search code examples
pythonsconsbuild-tools

Dividing SConstruct code into set of aliases; no code called by default


I'm trying to divide up my SConstruct file into blocks of code, where each block is controlled by an Alias, and no code is run by default; i.e. just by runningscons. The Aliases are of course run from the command line e.g. (in the example below):

scons h

Here is some example code. This appears to work Ok. However, three questions.

  1. Is there a better way to do this?

  2. More specifically, I don't understand how the target arguments in the Alias call get passed to the h and h3 action functions. I notice if I leave them blank the build does not work. However there is no obvious way for the targets to be passed to these functions, since they do not take any arguments.

  3. Relatedly, the documentation says that action functions requires target, source, and env arguments. These action functions don't have these but work anyway. How come?

Code follows:

#!/usr/bin/python                                                                                                                                         

Default(None)

def h(env):
    x = env.Program("hello1", "hello1.c")
    y = env.Program("hello2", "hello2.c")
    return 0

def h3(env):
    y = env.Program("hello3", "hello3.c")
    return 0

env = Environment()
env.AddMethod(h, "HELLO")
env.AddMethod(h3, "HELLO3")
env.Alias("h", ["hello1", "hello2"], env.HELLO())
env.Alias("h3", ["hello3"],env.HELLO3())

Solution

  • To answer your first question: yes, there is a better way.

    env = Environment()
    
    # h:
    x = env.Program("hello1", "hello1.c")
    y = env.Program("hello2", "hello2.c")
    env.Alias("h", [x,y])
    # equivalently: env.alias("h", ["hello1", "hello2"])
    
    # h3
    y = env.Program("hello3", "hello3.c")
    env.Alias("h3", y)
    
    Default(None)
    

    Alternatively, if you like grouping your Program() calls in a subroutine, that's okay, too. You just don't need AddMethod() for what you're doing:

    env = Environment()
    
    def h(env):
        x = env.Program("hello1", "hello1.c")
        y = env.Program("hello2", "hello2.c")
        return x,y
    
    def h3(env):
        return env.Program("hello3", "hello3.c")
    
    env.Alias("h", h(env))
    env.Alias("h3", h3(env))
    Default(None)