Skip to content

Commit

Permalink
add check.py to perform syntax-checking of generated cpp
Browse files Browse the repository at this point in the history
it would be nice to run this in ci. note llvm 17 dependency
  • Loading branch information
katrinafyi committed Jul 22, 2024
1 parent 467c981 commit 963c670
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions offlineASL-cpp/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3

"""
Configures a meson build for syntax-checking only.
To use, run `meson compile -C build` after this script.
This script works by manually editing the meson-generated
ninja files. This is obviously a bit hacky, but meson does
not yet properly support -fsyntax-only.
"""

import os
import shutil
import subprocess

clangpp = shutil.which('clang++')
true = shutil.which('true')
assert clangpp
assert true

def main():
os.environ['CXX'] = str(clangpp)

subprocess.check_call('meson setup --reconfigure build'.split())

with open('build/build.ninja', 'rb') as f:
build = f.readlines()

bstr = lambda x: str(x).encode('utf-8')

# replacement rules, keyed by sentinel line, and values are
# replacements to perform on the NEXT line.
repls = {
b'rule cpp_COMPILER\n': (b' $ARGS ', b' $ARGS -fsyntax-only '),
b'rule cpp_LINKER\n': (bstr(clangpp), bstr(true)),
b'rule cpp_LINKER_RSP\n': (bstr(clangpp), bstr(true)),
}

with open('build/build.ninja', 'wb') as f:
repl = None
for l in build:
if repl is not None:
assert repl[0] in l, f'replacement failed {repl} in {l!r}'
f.write(l.replace(repl[0], repl[1]))
repl = None
else:
repl = repls.get(l) # must match exactly
f.write(l)

if __name__ == '__main__':
main()

0 comments on commit 963c670

Please sign in to comment.