From 51c91e98efc5a6ae1ee59f3fff44971a60c7f94d Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 10 Sep 2022 17:37:28 +0200 Subject: build: Add protonium builder. --- build/env.py | 1 + build/scons_tools/tool_protonium.py | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 build/scons_tools/tool_protonium.py diff --git a/build/env.py b/build/env.py index 082195d..46d069c 100644 --- a/build/env.py +++ b/build/env.py @@ -11,6 +11,7 @@ env = Environment( 'tool_firmware', 'tool_jinja2', 'tool_platform_spec', + 'tool_protonium', ], ) diff --git a/build/scons_tools/tool_protonium.py b/build/scons_tools/tool_protonium.py new file mode 100644 index 0000000..9a6e472 --- /dev/null +++ b/build/scons_tools/tool_protonium.py @@ -0,0 +1,56 @@ +from re import sub +from SCons.Script import * + +import pathlib +import subprocess + +def path_translate(env, source): + protopath = [pathlib.Path(p) for p in env['PROTOPATH']] + + for p in protopath: + f = p / f'{source}.proto' + + if not f.exists(): + continue + + return str(p / source) + + return source + +def Protonium(env, target_dir, sources): + return [env.ProtoniumBuild(f'{target_dir}/{source}', path_translate(env, source), PROTOC_OUT_DIR = target_dir) for source in sources] + #return [env.ProtoniumBuild(f'{target_dir}/{source}', path_translate(env, source)) for source in sources] + +def protonium_builder_actions(source, target, env, for_signature): + #target_dir = target[0].Dir('.') + target_dir = env['PROTOC_OUT_DIR'] + + protoc_path = ' '.join(f'-I{path}' for path in env['PROTOPATH']) + + return f'protoc {protoc_path} $SOURCES --protonium_out={target_dir} --python_out={target_dir} --protonium_python_out={target_dir}' + +protonium_builder = Builder( + generator = protonium_builder_actions, + #action = 'protoc $SOURCES --protonium_out=$TARGET', + suffix = '_pb.h', + src_suffix = '.proto', +) + +def exists(): + return subprocess.call('protonium', shell = True, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL) == 0 + +def generate(env): + if not exists(): + return + + protopath = [arg[2:] for arg in subprocess.check_output('pkg-config protobuf --cflags-only-I', shell = True, encoding = 'utf-8').split() if arg.startswith('-I')] + protopath += [arg[2:] for arg in subprocess.check_output('protonium --cflags', shell = True, encoding = 'utf-8').split() if arg.startswith('-I')] + + env.Append( + BUILDERS = {'ProtoniumBuild': protonium_builder}, + PROTOPATH = ['.', *protopath], + ) + + env.ParseConfig('protonium --cflags') + + env.AddMethod(Protonium) -- cgit v1.2.3