about summary refs log tree commit diff
path: root/sysdeps/unix/sysv
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 /sysdeps/unix/sysv
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 'sysdeps/unix/sysv')
-rw-r--r--sysdeps/unix/sysv/linux/Makefile11
-rw-r--r--sysdeps/unix/sysv/linux/tst-signal-numbers.py48
-rw-r--r--sysdeps/unix/sysv/linux/tst-signal-numbers.sh86
3 files changed, 55 insertions, 90 deletions
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 988855d897..da44c274c6 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -115,11 +115,14 @@ tests-special += $(objpfx)tst-signal-numbers.out
 # in this context, but signal.c includes signal.h and not much else so it'll
 # be conservatively correct.
 $(objpfx)tst-signal-numbers.out: \
-		../sysdeps/unix/sysv/linux/tst-signal-numbers.sh \
+		../sysdeps/unix/sysv/linux/tst-signal-numbers.py \
 		$(objpfx)signal.o*
-	AWK=$(AWK) $(SHELL) ../sysdeps/unix/sysv/linux/tst-signal-numbers.sh \
-	$(CC) $(patsubst -DMODULE_NAME=%,-DMODULE_NAME=testsuite,$(CPPFLAGS)) \
-	< /dev/null > $@; $(evaluate-test)
+	PYTHONPATH=../scripts \
+	$(PYTHON) ../sysdeps/unix/sysv/linux/tst-signal-numbers.py \
+		   --cc="$(CC) $(patsubst -DMODULE_NAME=%, \
+					  -DMODULE_NAME=testsuite, \
+					  $(CPPFLAGS))" \
+	< /dev/null > $@ 2>&1; $(evaluate-test)
 endif
 
 ifeq ($(subdir),socket)
diff --git a/sysdeps/unix/sysv/linux/tst-signal-numbers.py b/sysdeps/unix/sysv/linux/tst-signal-numbers.py
new file mode 100644
index 0000000000..48c63d1218
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-signal-numbers.py
@@ -0,0 +1,48 @@
+#!/usr/bin/python3
+# Test that glibc's signal numbers match the kernel's.
+# Copyright (C) 2018 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+#
+# The GNU C Library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# The GNU C Library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with the GNU C Library; if not, see
+# <http://www.gnu.org/licenses/>.
+
+import argparse
+import sys
+
+import glibcextract
+
+
+def main():
+    """The main entry point."""
+    parser = argparse.ArgumentParser(
+        description="Test that glibc's signal numbers match the kernel's.")
+    parser.add_argument('--cc', metavar='CC',
+                        help='C compiler (including options) to use')
+    args = parser.parse_args()
+    sys.exit(glibcextract.compare_macro_consts(
+        '#define _GNU_SOURCE 1\n'
+        '#include <signal.h>\n',
+        '#define _GNU_SOURCE 1\n'
+        '#include <stddef.h>\n'
+        '#include <asm/signal.h>\n',
+        args.cc,
+        # Filter out constants that aren't signal numbers.
+        'SIG[A-Z]+',
+        # Discard obsolete signal numbers and unrelated constants:
+        #    SIGCLD, SIGIOT, SIGSWI, SIGUNUSED.
+        #    SIGSTKSZ, SIGRTMIN, SIGRTMAX.
+        'SIG(CLD|IOT|RT(MIN|MAX)|STKSZ|SWI|UNUSED)'))
+
+if __name__ == '__main__':
+    main()
diff --git a/sysdeps/unix/sysv/linux/tst-signal-numbers.sh b/sysdeps/unix/sysv/linux/tst-signal-numbers.sh
deleted file mode 100644
index e1f7be0337..0000000000
--- a/sysdeps/unix/sysv/linux/tst-signal-numbers.sh
+++ /dev/null
@@ -1,86 +0,0 @@
-#! /bin/sh
-# Test that glibc's signal numbers match the kernel's.
-# Copyright (C) 2017-2018 Free Software Foundation, Inc.
-# This file is part of the GNU C Library.
-
-# The GNU C Library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-
-# The GNU C Library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-
-# You should have received a copy of the GNU Lesser General Public
-# License along with the GNU C Library; if not, see
-# <http://www.gnu.org/licenses/>.
-
-set -e
-if [ -n "$BASH_VERSION" ]; then set -o pipefail; fi
-LC_ALL=C; export LC_ALL
-
-# We cannot use Linux's asm/signal.h to define signal numbers, because
-# it isn't sufficiently namespace-clean.  Instead, this test checks
-# that our signal numbers match the kernel's.  This script expects
-# "$@" to be $(CC) $(CPPFLAGS) as set by glibc's Makefiles, and $AWK
-# to be set in the environment.
-
-# Before doing anything else, fail if the compiler doesn't work.
-"$@" -E -xc -dM - < /dev/null > /dev/null
-
-tmpG=`mktemp -t signums_glibc.XXXXXXXXX`
-tmpK=`mktemp -t signums_kernel.XXXXXXXXX`
-trap "rm -f '$tmpG' '$tmpK'" 0
-
-# Filter out constants that aren't signal numbers.
-# If SIGPOLL is defined as SIGIO, swap it around so SIGIO is defined as
-# SIGPOLL. Similarly for SIGABRT and SIGIOT.
-# Discard obsolete signal numbers and unrelated constants:
-#    SIGCLD, SIGIOT, SIGSWI, SIGUNUSED.
-#    SIGSTKSZ, SIGRTMIN, SIGRTMAX.
-# Then sort the list.
-filter_defines ()
-{
-    $AWK '
-/^#define SIG[A-Z]+ ([0-9]+|SIG[A-Z0-9]+)$/ { signals[$2] = $3 }
-END {
-  if ("SIGPOLL" in signals && "SIGIO" in signals &&
-      signals["SIGPOLL"] == "SIGIO") {
-    signals["SIGPOLL"] = signals["SIGIO"]
-    signals["SIGIO"] = "SIGPOLL"
-  }
-  if ("SIGABRT" in signals && "SIGIOT" in signals &&
-      signals["SIGABRT"] == "SIGIOT") {
-    signals["SIGABRT"] = signals["SIGIOT"]
-    signals["SIGIOT"] = "SIGABRT"
-  }
-  for (sig in signals) {
-    if (sig !~ /^SIG(CLD|IOT|RT(MIN|MAX)|STKSZ|SWI|UNUSED)$/) {
-      printf("#define %s %s\n", sig, signals[sig])
-    }
-  }
-}' | sort
-}
-
-# $CC may contain command-line switches, so it should be word-split.
-printf '%s' '#define _GNU_SOURCE 1
-#include <signal.h>
-' |
-    "$@" -E -xc -dM - |
-    filter_defines > "$tmpG"
-
-printf '%s' '#define _GNU_SOURCE 1
-#define __ASSEMBLER__ 1
-#include <asm/signal.h>
-' |
-    "$@" -E -xc -dM - |
-    filter_defines > "$tmpK"
-
-if cmp -s "$tmpG" "$tmpK"; then
-    exit 0
-else
-    diff -u "$tmpG" "$tmpK"
-    exit 1
-fi