Correcting import path selection for protoc

When there are multiple proto file inputs, they are matched with the provided proto directories (-I option). These directories are tested sequentially for each input proto file and if input file is in a subdirectory of provided proto directories, this directory is considered as base for calculating output directory. This update provides same manner and removes limitations imposed by using `${CMAKE_CURRENT_SOURCE_DIR}` as main proto import directory.
1- `${CMAKE_CURRENT_SOURCE_DIR}` is only added to include directories if no import directory is provided and we are not in `APPEND_PATH` mode. In addition it is added as last possible directory to decrease its priority in searching.
2- Each directory is checked against `${_protobuf_include_path}` to find first possible directory which is parent directory of input proto file. If a directory is found, `${_rel_dir}` is calculated based on its value. If no suitable folder is found, an error will be generated.
This commit is contained in:
afshinpir 2020-04-04 21:38:42 +04:30 committed by Adam Cozzette
parent c62ffaa539
commit c91ab40081

View File

@ -81,8 +81,6 @@ function(protobuf_generate)
list(APPEND _protobuf_include_path -I ${_abs_path})
endif()
endforeach()
else()
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
endif()
foreach(DIR ${protobuf_generate_IMPORT_DIRS})
@ -93,12 +91,31 @@ function(protobuf_generate)
endif()
endforeach()
if(NOT _protobuf_include_path)
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
endif()
set(_generated_srcs_all)
foreach(_proto ${protobuf_generate_PROTOS})
get_filename_component(_abs_file ${_proto} ABSOLUTE)
get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
get_filename_component(_basename ${_proto} NAME_WLE)
file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
set(_suitable_include_found FALSE)
foreach(DIR ${_protobuf_include_path})
if(NOT DIR STREQUAL "-I")
file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir})
if(NOT "${_rel_dir}" MATCHES "^\.\.[/\\].*")
set(_suitable_include_found TRUE)
break()
endif()
endif()
endforeach()
if(NOT _suitable_include_found)
message(SEND_ERROR "Error: protobuf_generate could not find any correct proto include directory.")
return()
endif()
set(_generated_srcs)
foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})