add ppc_flags.py so we can get the ppc versions of all the flags we need
authorJacob Lifshay <programmerjake@gmail.com>
Mon, 27 Nov 2023 03:11:35 +0000 (19:11 -0800)
committerJacob Lifshay <programmerjake@gmail.com>
Fri, 1 Dec 2023 20:42:15 +0000 (12:42 -0800)
tells gcc to dump all #defines, and parses that.

src/openpower/syscalls/ppc_flags.py [new file with mode: 0644]

diff --git a/src/openpower/syscalls/ppc_flags.py b/src/openpower/syscalls/ppc_flags.py
new file mode 100644 (file)
index 0000000..b92971f
--- /dev/null
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: LGPLv3+
+# Copyright (C) 2023 Jacob Lifshay <programmerjake@gmail.com>
+# Funded by NLnet http://nlnet.nl
+""" flags for PowerPC syscalls
+
+related bugs:
+
+* https://bugs.libre-soc.org/show_bug.cgi?id=1169
+"""
+
+def parse_defines(flags, compiler):
+    """ parse `#define`s into the dict `flags` using the given `compiler`
+    """
+    from subprocess import run, PIPE
+    inp = """
+#include <sys/mman.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+"""
+    if isinstance(compiler, str):
+        compiler = [compiler]
+    out = run([*compiler, '-E', '-dM', '-'], input=inp,
+              check=True, stdout=PIPE, encoding='utf-8').stdout
+    def_start = '#define '
+    defines = {}
+    for define in out.splitlines():
+        assert define.startswith(def_start)
+        define = define[len(def_start):]
+        name, space, value = define.partition(' ')
+        assert space == ' '
+        if not name.isidentifier():
+            continue
+        defines[name] = value
+    # resolve things defined in terms of other things
+    more_substitutions = True
+    while more_substitutions:
+        more_substitutions = False
+        for name, value in defines.items():
+            new_value = defines.get(value)
+            if new_value is not None and new_value != value:
+                defines[name] = new_value
+                more_substitutions = True
+    for name, value in defines.items():
+        if value.startswith('(') and value.endswith(')'):
+            value = value[1:-2]
+        if len(value) > 1 and value.startswith('0') and value[1].isdigit():
+            value = '0o' + value[1:]
+        try:
+            flags[name] = int(value, 0)
+        except ValueError:
+            pass
+    return flags
+
+parse_defines(globals(), 'powerpc64le-linux-gnu-gcc')
+
+def _host_defines():
+    import sysconfig
+    return parse_defines({}, sysconfig.get_config_var('CC').split(' '))
+
+host_defines = _host_defines()
+del _host_defines