summary refs log tree commit diff
path: root/scripts/gen-as-const.py
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2018-12-10 22:27:13 +0000
committerJoseph Myers <joseph@codesourcery.com>2018-12-10 22:27:13 +0000
commita8110b727e508f7ddf34f940af622e6f95435201 (patch)
tree7540af83545ddea47aed2c84d24693823b210996 /scripts/gen-as-const.py
parentf9ba9eb821c96ae41038460ee1fcd42308e074f2 (diff)
downloadglibc-a8110b727e508f7ddf34f940af622e6f95435201.tar.gz
glibc-a8110b727e508f7ddf34f940af622e6f95435201.tar.xz
glibc-a8110b727e508f7ddf34f940af622e6f95435201.zip
Move tst-signal-numbers to Python.
This patch converts the tst-signal-numbers test from shell + awk to
Python.

As with gen-as-const, the point is not so much that shell and awk are
problematic for this code, as that it's useful to build up general
infrastructure in Python for use of a range of code involving
extracting values from C headers.  This patch moves some code from
gen-as-const.py to a new glibcextract.py, which also gains functions
relating to listing macros, and comparing the values of a set of
macros from compiling two different pieces of code.

It's not just signal numbers that should have such tests; pretty much
any case where glibc copies constants from Linux kernel headers should
have such tests that the values and sets of constants agree except
where differences are known to be OK.  Much the same also applies to
structure layouts (although testing those without hardcoding lists of
fields to test will be more complicated).

Given this patch, another test for a set of macros would essentially
be just a call to glibcextract.compare_macro_consts (plus boilerplate
code - and we could move to having separate text files defining such
tests, like the .sym inputs to gen-as-const, so that only a single
Python script is needed for most such tests).  Some such tests would
of course need new features, e.g. where the set of macros changes in
new kernel versions (so you need to allow new macro names on the
kernel side if the kernel headers are newer than the version known to
glibc, and extra macros on the glibc side if the kernel headers are
older).  tst-syscall-list.sh could become a Python script that uses
common code to generate lists of macros but does other things with its
own custom logic.

There are a few differences from the existing shell + awk test.
Because the new test evaluates constants using the compiler, no
special handling is needed any more for one signal name being defined
to another.  Because asm/signal.h now needs to pass through the
compiler, not just the preprocessor, stddef.h is included as well
(given the asm/signal.h issue that it requires an externally provided
definition of size_t).  The previous code defined __ASSEMBLER__ with
asm/signal.h; this is removed (__ASSEMBLY__, a different macro,
eliminates the requirement for stddef.h on some but not all
architectures).

Tested for x86_64, and with build-many-glibcs.py.

	* scripts/glibcextract.py: New file.
	* scripts/gen-as-const.py: Do not import os.path, re, subprocess
	or tempfile.  Import glibcexctract.
	(compute_c_consts): Remove.  Moved to glibcextract.py.
	(gen_test): Update reference to compute_c_consts.
	(main): Likewise.
	* sysdeps/unix/sysv/linux/tst-signal-numbers.py: New file.
	* sysdeps/unix/sysv/linux/tst-signal-numbers.sh: Remove.
	* sysdeps/unix/sysv/linux/Makefile
	($(objpfx)tst-signal-numbers.out): Use tst-signal-numbers.py.
	Redirect stderr as well as stdout.
Diffstat (limited to 'scripts/gen-as-const.py')
-rw-r--r--scripts/gen-as-const.py60
1 files changed, 3 insertions, 57 deletions
diff --git a/scripts/gen-as-const.py b/scripts/gen-as-const.py
index eb85ef1aa0..f85e359394 100644
--- a/scripts/gen-as-const.py
+++ b/scripts/gen-as-const.py
@@ -24,68 +24,14 @@
 # A line giving just a name implies an expression consisting of just that name.
 
 import argparse
-import os.path
-import re
-import subprocess
-import tempfile
 
-
-def compute_c_consts(sym_data, cc):
-    """Compute the values of some C constants.
-
-    The first argument is a list whose elements are either strings
-    (preprocessor directives, or the special string 'START' to
-    indicate this function should insert its initial boilerplate text
-    in the output there) or pairs of strings (a name and a C
-    expression for the corresponding value).  Preprocessor directives
-    in the middle of the list may be used to select which constants
-    end up being evaluated using which expressions.
-
-    """
-    out_lines = []
-    for arg in sym_data:
-        if isinstance(arg, str):
-            if arg == 'START':
-                out_lines.append('void\ndummy (void)\n{')
-            else:
-                out_lines.append(arg)
-            continue
-        name = arg[0]
-        value = arg[1]
-        out_lines.append('asm ("@@@name@@@%s@@@value@@@%%0@@@end@@@" '
-                         ': : \"i\" ((long int) (%s)));'
-                         % (name, value))
-    out_lines.append('}')
-    out_lines.append('')
-    out_text = '\n'.join(out_lines)
-    with tempfile.TemporaryDirectory() as temp_dir:
-        c_file_name = os.path.join(temp_dir, 'test.c')
-        s_file_name = os.path.join(temp_dir, 'test.s')
-        with open(c_file_name, 'w') as c_file:
-            c_file.write(out_text)
-        # Compilation has to be from stdin to avoid the temporary file
-        # name being written into the generated dependencies.
-        cmd = ('%s -S -o %s -x c - < %s' % (cc, s_file_name, c_file_name))
-        subprocess.check_call(cmd, shell=True)
-        consts = {}
-        with open(s_file_name, 'r') as s_file:
-            for line in s_file:
-                match = re.search('@@@name@@@([^@]*)'
-                                  '@@@value@@@[^0-9Xxa-fA-F-]*'
-                                  '([0-9Xxa-fA-F-]+).*@@@end@@@', line)
-                if match:
-                    if (match.group(1) in consts
-                        and match.group(2) != consts[match.group(1)]):
-                        raise ValueError('duplicate constant %s'
-                                         % match.group(1))
-                    consts[match.group(1)] = match.group(2)
-        return consts
+import glibcextract
 
 
 def gen_test(sym_data):
     """Generate a test for the values of some C constants.
 
-    The first argument is as for compute_c_consts.
+    The first argument is as for glibcextract.compute_c_consts.
 
     """
     out_lines = []
@@ -158,7 +104,7 @@ def main():
     if args.test:
         print(gen_test(sym_data))
     else:
-        consts = compute_c_consts(sym_data, args.cc)
+        consts = glibcextract.compute_c_consts(sym_data, args.cc)
         print(''.join('#define %s %s\n' % c for c in sorted(consts.items())), end='')
 
 if __name__ == '__main__':