Difficulties using a third-party cmake projects: missing <package>_INCLUDE_DIR(S) (and other)

I’m trying to use a library named Alglib which has no CMake support. Such support is provided here: https://github.com/S-Dafarra/alglib-cmake.

My issue is that it generates a target Alglib without namespace and, in my own framework I expect only dependencies in the form package::component. Whenever I’m finding a target without namespace, I create a fake (INTERFACE IMPORTED) configuring it from the original target _INCLUDE_DIR(S) and LIBRARY or LIBRARIES. This strategy has worked, so far, with all third-party library I encountered.

But in the case of alglib-cmake it seems that none of these variables exists and I don’t get why.

So far my obvious workaround has been to modify the provided CMakeLists.txt to create Alglib::Alglib but I’d like to understand why I don’t get the expected variables and if I should adapt my strategy for this situation (and how).

Thanks in advance, regards,

A.

Why not use an ALIAS library instead?

https://cmake.org/cmake/help/latest/command/add_library.html#alias-libraries

Hi

I wanted to enforce the format package::component. Does ALIAS allows something like add_library(Alglib::Alglib ALIAS Alglib)?. In this case it will simplify a great lot my wrapping :slight_smile: .

Regards,

A.

Yes, it does. I’m using this feature extensively.

Thanks a lot.
I’m trying something like:

# ${CompName} is set to ${PackName} by default
# ${TargetName} the target that will link ${PackName}
# ${Inheritance} is one of PUBLIC/PRIVATE/INTERFACE
if(NOT(${PackName}_FOUND))
	# No one has called find_package previously
	find_package(${PackName} REQUIRED)
endif()
# ${CompName} is set to ${PackName} by default
if(${PackName}_FOUND AND NOT TARGET ${PackName}::${CompName})
	message("${PackName} found")
	add_library(${PackName}::${PackName} ALIAS ${PackName})
	target_link_libraries(${TargetName} ${Inheritance} ${PackName}::${PackName})
endif()

But, for instance, with OpenSSL as ${PackName}, I’m getting

OpenSSL found

Then an error on add_library:

add_library cannot create ALIAS target “OpenSSL::OpenSSL” because target
“OpenSSL” does not already exist

What am I missing?

find_package(OpenSSL) creates two targets for me: OpenSSL::Crypto and OpenSSL::SSL

ah maybe. It’s a situation I didn’t think about.
I’ll check and if it’s the case I’ll need a different strategy:
Is there a way to enumerate the components found and merge them into a single one? (ie merging OpenSSL::Crypto and OpenSSL::SSL into OpenSSL::OpenSSL).

Thanks again for your precious help.

Well… Maybe something like this (untested)?

get_property(importedTargets DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)

find_package(OpenSSL REQUIRED)

get_property(importedTargetsAfter DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)
list(REMOVE_ITEM importedTargetsAfter ${importedTargets})

add_library(OpenSSL::OpenSSL INTERFACE)
target_link_libraries(OpenSSL::OpenSSL INTERFACE ${importedTargetsAfter})

You’ll probably have to play around with that to get what you want.
Otherwise I typically just get the possible targets from the documentation and give up on automatic discovery.