Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake extract substring from variable

Tags:

cmake

I have variable CC_OPTIONS has value set like below

-arch arm64 -mcpu=abc1 -c --debug -O2 -static -fstack-protector -ffreestanding -nostartfiles -std=c11

I wanted to extract -mcpu=abc1 from CC_OPTIONS

Tried below approach, but getting more than what i wanted.

string(REGEX REPLACE ".*mcpu=(.*)\ .*" "\\1" CPU_TYPE "${CC_OPTIONS}")

any suggestions?

like image 883
unblocker Avatar asked May 18 '26 18:05

unblocker


1 Answers

Like that:

cmake_minimum_required (VERSION 2.8.11)
project (HELLO)
set(CC_OPTIONS "-arch arm64 -mcpu=abc1 -c --debug -O2 -static -fstack-protector -ffreestanding -nostartfiles -std=c11")
message(${CC_OPTIONS})

string(REGEX MATCH "\\-mcpu=[^ $]+" CPU_TYPE ${CC_OPTIONS})
message(${CPU_TYPE})

Example:

$ cmake .
-arch arm64 -mcpu=abc1 -c --debug -O2 -static -fstack-protector -ffreestanding -nostartfiles -std=c11
-mcpu=abc1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ja/cmake
like image 190
Arkadiusz Drabczyk Avatar answered May 21 '26 19:05

Arkadiusz Drabczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!