|
| 1 | +--- |
| 2 | +title: Dump |
| 3 | +description: How to dump variables. |
| 4 | +keywords: debug, debugging, dump, dd, var_dump, cmake |
| 5 | +author: RSP Systems A/S |
| 6 | +--- |
| 7 | + |
| 8 | +# Dump Variables |
| 9 | + |
| 10 | +[TOC] |
| 11 | + |
| 12 | +## `dump()` |
| 13 | + |
| 14 | +Outputs the names and values of given variables. |
| 15 | +Behind the scene, [`var_dump()`](#var_dump) is used. |
| 16 | + |
| 17 | +```cmake |
| 18 | +set(build_assets true) |
| 19 | +set(assets_dir "/home/code/my-project/resources") |
| 20 | +
|
| 21 | +dump( |
| 22 | + build_assets |
| 23 | + assets_dir |
| 24 | +) |
| 25 | +
|
| 26 | +message("Onward...") |
| 27 | +``` |
| 28 | + |
| 29 | +The above will output a [`WARNING`](https://cmake.org/cmake/help/latest/command/message.html#general-messages) message, |
| 30 | +and continue to process your cmake script: |
| 31 | + |
| 32 | +```txt |
| 33 | +CMake Warning at cmake/rsp/debug.cmake:31 (message): |
| 34 | +dump: |
| 35 | +
|
| 36 | + build_assets = (bool) true |
| 37 | + assets_dir = (string 31) "/home/code/my-project/resources" |
| 38 | +Call Stack (most recent call first): |
| 39 | + CMakeLists.txt:132 (dump) |
| 40 | +
|
| 41 | +Onward... |
| 42 | +``` |
| 43 | + |
| 44 | +## `dd()` |
| 45 | + |
| 46 | +Outputs the names and values of given variables, and stops the build (_dump and die_). |
| 47 | +Behind the scene, [`var_dump()`](#var_dump) is used. |
| 48 | + |
| 49 | +```cmake |
| 50 | +set(build_assets true) |
| 51 | +set(assets_dir "/home/code/my-project/resources") |
| 52 | +
|
| 53 | +dd( |
| 54 | + build_assets |
| 55 | + assets_dir |
| 56 | +) |
| 57 | +
|
| 58 | +# This message will never be reached! |
| 59 | +message("Onward...") |
| 60 | +``` |
| 61 | + |
| 62 | +The `dd()` function will output using cmake's [`FATAL_ERROR`](https://cmake.org/cmake/help/latest/command/message.html#general-messages) |
| 63 | +message mode. Your cmake script will not continue to be processed: |
| 64 | + |
| 65 | +```txt |
| 66 | +CMake Error at cmake/rsp/debug.cmake:54 (message): |
| 67 | +dd: |
| 68 | +
|
| 69 | + build_assets = (bool) true |
| 70 | + assets_dir = (string 31) "/home/code/my-project/resources" |
| 71 | +Call Stack (most recent call first): |
| 72 | + CMakeLists.txt:132 (dd) |
| 73 | +``` |
| 74 | + |
| 75 | +## `var_dump()` |
| 76 | + |
| 77 | +Outputs human-readable information about given properties. |
| 78 | +It accepts the following parameters: |
| 79 | + |
| 80 | +* `PROPERTIES`: _The variables to be dumped._ |
| 81 | +* `OUTPUT`: (_optional_), _output variable. If specified, message is assigned to variable, instead of being printed to `stdout` or `stderr`._ |
| 82 | +* `WITHOUT_NAMES`: (_option_), _Output information without the variable names._ |
| 83 | +* `IGNORE_LIST`: (_option_), _Variables of the type "list" are treated as "string" type instead._ |
| 84 | + |
| 85 | +!!! info "Note" |
| 86 | + Unless `OUTPUT` is specified, `var_dump()` will output using cmake's [`NOTICE`](https://cmake.org/cmake/help/latest/command/message.html#general-messages) |
| 87 | + message mode. |
| 88 | + |
| 89 | +```cmake |
| 90 | +set(my_str "Hi there") |
| 91 | +set(my_empty_str "") |
| 92 | +set(my_num 23) |
| 93 | +set(my_float 1.1234) |
| 94 | +set(my_bool true) |
| 95 | +set(my_const on) |
| 96 | +set(my_list "foo;bar;42;true") |
| 97 | +set(my_cached_prop "My cached var..." CACHE STRING "Testing") |
| 98 | +set(ENV{my_env_prop} "/home/other/place") |
| 99 | +
|
| 100 | +macro(my_macro) |
| 101 | +endmacro() |
| 102 | +
|
| 103 | +function(my_function) |
| 104 | +endfunction() |
| 105 | +
|
| 106 | +var_dump(PROPERTIES |
| 107 | + my_str |
| 108 | + my_empty_str |
| 109 | + my_num |
| 110 | + my_float |
| 111 | + my_const |
| 112 | + my_undefined_prop |
| 113 | + my_list |
| 114 | + my_cached_prop |
| 115 | + my_env_prop |
| 116 | + my_macro |
| 117 | + my_function |
| 118 | +) |
| 119 | +``` |
| 120 | + |
| 121 | +The above will output: |
| 122 | + |
| 123 | +```txt |
| 124 | +my_str = (string 8) "Hi there" |
| 125 | +my_empty_str = (string 0) "" |
| 126 | +my_num = (int) 23 |
| 127 | +my_float = (float) 1.1234 |
| 128 | +my_const = (string 2) "on" |
| 129 | +my_undefined_prop = (undefined) |
| 130 | +my_list = (list 4) [ |
| 131 | + 0: (string 3) "foo" |
| 132 | + 1: (string 3) "bar" |
| 133 | + 2: (int) 42 |
| 134 | + 3: (bool) true |
| 135 | +] |
| 136 | +my_cached_prop = (string 4, cached) "Yolo" |
| 137 | +my_env_prop = (string 17, ENV) "/home/other/place" |
| 138 | +my_macro = (command) my_macro() |
| 139 | +my_function = (command) my_function() |
| 140 | +``` |
| 141 | + |
| 142 | +### Without Names |
| 143 | + |
| 144 | +If the `WITHOUT_NAMES` option is set, then variable names are not part of the output. |
| 145 | + |
| 146 | +```cmake |
| 147 | +var_dump( |
| 148 | + WITHOUT_NAMES |
| 149 | + PROPERTIES |
| 150 | + my_str |
| 151 | +) |
| 152 | +``` |
| 153 | + |
| 154 | +Outputs: |
| 155 | + |
| 156 | +```txt |
| 157 | +(string 8) "Hi there" |
| 158 | +``` |
| 159 | + |
| 160 | +### Ignore List |
| 161 | + |
| 162 | +By default, `var_dump()` will attempt to parse any list variable and output each item on a new line. |
| 163 | +To disable this behaviour, set the `IGNORE_LIST` option. When doing so, lists are treated as a regular string. |
| 164 | + |
| 165 | +**Default behaviour** |
| 166 | + |
| 167 | +```cmake |
| 168 | +var_dump( |
| 169 | + PROPERTIES |
| 170 | + my_list |
| 171 | +) |
| 172 | +``` |
| 173 | + |
| 174 | +Outputs: |
| 175 | + |
| 176 | +```txt |
| 177 | +my_list = (list 4) [ |
| 178 | + 0: (string 3) "foo" |
| 179 | + 1: (string 3) "bar" |
| 180 | + 2: (int) 42 |
| 181 | + 3: (bool) true |
| 182 | +] |
| 183 | +``` |
| 184 | + |
| 185 | +**With ignore list option** |
| 186 | + |
| 187 | +```cmake |
| 188 | +var_dump( |
| 189 | + IGNORE_LIST |
| 190 | + PROPERTIES |
| 191 | + my_list |
| 192 | +) |
| 193 | +``` |
| 194 | + |
| 195 | +Outputs: |
| 196 | + |
| 197 | +```txt |
| 198 | +my_list = (string 15) "foo;bar;42;true" |
| 199 | +``` |
0 commit comments