Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[opencv2] build failure #21154

Closed
Chaoses-Ib opened this issue Nov 3, 2021 · 10 comments · Fixed by #21249
Closed

[opencv2] build failure #21154

Chaoses-Ib opened this issue Nov 3, 2021 · 10 comments · Fixed by #21249
Assignees
Labels
category:port-bug The issue is with a library, which is something the port should already support

Comments

@Chaoses-Ib
Copy link

Host Environment

  • OS: Windows 10 21H2
  • Compiler: MSVC 19.29.30038.1

To Reproduce
Steps to reproduce the behavior:

cd C:\C++\vcpkg
.\vcpkg install opencv2

Failure logs

-- Found external ninja('1.10.2').
-- Configuring x64-windows
CMake Error at scripts/cmake/vcpkg_execute_required_process.cmake:127 (message):
    Command failed: ninja -v
    Working Directory: C:/C++/vcpkg/buildtrees/opencv2/x64-windows-rel/vcpkg-parallel-configure
    Error code: 1
    See logs for more information:
      C:\C++\vcpkg\buildtrees\opencv2\config-x64-windows-out.log

Call Stack (most recent call first):
  installed/x64-windows/share/vcpkg-cmake/vcpkg_cmake_configure.cmake:364 (vcpkg_execute_required_process)
  ports/opencv2/portfile.cmake:61 (vcpkg_cmake_configure)
  scripts/ports.cmake:142 (include)


Error: Building package opencv2:x64-windows failed with: BUILD_FAILED
Please ensure you're using the latest portfiles with `git pull` and `.\vcpkg update`, then
submit an issue at https://github.com/Microsoft/vcpkg/issues including:
  package: opencv2[core,eigen,jpeg,png,tiff]:x64-windows -> 2.4.13.7#6
  vcpkg version: 2021-11-02-af04ebf6274fd6f7a941bff4662b3955c64f6f42
  vcpkg-tool version: 7375bb408 2021-11-03 (71 minutes ago)

config-x64-windows-out.log:

-- Detecting C compile features - done
CMake Error at cmake/OpenCVUtils.cmake:90 (if):
  if given arguments:

    "C:/C++/vcpkg/buildtrees/opencv2/x64-windows-dbg" "MATCHES" "^C:/C++/vcpkg/buildtrees/opencv2/src/2.4.13.7-64eb3208d2.clean" "OR" "C:/C++/vcpkg/buildtrees/opencv2/x64-windows-dbg" "MATCHES" "^C:/C++/vcpkg/buildtrees/opencv2/x64-windows-dbg"

  Regular expression
  "^C:/C++/vcpkg/buildtrees/opencv2/src/2.4.13.7-64eb3208d2.clean" cannot
  compile
Call Stack (most recent call first):
  CMakeLists.txt:351 (ocv_include_directories)


-- Configuring incomplete, errors occurred!
See also "C:/C++/vcpkg/buildtrees/opencv2/x64-windows-dbg/CMakeFiles/CMakeOutput.log".
RegularExpression::compile(): Nested *?+.
RegularExpression::compile(): Error in compile.

OpenCVUtils.cmake:

# adds include directories in such a way that directories from the OpenCV source tree go first
function(ocv_include_directories)
  set(__add_before "")
  foreach(dir ${ARGN})
    get_filename_component(__abs_dir "${dir}" ABSOLUTE)
    if("${__abs_dir}" MATCHES "^${OpenCV_SOURCE_DIR}" OR "${__abs_dir}" MATCHES "^${OpenCV_BINARY_DIR}")
      list(APPEND __add_before "${dir}")
    elseif(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0" AND
           dir MATCHES "/usr/include$")
      # workaround for GCC 6.x bug
    else()
      include_directories(AFTER SYSTEM "${dir}")
    endif()
  endforeach()
  include_directories(BEFORE ${__add_before})
endfunction()

Additional context
One solution (from opencv4/OpenCVUtils.cmake):

