Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string to 3 variables in CMake

Tags:

I have a program version variable

set(MY_PROGRAM_VERSION "2.5.1") 

and I want to save 2,5,1 to 3 different variables, like

MY_PROGRAM_VERSION_MAJOR=2 MY_PROGRAM_VERSION_MINOR=5 MY_PROGRAM_VERSION_PATCH=1 

But I really don't know how to access single elements in a CMake list. Some idea?

like image 354
linello Avatar asked Sep 06 '13 12:09

linello


People also ask

How does CMake distinguish between lists and plain strings?

It's important to know how CMake distinguishes between lists and plain strings. When you write: you create a string with the value "a b c". But when you write this line without quotes: You create a list of three items instead: "a", "b" and "c".

How to overwrite a cached variable in CMake?

In case a cached variable is already defined in the cache when CMake processes the respective line (e.g. when CMake is rerun), it is not altered. To overwrite the default, append FORCE as the last argument: set (my_global_overwritten_string "foo" CACHE STRING "this is overwritten each time CMake is run" FORCE)

How do I set a global variable in CMake?

Variables and the Global Variables Cache Mostly you will use "normal variables": set (VAR TRUE) set (VAR "main.cpp") set (VAR1 $ {VAR2}) But CMake does also know global "cached variables" (persisted in CMakeCache.txt).

Are CMake variables case-sensitive?

The simplicity of basic CMake variables belies the complexity of the full variable syntax. This page documents the various variable cases, with examples, and points out the pitfalls to avoid. Variable names are case-sensitive. Their values are of type string. The value of a variable is referenced via:


2 Answers

According to this and this i would guess you need to transform the string to something like a list literal and use GET on your new list:

cmake_minimum_required(VERSION 2.8)  set(MY_PROGRAM_VERSION "2.5.1")  string(REPLACE "." ";" VERSION_LIST ${MY_PROGRAM_VERSION}) list(GET VERSION_LIST 0 MY_PROGRAM_VERSION_MAJOR) list(GET VERSION_LIST 1 MY_PROGRAM_VERSION_MINOR) list(GET VERSION_LIST 2 MY_PROGRAM_VERSION_PATCH) 
like image 118
delbertooo Avatar answered Sep 20 '22 17:09

delbertooo


You can use the following helper function to automatically have the version component variables set up:

macro (setup_package_version_variables _packageName)     if (DEFINED ${_packageName}_VERSION)         string (REGEX MATCHALL "[0-9]+" _versionComponents "${${_packageName}_VERSION}")         list (LENGTH _versionComponents _len)         if (${_len} GREATER 0)             list(GET _versionComponents 0 ${_packageName}_VERSION_MAJOR)         endif()         if (${_len} GREATER 1)             list(GET _versionComponents 1 ${_packageName}_VERSION_MINOR)         endif()         if (${_len} GREATER 2)             list(GET _versionComponents 2 ${_packageName}_VERSION_PATCH)         endif()         if (${_len} GREATER 3)             list(GET _versionComponents 3 ${_packageName}_VERSION_TWEAK)         endif()         set (${_packageName}_VERSION_COUNT ${_len})     else()         set (${_packageName}_VERSION_COUNT 0)         set (${_packageName}_VERSION "")     endif() endmacro() 

The macro can be invoked in the following way:

set(MY_PROGRAM_VERSION "2.5.1") setup_package_version_variables(MY_PROGRAM) 

The macro also sets MY_PROGRAM_VERSION_COUNT to the number of version components and MY_PROGRAM_VERSION_TWEAK if the version number has 4 components (e.g., "2.5.1.0")

like image 27
sakra Avatar answered Sep 20 '22 17:09

sakra