Monday, May 9, 2022

[SOLVED] cmake find_path not working for simple file

Issue

I'm trying to find a problem why cmake is not able to find a custom openssl version, therefore I debugged the original findOpenssl.cmake.

There they search for the ssl.h file. So, I created a small cmake file which tries to find out the issue:

project(CMAKETEST)
cmake_minimum_required(VERSION 3.16)

set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/include/openssl/ssl.h")
message(STATUS CUSTOMOPENSSLPATH: ${OPENSSL_CUSTOM_PATH})

find_path(TEST123
    NAMES ${OPENSSL_CUSTOM_PATH}
)

message(STATUS ${TEST123})

I created an empty ssl.h file in the above folder, but cmake is not able to find the file. TEST123 is always NOT-FOUND.

Solution:

set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/include/openssl")
find_path(TEST123 NAMES ssl.h PATHS "${OPENSSL_CUSTOM_PATH}")

Update

This more concrete example does not work as expected:

project(CMAKETEST)
cmake_minimum_required(VERSION 3.16)

set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/")
find_path(TEST123
    NAMES openssl/ssl.h HINTS "${OPENSSL_CUSTOM_PATH}"
    PATH_SUFFIX include
    NO_DEFAULT_PATH
)

message(STATUS ${TEST123})

when removing NO_DEFAULT_PATH then it finds the system openssl library in /usr/include

Update 2

https://github.com/Kitware/CMake/blob/master/Modules/FindOpenSSL.cmake#L195-L204

find_path(OPENSSL_INCLUDE_DIR
  NAMES
    openssl/ssl.h
  ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  HINTS
    ${_OPENSSL_INCLUDEDIR}
    ${_OPENSSL_INCLUDE_DIRS}
  PATH_SUFFIXES
    include
)

finds first the path in /usr/include, if I add NO_DEFAULT_PATH it finds it. Probably it was because some caching that it was not found during I wrote this question.


Solution

It isn't working because you use it incorrectly, for your case it should be like this:

set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/include/openssl")
find_path(TEST123 NAMES ssl.h PATHS "${OPENSSL_CUSTOM_PATH}")

Or simply find_path(TEST123 ssl.h PATHS "${OPENSSL_CUSTOM_PATH}")


In your update you have a error PATH_SUFFIX should be PATH_SUFFIXES.



Answered By - ixSci
Answer Checked By - Gilberto Lyons (WPSolving Admin)