Skip to content

add_android_openssl_libraries: Automatically link OpenSSL on argument… #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

aambrosano
Copy link

… targets

@bog-dan-ro bog-dan-ro requested a review from Issam-b January 10, 2025 09:47
@aambrosano
Copy link
Author

For the record, OpenSSL::OpenSSL is not really the right target. FindOpenSSL will actually create OpenSSL::crypto and OpenSSL::ssl, however linking against those targets is still generating errors at the CMake generation step. Couldn't figure out why yet.

@@ -1,26 +1,30 @@
function(add_android_openssl_libraries)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(ssl_root_path ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/no-asm)
set(SSL_ROOT_PATH ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/no-asm)
Copy link
Collaborator

Choose a reason for hiding this comment

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

keep local variable as lowercase

${SSL_ROOT_PATH}/ssl_1.1/${CMAKE_ANDROID_ARCH_ABI}/libssl_1_1.so)
set(OPENSSL_CRYPTO_LIBRARY ${SSL_ROOT_PATH}/ssl_1.1/${CMAKE_ANDROID_ARCH_ABI}/libcrypto_1_1.so)
set(OPENSSL_SSL_LIBRARY ${SSL_ROOT_PATH}/ssl_1.1/${CMAKE_ANDROID_ARCH_ABI}/libssl_1_1.so)
set(OPENSSL_INCLUDE_DIR ${SSL_ROOT_PATH}/ssl_1.1/include)
endif()

set_target_properties(${ARGN} PROPERTIES QT_ANDROID_EXTRA_LIBS
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this can also be under the ${ARGN} loop

endif()

if(Qt6_VERSION VERSION_GREATER_EQUAL 6.5.0)
if(NOT OPENSSL_ROOT_DIR)
set(OPENSSL_ROOT_DIR ${SSL_ROOT_PATH}/ssl_3/${CMAKE_ANDROID_ARCH_ABI})
endif()
list(APPEND android_extra_libs
Copy link
Collaborator

Choose a reason for hiding this comment

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

we no longer need to assign this android_extra_libs list if we're doing it for OPENSSL_SSL_LIBRARY and OPENSSL_INCLUDE_DIR, we can use those latter two variables directly for QT_ANDROID_EXTRA_LIBS

Copy link
Collaborator

@Issam-b Issam-b left a comment

Choose a reason for hiding this comment

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

For the record, OpenSSL::OpenSSL is not really the right target. FindOpenSSL will actually create OpenSSL::crypto and OpenSSL::ssl, however linking against those targets is still generating errors at the CMake generation step. Couldn't figure out why yet.

This doesn't work for me, I get an error:

CMake Error at ~/Library/Android/sdk/android_openssl/android_openssl.cmake:24 (target_link_libraries):
Target "tst_openssl" links to:

OpenSSL::OpenSSL

but the target was not found. Possible reasons include:

* There is a typo in the target name.
* A find_package call is missing for an IMPORTED target.
* An ALIAS target is missing.

Call Stack (most recent call first):
CMakeLists.txt:17 (add_android_openssl_libraries)

@Issam-b
Copy link
Collaborator

Issam-b commented Feb 2, 2025

@aambrosano btw the commit message doesn't explain why such patch is needed, is it trying to fix some bug?

@Issam-b
Copy link
Collaborator

Issam-b commented Feb 2, 2025

@aambrosano on this

For the record, OpenSSL::OpenSSL is not really the right target. FindOpenSSL will actually create OpenSSL::crypto and OpenSSL::ssl, however linking against those targets is still generating errors at the CMake generation step. Couldn't figure out why yet.

It doesn't work because the targets should be OpenSSL::Crypto and OpenSSL::SSL not OpenSSL::crypto and OpenSSL::ssl.

@aambrosano
Copy link
Author

aambrosano commented Feb 19, 2025

@Issam-b it just made sense that we linked against OpenSSL in the targets we passed to add_android_openssl_libraries, which we weren't doing. Otherwise we'd need to look up for those libraries twice and that would be redundant. All other changes were just small adjustments.

@aambrosano btw the commit message doesn't explain why such patch is needed, is it trying to fix some bug?

@aambrosano
Copy link
Author

aambrosano commented Feb 19, 2025

@aambrosano on this

For the record, OpenSSL::OpenSSL is not really the right target. FindOpenSSL will actually create OpenSSL::crypto and OpenSSL::ssl, however linking against those targets is still generating errors at the CMake generation step. Couldn't figure out why yet.

It doesn't work because the targets should be OpenSSL::Crypto and OpenSSL::SSL not OpenSSL::crypto and OpenSSL::ssl.

I was just typing targets names off the top of my head when commenting, but was using the right names when coding. That will not work anyway because for some reason targets found within a function are not available outside of the scope of the function. Trying to link against OpenSSL::SSL and OpenSSL::Crypto will result in an error message like this one:

CMake Error in CMakeLists.txt:
  The dependency target "OpenSSL::SSL" of target
  "mytarget_autogen_timestamp_deps" does not exist.

Even when converting the function to a macro (which has its own caveats anyway) the listing will produce the same error. Will have to think about this one a bit more.

@Issam-b
Copy link
Collaborator

Issam-b commented Jun 10, 2025

@aambrosano do you still have the issue above? things work right on my side with this PR, I'd like to check that before submitting this.

endif()

set_target_properties(${ARGN} PROPERTIES QT_ANDROID_EXTRA_LIBS
"${android_extra_libs}")
find_package(OpenSSL REQUIRED GLOBAL)

Choose a reason for hiding this comment

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

2 comments here

  1. GLOBAL option was introduced in cmake 3.24. Any project using a lower cmake version would not be able to use this function anymore. This includes also latest Qt projects, which require a minimum of 3.22.

  2. Even if the cmake verison was not an issue, creating global targets is generally not great, because it prevents being able to create local (directory-scoped) targets with the same name, but in different directories.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I took the liberty to remove that, in the last commit, @alcroito does the patch look good for you?

Choose a reason for hiding this comment

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

Personally, i don't think it looks good, but i'm not the maintainer.

aambrosano mentioned that without GLOBAL, the targets are not available in certain cases. I believe him. So for some use cases it likely won't work. Probably has to do with the scope where the function is called, and where the app is created.

Furthermore, doing find_package inside functions is frowned upon, because all the variables that find_package sets won't be available outside.

Also, I find it strange to do find_package(OpenSSL) to create the targets with pre-filled input from the computed variables.

At that point you could just create the imported targets manually, and avoid the find_package call altogether.

Issam-b added 2 commits June 11, 2025 17:06
If the OpenSSL targets are not defined, add them with add_library() and set OPENSSL_LIB_DIR and OPENSSL_INCLUDE_DIR and store the paths to the shared libraries in the cache so that add_android_openssl_libraries() can re-use them later if OpenSSL targets are already defined.
@Issam-b
Copy link
Collaborator

Issam-b commented Jun 16, 2025

I pushed a new version that avoids the issues pointed out by @alcroito. This is working correctly for me, @aambrosano does it work in the cases that were problematic for you before?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants