1# Copyright 2019 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"""Prints an error for an incorrect usage of a pw_proto_library template."""
15
16import argparse
17import logging
18import sys
19
20
21_LOG = logging.getLogger(__name__)
22
23
24def argument_parser(
25    parser: argparse.ArgumentParser | None = None,
26) -> argparse.ArgumentParser:
27    """Registers the script's arguments on an argument parser."""
28
29    if parser is None:
30        parser = argparse.ArgumentParser(description=__doc__)
31
32    parser.add_argument('--dir', required=True, help='Target directory')
33    parser.add_argument('--root', required=True, help='GN root')
34    parser.add_argument('--target', required=True, help='Build target')
35    parser.add_argument(
36        'generators',
37        metavar='GEN',
38        nargs='+',
39        help='Supported protobuf generators',
40    )
41
42    return parser
43
44
45def main() -> int:
46    """Prints an error message."""
47
48    args = argument_parser().parse_args()
49    relative_dir = args.dir[len(args.root) :].rstrip('/')
50
51    _LOG.error('')
52    _LOG.error('The target %s is not a compiled protobuf library.', args.target)
53    _LOG.error('')
54    _LOG.error('A different target is generated for each active generator.')
55    _LOG.error('Depend on one of the following targets instead:')
56    _LOG.error('')
57    for gen in args.generators:
58        _LOG.error('  //%s:%s.%s', relative_dir, args.target, gen)
59    _LOG.error('')
60
61    return 1
62
63
64if __name__ == '__main__':
65    try:
66        # If pw_cli is available, use it to initialize logs.
67        from pw_cli import log
68
69        log.install(logging.INFO)
70    except ImportError:
71        # If pw_cli isn't available, display log messages like a simple print.
72        logging.basicConfig(format='%(message)s', level=logging.INFO)
73
74    sys.exit(main())
75