1 import SCons.Environment, SCons.Util, SCons.Script, SCons.Conftest
4 # Fix for SCons 0.97 compatibility
6 try: SCons.SConf.SConfBase.Define
9 def Define(self, name, value = None, comment = None):
12 comment_str = "/* %s */" % comment
13 lines.append(comment_str)
15 define_str = "#define %s %s" % (name, value)
17 define_str = "#define %s" % name
18 lines.append(define_str)
20 self.config_h_text = self.config_h_text + string.join(lines, '\n')
21 SCons.SConf.SConfBase.Define = Define
23 class ConfTest(object):
24 """Configuration test decorator.
26 This class is used as a decorator to define SCons Configure
27 tests. There are three ways to use this facility:
30 DIRECT WITH EXTERNAL REGISTRY
32 In this scenario, you pass the test registry, a map, as argument to
33 the decorator. This usage is primarily interesting for tool writers:
35 from CustomTests import ConfTest
40 def CheckMyFoo(context):
44 env.Append(CUSTOM_TESTS = MY_TESTS)
46 This will place CheckMyFoo into the MY_TESTS dictionary which you can
47 later add to the CUSTOM_TESTS environment variable.
50 USING A REGISTRY ALIAS
52 In this scenario, you define a local alias for the decorator which
53 serves as the registry. This usage is again primarily interesting for
54 tool and extension writers:
58 MyConfTest = CustomTests.ConfTest()
61 def CheckMyFoo(context):
65 env.Append(CUSTOM_TESTS = MyConfTest.tests)
67 Logically this is like the previous method.
70 USING THE ENVIRONMENT MEMBER
72 This usage is interesting for SConstruct and SConscript writers:
75 env.Tool('CustomTests')
78 def CheckMyFoo(context):
81 conf = env.Configure()
85 The new configuration test is automatically added to
86 env['CUSTOM_TESTS'] and is thus automatically available to all
87 configuration contexts created from the environment.
90 def __init__(self, registry=None):
93 elif isinstance(registry, SCons.Environment.SubstitutionEnvironment):
94 self.tests =registry['CUSTOM_TESTS']
98 def __call__(self, func):
99 self.tests[func.__name__] = func
102 def Configure(self, *args, **kw):
103 try: kw['custom_tests'].update(self['CUSTOM_TESTS'])
104 except KeyError: kw['custom_tests'] = dict(self['CUSTOM_TESTS'])
105 return self._CustomTests_orig_Configure(*args, **kw)
107 def Fail(context, message, condition=True):
109 SCons.Util.display("scons: *** %s" % message)
112 DefaultTest = ConfTest()
115 def CheckSymbolWithExpression(context, symbol, expression, header="", language="C"):
116 lang, suffix, msg = SCons.Conftest._lang2suffix(language)
118 context.Message("Cannot check for %s: " % symbol)
122 text = ("#include <assert.h>\n"
127 "}\n") % (header, expression)
129 context.Message("Checking for %s... " % symbol)
130 ret = context.TryLink(text, suffix)
134 key = re.sub('[^A-Z0-9_]', '_', key)
135 context.sconf.Define("HAVE_%s" % key, 1,
136 "Define to 1 if you have `%s'" % symbol)
141 def CheckByteorder(context):
142 context.Message("Checking byteorder... ")
143 ret = context.TryRun('#include <stdio.h>\n'
144 'union byteorder_test { int i; char b; };\n'
146 ' union byteorder_test t; t.i=1;\n'
147 ' printf(t.b ? "little\\n" : "big\\n");\n'
152 context.Result("failed")
155 context.Result(ret+"-endian")
156 context.sconf.Define("BYTEORDER_%s_ENDIAN" % ret.upper(), 1,
157 "Define BYTEORDER_LITTLE_ENDIAN or BYTEORDER_BIG_ENDIAN")
161 env.Append( CUSTOM_TESTS = DefaultTest.tests )
162 env._CustomTests_orig_Configure = env.Configure
163 env.AddMethod(Configure)
164 env.AddMethod(ConfTest)