Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test if a filename matching a glob exists in cmake?

Tags:

glob

cmake

I want to create a condition which is true only if a filename with a specified glob exists. Like this (it is not working cmake code, just to express my intent):

if(EXISTS "${LIBRARY_PATH}/lib*.a")
  ...
elseif(EXISTS "${LIBRARY_PATH}/lib/*.dll")
  ...
like image 742
Gabor Marton Avatar asked Oct 27 '25 04:10

Gabor Marton


1 Answers

file( GLOB cmake_varibale ${LIBRARY_PATH}/lib*.a )

Will put a list of all matching files in cmake_variable, which you can then test for being empty or not.

From cmake --help-command file:

     ...
     file(GLOB variable [RELATIVE path] [globbing expressions]...)
     file(GLOB_RECURSE variable [RELATIVE path] 
          [FOLLOW_SYMLINKS] [globbing expressions]...)
     ...

   ...

   GLOB will generate a list of all files that match the globbing
   expressions and store it into the variable.  Globbing expressions are
   similar to regular expressions, but much simpler.  If RELATIVE flag is
   specified for an expression, the results will be returned as a
   relative path to the given path.  (We do not recommend using GLOB to
   collect a list of source files from your source tree.  If no
   CMakeLists.txt file changes when a source is added or removed then the
   generated build system cannot know when to ask CMake to regenerate.)

   Examples of globbing expressions include:

      *.cxx      - match all files with extension cxx
      *.vt?      - match all files with extension vta,...,vtz
      f[3-5].txt - match files f3.txt, f4.txt, f5.txt

   GLOB_RECURSE will generate a list similar to the regular GLOB, except
   it will traverse all the subdirectories of the matched directory and
   match the files.  Subdirectories that are symlinks are only traversed
   if FOLLOW_SYMLINKS is given or cmake policy CMP0009 is not set to NEW.
   See cmake --help-policy CMP0009 for more information.

   Examples of recursive globbing include:

      /dir/*.py  - match all python files in /dir and subdirectories

   ...
like image 143
DevSolar Avatar answered Oct 30 '25 03:10

DevSolar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!