Search code examples
androidcmakeopenssl

CMake cannot find OpenSSL


This seems like a frequent issue but I can't figure it out...

I'm trying to compile some libraries which use OpenSSL. They use CMake and are unable to find OpenSSL, even though I made a local build of BoringSSL and set OPENSSL_ROOT_DIR to its install directory.

To work on the issue more easily, I made a test CMakeLists.txt, here it is completely:

cmake_minimum_required(VERSION 3.22.1)

project(test)

find_package(OpenSSL REQUIRED)

This fails with:

  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY
  OPENSSL_INCLUDE_DIR)

I do have OPENSSL_ROOT_DIR environment variable set to the install directory inside my local BoringSSL build:

% echo $OPENSSL_ROOT_DIR
/Users/[...]/deps/boringssl/cmake-build-debug-arm64-v8a/install

The files are there:

% ls $OPENSSL_ROOT_DIR/include/openssl/                                             (main|…6)
aead.h          cipher.h        engine.h
....... ssl.h

% ls $OPENSSL_ROOT_DIR/lib                                                          (main|…6)
libcrypto.a libssl.a

I that know CMake (specifically FindOpenSSL.cmake) looks for openssl/ssl.h under include like this:

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

I even hacked FindOpenSSL.cmake to print out _OPENSSL_ROOT_HINTS_AND_PATHS here it is:

STATUS,*** Hints and paths = HINTS;/Users/[...]/deps/boringssl/cmake-build-debug-arm64-v8a/install;ENV;OPENSSL_ROOT_DIR;PATHS

Which has the right path already, plus it refers to the OPENSSL_ROOT_DIR environment variable.

And yet I still get the error.

This seems almost magical and in a bad way. Any ideas?

I've tried using cmake 3.22.1 from the Android SDK and the native (MacOS) cmake 3.24.3, both error out the same way.

Also tried:

set(OPENSSL_ROOT_DIR .......)

right CMakeLists.txt, same error. Magic!

Update

The error is caused by

-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake

If I remove it, the error goes away.

Yes I'm trying to compile for Android. So the Android toolchain is somehow messing up CMake's ability to find OpenSSL. I am using the latest NDK, 25.1.8937393.


Solution

  • There is detailed description of why this happens here:

    Android CMake: Could NOT find OpenSSL

    This works for me:

    list(APPEND CMAKE_FIND_ROOT_PATH "$ENV{OPENSSL_CUSTOM_ROOT_DIR}")
    find_package(OpenSSL)