Call find_file from a cmake function?


I’m having trouble making my function FIND_THIRD_PARTY_LIB work.

Inside it I call find_file but the parameter holding the result doesn’t renew apparently because

If the full path to a file is found the result is stored in the variable and the search will not be repeated unless the variable is cleared.

function(LS_FIND_THIRD_PARTY_LIB name return_path )

** execute_process( **
** COMMAND ls -r -1 …/…/…/3rdParty/${name} **
** OUTPUT_VARIABLE out_dirs**
** )**
** string(REPLACE “\n” “;” l1 ${out_dirs})**
** list (GET l1 0 HEAD)**

** find_file(var_return NAMES “lib${return_path}.a” PATHS “…/…/…/…/…/3rdParty/${name}/${HEAD}/macOS” NO_CACHE NO_DEFAULT_PATH)**

** set(${return_path} ${var_return} PARENT_SCOPE)**


So basically after each call of that function var_return is the same (the very first result)

Please help, perhaps I’m doing something wrong with the variable settings? Or perhaps it’s impossible to use find_file like that?

You just have to vary the variable name. An easy way to do that would be something like:

           NAMES “lib${return_path}.a” #BTW: is this right?
           PATHS “…/…/…/…/…/3rdParty/${name}/${HEAD}/macOS”

set(${return_path} ${var_return_${name}} PARENT_SCOPE)

Thanks. I need to have a different name for var_return_ each call?

(yes, that’s right in the code, I reuse that parameter, set that to lib name first and then return as a new path)

Yep. That’s used for caching, so if you look for the same name more than once it’ll just return what’s in the cache. You can have other way to differentiate the name but it probably be somehow connected to the thig you’re looking for to take advantage of caching.
In the odd case (very unlikely) you don’t want caching behaviour at all you could also just reset the cache variable.