Skip to content

Instantly share code, notes, and snippets.

@Osyotr
Created August 12, 2022 20:00
Show Gist options
  • Save Osyotr/709e71a91a3b277b01b684a0d638e758 to your computer and use it in GitHub Desktop.
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 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()
@thxkiwi
Copy link

thxkiwi commented Aug 13, 2022

Does this work because vcpkg injects its $VCPKG_INSTALLED_DIR (usually vcpkg_installed in manifest mode) into the search paths of cmake?

@Osyotr
Copy link
Author

Osyotr commented Aug 13, 2022

Yes.

@thxkiwi
Copy link

thxkiwi commented Aug 13, 2022

Thanks for your help. Got it all working!

@thxkiwi
Copy link

thxkiwi commented Aug 13, 2022

...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.

@Osyotr
Copy link
Author

Osyotr commented Aug 13, 2022

fribidi_LIBRARY_Debug, fribidi_LIBRARY_Release

CMake variables are case-sensitive. Try fribidi_LIBRARY_DEBUG and fribidi_LIBRARY_RELEASE.

@thxkiwi
Copy link

thxkiwi commented Aug 14, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment