Please note:The SCons wiki is now restored from the attack in March 2013. All old passwords have been invalidated. Please reset your password if you have an account. If you note missing pages, please report them to webmaster@scons.org. Also, new account creation is currently disabled due to an ongoing spam flood (2013/08/27).

We stick this in our SConstruct:

   1 def print_config(msg, two_dee_iterable):
   2     # this function is handy and can be used for other configuration-printing tasks
   3     print
   4     print msg
   5     print
   6     for key, val in two_dee_iterable:
   7         print "    %-20s %s" % (key, val)
   8     print
   9 
  10 def config_h_build(target, source, env):
  11 
  12     config_h_defines = {
  13         # this is where you put all of your custom configuration values
  14         "install_prefix": prefix_variable_fed_by_user,
  15         "version_str": "1.0",
  16         "debug": debug # this is an int.  1 for true, 0 for false
  17     }
  18 
  19     config_h_defines["foo"] = "hey look i added something else"
  20 
  21     print_config("Generating config.h with the following settings:",
  22                   config_h_defines.items())
  23 
  24     for a_target, a_source in zip(target, source):
  25         config_h = file(str(a_target), "w")
  26         config_h_in = file(str(a_source), "r")
  27         config_h.write(config_h_in.read() % config_h_defines)
  28         config_h_in.close()
  29         config_h.close()

If you want to get values directly from your environment you can set config_h_defines to env.Dictionary():

   1 def config_h_build(target, source, env):
   2 
   3     config_h_defines = env.Dictionary()
   4 
   5     print_config("Generating config.h with the following settings:",
   6                   config_h_defines.items())
   7 
   8     for a_target, a_source in zip(target, source):
   9         config_h = file(str(a_target), "w")
  10         config_h_in = file(str(a_source), "r")
  11         config_h.write(config_h_in.read() % config_h_defines)
  12         config_h_in.close()
  13         config_h.close()

And we call it like this:

   1 env.AlwaysBuild(env.Command('config.h', 'config.h.in', config_h_build))

And of course here is our mocked-up config.h.in:

#define INSTALL_PREFIX "%(install_prefix)s"

#define VERSION_STR "%(version_str)s"

#define FOO "%(foo)s"

#if %(debug)d
#define YES_THIS_IS_A_DEBUG_BUILD 1
#else
#define NDEBUG 1
#endif

This would generate the following config.h. We assume that the prefix was set to /usr/local and that it is *not* a debug build.

#define INSTALL_PREFIX "/usr/local"

#define VERSION_STR "1.0"

#define FOO "hey look i added something else"

#if 0
#define YES_THIS_IS_A_DEBUG_BUILD 1
#else
#define NDEBUG 1
#endif

The whole sequence of:

#if %(blah)d
#define WHATEVER
#endif

Is kind of ugly, I'm pretty sure you could also do:

#define WHATEVER %(blah)d

and then in your code, check for it by doing #if WHATEVER, instead of #ifdef WHATEVER.

GenerateConfig (last edited 2009-10-09 13:51:52 by SergeyVidyuk)