• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""
15Collects binary size JSON outputs from bloat targets into a single file.
16"""
17
18import argparse
19import json
20import logging
21from pathlib import Path
22import sys
23
24from typing import Dict, List
25
26import pw_cli.log
27
28_LOG = logging.getLogger(__package__)
29
30
31def _parse_args() -> argparse.Namespace:
32    """Parses the script's arguments."""
33
34    parser = argparse.ArgumentParser(__doc__)
35    parser.add_argument(
36        '--output',
37        type=Path,
38        required=True,
39        help='Output JSON file',
40    )
41    parser.add_argument(
42        'inputs',
43        type=Path,
44        nargs='+',
45        help='Input JSON files',
46    )
47
48    return parser.parse_args()
49
50
51def main(inputs: List[Path], output: Path) -> int:
52    all_data: Dict[str, int] = {}
53
54    for file in inputs:
55        try:
56            all_data |= json.loads(file.read_text())
57        except FileNotFoundError:
58            target_name = file.name.split('.')[0]
59            _LOG.error('')
60            _LOG.error('JSON input file %s does not exist', file)
61            _LOG.error('')
62            _LOG.error(
63                'Check that the build target "%s" is a pw_size_report template',
64                target_name,
65            )
66            _LOG.error('')
67            return 1
68
69    output.write_text(json.dumps(all_data, sort_keys=True, indent=2))
70    return 0
71
72
73if __name__ == '__main__':
74    pw_cli.log.install()
75    sys.exit(main(**vars(_parse_args())))
76