aboutsummaryrefslogtreecommitdiff
path: root/pw_bloat/bloat.gni
blob: 432358c1ccb37f126d2b881b26e81569c908db5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright 2019 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/python_action.gni")

declare_args() {
  # Path to the Bloaty configuration file that defines the memory layout and
  # capacities for the target binaries.
  pw_bloat_BLOATY_CONFIG = ""

  # List of toolchains to use in pw_toolchain_size_report templates.
  #
  # Each entry is a scope containing the following variables:
  #
  #   name: Human-readable toolchain name.
  #   target: GN target that defines the toolchain.
  #   linker_script: Optional path to a linker script file to build for the
  #     toolchain's target.
  #   bloaty_config: Optional Bloaty confirugation file defining the memory
  #     layout of the binaries as specified in the linker script.
  #
  # If this list is empty, pw_toolchain_size_report targets become no-ops.
  pw_bloat_TOOLCHAINS = []
}

# Creates a target which runs a size report diff on a set of executables.
#
# Args:
#   base: The default base executable target to run the diff against. May be
#     omitted if all binaries provide their own base.
#   binaries: List of executables to compare in the diff.
#     Each binary in the list is a scope containing up to three variables:
#       label: Descriptive name for the executable. Required.
#       target: Build target for the executable. Required.
#       base: Optional base diff target. Overrides global base argument.
#   source_filter: Optional regex to filter data source names in Bloaty.
#   title: Optional title string to display with the size report.
#   full_report: Optional boolean flag indicating whether to produce a full
#     symbol size breakdown or a summary.
#
# Example:
#   pw_size_report("foo_bloat") {
#     base = ":foo_base"
#     binaries = [
#       {
#         target = ":foo_static"
#         label = "Static"
#       },
#       {
#         target = ":foo_dynamic"
#         label = "Dynamic"
#       },
#     ]
#     title = "static vs. dynamic foo"
#   }
#
template("pw_size_report") {
  if (pw_bloat_BLOATY_CONFIG != "") {
    if (defined(invoker.base)) {
      _global_base = invoker.base
      _all_target_dependencies = [ _global_base ]
    } else {
      _all_target_dependencies = []
    }

    if (defined(invoker.title)) {
      _title = invoker.title
    } else {
      _title = target_name
    }

    # This template creates an action which invokes a Python script to run a
    # size report on each of the provided targets. Each of the targets is listed
    # as a dependency of the action so that the report gets updated when
    # anything is changed. Most of the code below builds the command-line
    # arguments to pass each of the targets into the script.

    _binary_paths = []
    _binary_labels = []
    _bloaty_configs = []

    # Process each of the binaries, resolving their full output paths and
    # building them into a list of command-line arguments to the bloat script.
    foreach(binary, invoker.binaries) {
      assert(defined(binary.label) && defined(binary.target),
             "Size report binaries must define 'label' and 'target' variables")
      _all_target_dependencies += [ binary.target ]

      _binary_path = "<TARGET_FILE(${binary.target})>"

      # If the binary defines its own base, use that instead of the global base.
      if (defined(binary.base)) {
        _binary_base = binary.base
        _all_target_dependencies += [ _binary_base ]
      } else if (defined(_global_base)) {
        _binary_base = _global_base
      } else {
        assert(false, "pw_size_report requires a 'base' file")
      }

      # Allow each binary to override the global bloaty config.
      if (defined(binary.bloaty_config)) {
        _bloaty_configs += [ rebase_path(binary.bloaty_config) ]
      } else {
        _bloaty_configs += [ rebase_path(pw_bloat_BLOATY_CONFIG) ]
      }

      _binary_path += ";" + "<TARGET_FILE($_binary_base)>"

      _binary_paths += [ _binary_path ]
      _binary_labels += [ binary.label ]
    }

    _bloat_script_args = [
      "--bloaty-config",
      string_join(";", _bloaty_configs),
      "--out-dir",
      rebase_path(target_gen_dir),
      "--target",
      target_name,
      "--title",
      _title,
      "--labels",
      string_join(";", _binary_labels),
    ]

    if (defined(invoker.full_report) && invoker.full_report) {
      _bloat_script_args += [ "--full" ]
    }

    if (defined(invoker.source_filter)) {
      _bloat_script_args += [
        "--source-filter",
        invoker.source_filter,
      ]
    }

    _doc_rst_output = "$target_gen_dir/${target_name}"

    if (host_os == "win") {
      # Bloaty is not yet packaged for Windows systems; display a message
      # indicating this.
      not_needed("*")
      not_needed(invoker, "*")

      pw_python_action(target_name) {
        metadata = {
          pw_doc_sources = rebase_path([ _doc_rst_output ], root_build_dir)
        }
        script = "$dir_pw_bloat/py/pw_bloat/no_bloaty.py"
        python_deps = [ "$dir_pw_bloat/py" ]
        args = [ rebase_path(_doc_rst_output) ]
        outputs = [ _doc_rst_output ]
      }

      group(target_name + "_UNUSED_DEPS") {
        deps = _all_target_dependencies
      }
    } else {
      # Create an action which runs the size report script on the provided
      # targets.
      pw_python_action(target_name) {
        metadata = {
          pw_doc_sources = rebase_path([ _doc_rst_output ], root_build_dir)
        }
        script = "$dir_pw_bloat/py/pw_bloat/bloat.py"
        python_deps = [ "$dir_pw_bloat/py" ]
        inputs = _bloaty_configs
        outputs = [
          "$target_gen_dir/${target_name}.txt",
          _doc_rst_output,
        ]
        deps = _all_target_dependencies
        args = _bloat_script_args + _binary_paths

        # Print size reports to stdout when they are generated.
        capture_output = false
      }
    }
  } else {
    not_needed(invoker, "*")
    group(target_name) {
    }
  }
}

