Hello!
I’m processing protobuf files with nanopb generator.
All works fine, expect that files are generated every time I build project. I would like to speedup build process and run generator only if source files were changed.
So, from *.proto
it genarates 2 files *.pb.c
and *.pb.h
I understand, that I need to add some dependency, to source .proto
files but I don’t know how to do that with platformio env.
Seems that I need to add custom target instead of calling env.Execute(cmd)
directly.
Can somebody help me example with custom target which has 1 dependecy and 2 output files?
library.json file:
"extraScript": "scripts/nanopb_generate.py"
nanopb_generate.py: (working example)
import os
import glob
Import("env")
mylib_root = os.path.join(os.getcwd(),'..')
generated_src_dir = os.path.join(mylib_root, 'src', 'generated')
protoc_generator = os.path.join(mylib_root, '3rd_party', 'nanopb', 'generator', 'protoc')
mylib_proto_dirs = [
os.path.join(mylib_root, 'mylib', 'proto'),
]
nanopb_options = [
f"--nanopb_out={generated_src_dir}",
]
proto_files = []
for proto_dir in mylib_proto_dirs:
protos = glob.glob(os.path.join(proto_dir, '*.proto'))
proto_files += protos
nanopb_options.append("--proto_path=" + proto_dir)
nanopb_options.append("--nanopb_opt=-I" + proto_dir)
# print(proto_dir)
for proto_file in proto_files:
proto_file_basename = os.path.basename(proto_file)
proto_file_without_ext = os.path.splitext(proto_file_basename)[0]
generated_targets = [
os.path.join(generated_src_dir, proto_file_without_ext + ".pb.c"),
os.path.join(generated_src_dir, proto_file_without_ext + ".pb.h")
]
# print(f"{proto_file_basename} -> {generated_targets}")
cmd = protoc_generator + " " + " ".join(nanopb_options) + " " + proto_file_basename
result = env.Execute(cmd)
if (result != 0):
print(f"Error({result}) processing cmd: '{cmd}'")
exit(1)