Reader small image

You're reading from  Modern CMake for C++

Product typeBook
Published inFeb 2022
PublisherPackt
ISBN-139781801070058
Edition1st Edition
Tools
Right arrow
Author (1)
Rafał Świdziński
Rafał Świdziński
author image
Rafał Świdziński

Rafał Świdziński works as a staff engineer at Google. With over 10 years of professional experience as a full stack developer, he has been able to experiment with a vast multitude of programming languages and technologies. During this time, he has been building software under his own company and for corporations including Cisco Meraki, Amazon, and Ericsson. Originally from Łódź, Poland, he now lives in London, UK, from where he runs a YouTube channel, "Smok," discussing topics related to software development. He tackles technical problems, including real-life and work-related challenges encountered by many people in the field. Throughout his work, he explains the technical concepts in detail and demystifies the art and science behind the role of software engineer. His primary focus is on high-quality code and the craftsmanship of programming.
Read more about Rafał Świdziński

Right arrow

Appendix: Miscellaneous Commands

Every language has utility commands that come in handy on a myriad of occasions. CMake is no different in that matter: it provides tools for simple arithmetic, bitwise operations, string manipulations, and operations on lists and on files. Interestingly enough, the occasions when they are necessary are relatively rare (thanks to all the enhancements and modules written over the years), but can still be required in more automated projects.

Hence this appendix, which is a short summary of miscellaneous commands and their multiple modes. Treat this as a handy offline reference or a simplified version of the official documentation. If you need more information, visit the provided links.

In this chapter, we're going to cover the following main topics:

  • The string() command
  • The list() command
  • The file() command
  • The math() command

The string() command

The string() command is used to manipulate strings. It comes with a variety of modes that perform different actions on the string: search and replace, manipulation, comparison, hashing, generation, and JSON operations (the last one available since CMake 3.19).

Full details can be found in the online documentation: https://cmake.org/cmake/help/latest/command/string.html.

string() modes that accept the <input> argument will accept multiple <input> values and concatenate them before the execution of the command:

string(PREPEND myVariable "a" "b" "c")

This is the equivalent of the following:

string(PREPEND myVariable "abc")

Let's explore all available string() modes.

Search and replace

The following modes are available:

  • string(FIND <haystack> <pattern> <out> [REVERSE]) searches for <pattern> in the <haystack> string and writes the position found as...

The list() command

This command provides basic operations on lists: reading, searching, modification, and ordering. Some modes will change list (mutate the original value). Be sure to copy the original value if you'll need it later.

Full details can be found in the online documentation:

https://cmake.org/cmake/help/latest/command/list.html

Reading

The following modes are available:

  • list(LENGTH <list> <out>) counts the elements in the <list> variable and stores the result in the <out> variable.
  • list(GET <list> <index>... <out>) copies the <list> elements specified with the list of <index> indexes to the <out> variable.
  • list(JOIN <list> <glue> <out>) interleaves <list> elements with the <glue> delimiter and stores the resulting string in the <out> variable.
  • list(SUBLIST <list> <begin> <length> <out>) works like the GET mode, but...

The file() command

This command provides all kinds of operations related to files: reading, transferring, locking, and archiving. It also provides modes to inspect the filesystem and operations on strings representing paths.

Full details can be found in the online documentation:

https://cmake.org/cmake/help/latest/command/file.html

Reading

The following modes are available:

  • file(READ <filename> <out> [OFFSET <o>] [LIMIT <max>] [HEX]) reads the file from <filename> to the <out> variable. The read optionally starts at offset <o> and follows the optional limit of <max> bytes. The HEX flag specifies that output should be converted to hexadecimal representation.
  • file(STRINGS <filename> <out>) reads strings from the file at <filename> to the <out> variable.
  • file(<algorithm> <filename> <out>) computes the <algorithm> hash from the file at <filename> and stores...

The math() command

CMake also supports some simple arithmetical operations. See the online documentation for full details:

https://cmake.org/cmake/help/latest/command/math.html

To evaluate a mathematical expression and store it in the <out> variable as the string in an optional <format> (HEXADECIMAL or DECIMAL), use the following signature:

math(EXPR <out> "<expression>" [OUTPUT_FORMAT <format>])

The <expression> value is a string that supports operators present in C code (they have the same meaning here):

  • Arithmetical: +, -, *, /, % modulo division
  • Bitwise: | or, & and, ^ xor, ~ not, << shift left, >> shift right
  • Parenthesis (...)

Constant values can be provided in decimal or hexadecimal format.

Why subscribe?

  • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
  • Improve your learning with Skill Plans built especially for you
  • Get a free eBook or video every month
  • Fully searchable for easy access to vital information
  • Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Modern CMake for C++
Published in: Feb 2022Publisher: PacktISBN-13: 9781801070058
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Rafał Świdziński

Rafał Świdziński works as a staff engineer at Google. With over 10 years of professional experience as a full stack developer, he has been able to experiment with a vast multitude of programming languages and technologies. During this time, he has been building software under his own company and for corporations including Cisco Meraki, Amazon, and Ericsson. Originally from Łódź, Poland, he now lives in London, UK, from where he runs a YouTube channel, "Smok," discussing topics related to software development. He tackles technical problems, including real-life and work-related challenges encountered by many people in the field. Throughout his work, he explains the technical concepts in detail and demystifies the art and science behind the role of software engineer. His primary focus is on high-quality code and the craftsmanship of programming.
Read more about Rafał Świdziński