JYP2011
JYP2011

Reputation: 33

Why does the following CMakeLists.txt cannot generate the output file of series of *.proto(protobuf)

I am not familiar with cmake and protobuf. I want to write a CMakeLists.txt to help me generate all protobuf file in a specified directory into another specified directory.

The following is the CMakeLists.txt I writen, but it cannot generate any *.pb.h or *.pb.cc now. Anyone can tell me what's wrong with my CMakeLists.txt? Thanks.

The cmake vesion I used is 3.12 and protoc version is 3.12.

file(GLOB_RECURSE children LIST_DIRECTORIES true "${CMAKE_CURRENT_SOURCE_DIR}/protos/*.proto")
SET(PROTO_META_BASE_DIR ${CMAKE_CURRENT_BINARY_DIR}/protos_cpp_hpp)
FOREACH(FIL ${children})
  GET_FILENAME_COMPONENT(ABS_FIL ${FIL} ABSOLUTE)
  GET_FILENAME_COMPONENT(FIL_WE ${FIL} NAME_WE)

  LIST(APPEND PROTO_SRCS "${CMAKE_CURRENT_BINARY_DIR}/protos_cpp_hpp/${FIL_WE}.pb.cc")
  LIST(APPEND PROTO_HDRS "${CMAKE_CURRENT_BINARY_DIR}/protos_cpp_hpp/${FIL_WE}.pb.h")

#   EXECUTE_PROCESS(
#       COMMAND PROTOBUF_GENERATE_CPP --cpp_out=${PROTO_META_BASE_DIR} ${FIL}
#       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/protos
#   )

  PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HDR ${CMAKE_SOURCE_DIR}/protos/${FIL})
ENDFOREACH()

Upvotes: 0

Views: 703

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65870

According to the documentation, the function PROTOBUF_GENERATE_CPP creates a custom command which OUTPUT lists resulted files. So, nothing will be generated unless there is a target which consumes these files as a dependency.

The most direct way is consuming the files as sources for add_executable:

add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})

This way is described in the documentation.

If, for some reason, you don't want create anything from the resulted files, then you may create a custom target which consumes these files:

add_custom_target(my_command ALL DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})

Upvotes: 2

Related Questions