0

I am using python language for my server in gRPC now I need to create the prpto files from cmake as my client in gRPC is written in c++ I have to run this command in CMAkeLISTS.txt file but when I try to run It , no file is created:

python3 -m grpc_tools.protoc -I ../protos --python_out=. -- 
pyi_out=. --grpc_python_out=. ../helloworld.proto

I want to add this command to CMakeLists.txt :

cmake_minimum_required(VERSION 3.15)

# Project
project(myproj)

# Protobuf
set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${protobuf_VERSION}")

# Protobuf-compiler
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)

 # gRPC
 find_package(gRPC CONFIG REQUIRED)
 message(STATUS "Using gRPC ${gRPC_VERSION}")
 set(_GRPC_GRPCPP gRPC::grpc++)
 set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
 set(_GRPC_PY_TOOL $<TARGET_FILE:gRPC::grpc_tools::protoc>)

 # Proto file
get_filename_component(hw_proto "helloworld.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# Generated sources

set(hw_grpc_src_py "${CMAKE_CURRENT_BINARY_DIR}/helloworld_pb2_grpc.py")
set(hw_proto_src_py "${CMAKE_CURRENT_BINARY_DIR}/helloworld_pb2.py")
set(hw_proto_src_pyi "${CMAKE_CURRENT_BINARY_DIR}/helloworld_pb2.pyi")


find_package(PythonInterp REQUIRED)
find_package(Python3 REQUIRED)
add_custom_command(
  OUTPUT "${hw_grpc_src_py}" "${hw_proto_src_py}" "${hw_proto_src_pyi}"
  COMMAND python3 
  ARGS -m  "${_GRPC_PY_TOOL}"
   -I "${hw_proto_path}"
  --python_out "${CMAKE_CURRENT_BINARY_DIR}"
  --pyi_out "${CMAKE_CURRENT_BINARY_DIR}"
  --grpc_python_out "${CMAKE_CURRENT_BINARY_DIR}"
  "${hw_proto}"
  DEPENDS "${hw_proto}")

but it does not create the files, did I miss something? adding

add_custom_target(run ALL
DEPENDS "${hw_proto}")

also did not work.

sama
  • 333
  • 2
  • 11
  • For make COMMAND in `add_custom_command` to be actually executed during the build, the OUTPUT of that command should be **consumed** by some target. For example, it could be a custom target (`add_custom_target`) with appropriate DEPENDS option, or `add_executable` which lists OUTPUT file in its sources. See more in the [duplicate question](https://stackoverflow.com/questions/2937128/cmake-add-custom-command-not-being-run). – Tsyvarev Nov 16 '22 at 15:09
  • I also added add_custom_target(run ALL DEPENDS "${dc_proto}") but again no file created. – sama Nov 16 '22 at 15:13
  • I see no variable `dc_proto` in your code, so most likely `${dc_proto}` is empty and your target depends on nothing. – Tsyvarev Nov 16 '22 at 15:19
  • it was a typo I corrected it. Ithink maybe there is problem in add_custome_command or maybe some packages is needed. But I dont know which and how? – sama Nov 16 '22 at 15:25
  • hw_proto contains file :get_filename_component(hw_proto "helloworld.proto" ABSOLUTE) get_filename_component(hw_proto_path "${hw_proto}" PATH) – sama Nov 16 '22 at 15:37
  • Why you don't use simple `"${hw_grpc_src_py}"` in your DEPENDS clause? That way you will be sure that the **same file** is listed in the OUTPUT of the custom command and in the DEPENDS of the custom target. – Tsyvarev Nov 16 '22 at 15:40
  • as you know in protobuffer we have a .proto file and we have to use the command that I wrote to create the python files, so I have to create these files (helloworld_pb2_grpc.py, helloworld_pb2.py,helloworld_pb2.pyi) from helloworld.proto – sama Nov 16 '22 at 15:43
  • Using same DEPENDS in custom command and in custom target doesn't trigger that custom command to be executed. For trigger the command DEPENDS of the custom target should be the same as OUTPUT of the custom command. – Tsyvarev Nov 16 '22 at 15:44
  • I already have done this and it is working for creating c++ proto files:add_custom_command( OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}" COMMAND ${_PROTOBUF_PROTOC} ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" --cpp_out "${CMAKE_CURRENT_BINARY_DIR}" -I "${hw_proto_path}" --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" "${hw_proto}" DEPENDS "${hw_proto}") – sama Nov 16 '22 at 15:48

0 Answers0