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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With