Jepessen
Jepessen

Reputation: 12435

check if a string ends with a name in 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()

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

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

What can I do in order to fix the match?

Upvotes: 26

Views: 27764

Answers (2)

Tsyvarev
Tsyvarev

Reputation: 66088

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

CMAKE_SOURCE_DIR MATCHES "MyFolderName$"

CMake regular expressions are described here.

Upvotes: 40

zaufi
zaufi

Reputation: 7129

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.

Upvotes: 8

Related Questions