194aa3
commit 477a02f63751c4b759ddd9454d17f2a7ad120ee3
194aa3
Author: Joseph Myers <joseph@codesourcery.com>
194aa3
Date:   Mon Dec 3 22:08:50 2018 +0000
194aa3
194aa3
    Make gen-as-const.py handle '--' consistently with awk script.
194aa3
    
194aa3
    It was reported in
194aa3
    <https://sourceware.org/ml/libc-alpha/2018-12/msg00045.html> that
194aa3
    gen-as-const.py fails to generate test code in the case where a .sym
194aa3
    file has no symbols in it, so resulting in a test failing to link for
194aa3
    Hurd.
194aa3
    
194aa3
    The relevant difference from the old awk script is that the old script
194aa3
    treated '--' lines as indicating that the text to do at the start of
194aa3
    the test (or file used to compute constants) should be output at that
194aa3
    point if not already output, as well as treating lines with actual
194aa3
    entries for constants like that.  This patch changes gen-as-const.py
194aa3
    accordingly, making it the sole responsibility of the code parsing
194aa3
    .sym files to determine when such text should be output and ensuring
194aa3
    it's always output at some point even if there are no symbols and no
194aa3
    '--' lines, since not outputting it means the test fails to link.
194aa3
    Handling '--' like that also avoids any problems that would arise if
194aa3
    the first entry for a symbol were inside #ifdef (since the text in
194aa3
    question must not be output inside #ifdef).
194aa3
    
194aa3
    Tested for x86_64, and with build-many-glibcs.py for i686-gnu.  Note
194aa3
    that there are still compilation test failures for i686-gnu
194aa3
    (linknamespace tests, possibly arising from recent posix_spawn-related
194aa3
    changes).
194aa3
    
194aa3
            * scripts/gen-as-const.py (compute_c_consts): Take an argument
194aa3
            'START' to indicate that start text should be output.
194aa3
            (gen_test): Likewise.
194aa3
            (main): Generate 'START' for first symbol or '--' line, or at end
194aa3
            of input if not previously generated.
194aa3
194aa3
diff --git a/scripts/gen-as-const.py b/scripts/gen-as-const.py
194aa3
index cabf401ed15e8367..eb85ef1aa0f4934d 100644
194aa3
--- a/scripts/gen-as-const.py
194aa3
+++ b/scripts/gen-as-const.py
194aa3
@@ -34,28 +34,28 @@ def compute_c_consts(sym_data, cc):
194aa3
     """Compute the values of some C constants.
194aa3
 
194aa3
     The first argument is a list whose elements are either strings
194aa3
-    (preprocessor directives) or pairs of strings (a name and a C
194aa3
+    (preprocessor directives, or the special string 'START' to
194aa3
+    indicate this function should insert its initial boilerplate text
194aa3
+    in the output there) or pairs of strings (a name and a C
194aa3
     expression for the corresponding value).  Preprocessor directives
194aa3
     in the middle of the list may be used to select which constants
194aa3
     end up being evaluated using which expressions.
194aa3
 
194aa3
     """
194aa3
     out_lines = []
194aa3
-    started = False
194aa3
     for arg in sym_data:
194aa3
         if isinstance(arg, str):
194aa3
-            out_lines.append(arg)
194aa3
+            if arg == 'START':
194aa3
+                out_lines.append('void\ndummy (void)\n{')
194aa3
+            else:
194aa3
+                out_lines.append(arg)
194aa3
             continue
194aa3
         name = arg[0]
194aa3
         value = arg[1]
194aa3
-        if not started:
194aa3
-            out_lines.append('void\ndummy (void)\n{')
194aa3
-            started = True
194aa3
         out_lines.append('asm ("@@@name@@@%s@@@value@@@%%0@@@end@@@" '
194aa3
                          ': : \"i\" ((long int) (%s)));'
194aa3
                          % (name, value))
