scons: Create a namedtuple for debug flag info.

This avoids having to rely on certain bits of information being in
certain positions, and also makes it more obvious which piece of
information you're referring to when manipulating the objects.

Change-Id: I93799d00261002996a42a62a7de34c4c275847c5
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/49385
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-by: Hoa Nguyen <hoanguyen@ucdavis.edu>
This commit is contained in:
Gabe Black
2021-08-04 22:35:35 -07:00
parent 7a133da281
commit 6a36839da5

View File

@@ -38,6 +38,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import bisect
import collections
import distutils.spawn
import importlib
import importlib.abc
@@ -406,15 +407,17 @@ Export('GTest')
#
def makeDebugFlagHH(target, source, env):
assert len(target) == 1 and len(source) == 1
assert len(target) == 1
name, components, desc, fmt = FromValue(source[0])
flag = env['DEBUG_FLAG'][0]
name, desc, components, fmt = \
flag.name, flag.desc, flag.components, flag.fmt
code = code_formatter()
typename = "CompoundFlag" if components else "SimpleFlag"
component_flag_decls = \
''.join('extern SimpleFlag& %s;\n' % flag for flag in components)
typename = "CompoundFlag" if flag.components else "SimpleFlag"
component_flag_decls = ''.join('extern SimpleFlag& %s;\n' % simple for
simple in components)
# file header boilerplate
code('''\
@@ -443,19 +446,22 @@ ${component_flag_decls}
code.write(str(target[0]))
DebugFlagInfo = collections.namedtuple('DebugFlag',
['name', 'desc', 'components', 'fmt'])
def DebugFlag(name, desc=None, fmt=False):
if name == "All":
raise AttributeError('The "All" flag name is reserved')
debug_flags = env.get('DEBUG_FLAGS', [])
if any(name == flag[0] for flag in debug_flags):
if any(name == flag.name for flag in debug_flags):
raise AttributeError(f'Flag {name} already specified')
flag = (name, (), desc, fmt)
flag = DebugFlagInfo(name, desc, (), fmt)
env.Append(DEBUG_FLAGS=[flag])
hh_file = Dir(env['BUILDDIR']).Dir('debug').File(f'{name}.hh')
env.Command(hh_file, ToValue(flag),
MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
env.Command(hh_file, [], DEBUG_FLAG=[flag],
action=MakeAction(makeDebugFlagHH, Transform("TRACING", 0),
varlist=['DEBUG_FLAG']))
def CompoundFlag(name, flags, desc=None):
if name == "All":
@@ -464,12 +470,13 @@ def CompoundFlag(name, flags, desc=None):
if any(name == flag[0] for flag in debug_flags):
raise AttributeError(f'Flag {name} already specified')
flag = (name, flags, desc, False)
flag = DebugFlagInfo(name, desc, flags, None)
env.Append(DEBUG_FLAGS=[flag])
env.Command(Dir(env['BUILDDIR']).Dir('debug').File(f'{name}.hh'),
ToValue(flag),
MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
hh_file = Dir(env['BUILDDIR']).Dir('debug').File(f'{name}.hh')
env.Command(hh_file, [], DEBUG_FLAG=[flag],
action=MakeAction(makeDebugFlagHH, Transform("TRACING", 0),
varlist=['DEBUG_FLAG']))
def DebugFormatFlag(name, desc=None):
DebugFlag(name, desc, True)
@@ -906,8 +913,8 @@ namespace debug
''')
all_flags = env.get('DEBUG_FLAGS', [])
simple_flags = sorted(filter(lambda f: not f[1], all_flags))
compound_flags = sorted(filter(lambda f: f[1], all_flags))
simple_flags = sorted(filter(lambda f: not f.components, all_flags))
compound_flags = sorted(filter(lambda f: f.components, all_flags))
# We intentionally make flag a reference to a heap allocated object so
# (1) It has a similar interface to a global object like before
@@ -915,16 +922,20 @@ namespace debug
# The second property is desirable as global objects from different
# translation units do not have a defined destruction order, so it'll
# be unsafe to access debug flags in their destructor in such cases.
for name, _, desc, fmt in simple_flags:
fmt = 'true' if fmt else 'false'
for flag in simple_flags:
name, desc, components, fmt = \
flag.name, flag.desc, flag.components, flag.fmt
fmt = 'true' if flag.fmt else 'false'
code('''
SimpleFlag& $name = *(
new SimpleFlag("$name", "$desc", $fmt));''')
for name, compound, desc, _ in compound_flags:
for flag in compound_flags:
name, desc, components, fmt = \
flag.name, flag.desc, flag.components, flag.fmt
code('''
CompoundFlag& $name = *(new CompoundFlag("$name", "$desc", {
${{', '.join('&' + flag for flag in compound)}}
${{', '.join('&' + simple for simple in components)}}
}));''')
code('''