Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake regex match directories in list

Tags:

regex

list

cmake

So I have list of files (or values) like this

set(HEADER_FILES DirA/header1.h DirA/header2.hpp
                DirB/header3.h DirB/stuff.hpp)

Then how do I get sub list of files only in DirA ? I'm using CMake 2.8.x and I tired regex match:

string(REGEX MATCHALL "DirA/(.+)" DirA_FILES ${HEADER_FILES})

But result is just copy of the orginal string like: "DirA/header1.h DirA/header2.hpp DirB/header3.h DirB/stuff.hpp" or nothing.

Well again as always, while writing this question I solved it:

set(SubListMatch)
foreach(ITR ${HEADER_FILES})
    if(ITR MATCHES "(.*)DirA/(.*)")
        list(APPEND SubListMatch ${ITR})
    endif()
endforeach()

But CMake is quite new thing to me so how do I wrap that code into function? I have never written any CMake functions so far.

like image 417
JATothrim Avatar asked Mar 22 '13 15:03

JATothrim


1 Answers

You can turn it into a function like this:

# Define function
function(GetSubList resultVar)
  set(result)
  foreach(ITR ${ARGN})  # ARGN holds all arguments to function after last named one
    if(ITR MATCHES "(.*)DirA/(.*)")
      list(APPEND result ${ITR})
    endif()
  endforeach()
  set(${resultVar} ${result} PARENT_SCOPE)
endfunction()

# Call it
GetSubList(SubListMatch ${HEADER_FILES})

When this code runs, SubListMatch will hold the matched elements.

You could improve the function by e.g. giving it an extra parameter for the directory name, so that others than DirA can be matched for:

function(GetSubList resultVar dirName)
  # as before, except the if()
    if(ITR MATCHES "(.*)${dirName}/(.*)")
  # same as before
endfunction()
like image 106
Angew is no longer proud of SO Avatar answered Nov 07 '22 07:11

Angew is no longer proud of SO