I'm trying to use nanopb with a raspberry pi pico (RP2040) project. I've used nanopb previously and know how to use protoc to generate the .c and .h for my protobufs, but it's always bugged me how the project now has untracked system dependencies.
One of cmake's benefits is an abstracted build system and nanopb certainly builds well with cmake, but I cannot figure out how to add nanopb as a git submodule and have my cmake project build nanopb and then use that build to generate the .c and .h files. It seems like no matter what, it builds nanopb but then appears to try using the system protoc to generate my source/headers instead of the freshly-build-in-project version of nanopb.
First I tried to do it simply, in the toplevel CMakeLists.txt:
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/nanopb/extra)
find_package(Nanopb MODULE REQUIRED)
set(PROTO_FILES
"${CMAKE_SOURCE_DIR}/myprotos/foo.proto"
# Add more proto files here
)
nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
This builds nanopb just fine, but then when it tries to generate the header/source files it bombs out with
[ 12%] Generating nanopb/generator/nanopb_generator.py, nanopb/generator/proto/nanopb.proto
[ 13%] Generating nanopb/generator/proto/nanopb_pb2.py
Traceback (most recent call last):
File "/Users/andrew/testproj/nanopb/generator/protoc", line 44, in <module>
status = invoke_protoc(['protoc'] + args)
File "/Users/andrew/testproj/nanopb/generator/proto/_utils.py", line 76, in invoke_protoc
return subprocess.call(argv)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'protoc'
make[2]: *** [nanopb/generator/proto/nanopb_pb2.py] Error 1
make[1]: *** [CMakeFiles/testproj.dir/all] Error 2
make: *** [all] Error 2
Now I've tried various incarnations of this, and after some googling even tried having cmake create a venv in the project directory and install nanopb in there, but it bombs out the same when it tries to generate the .c and .h protobuf files.
I see issues
#911,
#481 and others, but they seem to be stale.
Does anyone have a working example of how to get nanopb to build within a project without a) having nanopb installed in the host system or b) requiring manual installation of a venv?
Thanks,
Andrew