protobuf and imports relative to project root (and --proto-path) + patch proposal
When a .proto file references another .proto files with imports relatives to the root of the project, I did not find a way to have compilable files.
I got no answer from https://cmake.org/pipermail/cmake/2018-August/068141.html
Here's a mockup :
- projectroot/lib1
- projectroot/lib1/CMakeLists.txt
- projectroot/lib1/a.proto
- projectroot/lib1/b.proto
- projectroot/lib1/test.cc
See below for the content of the files.
Here's the cmake output :
$ cmake --build build
[0/1] Re-running CMake...
-- Configuring done
-- Generating done
-- Build files have been written to: XXX/temp/build
[1/3] Building CXX object lib1/CMakeFiles/protolib1.dir/a.pb.cc.o
FAILED: lib1/CMakeFiles/protolib1.dir/a.pb.cc.o
XXX/bin/g++ -I. -isystem XXX/include -std=c++14 -Wall -std=c++14
-MD -MT lib1/CMakeFiles/protolib1.dir/a.pb.cc.o -MF
lib1/CMakeFiles/protolib1.dir/a.pb.cc.o.d -o lib1/CMakeFiles/protoli
b1.dir/a.pb.cc.o -c lib1/a.pb.cc
lib1/a.pb.cc: In function 'void protobuf_AddDesc_a_2eproto()':
lib1/a.pb.cc:79:3: error: '::protobuf_AddDesc_lib1_2fb_2eproto' has
not been declared
::protobuf_AddDesc_lib1_2fb_2eproto();
^~
ninja: build stopped: subcommand failed.
Here's what's generated from b.proto :
$ cat build/lib1/b.pb.h | grep AddDesc
void protobuf_AddDesc_b_2eproto();
friend void protobuf_AddDesc_b_2eproto();
The patch below allows me to support this import style : If I remove the PROTOBUF_IMPORT_DIRS and add "PROTO_PATH ${CMAKE_SOURCE_DIR}" on protobuf_generate_cpp to use my patch (this injects --proto_path on protoc command line), everything works. e.g :
#set(PROTOBUF_IMPORT_DIRS ${CMAKE_SOURCE_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HRDS PROTO_PATH
${CMAKE_SOURCE_DIR} a.proto b.proto)
And indeed, the namespace is now correct :
$ cat build/lib1/b.pb.h | grep AddDesc
void protobuf_AddDesc_lib1_2fb_2eproto();
friend void protobuf_AddDesc_lib1_2fb_2eproto();
Is there a way to make this works without the patch ?
David
File contents
$ cat lib1/CMakeLists.txt CMakeLists.txt
$ cat lib1/a.proto a.proto
$ cat lib1/b.proto b.proto
$ cat lib1/test.cc test.cc
$ cat CMakeLists.txt CMakeLists.txt