# adds include directories in such a way that directories from the OpenCV source tree go first
function(ocv_include_directories)
  ocv_debug_message("ocv_include_directories( ${ARGN} )")
  set(__add_before "")
  foreach(dir ${ARGN})
    ocv_is_opencv_directory(__is_opencv_dir "${dir}")
    if(__is_opencv_dir)
      list(APPEND __add_before "${dir}")
    elseif(((CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0") OR CV_CLANG) AND
           dir MATCHES "/usr/include$")
      # workaround for GCC 6.x bug
    else()
      include_directories(AFTER SYSTEM "${dir}")
    endif()
  endforeach()
  include_directories(BEFORE ${__add_before})
endfunction()

function(ocv_is_opencv_directory result_var dir)
  set(result FALSE)
  foreach(parent ${OpenCV_SOURCE_DIR} ${OpenCV_BINARY_DIR} ${OPENCV_EXTRA_MODULES_PATH})
    ocv_is_subdir(result "${parent}" "${dir}")
    if(result)
      break()
    endif()
  endforeach()
  set(${result_var} ${result} PARENT_SCOPE)
endfunction()

# check if "sub" (file or dir) is below "dir"
function(ocv_is_subdir res dir sub )
  get_filename_component(dir "${dir}" ABSOLUTE)
  get_filename_component(sub "${sub}" ABSOLUTE)
  file(TO_CMAKE_PATH "${dir}" dir)
  file(TO_CMAKE_PATH "${sub}" sub)
  set(dir "${dir}/")
  string(LENGTH "${dir}" len)
  string(LENGTH "${sub}" len_sub)
  if(NOT len GREATER len_sub)
    string(SUBSTRING "${sub}" 0 ${len} prefix)
  endif()
  if(prefix AND prefix STREQUAL dir)
    set(${res} TRUE PARENT_SCOPE)
  else()
    set(${res} FALSE PARENT_SCOPE)
  endif()
endfunction()
@JonLiu1993 JonLiu1993 added the requires:repro The issue is not currently repro-able label Nov 4, 2021
@JonLiu1993
Copy link
Member

@Chaoses-Ib ,Thanks for posting this issue,I can install opencv2:x64-windows successfully, could you please re-clone a vcpkg in a non-C drive and try it

Installing package opencv2[core,eigen,jpeg,png,tiff]:x64-windows...
Elapsed time for package opencv2:x64-windows: 8.77 min

Total elapsed time: 9.648 min

The package opencv2 provides CMake targets:

    find_package(OpenCV CONFIG REQUIRED)
    # Note: 15 target(s) were omitted.
    target_link_libraries(main PRIVATE opencv_ml opencv_ts opencv_gpu opencv_ocl)

@Chaoses-Ib
Copy link
Author

It is not a matter of C-drive, but of "++". Look at this line from ocv_include_directories in OpenCVUtils.cmake:

if("${__abs_dir}" MATCHES "^${OpenCV_SOURCE_DIR}" OR "${__abs_dir}" MATCHES "^${OpenCV_BINARY_DIR}")

This is an incorrect way to match a path since many characters are used as meta characters in regular expressions, like ^.[]+(). Many directory names can lead to an error, such as [2021-11-4]project, project (temp) and C++ project.

One solution is to do what opencv4 does as I quoted, and the other is to escape ${OpenCV_SOURCE_DIR} and ${OpenCV_BINARY_DIR}.

@JackBoosY JackBoosY removed the requires:repro The issue is not currently repro-able label Nov 4, 2021
@JackBoosY
Copy link
Contributor

According to the cmake https://cmake.org/cmake/help/latest/command/string.html#regex-specification, all special characters such as +, ., ^, ?, etc. should be prefixed with \.

Since this is a path and use cmake keyword MATCHES, I'm not sure whether it's a cmake bug.
I will report this to cmake later.

@JackBoosY
Copy link
Contributor

Already report to cmake: https://gitlab.kitware.com/cmake/cmake/-/issues/22854.

@JackBoosY JackBoosY added the depends:upstream-changes Waiting on a change to the upstream project label Nov 4, 2021
@JackBoosY
Copy link
Contributor

JackBoosY commented Nov 4, 2021

Temporary solution:
Replace if("${__abs_dir}" MATCHES "^${OpenCV_SOURCE_DIR}" OR "${__abs_dir}" MATCHES "^${OpenCV_BINARY_DIR}") with the following code:

foreach (dir IN ITEMS __abs_dir OpenCV_SOURCE_DIR OpenCV_BINARY_DIR)
	set(${dir}_fixed "${${dir}}")
	foreach(char IN ITEMS "^" "$" "." "[" "]" "-" "*" "+" "?" "|" "(" ")")
		set(char "\\${char}")
		string(REGEX REPLACE "${char}" "\\${char}" ${dir}_fixed "${dir}_fixed")
	endforeach()
endforeach()
if("${__abs_dir_fixed}" MATCHES "^${OpenCV_SOURCE_DIR_fixed}" OR "${__abs_dir_fixed}" MATCHES "^${OpenCV_BINARY_DIR_fixed}")
endif()

@Chaoses-Ib
Copy link
Author

The related issue in opencv: opencv/opencv#6046
It was marked wontfix but in fact it was fixed in opencv/opencv@b640e72

@dg0yt
Copy link
Contributor

dg0yt commented Nov 4, 2021

Already report to cmake: https://gitlab.kitware.com/cmake/cmake/-/issues/22854.

It is not a cmake bug. If you call MATCHES, you are responsible to provide a valid regular expression. A plain file path is not suitable and special regex characters must be escaped by the user.

@JackBoosY JackBoosY added category:port-bug The issue is with a library, which is something the port should already support and removed depends:upstream-changes Waiting on a change to the upstream project labels Nov 5, 2021
@JackBoosY JackBoosY assigned Cheney-W and unassigned JackBoosY Nov 5, 2021
@Cheney-W Cheney-W added category:question This issue is a question and removed category:port-bug The issue is with a library, which is something the port should already support labels Nov 5, 2021
@Cheney-W
Copy link
Contributor

Cheney-W commented Nov 5, 2021

opencv/opencv@b640e72 does not fix this issue.

@Chaoses-Ib
Copy link
Author

Sorry, it should be opencv/opencv@82aff11, which changed

function(ocv_is_opencv_directory result_var dir)
  get_filename_component(__abs_dir "${dir}" ABSOLUTE)
  if("${__abs_dir}" MATCHES "^${OpenCV_SOURCE_DIR}"
      OR "${__abs_dir}" MATCHES "^${OpenCV_BINARY_DIR}"
      OR (OPENCV_EXTRA_MODULES_PATH AND "${__abs_dir}" MATCHES "^${OPENCV_EXTRA_MODULES_PATH}"))
    set(${result_var} 1 PARENT_SCOPE)
  else()
    set(${result_var} 0 PARENT_SCOPE)
  endif()
endfunction()

to

function(ocv_is_opencv_directory result_var dir)
  set(result FALSE)
  foreach(parent ${OpenCV_SOURCE_DIR} ${OpenCV_BINARY_DIR} ${OPENCV_EXTRA_MODULES_PATH})
    is_subdir(result "${parent}" "${dir}")
    if(result)
      break()
    endif()
  endforeach()
  set(${result_var} ${result} PARENT_SCOPE)
endfunction()

# check if "sub" (file or dir) is below "dir"
function(is_subdir res dir sub )
  get_filename_component(dir "${dir}" ABSOLUTE)
  get_filename_component(sub "${sub}" ABSOLUTE)
  file(TO_CMAKE_PATH "${dir}" dir)
  file(TO_CMAKE_PATH "${sub}" sub)
  set(dir "${dir}/")
  string(LENGTH "${dir}" len)
  string(LENGTH "${sub}" len_sub)
  if(NOT len GREATER len_sub)
    string(SUBSTRING "${sub}" 0 ${len} prefix)
  endif()
  if(prefix AND prefix STREQUAL dir)
    set(${res} TRUE PARENT_SCOPE)
  else()
    set(${res} FALSE PARENT_SCOPE)
  endif()
endfunction()

@Cheney-W
Copy link
Contributor

Cheney-W commented Nov 5, 2021

Thanks for your reply! This issue was indeed fixed by the second change.

@Cheney-W Cheney-W added category:port-bug The issue is with a library, which is something the port should already support and removed category:question This issue is a question labels Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:port-bug The issue is with a library, which is something the port should already support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants