mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-05 17:40:40 +02:00
more checks in generate functions
This commit is contained in:
parent
561777dad6
commit
78823760b3
@ -6,57 +6,57 @@ sls::Detector class. The tool needs the libclang bindings
|
|||||||
to be installed.
|
to be installed.
|
||||||
|
|
||||||
When the Detector API is updated this file should be run
|
When the Detector API is updated this file should be run
|
||||||
manually
|
manually.
|
||||||
"""
|
"""
|
||||||
from clang import cindex
|
from clang import cindex
|
||||||
import subprocess
|
import subprocess
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
from parse import system_include_paths, clang_format_version
|
from parse import system_include_paths, clang_format_version
|
||||||
|
|
||||||
required_version = 12
|
REDC = '\033[91m'
|
||||||
RED = '\033[91m'
|
GREENC = '\033[92m'
|
||||||
ENDC = '\033[0m'
|
ENDC = '\033[0m'
|
||||||
if (ver := clang_format_version()) != required_version:
|
def red(msg):
|
||||||
print(f'{RED}Clang format version {required_version} required, detected: {ver}. Bye!{ENDC}')
|
return f'{REDC}{msg}{ENDC}'
|
||||||
sys.exit(1)
|
|
||||||
|
def green(msg):
|
||||||
|
return f'{GREENC}{msg}{ENDC}'
|
||||||
|
|
||||||
|
def check_clang_format_version(required_version):
|
||||||
|
if (ver := clang_format_version()) != required_version:
|
||||||
|
msg = red(f'Clang format version {required_version} required, detected: {ver}. Bye!')
|
||||||
|
print(msg)
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
msg = green(f'Found clang-format version {ver}')
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
def check_for_compile_commands_json(path):
|
||||||
|
# print(f"Looking for compile data base in: {path}")
|
||||||
|
compile_data_base_file = path/'compile_commands.json'
|
||||||
|
if not compile_data_base_file.exists():
|
||||||
|
msg = red(f"No compile_commands.json file found in {path}. Bye!")
|
||||||
|
print(msg)
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
msg = green(f'Found: {compile_data_base_file}')
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
|
||||||
default_build_path = "/home/l_frojdh/sls/build/"
|
default_build_path = "/home/l_frojdh/sls/build/"
|
||||||
fpath = "../../slsDetectorSoftware/src/Detector.cpp"
|
fpath = "../../slsDetectorSoftware/src/Detector.cpp"
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument(
|
|
||||||
"-p",
|
|
||||||
"--build_path",
|
|
||||||
help="Path to the build database",
|
|
||||||
type=str,
|
|
||||||
default=default_build_path,
|
|
||||||
)
|
|
||||||
cargs = parser.parse_args()
|
|
||||||
|
|
||||||
db = cindex.CompilationDatabase.fromDirectory(cargs.build_path)
|
|
||||||
index = cindex.Index.create()
|
|
||||||
args = db.getCompileCommands(fpath)
|
|
||||||
args = list(iter(args).__next__().arguments)[0:-1]
|
|
||||||
args = args + "-x c++ --std=c++11".split()
|
|
||||||
syspath = system_include_paths("clang++")
|
|
||||||
incargs = ["-I" + inc for inc in syspath]
|
|
||||||
args = args + incargs
|
|
||||||
|
|
||||||
|
|
||||||
tu = index.parse(fpath, args=args)
|
|
||||||
|
|
||||||
|
|
||||||
m = []
|
m = []
|
||||||
ag = []
|
ag = []
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
|
|
||||||
ag2 = []
|
ag2 = []
|
||||||
|
|
||||||
cn = []
|
cn = []
|
||||||
|
|
||||||
def get_arguments(node):
|
def get_arguments(node):
|
||||||
@ -119,25 +119,67 @@ def visit(node):
|
|||||||
lines.append(
|
lines.append(
|
||||||
f'.def("{child.spelling}",{fs} &Detector::{child.spelling}{args})'
|
f'.def("{child.spelling}",{fs} &Detector::{child.spelling}{args})'
|
||||||
)
|
)
|
||||||
print(f'&Detector::{child.spelling}{args})')
|
if cargs.verbose:
|
||||||
|
print(f'&Detector::{child.spelling}{args})')
|
||||||
cn.append(child)
|
cn.append(child)
|
||||||
for child in node.get_children():
|
for child in node.get_children():
|
||||||
visit(child)
|
visit(child)
|
||||||
|
|
||||||
|
|
||||||
visit(tu.cursor)
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
|
"--build_path",
|
||||||
|
help="Path to the build database",
|
||||||
|
type=Path,
|
||||||
|
default=default_build_path,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-v",
|
||||||
|
"--verbose",
|
||||||
|
help="more output",
|
||||||
|
action='store_true',
|
||||||
|
)
|
||||||
|
cargs = parser.parse_args()
|
||||||
|
|
||||||
|
check_clang_format_version(12)
|
||||||
|
check_for_compile_commands_json(cargs.build_path)
|
||||||
|
|
||||||
|
print("Parsing functions in Detector.h - ", end = "", flush = True)
|
||||||
|
t0 = time.perf_counter()
|
||||||
|
#parse functions
|
||||||
|
db = cindex.CompilationDatabase.fromDirectory(cargs.build_path)
|
||||||
|
index = cindex.Index.create()
|
||||||
|
args = db.getCompileCommands(fpath)
|
||||||
|
args = list(iter(args).__next__().arguments)[0:-1]
|
||||||
|
args = args + "-x c++ --std=c++11".split()
|
||||||
|
syspath = system_include_paths("clang++")
|
||||||
|
incargs = ["-I" + inc for inc in syspath]
|
||||||
|
args = args + incargs
|
||||||
|
tu = index.parse(fpath, args=args)
|
||||||
|
visit(tu.cursor)
|
||||||
|
print(green('OK'))
|
||||||
|
print(f'Parsing took {time.perf_counter()-t0:.3f}s')
|
||||||
|
|
||||||
with open("../src/detector_in.cpp") as f:
|
print("Read detector_in.cpp - ", end = "")
|
||||||
data = f.read()
|
with open("../src/detector_in.cpp") as f:
|
||||||
s = "".join(lines)
|
data = f.read()
|
||||||
s += ";"
|
s = "".join(lines)
|
||||||
text = data.replace("[[FUNCTIONS]]", s)
|
s += ";"
|
||||||
warning = "/* WARINING This file is auto generated any edits might be overwritten without warning */\n\n"
|
text = data.replace("[[FUNCTIONS]]", s)
|
||||||
with open("../src/detector.cpp", "w") as f:
|
warning = "/* WARINING This file is auto generated any edits might be overwritten without warning */\n\n"
|
||||||
f.write(warning)
|
print(green("OK"))
|
||||||
f.write(text)
|
print("Writing to detector.cpp - ", end = "")
|
||||||
|
with open("../src/detector.cpp", "w") as f:
|
||||||
|
f.write(warning)
|
||||||
|
f.write(text)
|
||||||
|
print(green('OK'))
|
||||||
|
|
||||||
# run clang format on the output
|
# run clang format on the output
|
||||||
subprocess.run(["clang-format", "../src/detector.cpp", "-i"])
|
print('Running clang format on generated source -', end = "")
|
||||||
|
subprocess.run(["clang-format", "../src/detector.cpp", "-i"])
|
||||||
|
print(green(" OK"))
|
||||||
|
|
||||||
|
print("Changes since last commit:")
|
||||||
|
subprocess.run(['git', 'diff', '../src/detector.cpp'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user