7f2fe507efc5983f48d92759ba4f115525ae6af5
[openpower-isa.git] / src / openpower / syscalls / ppc_flags.py
1 # SPDX-License-Identifier: LGPLv3+
2 # Copyright (C) 2023 Jacob Lifshay <programmerjake@gmail.com>
3 # Funded by NLnet http://nlnet.nl
4 """ flags for PowerPC syscalls
5
6 related bugs:
7
8 * https://bugs.libre-soc.org/show_bug.cgi?id=1169
9 """
10
11 def parse_defines(flags, compiler):
12 """ parse `#define`s into the dict `flags` using the given `compiler`
13 """
14 from subprocess import run, PIPE
15 inp = """
16 #include <sys/mman.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <sys/syscall.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <linux/utsname.h>
24 #include <linux/auxvec.h>
25 """
26 if isinstance(compiler, str):
27 compiler = [compiler]
28 out = run([*compiler, '-E', '-dM', '-'], input=inp,
29 check=True, stdout=PIPE, encoding='utf-8').stdout
30 def_start = '#define '
31 defines = {}
32 for define in out.splitlines():
33 assert define.startswith(def_start)
34 define = define[len(def_start):]
35 name, space, value = define.partition(' ')
36 assert space == ' '
37 if not name.isidentifier():
38 continue
39 defines[name] = value
40 # resolve things defined in terms of other things
41 more_substitutions = True
42 while more_substitutions:
43 more_substitutions = False
44 for name, value in defines.items():
45 new_value = defines.get(value)
46 if new_value is not None and new_value != value:
47 defines[name] = new_value
48 more_substitutions = True
49 for name, value in defines.items():
50 if value.startswith('(') and value.endswith(')'):
51 value = value[1:-2]
52 if len(value) > 1 and value.startswith('0') and value[1].isdigit():
53 value = '0o' + value[1:]
54 try:
55 flags[name] = int(value, 0)
56 except ValueError:
57 pass
58 return flags
59
60 parse_defines(globals(), 'powerpc64le-linux-gnu-gcc')
61
62 def _host_defines():
63 import sysconfig
64 return parse_defines({}, sysconfig.get_config_var('CC').split(' '))
65
66 host_defines = _host_defines()
67 del _host_defines