CMake is an open-source platform that helps in the building of projects. It helps serve the purpose of an IDE by compiling the code provided to us as the script in the project. CMake specifies the build configuration in the projects to help developers generate native build files for various platforms. CMake simplifies the process of building complex projects across different environments. Like many other functions, CMake provides the list()
function for storing multiple values in a single instance. The list()
has semicolon-separated values and offers multiple functions that can be performed on it.
In this Answer, we’ll look at the possible function we can perform on the semicolon-separated lists.
The basic functions that the list()
function of CMake provides are categorized into four subcategories:
Reading
Searching
Modification
Ordering
It is important to store the original value of the list
somewhere else because we will be doing modifications to the data. Let’s discuss each of these subcategories and their respective functions in detail. Before we begin we need to define a list and assign values to the list. We use the set()
function for that and the message()
function to print the output:
set(list_instance "index1" "index2" "index3")message("list items: ${my_list}\n")
The reading subcommands of the list
include the following functions:
Length: We can calculate the length of the provided list using the command list(LENGTH <list> <out>)
where <list>
points to the list, and the output is stored in the output variable <out>
.
Get: We can print the element at a specific index using the command list(GET <list> <index>... <out>)
where <list>
points to the list, <index>
points to the index to print, and the output is stored in the output variable <out>
.
Join: We can join the contents of a list into a single comma-separated string using the command list(JOIN <list> <glue> <out>)
, it takes the value from <list>
, uses <glue>
as a delimiter and saves the output in <out>
variable.
Sublist: We can extract out a sublist from the given <list>
using list(SUBLIST <list> <begin> <length> <out>)
. We specify the beginning and length of the substring to be extracted as <begin>
and <length>
respectively and save it in <out>
.
The searching subcommands of the list
include the following functions:
Find: The find subcommand can locate the index of the given <list>
using list(FIND <list> <needle> <out>)
and store it in the <out>
.
The modification subcommands of the list
include the following functions:
Append: The append subcommands add one or more <element>
at the end of the <list>
using list(APPEND <list> <element>...)
.
Prepend: The prepend subcommands add one or more <element>
at the end of the <list>
using list(PREPEND <list> <element>...)
.
Insert: The insert subcommand adds one or more <element>
at the given <index>
using list(INSERT <list> <index> [<element>...])
.
Pop back: The pop back subcommand removes one <element>
from the end of the <list>
using list(POP_BACK <list> [<out>...])
and save it in the <out>
variable.
Pop front: The pop front subcommand removes one <element>
from the start of the <list>
using list(POP_FRONT <list> [<out>...])
and save it in the <out>
variable.
Remove at: The remove at subcommand the <element>
at <index>
from the <list>
using list(REMOVE_AT <list> <index>...)
.
Remove duplicates: The remove duplicates command traverses the <list>
and remove the duplicates from it using list(REMOVE_DUPLICATES <list>)
.
Transform: The transform subcommand list(TRANSFORM <list> <action> [<selector>] [OUTPUT_VARIABLE <out>])
performs a certain action to all elements of the <list>
or to the selected elements of the list using a <selector>
. The command '' stores the output in the ouput variable <out>
if specified, or else modifies the <list>
. The <actions>
that can be performed are APPEND <string>
, PREPEND <string>
, TOLOWER
, TOUPPER
, STRIP
, GENEX_STRIP
, and REPLACE <pattern> <expression>
. And the possible <selector>
are AT <index>
, FOR <start> <stop> [<step>]
, and REGEX <pattern>
.
The ordering subcommands of the list
include the following functions:
Reverse: The reverse subcommand reverses the elements of the <list>
using list(REVERSE <list>)
.
Sort: The sort subcommand sorts the elements of the <list>
using list(SORT <list>)
.
We have learned how CMake uses the data type List()
to hold a set of values. We can use these lists in various ways that fit our requirements.
Here's a simple demonstration of the list functions that one can perform. You can execute the commands on the list and observe the changes in the list.
cmake_minimum_required(VERSION 3.10) # Creating a list set(list_instance "index1" "index2" "index3") # Printing the list message("List: ${list_instance}") # Read # Get length of the list list(LENGTH list_instance length_list) message("Length of list: ${length_list}") # Extract at index 3 - 0 1 2 list(GET list_instance 2 item) message("Third index: ${item}") # Contatenate into a single string string(JOIN ", " conc_list ${list_instance}) message("Concatenated list: ${conc_list}") # Extract Sublist list(SUBLIST list_instance 0 2 sub_list) message("Sub list from index 1-2: ${sub_list}") # Searching #Find index of the item from the list list(FIND list_instance index1 index_located) message("\nThe index for item index1 is: ${index_located}") # Modification # Appending elements to the list list(APPEND list_instance "index4" "index5" "index6" "index1") message("\nList after appending: ${list_instance}") # Inserting an element at a specific index list(INSERT list_instance 0 "index0") message("List after insert at first index: ${list_instance}") # Removing an element from the list list(REMOVE_ITEM list_instance "index0") message("List after removing index0: ${list_instance}") # Removing all duplicates list(REMOVE_DUPLICATES list_instance) message("List after removing duplicates: ${list_instance}") # Popping element from end list(POP_BACK list_instance popback) message("List after popping back: ${list_instance}") # Popping element from start list(POP_FRONT list_instance popfront) message("List after popping front: ${list_instance}") # Transforming to upper case list(TRANSFORM list_instance TOUPPER) message("List after transforming to upper case: ${list_instance}") # Ordering # Reversing the list list(REVERSE list_instance) message("\nList after reversing: ${list_instance}") # Sorting the list list(SORT list_instance) message("List after sorting: ${list_instance}")
To sum up, the CMake List() command describes a basic function used to work with lists in CMake scripts. It provides basic features like appending items to lists, getting things out of lists, determining the size of lists, and more. The List() command is essential to CMake’s variable and data collection management, making list organization and manipulation more efficient. CMake scripts can manage complicated setups and dependencies, and build processes more efficiently by utilizing this command, resulting in reliable and maintainable build systems for various platforms and applications.
Free Resources