[CMake] CMake to find path to a file which its name contains a certain string

sina Tavakoli sina_11235 at yahoo.com
Mon Jun 8 02:48:05 EDT 2015


So I am writing a CMake module to find some libraries which is being used a lot in our company. These libraries all have different versions and are such a mess here. In a certain library, even the name of header files and binary files don't match. So I am writing a CMake script to handle all the problems of finding each library once and for all. What I have in my mind is to write something like how we find boost packages but include the version of each component as well. Something like this:find_package(OrgName COMPONENTS librarya-1.4.3 libraryb-2.3.1 libraryc-3.6.0)So I created a FindOrgName.cmake file and iterated on the requested components, then I processed the string which is passed and gained the library name along with its version information something like this (never mind the difference between include and binary files):IF(OrgName_FIND_COMPONENTS)
   FOREACH(comp ${OrgName_FIND_COMPONENTS})
      SET(OrgName_${comp}_FOUND 0)

      STRING(FIND ${comp} "-" dashind REVERSE)
      STRING(LENGTH ${comp} length)
      STRING(SUBSTRING ${comp} 0 ${dashind} name)
      MATH(EXPR s "${dashind}+1")
      MATH(EXPR l "${length}-${dashind}-1")
      STRING(SUBSTRING ${comp} ${s} ${l} version)

      SET(OrgName_${name}_INCLUDE_DIR "/usr/local/include/OrgName/${comp}/")
      find_library(OrgName_${comp}_LIBRARIES NAMES "${comp}" HINTS "/usr/lib")

      IF(OrgName_${comp}_INCLUDE_DIR AND OrgName_${comp}_LIBRARIES)
         SET(OrgName_${comp}_FOUND 1)
      ENDIF()

      IF(NOT OrgName_${comp}_FOUND AND OrgName_FIND_REQUIRED_${comp})
         MESSAGE(FATAL_ERROR "OrgName ${comp} not available.")
      ENDIF()

      SET (OrgName_INCLUDE_DIR ${OrgName_INCLUDE_DIR} ${OrgName_${comp}_INCLUDE_DIR})
      SET (OrgName_LIBRARIES ${OrgName_LIBRARIES} ${OrgName_${comp}_LIBRARIES})
  ENDFOREACH()
ENDIF()
Now here is the problem, imagine someone didn't enter the version part in components names. I want to search for the versions which he has installed (assume the path to library is always the same) and use the last version it can find, so I have to search for the files which their name contains the library name (${name}). First of all how can I do this? Second, Am I doing things right? I mean is there an easier way to do this task? 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150608/3431097c/attachment-0001.html>


More information about the CMake mailing list