Why can't I find curl when cross compiling?

EDIT: to reach more people, cross posted here

Hi all! I’m trying to add curl to my project. I made some tests on native build, and it works fine like this:

find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${CURL_LIBRARIES})

However, I also need to cross-compile my code. Since I am targeting an old Debian stretch system, I compiled the correct version of GCC and glibc, and it works fine for other local libraries. I also made a copy of the target sysroot with this script:

rsync -au --progress \
      --include=/lib/*** \
      --include=/opt/*** \
      --include=/usr \
      --include=/usr/include/*** \
      --include=/usr/lib/*** \
      --include=/usr/local \
      --include=/usr/local/lib/*** \
      --include=/usr/local/include/*** \
      --include=/usr/share \
      --include=/usr/share/cmake-3.7/*** \
      --exclude='*' \
      <target_system>:/ \
      /opt/<local-sysroot>

However, when cross compiling, now configuration fails:

CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR) (found version
  "7.81.0")
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindCURL.cmake:181 (find_package_handle_standard_args)
  CMakeLists.txt:162 (find_package)

notice that 7.81.0 is the version on my host system, while the one present on the target (7.52.1) doesn’t get found. However, my toolchain file contains:

set(root_fs_dir /opt/<local-sysroot>)
set(CMAKE_FIND_ROOT_PATH ${root_fs_dir})
set(CMAKE_SYSROOT ${root_fs_dir})
set(CMAKE_SYSTEM_PREFIX_PATH ${root_fs_dir})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

thinking it could be due to the different path of CMake module search, I created the symlink

ln -s /opt/box-root-fs/usr/share/cmake-3.7/ /opt/box-root-fs/usr/share/cmake-3.22

but the error is still present

CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR) (found version
  "7.81.0")
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindCURL.cmake:181 (find_package_handle_standard_args)
  CMakeLists.txt:162 (find_package)

so, why can’t CMake find the libraries, if the module file is present? Thank you!

I recommend using the --debug-find flag to configure to figure out what is going on when CMake searches for CURL bits.