find_(library,file,program): add validation function
For now, when a file is searched using one of the find_library
, find_program
, and find_file
commands, the selection criteria are only based on paths and names.
Unfortunately, possibly, they are multiple files matching with the criteria. In this case, only the first one is returned even is, finally, it is not an acceptable choice.
For example, it is very common, especially on Windows
, to have multiple occurrences of python.exe
but, depending on what we are looking for (for example 32bit
versus 64bit
), the file founded is not acceptable.
To remove this limitation, I propose to add the possibility to specify a function which will be called for each file to check if this one is acceptable or not.
This function will take, as arguments, a variable name to output the result of the check and the filename.
function(check_library status filename)
# status output is used to return check result
set(${status} "TRUE" PARENT_SCOPE)
endfunction()
find_library(MY_LIB NAMES ... VALIDATOR check_library ...)
Remarks:
-
VALIDATOR
as option name is just a suggestion. Any other suggestions for this option are welcomed. - Specifying a function rather than a script offers more flexibility: the function has access to the environment where this function is defined: typically the environment of a
find_package
module. - It will be judicious to request that the argument to
VALIDATOR
must be a function rather than a macro to avoid side effects on the environment. By the way, how to know, inC++
, if a command is a macro or a function?