Here is a simple Builder that runs a configure script inside a directory. It's intended to be used along with UnTarBuilder and MakeBuilder to extract some source code, configure it, and build it.

Definition

   1 # AutoConfig Builder:  Runs ./configure inside a directory.
   2 #
   3 # Parameters:
   4 #    AutoConfigParams -- Sequence of parameter strings to include on the
   5 #                        configure command line.
   6 #                        Default: [ ]
   7 #    AutoConfigTarget -- File that configure will create.
   8 #                        Default: config.h
   9 #    AutoConfigSource -- File that configure depends on.
  10 #                        Default: Makefile.in
  11 
  12 import os
  13 import subprocess
  14 
  15 from SCons.Script import *
  16 
  17 def parms(target, source, env):
  18     """Assemble various AutoConfig parameters."""
  19     workdir = os.path.dirname(str(source[0]))
  20     params = None
  21     if 'AutoConfigParams' in env.Dictionary().keys():
  22         params = env['AutoConfigParams']
  23         if not isinstance(params, list):
  24             print 'AutoConfigParams must be a sequence'
  25             Exit(1)
  26     targetfile = 'config.h'
  27     if 'AutoConfigTarget' in env.Dictionary().keys():
  28         targetfile = env['AutoConfigTarget']
  29     sourcefile = 'Makefile.in'
  30     if 'AutoConfigSource' in env.Dictionary().keys():
  31         sourcefile = env['AutoConfigSource']
  32     return (workdir, params, targetfile, sourcefile)
  33 
  34 def message(target, source, env):
  35     """Return a pretty AutoConfig message."""
  36     (dirname,
  37      params,
  38      targetfile,
  39      sourcefile) = parms(target, source, env)
  40     if 'AUTOCONFIGCOMSTR' in env.Dictionary().keys():
  41         return env.subst(env['AUTOCONFIGCOMSTR'],
  42                          target = target, source = source, raw = 1)
  43     msg = 'cd ' + dirname + ' && ./configure'
  44     if params is not None:
  45         msg += ' ' + ' '.join(params)
  46     return msg
  47 
  48 def emitter(target, source, env):
  49     """Remap the source & target to path/$AutoConfigSource and path/$AutoConfigTarget."""
  50     (dirname,
  51      params,
  52      targetfile,
  53      sourcefile) = parms(target, source, env)
  54 
  55     # NOTE: Using source[0] instead of target[0] for the target's path!
  56     # If there's only one . in the source[0] value, then Scons strips off the
  57     # extension when it determines the target[0] value.  For example,
  58     #    AutoConfig('foo.blah')
  59     # sets
  60     #    source[0] = 'foo.blah'
  61     #    target[0] = 'foo'
  62     # (SCons does NOT do this if source[0] has more than one . )
  63     # Since this emitter expects the incoming source[0] value to be a directory
  64     # name, we can use it here for the rewritten target[0].
  65 
  66     return ([ os.path.join(str(source[0]), targetfile) ],
  67             [ os.path.join(str(source[0]), sourcefile) ])
  68 
  69 def builder(target, source, env):
  70     """Run ./configure in a directory."""
  71     ( dirname,
  72       params,
  73       targetfile,
  74       sourcefile) = parms(target, source, env)
  75     real_stdout = subprocess.PIPE
  76     if 'AUTOCONFIGCOMSTR' not in env.Dictionary().keys():
  77         real_stdout = None
  78     cmd = './configure'
  79     if params is not None:
  80         cmd = [ cmd ] + params
  81     return subprocess.call( cmd,
  82                             cwd = dirname,
  83                             stdout = real_stdout,
  84                             stderr = real_stdout)
  85 
  86 def generate(env, **kwargs):
  87     env['BUILDERS']['AutoConfig'] = env.Builder(
  88         action = env.Action(builder, message),
  89         emitter = emitter,
  90         single_source = True)
  91 
  92 def exists(env):
  93     return True

Installation

To load this Builder into your environment, save the above as AutoConfig.py in your site_scons/site_tools/ directory, then add it as a tool to your environment:

   1 env = Environment(tools = ['default', 'AutoConfig'])
   2 # or
   3 otherenv.Tool('AutoConfig')

Usage

You use this builder by giving it the directory of the code you want to configure:

   1 # Run configure in the foothing-1.2.3/ directory
   2 configured = env.AutoConfig('foothing-1.2.3')

If your environment has AUTOCONFIGCOMSTR defined, the builder uses that string to print its command message and also hides the command's output. Without AUTOCONFIGCOMSTR the builder prints the full configure command as well as the command's output.

Parameters

* AutoConfigParams -- (Default: none) Sequence of parameter strings to pass to the configure script.

* AutoConfigSource -- (Default: Makefile.in) The file inside the source directory that SCons should use to decide if the directory needs to be reconfigured.

* AutoConfigTarget -- (Default: config.h) The file that the configure process should create. If this file doesn't get created, SCons will always configure the directory.

AutoConfigBuilder (last edited 2009-07-15 18:52:46 by MarcBranchaud)