StringManip

Operations on strings. It requires CMake 3.20 or newer.

Synopsis

string_manip(SPLIT <string> <output_list_var>)
string_manip(SPLIT_TRANSFORM <string_var> <ACTION> [OUTPUT_VARIABLE <output_var>])
string_manip(STRIP_INTERFACES <string_var> [OUTPUT_VARIABLE <output_var>])
string_manip(EXTRACT_INTERFACE <string_var> <BUILD|INSTALL> [OUTPUT_VARIABLE <output_var>])

Usage

string_manip(SPLIT <string> <output_list_var>)

Splits the input string into a list of substrings based on specific pattern rules. This command analyzes the given <string> and splits it into components using the following criteria:

  • Transitions between lowercase and uppercase letters (e.g., MyValue becomes My;Value).

  • Non-alphanumeric characters, as defined by the string(MAKE_C_IDENTIFIER) transformation in CMake.

The resulting list is stored in <output_list_var>. If no split point is detected, the original string is returned as a single-element list.

Example usage:

# No split point detected
string_manip(SPLIT "mystringtosplit" output)
# output is "mystringtosplit"
string_manip(SPLIT "my1string2to3split" output)
# output is:
#   my1string2to3split

# Split on uppercase
string_manip(SPLIT "myStringToSplit" output)
# output is:
#   my;String;To;Split

# Split on non-alphanumeric
string_manip(SPLIT "my-string/to*split" output)
# output is:
#   my;string;to;split

# Split on multiple criteria
string_manip(SPLIT "myString_to*Split" output)
# output is:
#   my;String;to;Split
string_manip(SPLIT_TRANSFORM <string_var> <ACTION> [OUTPUT_VARIABLE <output_var>])

Applies the string_manip(SPLIT) operation to the value stored in <string_var>, transforms each resulting element according to the specified <ACTION>, then joins the list into a single string. The final result is either stored back in <string_var>, or in <output_var> if the OUTPUT_VARIABLE option is provided.

The available values for <ACTION> are:

START_CASE

Converts each word to Start Case (first letter uppercase, others lowercase).

C_IDENTIFIER_UPPER

Applies a transformation inspired by string(MAKE_C_IDENTIFIER): each word is converted to uppercase and suffixed with an underscore. If the first character is a digit, an underscore is also prepended to the result.

Example of transformations:

Input

Action

Output

"myVariableName"

START_CASE

"MyVariableName"

"myVariableName"

C_IDENTIFIER_UPPER

"MY_VARIABLE_NAME_" (joined string)

If no split points are detected, the input is treated as a single-element list and transformed accordingly.

string_manip(STRIP_INTERFACES <string_var> [OUTPUT_VARIABLE <output_var>])

Removes CMake generator expressions of the form $<BUILD_INTERFACE:...> and $<INSTALL_INTERFACE:...> from the value stored in <string_var>. The expressions are removed entirely, including any leading semicolon if present before the expression.

The resulting string is either stored back in <string_var>, or in <output_var> if the OUTPUT_VARIABLE option is provided.

string_manip(EXTRACT_INTERFACE <string_var> <BUILD|INSTALL> [OUTPUT_VARIABLE <output_var>])

Extracts the content of either $<BUILD_INTERFACE:...> or $<INSTALL_INTERFACE:...> generator expressions from the value stored in <string_var>, depending on the specified mode.

The value of <string_var> can be either a single string or a semicolon-separated list of strings. Generator expressions may be split across multiple list elements.

The <BUILD|INSTALL> argument selects which generator expression to extract:

  • BUILD: Extracts the content of all $<BUILD_INTERFACE:...> expressions.

  • INSTALL: Extracts the content of all $<INSTALL_INTERFACE:...> expressions.

When multiple matching generator expressions are found, their contents are concatenated into a single semicolon-separated string.

The result is stored in <output_var> if the OUTPUT_VARIABLE option is specified. Otherwise, <string_var> is updated in place. If no matching expression is found, an empty string is returned.

Example usage:

# Case 1: Extract from a single BUILD_INTERFACE expression in place
set(value_1 "file1.h;$<BUILD_INTERFACE:file2.h;file3.h>;file4.h")
string_manip(EXTRACT_INTERFACE value_1 BUILD)
# output is:
#   file2.h;file3.h

# Case 2: Extract from a single INSTALL_INTERFACE expression in place
set(value_1 "file5.h;$<INSTALL_INTERFACE:file6.h;file7.h>;file8.h")
string_manip(EXTRACT_INTERFACE value_1 INSTALL)
# output is:
#    file6.h;file7.h

# Case 3: Multiple expressions (BUILD + INSTALL), extract only BUILD
set(value_3 "file1.h;$<BUILD_INTERFACE:file2.h;file3.h>;file4.h;file5.h;$<INSTALL_INTERFACE:file6.h;file7.h>;file8.h")
string_manip(EXTRACT_INTERFACE value_3 BUILD)
# output is:
#   file2.h;file3.h