Gabe Black has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/48135 )

Change subject: scons: Factor out the core of blobToCpp.
......................................................................

scons: Factor out the core of blobToCpp.

blobToCpp is called in two places, one which uses all its functionality,
and one which disables most of it. Instead, factor out the small core so
that it can be called directly by the call sight which uses only that
part, and blobToCpp itself.

This change also removes the ability to leave out a namespace or header
file code formatter, since the function is not called that way any more.
That simplifies blobToCpp significantly.

Change-Id: I63139311521bb4f9287fe41ff51e4e5301a18349
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/48135
Reviewed-by: Gabe Black <[email protected]>
Maintainer: Gabe Black <[email protected]>
Tested-by: kokoro <[email protected]>
---
M src/SConscript
1 file changed, 39 insertions(+), 36 deletions(-)

Approvals:
  Gabe Black: Looks good to me, approved; Looks good to me, approved
  kokoro: Regressions pass




diff --git a/src/SConscript b/src/SConscript
index 0eedff2..4351f8a 100644
--- a/src/SConscript
+++ b/src/SConscript
@@ -273,7 +273,21 @@
             self.shared_objs[key] = env.SharedObject(self.tnode)
         return self.shared_objs[key]

-def blobToCpp(data, symbol, cpp_code, hpp_code=None, namespace=None):
+def bytesToCppArray(code, symbol, data):
+    '''
+ Output an array of bytes to a code formatter as a c++ array declaration.
+    '''
+    code('const std::uint8_t ${symbol}[] = {')
+    code.indent()
+    step = 16
+    for i in range(0, len(data), step):
+        x = array.array('B', data[i:i + step])
+        strs = map(lambda i: f'{i},', x)
+        code(functools.reduce(lambda x, y: x + y, strs))
+    code.dedent()
+    code('};')
+
+def blobToCpp(data, symbol, cpp_code, hpp_code, namespace):
     '''
     Convert bytes data into C++ .cpp and .hh uint8_t byte array
     code containing that binary data.
@@ -282,41 +296,32 @@
     :param symbol: name of the symbol
     :param cpp_code: append the generated cpp_code to this object
     :param hpp_code: append the generated hpp_code to this object
-                     If None, ignore it. Otherwise, also include it
-                     in the .cpp file.
-    :param namespace: namespace to put the symbol into. If None,
-                      don't put the symbols into any namespace.
+                     Also include it in the .cpp file.
+    :param namespace: namespace to put the symbol into.
     '''
-    symbol_len_declaration = 'const std::size_t {}_len'.format(symbol)
-    symbol_declaration = 'const std::uint8_t {}[]'.format(symbol)
-    if hpp_code is not None:
-        cpp_code('''\
-#include "blobs/{}.hh"
-'''.format(symbol))
-        hpp_code('''\
+    hpp_code('''\
 #include <cstddef>
 #include <cstdint>
+
+namespace ${namespace}
+{
+
+extern const std::size_t ${symbol}_len;
+extern const std::uint8_t ${symbol}[];
+
+}
 ''')
-        if namespace is not None:
-            hpp_code('namespace {} {{'.format(namespace))
-        hpp_code('extern ' + symbol_len_declaration + ';')
-        hpp_code('extern ' + symbol_declaration + ';')
-        if namespace is not None:
-            hpp_code('}')
-    if namespace is not None:
-        cpp_code('namespace {} {{'.format(namespace))
-    if hpp_code is not None:
-        cpp_code(symbol_len_declaration + ' = {};'.format(len(data)))
-    cpp_code(symbol_declaration + ' = {')
-    cpp_code.indent()
-    step = 16
-    for i in range(0, len(data), step):
-        x = array.array('B', data[i:i+step])
-        cpp_code(''.join('%d,' % d for d in x))
-    cpp_code.dedent()
-    cpp_code('};')
-    if namespace is not None:
-        cpp_code('}')
+
+    cpp_code('''\
+#include "blobs/${symbol}.hh"
+
+namespace ${namespace}
+{
+
+const std::size_t ${symbol}_len = ${{len(data)}};
+''')
+    bytesToCppArray(cpp_code, symbol, data)
+    cpp_code('} // namespace ${namespace}')

 def Blob(blob_path, symbol):
     '''
@@ -1225,10 +1230,8 @@
 {

 ''')
-    blobToCpp(data, 'data_' + sym, code)
-    code('''\
-
-
+    bytesToCppArray(code, 'data_{}'.format(sym), data)
+    code('''
 EmbeddedPython embedded_${sym}(
     ${{c_str(pysource.arcname)}},
     ${{c_str(pysource.abspath)}},

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/48135
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I63139311521bb4f9287fe41ff51e4e5301a18349
Gerrit-Change-Number: 48135
Gerrit-PatchSet: 6
Gerrit-Owner: Gabe Black <[email protected]>
Gerrit-Reviewer: Andreas Sandberg <[email protected]>
Gerrit-Reviewer: Bobby Bruce <[email protected]>
Gerrit-Reviewer: Daniel Carvalho <[email protected]>
Gerrit-Reviewer: Gabe Black <[email protected]>
Gerrit-Reviewer: Giacomo Travaglini <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: kokoro <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to