194aa3
-    if started:
194aa3
-        out_lines.append('}')
194aa3
+    out_lines.append('}')
194aa3
     out_lines.append('')
194aa3
     out_text = '\n'.join(out_lines)
194aa3
     with tempfile.TemporaryDirectory() as temp_dir:
194aa3
@@ -89,32 +89,32 @@ def gen_test(sym_data):
194aa3
 
194aa3
     """
194aa3
     out_lines = []
194aa3
-    started = False
194aa3
     for arg in sym_data:
194aa3
         if isinstance(arg, str):
194aa3
-            out_lines.append(arg)
194aa3
+            if arg == 'START':
194aa3
+                out_lines.append('#include <stdint.h>\n'
194aa3
+                                 '#include <stdio.h>\n'
194aa3
+                                 '#include <bits/wordsize.h>\n'
194aa3
+                                 '#if __WORDSIZE == 64\n'
194aa3
+                                 'typedef uint64_t c_t;\n'
194aa3
+                                 '# define U(n) UINT64_C (n)\n'
194aa3
+                                 '#else\n'
194aa3
+                                 'typedef uint32_t c_t;\n'
194aa3
+                                 '# define U(n) UINT32_C (n)\n'
194aa3
+                                 '#endif\n'
194aa3
+                                 'static int\n'
194aa3
+                                 'do_test (void)\n'
194aa3
+                                 '{\n'
194aa3
+                                 # Compilation test only, using static
194aa3
+                                 # assertions.
194aa3
+                                 '  return 0;\n'
194aa3
+                                 '}\n'
194aa3
+                                 '#include <support/test-driver.c>')
194aa3
+            else:
194aa3
+                out_lines.append(arg)
194aa3
             continue
194aa3
         name = arg[0]
194aa3
         value = arg[1]
194aa3
-        if not started:
194aa3
-            out_lines.append('#include <stdint.h>\n'
194aa3
-                             '#include <stdio.h>\n'
194aa3
-                             '#include <bits/wordsize.h>\n'
194aa3
-                             '#if __WORDSIZE == 64\n'
194aa3
-                             'typedef uint64_t c_t;\n'
194aa3
-                             '# define U(n) UINT64_C (n)\n'
194aa3
-                             '#else\n'
194aa3
-                             'typedef uint32_t c_t;\n'
194aa3
-                             '# define U(n) UINT32_C (n)\n'
194aa3
-                             '#endif\n'
194aa3
-                             'static int\n'
194aa3
-                             'do_test (void)\n'
194aa3
-                             '{\n'
194aa3
-                             # Compilation test only, using static assertions.
194aa3
-                             '  return 0;\n'
194aa3
-                             '}\n'
194aa3
-                             '#include <support/test-driver.c>')
194aa3
-            started = True
194aa3
         out_lines.append('_Static_assert (U (asconst_%s) == (c_t) (%s), '
194aa3
                          '"value of %s");'
194aa3
                          % (name, value, name))
194aa3
@@ -134,6 +134,7 @@ def main():
194aa3
     args = parser.parse_args()
194aa3
     sym_data = []
194aa3
     with open(args.sym_file, 'r') as sym_file:
194aa3
+        started = False
194aa3
         for line in sym_file:
194aa3
             line = line.strip()
194aa3
             if line == '':
194aa3
@@ -143,12 +144,17 @@ def main():
194aa3
                 sym_data.append(line)
194aa3
                 continue
194aa3
             words = line.split(maxsplit=1)
194aa3
+            if not started:
194aa3
+                sym_data.append('START')
194aa3
+                started = True
194aa3
             # Separator.
194aa3
             if words[0] == '--':
194aa3
                 continue
194aa3
             name = words[0]
194aa3
             value = words[1] if len(words) > 1 else words[0]
194aa3
             sym_data.append((name, value))
194aa3
+        if not started:
194aa3
+            sym_data.append('START')
194aa3
     if args.test:
194aa3
         print(gen_test(sym_data))
194aa3
     else: