blob: 1ab66425b6ac4dafbc34019f7aa145f43682362d [file] [log] [blame]
# 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("$dir_pw_build/python_script.gni")
# 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") {
_bloaty_config = pw_executable_config.bloaty_config_file
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 = []
# 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 ]
_target_dir = get_label_info(binary.target, "target_out_dir")
_target_name = get_label_info(binary.target, "name")
_binary_path = get_path_info(_target_dir, "abspath") + ":$_target_name"
# 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")
}
_base_dir = get_label_info(_binary_base, "target_out_dir")
_base_name = get_label_info(_binary_base, "name")
_binary_path += ";" + get_path_info(_base_dir, "abspath") + ":$_base_name"
_binary_paths += [ _binary_path ]
_binary_labels += [ binary.label ]
}
_bloat_script_args = [
"--bloaty-config",
get_path_info(_bloaty_config, "abspath"),
"--out-dir",
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}.rst"
# Create an action which runs the size report script on the provided targets.
pw_python_script(target_name) {
metadata = {
pw_doc_sources = rebase_path([ _doc_rst_output ], root_build_dir)
}
script = "$dir_pw_bloat/py/bloat.py"
inputs = [
_bloaty_config,
"$dir_pw_bloat/py/binary_diff.py",
"$dir_pw_bloat/py/bloat_output.py",
]
outputs = [
"$target_gen_dir/${target_name}.txt",
_doc_rst_output,
]
deps = _all_target_dependencies
args = _bloat_script_args + _binary_paths
}
}