Skip to content

Commit e30dc03

Browse files
committed
scripts/gensyms: Use real C++ preprocessor
So far we've skipped all the preprocessor directives and just used a regular expression to check whether one of WRAP_SYM or EXPORT_SYM occurs in one of the source files. While this works fine if we're using a linker version script, it fails when using PROVIDE-directives because the symbols actually need to exist. Some of our wrapped functions however are dependent on the configuration, for example we only wrap the listen function if we have systemd support enabled. To fix this, we're now using the real preprocessor on all of the source files while also ignoring "#include" directives. Of course, ignoring them is also a bit limited, especially if we relocate the *_SYM macros, but for very simple definitions like we have how it's sufficient. This should fix building without systemd support. Signed-off-by: aszlig <aszlig@nix.build>
1 parent ccf783b commit e30dc03

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

meson.build

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ main_sources = []
6565
includes = []
6666
subdir('src')
6767

68-
generate_sym_map = [python, script_gensyms, '--map', '@INPUT@']
68+
gen_extra_args = cc.cmd_array() + lib_cflags + cflags + ['--', '@INPUT@']
69+
70+
gen_sym_map = [python, script_gensyms, '--map']
6971
sym_map = custom_target('symmap', input: lib_sources, output: 'symbols.map',
70-
command: generate_sym_map, capture: true)
72+
command: gen_sym_map + gen_extra_args, capture: true)
7173

72-
generate_ldscript = [python, script_gensyms, '--ldscript', '@INPUT@']
74+
gen_ldscript = [python, script_gensyms, '--ldscript']
7375
ldscript = custom_target('ldscript', input: lib_sources, output: 'ldscript',
74-
command: generate_ldscript, capture: true)
76+
command: gen_ldscript + gen_extra_args, capture: true)
7577

7678
lib_ldflags += [
7779
'-Wl,--version-script,@0@'.format(sym_map.full_path()),

scripts/gensyms.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
import re
22
import sys
33

4+
from subprocess import Popen, PIPE
45
from typing import List
56

7+
RE_IS_INCLUDE = re.compile(rb'^\s*#\s*include\b')
8+
RE_RESULT = re.compile(rb'__RESULT\((.*?)\)RESULT__')
9+
10+
ACTION = sys.argv[1]
11+
SEP_INDEX = sys.argv[2:].index('--')
12+
CC_COMMAND = sys.argv[2:][:SEP_INDEX]
13+
FILES = sys.argv[2:][SEP_INDEX + 1:]
614

7-
def find_symbols(*wrapper_names: str) -> List[str]:
8-
fun_matches = '|'.join(map(re.escape, wrapper_names))
9-
wrap_sym_re = re.compile(rf'(?:{fun_matches})\s*\(\s*(\S+?)\s*\)')
1015

16+
def find_symbols(*wrapper_names: str) -> List[str]:
17+
args = map(lambda n: f'-D{n}(x)=__RESULT(x)RESULT__', wrapper_names)
18+
cmd = CC_COMMAND + ['-E'] + list(args) + ['-']
1119
symbols = []
12-
for path in sys.argv[2:]:
13-
with open(path, 'r') as src:
20+
for path in FILES:
21+
with open(path, 'rb') as src:
22+
process = Popen(cmd, stdin=PIPE, stdout=PIPE)
1423
for line in src:
15-
if line.lstrip().startswith('#'):
24+
if RE_IS_INCLUDE.match(line):
1625
continue
17-
for match in wrap_sym_re.finditer(line):
18-
symbols.append(match.group(1))
19-
return symbols
26+
process.stdin.write(line)
27+
process.stdin.close()
28+
data = process.stdout.read()
29+
process.wait()
30+
symbols += [m.group(1) for m in RE_RESULT.finditer(data)]
31+
return [sym.decode() for sym in symbols]
2032

2133

22-
if sys.argv[1] == '--map':
34+
if ACTION == '--map':
2335
symbols = find_symbols('WRAP_SYM', 'EXPORT_SYM')
2436
exported = ''.join(map(lambda s: ' "' + s + '";', symbols))
2537
sys.stdout.write("{\n global:" + exported + "\n local: *;\n};\n")
26-
elif sys.argv[1] == '--ldscript':
38+
elif ACTION == '--ldscript':
2739
symbols = find_symbols('WRAP_SYM')
2840
lines = map(lambda s: f'PROVIDE({s} = ip2unix_wrap_{s});', symbols)
2941
sys.stdout.write("\n".join(lines) + "\n")

0 commit comments

Comments
 (0)