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