Skip to content

Conversation

tomoaki0705
Copy link
Contributor

resolves #17804

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under OpenCV (BSD) License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@tomoaki0705
Copy link
Contributor Author

I followed same behavior of #17745, but I can switch this PR on 3.4 branch, as usual procedure.

Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets try to switch on C++ compiler.

If it works, lets re-target this PR to 3.4 branch.

if(OPENCV_CUDA_DETECTION_NVCC_FLAGS MATCHES "-ccbin")
# already specified by user
elseif(CUDA_HOST_COMPILER AND EXISTS "${CUDA_HOST_COMPILER}")
LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${CUDA_HOST_COMPILER}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

   elseif(CUDA_HOST_COMPILER AND EXISTS "${CUDA_HOST_COMPILER}")	
-    LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${CUDA_HOST_COMPILER}")
+    get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
+    # C compiler doesn't work with --run option, forcing C++ compiler instead
+    if(CUDA_HOST_COMPILER STREQUAL c_compiler_realpath)
+      if(DEFINED CMAKE_CXX_COMPILER)
+        get_filename_component(cxx_compiler_realpath "${CMAKE_CXX_COMPILER}" REALPATH)
+        LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${cxx_compiler_realpath}")
+      else()
+        message(STATUS "CUDA: CMAKE_CXX_COMPILER is not available. You may need to specify CUDA_HOST_COMPILER.")
+      endif()
+    else()
+      LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${CUDA_HOST_COMPILER}")
+    endif()
   elseif(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to remove
get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
This line will lead comparing /usr/bin/cc with /usr/bin/aarch64-linux-gnu-gcc-7
(CMAKE_C_COMPILER is /usr/bin/cc but get_filename_component retruns /usr/bin/aarch64-linux-gnu-gcc-7)
Thus, I had to remove get_filename_component and use CMAKE_CXX_COMPILER directly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks strange.

It is aligned with this part of code:

opencv/cmake/FindCUDA.cmake

Lines 467 to 472 in ff99218

if(DEFINED CMAKE_C_COMPILER AND NOT DEFINED CUDA_HOST_COMPILER)
get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
else()
set(c_compiler_realpath "")
endif()
set(CUDA_HOST_COMPILER "${c_compiler_realpath}" CACHE FILEPATH "Host side compiler used by NVCC")

Otherwise STREQUAL check may fail.

Could you find similar part from system CMake on Jetson?


Perhaps we can adapt check to:

if(CUDA_HOST_COMPILER STREQUAL c_compiler_realpath OR CUDA_HOST_COMPILER STREQUAL CMAKE_C_COMPILER)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The system CMake on Jetson is as following.

if(CMAKE_GENERATOR MATCHES "Visual Studio")
  set(CUDA_HOST_COMPILER "$(VCInstallDir)bin" CACHE FILEPATH "Host side compiler used by NVCC")
else()
  if(APPLE
      AND "${CMAKE_C_COMPILER_ID}" MATCHES "Clang"
      AND "${CMAKE_C_COMPILER}" MATCHES "/cc$")
    # Using cc which is symlink to clang may let NVCC think it is GCC and issue
    # unhandled -dumpspecs option to clang. Also in case neither
    # CMAKE_C_COMPILER is defined (project does not use C language) nor
    # CUDA_HOST_COMPILER is specified manually we should skip -ccbin and let
    # nvcc use its own default C compiler.
    # Only care about this on APPLE with clang to avoid
    # following symlinks to things like ccache
    if(DEFINED CMAKE_C_COMPILER AND NOT DEFINED CUDA_HOST_COMPILER)
      get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
      # if the real path does not end up being clang then
      # go back to using CMAKE_C_COMPILER
      if(NOT "${c_compiler_realpath}" MATCHES "/clang$")
        set(c_compiler_realpath "${CMAKE_C_COMPILER}")
      endif()
    else()
      set(c_compiler_realpath "")
    endif()
    set(CUDA_HOST_COMPILER "${c_compiler_realpath}" CACHE FILEPATH "Host side compiler used by NVCC")
  else()
    set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}"
      CACHE FILEPATH "Host side compiler used by NVCC")
  endif()
endif()

So basically, this line gets executed

    set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}"
      CACHE FILEPATH "Host side compiler used by NVCC")

Let's take the OR plan

@tomoaki0705 tomoaki0705 force-pushed the fixCUDAHostCompilerIssue branch from 508a091 to e97e8ce Compare July 11, 2020 23:05
@tomoaki0705 tomoaki0705 changed the base branch from master to 3.4 July 11, 2020 23:08
@tomoaki0705
Copy link
Contributor Author

Now switched to 3.4
Confirmed CC detection works fine on 3.4

LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${CUDA_HOST_COMPILER}")
get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
# C compiler doesn't work with --run option, forcing C++ compiler instead
if(CUDA_HOST_COMPILER STREQUAL ${c_compiler_realpath} OR CUDA_HOST_COMPILER STREQUAL ${CMAKE_C_COMPILER})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${c_compiler_realpath}
${CMAKE_C_COMPILER}

Why just c_compiler_realpath doesn't work for you?
Expanding without quotes breaks on paths with spaces (most observed problem on Windows).

Variable expanding in not required with modern CMake (2.8.12.2):

if(<variable|string> STREQUAL <variable|string>)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood what you mean.
I just forgot to quote, and I didn't knew that I can avoid expanding.

Pushed without expanding.

     * treat both CMAKE_C_COMPILER and c_compiler_realpath as candidate
@tomoaki0705 tomoaki0705 force-pushed the fixCUDAHostCompilerIssue branch from e97e8ce to 269b810 Compare July 12, 2020 12:22
Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 👍

@opencv-pushbot opencv-pushbot merged commit b75f6c0 into opencv:3.4 Jul 12, 2020
@tomoaki0705 tomoaki0705 deleted the fixCUDAHostCompilerIssue branch July 12, 2020 17:30
@alalek alalek mentioned this pull request Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CUDA: still automatic detection fails

3 participants