Hi,
I’m confused by the correct usage of find_package_handle_standard_args:
Lets say I want to write a simple Findmypackage.cmake to find a certain lib that is usually placed in e.g. /opt/mypackage
, and has include dirs /opt/mypackage/include
and lib dirs /opt/mypackage/lib
.
I want to generate the variables
${mypackage_INCLUDE_DIRS} ${mypackage_LIBRARY_DIRS}
and the target mypackage::mypackage
include(FindPackageHandleStandardArgs)
if(EXISTS "/opt/mypackage/include" AND EXISTS "/opt/mypackage/lib")
set(mypackage_INCLUDE_DIRS "/opt/mypackage/include")
set(mypackage_LIBRARY_DIRS "/opt/mypackage/lib")
if(EXISTS "/opt/mypackage/lib/libmypackage.so")
set(mypackage_FOUND TRUE)
if (NOT TARGET mypackage::mypackage)
# maybe find_library would be better?
add_library(mypackage::mypackage SHARED IMPORTED)
set_target_properties(mypackage::mypackage PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${mypackage_INCLUDE_DIRS}"
IMPORTED_LOCATION "${mypackage_LIBRARY_DIRS}/libmypackage.so"
)
endif()
endif()
endif()
find_package_handle_standard_args(
mypackage
FOUND_VAR mypackage_FOUND
REQUIRED_VARS mypackage_INCLUDE_DIRS mypackage_LIBRARY_DIRS
VERSION_VAR mypackage_VERSION
)
mark_as_advanced(mypackage_INCLUDE_DIRS mypackage_LIBRARY_DIRS)
My questions are:
- Is this a correct looking
Findmypackage.cmake
? It’s quite hard to find best practices other than the example given in https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html - I read that REQUIRED_VARS should be cache (possibly input?) variables (usually
mypackage_LIBRARY_DIR
andmypackage_INCLUDE_DIR
) - I interpret this as giving a method for advanced users to set/hint the locations of these folders directly by setting the variable before calling Findmypackage.cmake, and then in the Findmypackage.cmake I can check whether these folders exist and forward these to mypackage_LIBRARY_DIRS and mypackage_INCLUDE_DIRS. Is this interpretation correct? - However, I don’t expect most users to actually have to set those variables, and I want to only consider mypackage to be found if mypackage_INCLUDE_DIRS is set - how can I do this without putting mypackage_INCLUDE_DIRS in the
REQUIRED_VARS
offind_package_handle_standard_args
?
Thank you!