cmake: don't consider include paths on different drives (#14025)

The current logic of finding a suitable include path relies on CMake to always return a meaningful relative path. On Windows, however, if the two paths reside on different drives (e.g. `c:/Users/Me` and `d:/project/`) the absolute path gets returned unaltered. This yields the invalid final path `c:/Users/Me/d:/project/`.

This case is now detected and the corresponding include path is deemed unsuitable.

(fixes #9290)

Closes #14025

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/14025 from cgudrian:issue-9290-wrong-relative-path-in-protobuf-generate 3c0fd37324
PiperOrigin-RevId: 573315544
pull/14396/head
Christian Gudrian 1 year ago committed by Copybara-Service
parent e1e764e8a7
commit 4b203e85f5
  1. 6
      cmake/protobuf-generate.cmake

@ -108,6 +108,12 @@ function(protobuf_generate)
foreach(DIR ${_protobuf_include_path}) foreach(DIR ${_protobuf_include_path})
if(NOT DIR STREQUAL "-I") if(NOT DIR STREQUAL "-I")
file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir}) file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir})
if(_rel_dir STREQUAL _abs_dir)
# When there is no relative path from DIR to _abs_dir (e.g. due to
# different drive letters on Windows), _rel_dir is equal to _abs_dir.
# Therefore, DIR is not a suitable include path and must be skipped.
continue()
endif()
string(FIND "${_rel_dir}" "../" _is_in_parent_folder) string(FIND "${_rel_dir}" "../" _is_in_parent_folder)
if (NOT ${_is_in_parent_folder} EQUAL 0) if (NOT ${_is_in_parent_folder} EQUAL 0)
set(_suitable_include_found TRUE) set(_suitable_include_found TRUE)

Loading…
Cancel
Save