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