Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a string ends with a name in CMake

Tags:

regex

cmake

I want to check if the CMAKE_SOURCE_DIR variable ends with a specific name. I need to use MATCHES for it, but it does not seems to work.

I've written:

IF(CMAKE_SOURCE_DIR MATCHES "*MyFolderName")
# code
ENDIF(CMAKE_SOURCE_DIR MATCHES "*MyFolderName")

But it does not work. I obtain the following error:

RegularExpression::compile(): ?+* follows nothing.
RegularExpression::compile(): Error in compile

Want can I do in order to fix the match?

like image 663
Jepessen Avatar asked Feb 15 '18 10:02

Jepessen


2 Answers

Usually, in regular expressions "*" means "repeat preceding zero or more times". CMake is not exception. For match at the end of string, use $:

CMAKE_SOURCE_DIR MATCHES "MyFolderName$"

CMake regular expressions are described here.

like image 93
Tsyvarev Avatar answered Oct 12 '22 13:10

Tsyvarev


MATCH require a regular expression, not a "file glob" (wildcard).

if(CMAKE_SOURCE_DIR MATCHES ".*MyFolderName")
   # ...
endif()

or even try this: .*/MyFolderName. But it also would match /some/MyFolderName/path... Use .*/MyFolderName$ to match the last path name component.

like image 25
zaufi Avatar answered Oct 12 '22 12:10

zaufi