Created
August 12, 2022 20:00
-
-
Save Osyotr/709e71a91a3b277b01b684a0d638e758 to your computer and use it in GitHub Desktop.
Example of a cmake Find-module that looks for a library in vcpkg installed tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This module creates imported target "fribidi::fribidi" | |
include(FindPackageHandleStandardArgs) | |
include(SelectLibraryConfigurations) | |
find_path(fribidi_INCLUDE_DIR "fribidi.h" PATH_SUFFIXES "fribidi") | |
get_filename_component(fribidi_INCLUDE_DIR "${fribidi_INCLUDE_DIR}" DIRECTORY) | |
get_filename_component(_fribidi_installed_prefix "${fribidi_INCLUDE_DIR}" DIRECTORY) | |
if(NOT fribidi_LIBRARY) | |
find_library(fribidi_LIBRARY_RELEASE NAMES "fribidi" PATHS "${_fribidi_installed_prefix}/lib" NO_DEFAULT_PATH) | |
find_library(fribidi_LIBRARY_DEBUG NAMES "fribidi" PATHS "${_fribidi_installed_prefix}/debug/lib" NO_DEFAULT_PATH) | |
select_library_configurations(fribidi) | |
endif() | |
find_package_handle_standard_args(fribidi | |
REQUIRED_VARS | |
fribidi_INCLUDE_DIR | |
fribidi_LIBRARY | |
) | |
mark_as_advanced(fribidi_INCLUDE_DIR fribidi_LIBRARY) | |
if(fribidi_FOUND) | |
set(fribidi_INCLUDE_DIRS "${fribidi_INCLUDE_DIR}") | |
set(fribidi_LIBRARIES "${fribidi_LIBRARY}") | |
if(NOT TARGET fribidi::fribidi) | |
add_library(fribidi::fribidi UNKNOWN IMPORTED) | |
set_target_properties(fribidi::fribidi PROPERTIES | |
INTERFACE_INCLUDE_DIRECTORIES "${fribidi_INCLUDE_DIRS}" | |
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" | |
IMPORTED_LOCATION "${fribidi_LIBRARY}" | |
) | |
if(EXISTS "${fribidi_LIBRARY_RELEASE}") | |
set_property(TARGET fribidi::fribidi APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) | |
set_target_properties(fribidi::fribidi PROPERTIES | |
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX" | |
IMPORTED_LOCATION_RELEASE "${fribidi_LIBRARY_RELEASE}" | |
) | |
endif() | |
if(EXISTS "${fribidi_LIBRARY_DEBUG}") | |
set_property(TARGET fribidi::fribidi APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG) | |
set_target_properties(fribidi::fribidi PROPERTIES | |
IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX" | |
IMPORTED_LOCATION_DEBUG "${fribidi_LIBRARY_DEBUG}" | |
) | |
endif() | |
endif() | |
endif() |
Yes.
Thanks for your help. Got it all working!
...not quite. Been trying to workout how to select debug versus release when consuming the module but the generation fails. For me, select_library_configurations(fribidi)
sets fribidi_LIBRARY-NOTFOUND
. Both fribidi_LIBRARY_Debug
and fribidi_LIBRARY_Release
are set to their respective libraries and the generator is default (Visual Studio 16 2019 on Windows).
If you have any other tips they're greatly appreciated.
fribidi_LIBRARY_Debug
,fribidi_LIBRARY_Release
CMake variables are case-sensitive. Try fribidi_LIBRARY_DEBUG
and fribidi_LIBRARY_RELEASE
.
Thank you. I was also thinking of the default Visual Studio build configurations “Debug” and “Release” 🤦
From: Osyotr ***@***.***>
Sent: Saturday, August 13, 2022 8:19 AM
To: Osyotr ***@***.***>
Cc: Michael "Kiwi" Ngarimu ***@***.***>; Comment ***@***.***>
Subject: Re: Osyotr/Findfribidi.cmake
@Osyotr commented on this gist.
…________________________________
fribidi_LIBRARY_Debug, fribidi_LIBRARY_Release
CMake variables are case-sensitive. Try fribidi_LIBRARY_DEBUG and fribidi_LIBRARY_RELEASE.
—
Reply to this email directly, view it on GitHub<https://gist.github.com/709e71a91a3b277b01b684a0d638e758#gistcomment-4265965>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ASVPLZUD74DMAY7C562KRADVY64E3ANCNFSM56NQWFXA>.
You are receiving this because you commented.Message ID: ***@***.******@***.***>>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work because vcpkg injects its $VCPKG_INSTALLED_DIR (usually vcpkg_installed in manifest mode) into the search paths of cmake?