12

I have some proto definitions that I compile to cpp.
To generate the corresponding make target I use cmake like this:
protobuf_generate_cpp(CPP_SOURCES PROTO_HEADERS ${PROTO_FILES})
And I use the CPP_SOURCES to build my lib.

Now I need to compile the same proto files for python also and I added this:
protobuf_generate_python(PY_SOURCES ${PROTO_FILES})
This alone has no effect, and I am not sure what I should / can add more in order to have some make target that will trigger also the protoc for python

3
  • protobuf_generate_python does exist in FindProtobuf.cmake, so perhaps you aren't using it correctly? Commented Feb 25, 2018 at 14:53
  • 2
    Yes it does exist, it's not that it gives and error, but it has no effect. I actually noticed it works after adding the resulting python sources to my cpp library target, which is weird. Maybe I can add a non binary target to pass the python sources to? Commented Feb 25, 2018 at 16:38
  • Please provide a minimal, complete and verifiable example for your problem. There is nothing wrong with the code you posted, so the problem must be somewhere else. Commented Feb 26, 2018 at 8:58

2 Answers 2

11

You already answered your question, but here is a complete answer.

The protobuf_generate_python() function add's a custom command. To trigger the command you need a define a target. So add a add_custom_target() like this

add_custom_target(myTarget ALL 
                  DEPENDS ${PY_SOURCES})
Sign up to request clarification or add additional context in comments.

1 Comment

Just for reference. I had to explicitly call cd build && make myTarget because I was not using it any where. To fix this, I added ${PY_SOURCE} to my main target DEPENDS.
0

The docs have been updated since the accepted answer was posted. The correct way to generate both Python and C++ files with protoc in Cmake is with the generic protobuf_generate command with different language arguments

# Write this again identically with a new language option
protobuf_generate(
    LANGUAGE cpp
    TARGET proto-objects
    IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}/backend/proto"
    PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
    APPEND_PATH
    FILES ${PROTO_FILES}
)

My full implementation looks like:

# Config keyword is optional
find_package(Protobuf CONFIG)

# Glob check if you have many proto files
file(GLOB_RECURSE PROTO_FILES "${CMAKE_CURRENT_LIST_DIR}/backend/proto/*.proto")
add_library(proto-objects OBJECT ${PROTO_FILES})
target_link_libraries(proto-objects PUBLIC protobuf::libprotobuf)
set(PROTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/backend/proto/generated/")
file(MAKE_DIRECTORY ${PROTO_BINARY_DIR})

target_include_directories(proto-objects PUBLIC "$<BUILD_INTERFACE:${PROTO_BINARY_DIR}>")

protobuf_generate(
    LANGUAGE cpp
    TARGET proto-objects
    IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}/backend/proto"
    PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
    APPEND_PATH
    FILES ${PROTO_FILES}
)

protobuf_generate(
    LANGUAGE python
    TARGET proto-objects
    IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}/backend/proto"
    PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
    APPEND_PATH
    FILES ${PROTO_FILES}
)


target_link_libraries(target_name PUBLIC
    $<TARGET_OBJECTS:proto-objects>
)

Docs : https://github.com/protocolbuffers/protobuf/blob/main/docs/cmake_protobuf_generate.md

And yeah, the offical FindProtobuf docs are out of date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.