I am writing a proof of concept project using Google protobuf-c-rpc.
I want to use CMake for building the project. I am using CMake ver 3.19
Here is my directory structure:
├── build
│ ├── CMakeCache.txt
│ └── CMakeFiles
├── CMakeLists.txt
├── interfaces
|------ example.proto
└── src
├── client
├───── foo_client.py
└── server
|-- include
|── example-server.c
I have built and installed protobuf-c-rpc and it's dependencies locally
I want to use CMake to build as follows:
- Glob the interfaces folder and compile all
*.proto
files - Generate C headers and source files from the *.proto files and place them in ${PROJECT_SOURCE_DIR}/server/include and ${PROJECT_SOURCE_DIR}/server/ respectively
- Generate python code from the *.proto files and place them in ${PROJECT_SOURCE_DIR}/client/core
- Compile the C files generated (including example-server.c)
- Link the object files from step 4 with the protobuff-c libs (currently, I have only managed to build the static libs for protobuf-c*)
This is my CMakeLists.txt file so far:
cmake_minimum_required(VERSION 3.10)
# set the project name
project(MyProj VERSION 0.10)
file(GLOB PROTOBUF_DEFINITION_FILES "interfaces/*.proto")
set(PROTOBUF_INPUT_DIRECTORY "${PROJECT_SOURCE_DIR}")
set(PROTOBUF_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/src/")
foreach(file ${PROTOBUF_DEFINITION_FILES})
# Generate C stubs
set(PROTOBUF_C_ARGUMENTS "protoc-c --proto_path=\"${PROTOBUF_INPUT_DIRECTORY}\" --c_out=\"${PROTOBUF_OUTPUT_DIRECTORY}\server\" \"${file}\"")
execute_process(COMMAND ${PROTOBUF_C_ARGUMENTS}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE PROTOBUF_C_RESULT
OUTPUT_VARIABLE PROTOBUF_C_OUTPUT_VARIABLE)
# Generate Python bindings
set(PROTOBUF_ARGUMENTS "protoc --proto_path=\"${PROTOBUF_INPUT_DIRECTORY}\" --python_out=\"${PROTOBUF_OUTPUT_DIRECTORY}\client\" \"${file}\"")
execute_process(COMMAND ${PROTOBUF_ARGUMENTS}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE PROTOBUF_RESULT
OUTPUT_VARIABLE PROTOBUF_OUTPUT_VARIABLE)
endforeach()
# Move C headers to include folder
file(GLOB GENERATED_C_HEADERS
"${PROTOBUF_OUTPUT_DIRECTORY}/server/*.pb-c.h"
)
file(COPY ${GENERATED_C_HEADERS} DESTINATION "${PROTOBUF_OUTPUT_DIRECTORY}/server/include/")
# file(REMOVE "${GENERATED_C_HEADERS}" ) doesn't work? why can't I use variable here?
file(REMOVE "${PROTOBUF_OUTPUT_DIRECTORY}/server/*.pb-c.h")
file(GLOB PROTOBUF_MODELS_INCLUDES "src/server/*.pb-c.c" "src/server/includes/*.h")
# Need to build (compile + link) my custom C sources + generated C stubs + link to protobuf-c-* libraries
# ... ?
How do I modify the CMakeLists.txt above, to achieve the workflow I described above?
[[Edit]]
It would be great if I could also specify in the CMakeLists options for debug and release versions of the C executable