# Creates a report card comparing the sizes of the same binary compiled with
# different toolchains. The toolchains to use are listed in the build variable
# pw_bloat_TOOLCHAINS.
#
# Args:
#   base_executable: Scope containing a list of variables defining an executable
#     target for the size report base.
#   diff_executable: Scope containing a list of variables defining an executable
#     target for the size report comparison.
#
# Outputs:
#   $target_gen_dir/$target_name.txt
#   $target_gen_dir/$target_name.rst
#
# Example:
#
#   pw_toolchain_size_report("my_size_report") {
#     base_executable = {
#       sources = [ "base.cc" ]
#     }
#
#     diff_executable = {
#       sources = [ "base_with_libfoo.cc" ]
#       deps = [ ":libfoo" ]
#     }
#   }
#
template("pw_toolchain_size_report") {
  assert(defined(invoker.base_executable),
         "pw_toolchain_size_report requires a base_executable")
  assert(defined(invoker.diff_executable),
         "pw_toolchain_size_report requires a diff_executable")

  _size_report_binaries = []

  # Multiple build targets are created for each toolchain, which all need unique
  # target names, so throw a counter in there.
  i = 0

  # Create a base and diff executable for each toolchain, adding the toolchain's
  # linker script to the link flags for the executable, and add them all to a
  # list of binaries for the pw_size_report template.
  foreach(_toolchain, pw_bloat_TOOLCHAINS) {
    _prefix = "_${target_name}_${i}_pw_size"

    # Create a config which adds the toolchain's linker script as a linker flag
    # if the toolchain provides one.
    _linker_script_target_name = "${_prefix}_linker_script"
    config(_linker_script_target_name) {
      if (defined(_toolchain.linker_script)) {
        ldflags = [ "-T" + rebase_path(_toolchain.linker_script) ]
        inputs = [ _toolchain.linker_script ]
      } else {
        ldflags = []
      }
    }

    # Create a group which forces the linker script config its dependents.
    _linker_group_target_name = "${_prefix}_linker_group"
    group(_linker_group_target_name) {
      public_configs = [ ":$_linker_script_target_name" ]
    }

    # Define the size report base executable with the toolchain's linker script.
    _base_target_name = "${_prefix}_base"
    executable(_base_target_name) {
      forward_variables_from(invoker.base_executable, "*")
      if (!defined(deps)) {
        deps = []
      }
      deps += [ ":$_linker_group_target_name" ]
    }

    # Define the size report diff executable with the toolchain's linker script.
    _diff_target_name = "${_prefix}_diff"
    executable(_diff_target_name) {
      forward_variables_from(invoker.diff_executable, "*")
      if (!defined(deps)) {
        deps = []
      }
      deps += [ ":$_linker_group_target_name" ]
    }

    # Force compilation with the toolchain.
    _base_label = get_label_info(":$_base_target_name", "label_no_toolchain")
    _base_with_toolchain = "$_base_label(${_toolchain.target})"
    _diff_label = get_label_info(":$_diff_target_name", "label_no_toolchain")
    _diff_with_toolchain = "$_diff_label(${_toolchain.target})"

    # Append a pw_size_report binary scope to the list comparing the toolchain's
    # diff and base executables.
    _size_report_binaries += [
      {
        base = _base_with_toolchain
        target = _diff_with_toolchain
        label = _toolchain.name

        if (defined(_toolchain.bloaty_config)) {
          bloaty_config = _toolchain.bloaty_config
        }
      },
    ]

    i += 1
  }

  # TODO(frolv): Have a way of indicating that a toolchain should build docs.
  if (current_toolchain == default_toolchain && _size_report_binaries != []) {
    # Create the size report which runs on the binaries.
    pw_size_report(target_name) {
      forward_variables_from(invoker, [ "title" ])
      binaries = _size_report_binaries
    }
  } else {
    # If no toolchains are listed in pw_bloat_TOOLCHAINS, prevent GN from
    # complaining about unused variables and run a script that outputs a ReST
    # warning to the size report file.
    not_needed("*")
    not_needed(invoker, "*")

    _doc_rst_output = "$target_gen_dir/$target_name"
    pw_python_action(target_name) {
      metadata = {
        pw_doc_sources = rebase_path([ _doc_rst_output ], root_build_dir)
      }
      script = "$dir_pw_bloat/py/pw_bloat/no_toolchains.py"
      python_deps = [ "$dir_pw_bloat/py" ]
      args = [ rebase_path(_doc_rst_output) ]
      outputs = [ _doc_rst_output ]
    }
  }
}

# A base_executable for the pw_toolchain_size_report template which contains a
# main() function that loads the bloat_this_binary library and does nothing
# else.
pw_bloat_empty_base = {
  deps = [
    "$dir_pw_bloat:base_main",
    "$dir_pw_bloat:bloat_this_binary",
  ]
}