c++cmakecudalibtorchnvtx

How to avoid find_package_handle_standard_args package name warning


In LibTorch 2.0.1 I was able to use the library without installing it (download library and set CMAKE_PREFIX_PATH to allow CMake to find the library). Using LibTorch 2.5.1 and CUDA 12.6 I have to download nvToolsExt and place it in the path: {project}/third_party/NVTX/...

File: libtorch/share/cmake/Caffe2/public/cuda.cmake:

find_path(nvtx3_dir NAMES nvtx3 PATHS "${PROJECT_SOURCE_DIR}/third_party/NVTX/c/include" NO_DEFAULT_PATH)
find_package_handle_standard_args(nvtx3 DEFAULT_MSG nvtx3_dir)

So having the nvtx3 folder there, CMake is able to find nvtx3, but still CMake shows the warning message:

CMake Warning (dev) at /usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:438 (message):
  The package name passed to `find_package_handle_standard_args` (nvtx3) does
  not match the name of the calling package (Caffe2).  This can lead to
-- Found nvtx3: /home/dev/Documents/projects_cpp/Forex/third_party/NVTX/c/include  
  problems in calling code that expects `find_package` result variables
  (e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
  /home/dev/Documents/libtorch_2_5_1/share/cmake/Caffe2/public/cuda.cmake:180 (find_package_handle_standard_args)
  /home/dev/Documents/libtorch_2_5_1/share/cmake/Caffe2/Caffe2Config.cmake:86 (include)
  /home/dev/Documents/libtorch_2_5_1/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
  CMakeLists.txt:131 (find_package)
This warning is for project developers.

Going deeper, the standard CMake /usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake.txt is generating this warning:

  ``NAME_MISMATCHED``
    .. versionadded:: 3.17

    Indicate that the ``<PackageName>`` does not match
    ``${CMAKE_FIND_PACKAGE_NAME}``. This is usually a mistake and raises a
    warning, but it may be intentional for usage of the command for components
    of a larger package.
...
...
...
  if (DEFINED CMAKE_FIND_PACKAGE_NAME
      AND NOT FPHSA_NAME_MISMATCHED
      AND NOT _NAME STREQUAL CMAKE_FIND_PACKAGE_NAME)
    message(AUTHOR_WARNING
      "The package name passed to `find_package_handle_standard_args` "
      "(${_NAME}) does not match the name of the calling package "
      "(${CMAKE_FIND_PACKAGE_NAME}). This can lead to problems in calling "
      "code that expects `find_package` result variables (e.g., `_FOUND`) "
      "to follow a certain pattern.")
  endif ()

Are any of those options valid, or there is a better solution to avoid the warning?

Note: I am not using nvToolsExt, so removing the script that adds it also worked for me.


Solution

  • I would recommend reading the documentation before diving into the source code. It says:

    Note: If <PackageName> does not match CMAKE_FIND_PACKAGE_NAME for the calling module, a warning that there is a mismatch is given. The FPHSA_NAME_MISMATCHED variable may be set to bypass the warning if using the old signature and the NAME_MISMATCHED argument using the new signature. To avoid forcing the caller to require newer versions of CMake for usage, the variable's value will be used if defined when the NAME_MISMATCHED argument is not passed for the new signature (but using both is an error).

    So yes, option 1 seems fine (although I would just pass -DFPHSA_NAME_MISMATCHED at configuration time instead of changing any CMake files.