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?
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".
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)
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).
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:
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)
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")
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