[CMake] protobuf and imports relative to root (and --proto_path)

David Jobet djobet at tower-research.com
Thu Aug 23 07:50:14 EDT 2018


Hello,

I'm trying to port an existing project from premake to cmake.
I'm trying to avoid modifying the source files while doing so.

Right now, we have several libraries (read in different directories) using
proto files with imports between them.
All imports are made relative to the root of the project.

e.g :
work/lib1/sublib1/a.proto
work/lib1/sublib1/b.proto

with a.proto having a link to b.proto like this "import
lib1/sublib1/b.proto"

If I compile this with an unchanged FindProtobuf.cmake, I do this :

protobuf_generate_cpp(PROTO_SRCS PROTO_HRDS a.proto b.proto)
add_library(...)

the problem being that it looks like the "namespaces" generated by protoc
in b.proto do not match the one used in a.proto : it does not compile.

Is there a way to make this work ?

Otherwise, I made the patch below which solves my problem. Do you think it
could be included in cmake ?
it defines a new option PROTO_PATH, so the decl above becomes
protobuf_generate_cpp(PROTO_SRCS PROTO_HRDS PROTO_PATH ${CMAKE_SOURCE_DIR}
a.proto b.proto)

With regards

David

$ diff share/cmake-3.10/Modules/FindProtobuf.cmake.orig
share/cmake-3.10/Modules/FindProtobuf.cmake
123c123
<   cmake_parse_arguments(protobuf "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN})
---
>   cmake_parse_arguments(protobuf "" "PROTO_PATH;EXPORT_MACRO;DESCRIPTORS"
"" ${ARGN})
130a131,139
>   set(EXTRA_ARGS "")
>   set(OUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
>   if(protobuf_PROTO_PATH)
>     get_filename_component(ABS_PROTO_PATH ${protobuf_PROTO_PATH} ABSOLUTE)
>     set(EXTRA_ARGS "--proto_path=${ABS_PROTO_PATH}")
>     file(RELATIVE_PATH PROTO_REL_PATH ${CMAKE_SOURCE_DIR}
${ABS_PROTO_PATH})
>     set(OUT_PATH ${CMAKE_BINARY_DIR}/${PROTO_REL_PATH})
>   endif()
>
198c207,208
<                "--cpp_out=${DLL_EXPORT_DECL}${CMAKE_CURRENT_BINARY_DIR}"
---
>                ${EXTRA_ARGS}
>                "--cpp_out=${DLL_EXPORT_DECL}${OUT_PATH}"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180823/cbad2f44/attachment.html>


More information about the CMake mailing list