I'm trying to compile several subfolders of .proto files using CMake, and I'd like the output to end up in a similar folder structure. I'm pretty new to CMake, so I expect that's an elegant way to do it, but right now I'm manually specifying the subfolders so that I can see what's going on. At this point, running the CMakeLists.txt I've shown below successfully reads the .proto files and generates the source, but instead of ending up in ./proto_gen/include/frontseat/vehicle like I expected they all end up in ./proto_gen/include/frontseat/vehicle/vehicle.
I've tried printing out all the paths in the call to execute_process and none of them have this double-nesting "vehicle/vehicle" anywhere, so I'm assuming that this has something to do with how the protoc command is generating the filename. What's odd to me, though, is that previously I had all my proto files in one folder and everything seemed pretty clear about where the filenames were coming from. The only difference between this version and that is the places that I added the "vehicle" folder to the paths.
# Define CMake requirements and project name
cmake_minimum_required(VERSION 3.0)
project( frontseat )
# Define directories for .proto files and compiled output
GET_FILENAME_COMPONENT(PROTO_MAIN_DIR "./proto" ABSOLUTE)
GET_FILENAME_COMPONENT(PROTO_GEN_DIR "./proto_gen" ABSOLUTE)
# Get all the .proto files to be compiled from the first subfolder
file(GLOB PROTO_FILES ${PROTO_MAIN_DIR}/${PROJECT_NAME}/vehicle/*.proto)
# Make directories needed for protobuf files
file(MAKE_DIRECTORY ${PROTO_GEN_DIR}/include/${PROJECT_NAME}/vehicle)
file(MAKE_DIRECTORY ${PROTO_GEN_DIR}/src/vehicle)
# Iterate over the list of .proto files to compile
FOREACH (proto ${PROTO_FILES})
get_filename_component(basename ${proto} NAME_WE)
# Generate proto file using protoc executable
message(" Processing vehicle proto file: " ${basename})
execute_process(COMMAND protoc --cpp_out=${PROTO_GEN_DIR}/include/${PROJECT_NAME}/vehicle -I${PROTO_MAIN_DIR}/${PROJECT_NAME} ${PROTO_MAIN_DIR}/${PROJECT_NAME}/vehicle/${basename}.proto)
ENDFOREACH(proto)
Can anyone tell me where the extra layer of "vehicle" subfolder is coming from here and how to remove it?
This might be related to your issue: https://github.com/protocolbuffers/protobuf/issues/3044
Basically, the first import path you give is used as the root for the structure of proto files, and the output is generated with this relative structure in the output directory. So you just have to change your command to COMMAND protoc --cpp_out=${PROTO_GEN_DIR}/include/${PROJECT_NAME} -I${PROTO_MAIN_DIR}/${PROJECT_NAME} ${PROTO_MAIN_DIR}/${PROJECT_NAME}/vehicle/${basename}.proto