blob: 6710edbbd0968ba6c9458a00372c3309f4f945b5 [file] [log] [blame]
Chris Kay908f5be2021-07-22 16:39:33 +01001#[=======================================================================[.rst:
2ArmExpand
3---------
4
5.. default-domain:: cmake
6
7.. command:: arm_expand
8
9Force expansion of variables in a string.
10
11.. code-block:: cmake
12
13 arm_expand(OUTPUT <output> STRING <string> [ATONLY])
14
15This function scans the input string ``<string>`` for CMake-style and @-style
16variables and expands them into their current values, writing the result to
17``<output>``. If ``ATONLY`` is specified, variable replacement is restricted to
18references of the form ``@VAR@``.
19
20.. code-block:: cmake
21 :caption: Example usage
22 :linenos:
23
24 set(expand-me "@CMAKE_CURRENT_SOURCE_DIR@")
25
26 arm_expand(OUTPUT expanded STRING "${expand-me}")
27#]=======================================================================]
28
29include_guard()
30
31include(ArmAssert)
32
33function(arm_expand)
34 set(options ATONLY)
35 set(single-args OUTPUT STRING)
36 set(multi-args)
37
38 cmake_parse_arguments(PARSE_ARGV 0 ARG
39 "${options}" "${single-args}" "${multi-args}")
40
41 arm_assert(
42 CONDITION DEFINED ARG_OUTPUT
43 MESSAGE "No value was given for the `OUTPUT` argument.")
44
45 set(atonly-regex [[@([^@]+)@]])
46 set(brace-regex [[\${([^}]+)}]])
47
48 string(REGEX MATCH "${atonly-regex}" match "${ARG_STRING}")
49
50 while(match)
51 string(REPLACE "@${CMAKE_MATCH_1}@" "${${CMAKE_MATCH_1}}"
52 ARG_STRING "${ARG_STRING}")
53 string(REGEX MATCH ${atonly-regex} match "${ARG_STRING}")
54 endwhile()
55
56 if(NOT ARG_ATONLY)
57 string(REGEX MATCH "${brace-regex}" match "${ARG_STRING}")
58
59 while(match)
60 string(REPLACE "\${${CMAKE_MATCH_1}}" "${${CMAKE_MATCH_1}}"
61 ARG_STRING "${ARG_STRING}")
62 string(REGEX MATCH "${brace-regex}" match "${ARG_STRING}")
63 endwhile()
64 endif()
65
66 set(${ARG_OUTPUT} "${ARG_STRING}" PARENT_SCOPE)
67endfunction()