1

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. I also changed add_custome command to 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_grpc_src_py}") but also no files were generated.

sama
  • 333
  • 2
  • 11
  • Do you have the same issue when you eliminate CMake and just invoke grpc_tools directly? – Richard Belleville Nov 16 '22 at 18:28
  • I can make files without cmake and running the command directly in the terminal – sama Nov 16 '22 at 19:35
  • What is unclear from my comments to your [previous question](https://stackoverflow.com/questions/74462632/how-to-add-creating-protobuf-python-files) and its [duplicate question](https://stackoverflow.com/questions/2937128/cmake-add-custom-command-not-being-run)? For trigger the custom command to be executed, a custom target needs to **DEPENDS** from those file(s), which are listed in the **OUTPUT** clause of the `add_custom_command`. In your code the custom target has the same DEPENDS as for the custom command. And I clearly said you that it doesn't work in such way. – Tsyvarev Nov 16 '22 at 20:24
  • 1
    And I even [suggested](https://stackoverflow.com/questions/74462632/how-to-add-creating-protobuf-python-files#comment131448986_74462632) you the custom target, which would work: `add_custom_target(run ALL DEPENDS "${hw_grpc_src_py}")`. – Tsyvarev Nov 16 '22 at 20:26
  • @Tsyvarev I tried your solution it didn’t work – sama Nov 16 '22 at 20:57
  • Please, show (in the question post) that attempt. What is a purpose to show the attempt which you have told to be non-working? – Tsyvarev Nov 16 '22 at 21:14
  • Please read the [how to ask](https://stackoverflow.com/help/how-to-ask) help page. For one thing, I think the title here could use improvement. – starball Nov 17 '22 at 02:37
  • Custom target should work as Tsyvarev mentioned. It would be good to know whether protobuf is called at all or not. Make will delete the generated files if they only got partially generated, so it would be good to know whether generation is happening or not (in other words, a log). – kutschkem Nov 17 '22 at 08:16
  • I see, I also think that there is an issue with protobuf and calling it, but how can i make sure that it is called or not? – sama Nov 17 '22 at 08:29

1 Answers1

1

Removing

set(_GRPC_PY_TOOL $<TARGET_FILE:gRPC::grpc_tools::protoc>)

and

"${_GRPC_PY_TOOL}" 

from add_custom_command and write it like :

add_custom_command(
OUTPUT "${hw_grpc_src_py}" "${hw_proto_src_py}" "${hw_proto_src_pyi}"
COMMAND python3 
 ARGS -m  grpc_tools.protoc
-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}")

solved the problem and the files are created.

sama
  • 333
  • 2
  • 11