StringManip¶
Operations on strings. It requires CMake 4.0.1 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.,
MyValuebecomesMy;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 theOUTPUT_VARIABLEoption is provided.The available values for
<ACTION>are:START_CASEConverts each word to Start Case (first letter uppercase, others lowercase).
C_IDENTIFIER_UPPERApplies 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
myVariableNameSTART_CASEMyVariableNamemy_variable_nameSTART_CASEMyVariableNamemyVariableNameC_IDENTIFIER_UPPERMY_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 theOUTPUT_VARIABLEoption is provided.Example usage:
set(input "libA;$<BUILD_INTERFACE:src>;libB;libC;$<INSTALL_INTERFACE:include/>libD") string_manip(STRIP_INTERFACES input) # input is: # libA;libB;libClibD
- 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 theOUTPUT_VARIABLEoption 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) # value_1 is: # file2.h;file3.h # Case 2: Extract from a single INSTALL_INTERFACE expression in place set(value_2 "file5.h;$<INSTALL_INTERFACE:file6.h;file7.h>;file8.h") string_manip(EXTRACT_INTERFACE value_2 INSTALL) # value_2 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) # value_3 is: # file2.h;file3.h